Skip to content

Commit

Permalink
Fixed "unknown column" errors when using metadata columns in calculat…
Browse files Browse the repository at this point in the history
…ions without fully qualified names
  • Loading branch information
MarkMpn committed Aug 26, 2021
1 parent b3e5b73 commit a5ef49f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
26 changes: 26 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,5 +2913,31 @@ public void GroupByDatetimeWithoutDatePart()
</entity>
</fetch>");
}

[TestMethod]
public void MetadataExpressions()
{
var metadata = new AttributeMetadataCache(_service);
var planBuilder = new ExecutionPlanBuilder(metadata, new StubTableSizeCache(), this);

var query = "SELECT collectionschemaname + '.' + entitysetname FROM metadata.entity WHERE description LIKE '%test%'";

var plans = planBuilder.Build(query);

Assert.AreEqual(1, plans.Length);

var select = AssertNode<SelectNode>(plans[0]);
Assert.AreEqual("Expr1", select.ColumnSet[0].SourceColumn);

var computeScalar = AssertNode<ComputeScalarNode>(select.Source);
Assert.AreEqual("collectionschemaname + '.' + entitysetname", computeScalar.Columns["Expr1"].ToSql());

var filter = AssertNode<FilterNode>(computeScalar.Source);
Assert.AreEqual("description LIKE '%test%'", filter.Filter.ToSql());

var metadataQuery = AssertNode<MetadataQueryNode>(filter.Source);
Assert.AreEqual(MetadataSource.Entity, metadataQuery.MetadataSource);
CollectionAssert.AreEquivalent(new[] { "CollectionSchemaName", "EntitySetName", "Description" }, metadataQuery.Query.Properties.PropertyNames);
}
}
}
11 changes: 8 additions & 3 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/ComputeScalarNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,18 @@ public override IDataExecutionPlanNode FoldQuery(IAttributeMetadataCache metadat

public override void AddRequiredColumns(IAttributeMetadataCache metadata, IDictionary<string, Type> parameterTypes, IList<string> requiredColumns)
{
var schema = Source.GetSchema(metadata, parameterTypes);

var calcSourceColumns = Columns.Values
.SelectMany(expr => expr.GetColumns());
.SelectMany(expr => expr.GetColumns());

foreach (var col in calcSourceColumns)
{
if (!requiredColumns.Contains(col, StringComparer.OrdinalIgnoreCase))
requiredColumns.Add(col);
if (!schema.ContainsColumn(col, out var normalized))
continue;

if (!requiredColumns.Contains(normalized, StringComparer.OrdinalIgnoreCase))
requiredColumns.Add(normalized);
}

Source.AddRequiredColumns(metadata, parameterTypes, requiredColumns);
Expand Down
1 change: 1 addition & 0 deletions MarkMpn.Sql4Cds.Engine/MarkMpn.Sql4Cds.Engine.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Fixed "Aggregates are not supported" when using audit table
Fixed paging issues with aggregates
Fixed handling of "containvalues" filters on choices columns
Fixed "unknown column" errors when using metadata columns in calculations without fully qualified names
</releaseNotes>
<copyright>Copyright © 2020 Mark Carrington</copyright>
<language>en-GB</language>
Expand Down
3 changes: 2 additions & 1 deletion MarkMpn.Sql4Cds/MarkMpn.SQL4CDS.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Fixed "Aggregates are not supported" when using audit table
Fixed adding connection to Object Explorer after using Change Connection toolbar button
Improved performance of adding new connection by limiting properties shown in properties window
Fixed paging issues with aggregates
Fixed handling of "containvalues" filters on choices columns</releaseNotes>
Fixed handling of "containvalues" filters on choices columns
Fixed "unknown column" errors when using metadata columns in calculations without fully qualified names</releaseNotes>
<copyright>Copyright © 2019 Mark Carrington</copyright>
<language>en-GB</language>
<tags>XrmToolBox SQL CDS</tags>
Expand Down

0 comments on commit a5ef49f

Please sign in to comment.