Skip to content

Commit

Permalink
Fixed folding subquery alias to constant scan
Browse files Browse the repository at this point in the history
Fixes #546
  • Loading branch information
MarkMpn committed Sep 14, 2024
1 parent f40e048 commit 0619008
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/AdoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,5 +2441,30 @@ SELECT CAST(CAST(10.6496 AS float) AS int) AS trunc1,
}
}
}

[TestMethod]
public void QueryDerivedTableWithContradiction()
{
// https://github.com/MarkMpn/Sql4Cds/issues/546
using (var con = new Sql4CdsConnection(_localDataSources))
using (var cmd = con.CreateCommand())
{
cmd.CommandTimeout = 0;

cmd.CommandText = @"
SELECT *
FROM (SELECT a.accountid
FROM account AS a
WHERE 1 != 1) AS sub";

using (var reader = cmd.ExecuteReader())
{
var schema = reader.GetSchemaTable();

if (reader.Read())
Assert.Fail();
}
}
}
}
}
4 changes: 2 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/AliasNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override IDataExecutionPlanNodeInternal FoldQuery(NodeCompilationContext
{
// Remove any unused columns
var unusedColumns = constant.Schema.Keys
.Where(sourceCol => !ColumnSet.Any(col => col.SourceColumn.SplitMultiPartIdentifier().Last().EscapeIdentifier() == sourceCol))
.Where(sourceCol => !ColumnSet.Any(col => (String.IsNullOrEmpty(constant.Alias) && col.SourceColumn == sourceCol) || (!String.IsNullOrEmpty(constant.Alias) && col.SourceColumn == constant.Alias.EscapeIdentifier() + "." + sourceCol)))
.ToList();

foreach (var col in unusedColumns)
Expand All @@ -145,7 +145,7 @@ public override IDataExecutionPlanNodeInternal FoldQuery(NodeCompilationContext
// Copy/rename any columns using the new aliases
foreach (var col in ColumnSet)
{
var sourceColumn = col.SourceColumn.SplitMultiPartIdentifier().Last();
var sourceColumn = constant.Alias == null ? col.SourceColumn : col.SourceColumn.SplitMultiPartIdentifier().Last();

if (String.IsNullOrEmpty(constant.Alias) && col.OutputColumn != col.SourceColumn ||
!String.IsNullOrEmpty(constant.Alias) && col.OutputColumn != constant.Alias.EscapeIdentifier() + "." + col.SourceColumn)
Expand Down

0 comments on commit 0619008

Please sign in to comment.