Repository navigation
Description
I am busy adding the PrecompiledQuery functionality to EFCore.Jet provider including its test suite and have run into an error I'm not sure about
When running a test (e.g. BinaryExpression) in PrecompiledQueryJetTest (outside of naming and such pretty much similar to PrecompiledQuerySqlServerTest), I end up with the following error
Precompilation error: System.NotImplementedException: The method or operation is not implemented.
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.VisitTry(TryExpression tryNode)
at System.Linq.Expressions.TryExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.Translate[T](Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.TranslateList(IReadOnlyList`1 list)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.VisitNewArray(NewArrayExpression newArray)
at System.Linq.Expressions.NewArrayExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
...... stack trace with a bunch of Visits
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.TranslateCore(Expression node, IReadOnlyDictionary`2 constantReplacements, ISet`1 collectedNamespaces, ISet`1 unsafeAccessors, Boolean statementContext)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqToCSharpSyntaxTranslator.TranslateExpression(Expression node, IReadOnlyDictionary`2 constantReplacements, ISet`1 collectedNamespaces, ISet`1 unsafeAccessors)
at Microsoft.EntityFrameworkCore.Query.Internal.RuntimeModelLinqToCSharpSyntaxTranslator.TranslateExpression(Expression node, IReadOnlyDictionary`2 constantReplacements, IReadOnlyDictionary`2 memberAccessReplacements, ISet`1 collectedNamespaces, ISet`1 unsafeAccessors)
at Microsoft.EntityFrameworkCore.Query.Internal.PrecompiledQueryCodeGenerator.GenerateQueryExecutor(IndentedStringBuilder code, Int32 queryNum, Expression queryExecutor, HashSet`1 namespaces, HashSet`1 unsafeAccessors)
at Microsoft.EntityFrameworkCore.Query.Internal.PrecompiledQueryCodeGenerator.ProcessSyntaxTree(SyntaxTree syntaxTree, SemanticModel semanticModel, IReadOnlyList`1 locatedQueries, List`1 precompilationErrors, String suffix, ISet`1 generatedFileNames, CancellationToken cancellationToken)
Stack Trace:
PrecompiledQueryTestHelpers.FullSourceTest(String sourceCode, DbContextOptions dbContextOptions, Type dbContextType, Action`1 interceptorCodeAsserter, Action`1 errorAsserter, ITestOutputHelper testOutputHelper, Boolean alwaysPrintGeneratedSources, String callerName)
PrecompiledQueryTestHelpers.FullSourceTest(String sourceCode, DbContextOptions dbContextOptions, Type dbContextType, Action`1 interceptorCodeAsserter, Action`1 errorAsserter, ITestOutputHelper testOutputHelper, Boolean alwaysPrintGeneratedSources, String callerName)
PrecompiledQueryJetTest.BinaryExpression() line 31
--- End of stack trace from previous location ---
It obviously doesn't happen with Sql Server or Sqlite.
Debugging info
This seems to have something to do with the Execution Strategy being used. When it uses the default NonRetryingExecutionStrategy it comes up with the above error. However if I use the retrying execution strategy (JetRetryingExecutionStrategy or TestJetRetryingExecutionStrategy) it works fine as expected.
Further debugging brings me to the CompileQuery function within the ProcessSyntaxTree in this file https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs#L188
It is further in the ProcessSyntaxTree function when it calls GenerateQueryExecutor that we get the error.
Looking at the debug view of the queryExecutor variable we see the following difference (only relevant part shown - not the whole view)
This is what I get with Sql Server and Sqlite
$materializationContext1 = .New Microsoft.EntityFrameworkCore.Storage.MaterializationContext(
$emptyValueBuffer,
$queryContext.Context);
$instance1 = .Default(Microsoft.EntityFrameworkCore.Query.PrecompiledQueryRelationalTestBase+Blog);
$entry1 = .Call $queryContext.TryGetEntry(
$key,
.NewArray System.Object[] {
(System.Object).Call $dataReader.GetInt32(0)
},
True,
$hasNullKey1);
.If (
!$hasNullKey1
This is what I get on EFCore.Jet
$materializationContext1 = .New Microsoft.EntityFrameworkCore.Storage.MaterializationContext(
$emptyValueBuffer,
$queryContext.Context);
$instance1 = .Default(Microsoft.EntityFrameworkCore.Query.PrecompiledQueryRelationalTestBase+Blog);
$entry1 = .Call $queryContext.TryGetEntry(
$key,
.NewArray System.Object[] {
.Try {
(System.Object).Call $dataReader.GetInt32(0)
} .Catch (System.Exception $e) {
.Call Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+ShaperProcessingExpressionVisitor.ThrowReadValueException(
$e,
.Call $dataReader.GetFieldValue(0),
.Constant<System.Type>(System.Object),
$'property: Blog.Id (int) Required PK AfterSave:ThrowProperty')
}
},
True,
$hasNullKey1);
.If (
!$hasNullKey1
Note the part where the data reader call is wrapped in a try...catch. Not sure why it is being generated
As a further note, in the VisitTry function of LinqToCSharpSyntaxTranslator, the _context variable that the switch is done on is ExpressionContext.Expression hence the NotImplementedException
I know its still marked as experimental so maybe I have hit something not finished yet
SDK: .Net 9 preview 6
Visual Studio: 17.11.0 preview 7
EF Core: 9.0.0-rc.1.24374.2 ( a daily build a day or 2 after the last preview 7 so should be close to what the final preview 7 release will be)
Using the preview 6 branch of efcore to build and debug on the SQL Server and Sqlite for comparison
Any thoughts?
Activity
ChrisJollyAU commentedon Aug 11, 2024
Bit more info on this
Its actually part of the shaper created in
RelationalShapedQueryCompilingExpressionVisitor(underEFCore.Relational\Query)It is part of the function
CreateGetValueExpressionWhen you have detailed erros enabled you have the following block that executes of which it doesn't seem like the precompile undewrstands it yet
It is simple to recreate this issue - just add
.EnableDetailedErrors()to thebuilderinAddProviderOptions.e.g. with Sqlite you will have
[-]System.NotImplementedException with precompiledquery tests[/-][+]System.NotImplementedException for Precompiled Query with detailed errors enabled[/+]roji commentedon Oct 11, 2024
Thanks @ChrisJollyAU, the new precompiled query support is quite a massive piece of work, and there are definitely quite a few holes in there. Will put this on the consider list for 10, as we make the NativeAOt support more mature and hopefully bring it out of experimental status.
ChrisJollyAU commentedon Oct 11, 2024
Might have a go at this sometime
roji commentedon Oct 11, 2024
Sure thing! Feel free to submit a PR.
ChrisJollyAU commentedon Oct 13, 2024
Had a look at this. I think this may need a further discussion with design.
Here's the thing, NotImplemented is entirely correct. From what I understand (and I haven't done much with syntax generation and precompiled), the Try..catch can only be a statement and not an expression so you should never hit
VistTrywhen your_contextis anExpressionIn this case looking at the call stack it is preceded by a
VisitNewArrayandTranslateListso effectively what we have is something like the followingNote that when detailed errors are enabled, calls to the
dataReaderto get the field value, are wrapped in a try..catch (code show in a previous comment above)I don't believe you can have a try...catch in the initializer of an array and it doesn't look like the
SyntaxGeneratorcan do something like that.Somethings to think about:
translatedBodyas that would work there (aka detailed errors off)roji commentedon Oct 13, 2024
@ChrisJollyAU LINQ expression trees are indeed different from C# syntax: in LINQ expression trees, various things (e.g. try/catch) can be expressions although in C# they cannot. As a result, the translator needs to deal with these cases by lifting out the incompatible expression, doing something like the following:
The translator already does this for various constructs - take a look at the various tests; the same would need to be done for try/catch.
Lift try..catch out of an expression and replace with a temporary var…
ChrisJollyAU commentedon Oct 15, 2024
Ok, here's my attempt.
PrecompiledQuerySqlServerTest and AdHocPrecompiledQuerySqlServerTest (and same with sqlite) are all working locally
Lift try..catch out of an expression and replace with a temporary var…