Skip to content

Commit

Permalink
Improved handling of ambiguous column names from un-aliased TVFs
Browse files Browse the repository at this point in the history
Fixed collation of OPENJSON values
  • Loading branch information
MarkMpn committed Nov 18, 2023
1 parent 7d14f8e commit 595fcd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 14 additions & 5 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/NodeSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,24 @@ public NodeSchema(INodeSchema copy)
/// <returns><c>true</c> if the column name exists, or <c>false</c> otherwise</returns>
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;
}

Expand Down
15 changes: 8 additions & 7 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/OpenJsonNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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));
Expand Down Expand Up @@ -162,10 +166,7 @@ private string PrefixWithAlias(string name, IDictionary<string, IReadOnlyList<st
{
name = name.EscapeIdentifier();

if (Alias == null)
return name;

var fullName = Alias.EscapeIdentifier() + "." + name;
var fullName = Alias == null ? name : (Alias.EscapeIdentifier() + "." + name);

if (aliases != null)
{
Expand Down

0 comments on commit 595fcd4

Please sign in to comment.