Skip to content

Fix a bug where the result of a function that returns an array is not able to be accessed by array index.#3720

Merged
roji merged 2 commits intonpgsql:mainfrom
brianpursley:efcorepg-3383
Feb 7, 2026
Merged

Fix a bug where the result of a function that returns an array is not able to be accessed by array index.#3720
roji merged 2 commits intonpgsql:mainfrom
brianpursley:efcorepg-3383

Conversation

@brianpursley
Copy link
Contributor

Fix a bug where the result of a function that returns an array is not able to be accessed by array index.

This allows array indexing to be used on the result of an expression, not just a column. For example:

var count = context.Customers
            .Select(c => EF.Functions.StringToArray(c.ContactName, " ")[0])
            .Distinct()
            .Count(c => c == "Maria");

There were two problems that needed to be fixed:

  1. NpgsqlArrayTypeMapping was not being translated into an array index expression.
  2. The inner expression was not being wrapped in parentheses.

Fixes #3383

Copilot AI review requested due to automatic review settings February 6, 2026 16:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where array indexing could not be applied to the result of a function that returns an array. The issue occurred because the Npgsql EF Core provider did not properly translate array indexing operations on expressions (like function results) and did not wrap function expressions in parentheses when applying array indexing.

Changes:

  • Added support for translating array indexing on any expression with NpgsqlArrayTypeMapping
  • Added logic to wrap function expressions in parentheses when used with array indexing
  • Added test to verify array indexing on function results works correctly

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/EFCore.PG.FunctionalTests/Query/NorthwindDbFunctionsQueryNpgsqlTest.cs Adds test case StringToArray_with_index() to verify array indexing on function results produces correct SQL with parentheses
src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs Adds requirement for parentheses around SqlFunctionExpression when used as inner expression of array indexing
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlArrayMethodTranslator.cs Adds translation of ElementAt on expressions with NpgsqlArrayTypeMapping to PostgreSQL array index syntax

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Pushed some tweaks and will merge and backport for 10.0.

@roji roji enabled auto-merge (squash) February 7, 2026 00:30
@roji roji merged commit 5604097 into npgsql:main Feb 7, 2026
10 checks passed
roji added a commit that referenced this pull request Feb 7, 2026
… able to be accessed by array index. (#3720)

Fixes #3383

Co-authored-by: Shay Rojansky <roji@roji.org>
(cherry picked from commit 5604097)
@roji
Copy link
Member

roji commented Feb 7, 2026

Backported to 10.0.1 via 253d4ae

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array indexing over SqlExpression not translated

2 participants