VOOZH about

URL: https://deepwiki.com/npgsql/efcore.pg/4.1-expression-translation

⇱ Expression Translation | npgsql/efcore.pg | DeepWiki


Loading...
Menu

Expression Translation

This document details how LINQ expressions are translated to PostgreSQL-specific SQL expressions in the Npgsql Entity Framework Core Provider. Expression translation is a core component of the query pipeline, transforming .NET logic (method calls, member accesses, and logic operators) into efficient PostgreSQL SQL.

Overview of Translation Architecture

The translation system is primarily composed of method call translators, member translators, and expression visitors. These components work together to intercept .NET expression tree nodes and replace them with SqlExpression nodes representing PostgreSQL functions and operators.

Translation Flow

The following diagram illustrates the lifecycle of a LINQ expression as it moves through the Npgsql translation subsystem.

LINQ to PostgreSQL Translation Pipeline


Sources:

Method Call Translation

Method call translation is handled by classes implementing IMethodCallTranslator. The NpgsqlMethodCallTranslatorProvider coordinates these specialized translators.

String Methods

NpgsqlStringMethodTranslator maps standard .NET string methods to PostgreSQL equivalents. It also handles EF.Functions extensions like ILike.

.NET MethodPostgreSQL TranslationLogic Detail
string.IndexOf(s)strpos(instance, s) - 1Adjusts for 1-based indexing src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs97-113
string.Replace(o, n)replace(instance, o, n)src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs115-130
string.Substring(i, l)substring(instance, i+1, l)Handles 1-based indexing src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs141-154
string.ToLower()lower(instance)src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs132-139
Enumerable.First(str)substr(str, 1, 1)Translates char access src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlStringMethodTranslator.cs58-71

Sources:

Date and Time Methods

The provider supports complex translation for DateTime, DateTimeOffset, DateOnly, and TimeOnly through NpgsqlDateTimeMethodTranslator and NpgsqlDateTimeMemberTranslator.

Sources:

Array and Collection Methods

NpgsqlArrayMethodTranslator handles operations on .NET arrays and List<T>.

Sources:

Specialized Translators

Full-Text Search (FTS)

The NpgsqlFullTextSearchMethodTranslator handles PostgreSQL's advanced text search capabilities. It maps EF.Functions calls to PostgreSQL FTS functions like to_tsvector, plainto_tsquery, and ranking functions ts_rank.

FTS Entity Mapping


Sources:

Network and Range Types

Sources:

Expression Filtering

The NpgsqlEvaluatableExpressionFilter determines if an expression can be evaluated locally or must be translated to SQL. It explicitly prevents local evaluation of PostgreSQL-specific functions to ensure they are executed on the server.

Key behaviors:

Sources:

NodaTime Integration

When the NodaTime plugin is enabled, additional translators are injected via NpgsqlNodaTimeMemberTranslatorPlugin and NpgsqlNodaTimeMethodCallTranslatorPlugin. These handle NodaTime types like Instant, LocalDate, and Period, mapping them to PostgreSQL's timestamp, date, and interval types.

Sources: