question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrectly generated SQL containing LEFT JOIN LATERAL

See original GitHub issue

Assume I have a table Matches with the primary key MatchId.

The query

from _ in dbContext.Matches.AsQueryable()
select new { X = (from __ in dbContext.Matches.AsQueryable() select 1).ToList() };

generates

SELECT m."MatchId", 1, m0."MatchId"
FROM "Matches" AS m
LEFT JOIN LATERAL "Matches" AS m0 ON TRUE
ORDER BY m."MatchId", m0."MatchId"

which appears to be invalid:

Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "AS"
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
  Exception data:
    Severity: ERROR
    SqlState: 42601
    MessageText: syntax error at or near "AS"
    Position: 87
    File: scan.l
    Line: 1149
    Routine: scanner_yyerror

Provider: Npgsql.EntityFrameworkCore 3.1.4. Database: PostgreSQL 12.2, compiled by Visual C++ build 1914, 64-bit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
PatrikBakcommented, Nov 5, 2020

@roji

did you encounter this in a real-world query (if so, some context could be helpful)?

I came across this issue while debugging a different query. I thought I would report it just in case.

Also, note that you can omit the AsQueryables since DbSets are already IQueryable.

Sadly, I cannot, as I use System.Linq.Async

1reaction
smitpatelcommented, Nov 5, 2020

OUTER APPLY will return a single row with null for right when right is empty. Not the case for CROSS JOIN

Read more comments on GitHub >

github_iconTop Results From Across the Web

LEFT OUTER JOIN (gives extra rows) problem
I have two tables which I want to join together using a left outer join. However, even though my left table contains only...
Read more >
Best practice between using LEFT JOIN or NOT EXISTS
I am using queries within Access against a SQL Server database. sql-server · join · exists · Share.
Read more >
13.2.15.9 Lateral Derived Tables
A lateral derived table can occur only in a FROM clause, either in a list of tables separated with commas or in a...
Read more >
Slow left join lateral in subquery - postgresql
Based on what the analyze is saying, I believe the issue to be the subquery and the lateral join. Would anyone help me...
Read more >
[SQL] LATERAL JOIN - 1001 Ways Of Learning Anything
SELECT m.name FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true WHERE pname IS NULL; ON TRUE is for the join ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found