Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/analyzer/prune_columns.go: Turn off pruneColumns when a Subquery expression exists. #314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sql/analyzer/prune_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func pruneColumns(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.
return n, nil
}

if !pruneColumnsIsSafe(n) {
a.Log("not pruning columns because it is not safe.")
return n, nil
}

columns := columnsUsedByNode(n)
findUsedColumns(columns, n)

Expand All @@ -72,6 +77,20 @@ func pruneColumns(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.
return fixRemainingFieldsIndexes(n, scope)
}

func pruneColumnsIsSafe(n sql.Node) bool {
isSafe := true
// We do not run pruneColumns if there is a Subquery
// expression, because the field rewrites and scope handling
// in here are not principled.
plan.InspectExpressions(n, func(e sql.Expression) bool {
if _, ok := e.(*plan.Subquery); ok {
isSafe = false
}
return isSafe
})
return isSafe
}

func columnsUsedByNode(n sql.Node) usedColumns {
columns := make(usedColumns)

Expand Down
3 changes: 2 additions & 1 deletion sql/analyzer/prune_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func TestPruneColumns(t *testing.T) {
),
},
{
name: "Drop projected columns not in subquery",
name: "Keep projected columns when there is a subquery",
node: plan.NewProject(
[]sql.Expression{
gf(0, "t1", "foo"),
Expand Down Expand Up @@ -405,6 +405,7 @@ func TestPruneColumns(t *testing.T) {
plan.NewProject(
[]sql.Expression{
gf(0, "t1", "foo"),
gf(1, "t1", "bar"),
},
plan.NewResolvedTable(t1),
),
Expand Down