efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Allow expressing VALUES expressions

Open roji opened this issue 4 years ago • 4 comments

PostgreSQL supports the following expression: VALUES (1, 'one'), (2, 'two'), (3, 'three'). This is a table-returning expression which can be used anywhere a table can (e.g. in subqueries, JOINs), and can be useful for specifying constant tables. Note that this syntax makes up the standard SQL INSERT statement (it's what comes after INSERT INTO foo (<columns>) ...).

As part of EF Core 7.0 we may add row value expressions (see https://github.com/dotnet/efcore/issues/26822). If that's done, a PostgreSQL-specific ValuesExpression could be added, which is just a collection of RowValueExpressions.

/cc @smitpatel

Originally requested in https://stackoverflow.com/questions/70411262/constant-tables-in-ef-or-npgsql

roji avatar Dec 20 '21 12:12 roji

Would completion of this issue include enabling VALUES expressions in the ANY(…) operator? And if not, is there a separate issue for this functionality that I can upvote?

smarts avatar Nov 16 '23 23:11 smarts

This was actually done for EF 8.0 in https://github.com/dotnet/efcore/issues/30426.

Specifically for Contains, see https://github.com/dotnet/efcore/issues/30732; I'm not sure why you'd want to use VALUES in ANY, when you can simply use x IN (a, b, c) directly (the list inside IN functions just like VALUES). Am I missing something?

roji avatar Nov 17 '23 00:11 roji

@roji if you have a composite key (and therefore index) like ParentId, ChildId i believe using WHERE ("ParentId", "ChildId") in (VALUES(45, 67), VALUES(12, 34)) allows the index to be used. Edit: I think my syntax is off here. The VALUES keyword isn't necessary in the example above

smarts avatar Nov 21 '23 16:11 smarts

The VALUES keyword isn't necessary in the example above

Exactly, there's no need for it.

Other than that, no, this does not allow for embedding multiple row-values inside IN - nor is that supported anywhere else at the moment. That would mean supporting complex collections, which EF doesn't support yet.

roji avatar Nov 21 '23 17:11 roji