diff --git a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/NodeSchema.cs b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/NodeSchema.cs
index 01999a90..d2e5b7d2 100644
--- a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/NodeSchema.cs
+++ b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/NodeSchema.cs
@@ -77,15 +77,24 @@ public NodeSchema(INodeSchema copy)
/// true if the column name exists, or false otherwise
public bool ContainsColumn(string column, out string normalized)
{
- if (Schema.TryGetValue(column, out _))
+ if (Aliases.TryGetValue(column, out var names))
{
- normalized = Schema.Keys.Single(k => k.Equals(column, StringComparison.OrdinalIgnoreCase));
- return true;
+ if (names.Count > 1)
+ {
+ normalized = null;
+ return false;
+ }
+
+ if (names.Count == 1)
+ {
+ normalized = names[0];
+ return true;
+ }
}
- if (Aliases.TryGetValue(column, out var names) && names.Count == 1)
+ if (Schema.TryGetValue(column, out _))
{
- normalized = names[0];
+ normalized = Schema.Keys.Single(k => k.Equals(column, StringComparison.OrdinalIgnoreCase));
return true;
}
diff --git a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/OpenJsonNode.cs b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/OpenJsonNode.cs
index 8a1ed57b..afd0f9a9 100644
--- a/MarkMpn.Sql4Cds.Engine/ExecutionPlan/OpenJsonNode.cs
+++ b/MarkMpn.Sql4Cds.Engine/ExecutionPlan/OpenJsonNode.cs
@@ -92,9 +92,6 @@ public override IDataExecutionPlanNodeInternal FoldQuery(NodeCompilationContext
_jsonExpression = Json.Compile(ecc);
_pathExpression = Path?.Compile(ecc);
- Json.GetType(ecc, out var jsonType);
- _jsonCollation = (jsonType as SqlDataTypeReferenceWithCollation)?.Collation ?? context.PrimaryDataSource.DefaultCollation;
-
return this;
}
@@ -105,6 +102,13 @@ public override INodeSchema GetSchema(NodeCompilationContext context)
if (Schema == null)
{
+ if (_jsonCollation == null)
+ {
+ var ecc = new ExpressionCompilationContext(context, null, null);
+ Json.GetType(ecc, out var jsonType);
+ _jsonCollation = (jsonType as SqlDataTypeReferenceWithCollation)?.Collation ?? context.PrimaryDataSource.DefaultCollation;
+ }
+
columns.Add(PrefixWithAlias("key", aliases), new ColumnDefinition(DataTypeHelpers.NVarChar(4000, _keyCollation, CollationLabel.Implicit), false, false));
columns.Add(PrefixWithAlias("value", aliases), new ColumnDefinition(DataTypeHelpers.NVarChar(Int32.MaxValue, _jsonCollation, CollationLabel.Implicit), true, false));
columns.Add(PrefixWithAlias("type", aliases), new ColumnDefinition(DataTypeHelpers.Int, false, false));
@@ -162,10 +166,7 @@ private string PrefixWithAlias(string name, IDictionary