Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

analyzer: correctly search for subquery used columns #573

Merged
merged 1 commit into from
Dec 13, 2018
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
2 changes: 1 addition & 1 deletion sql/analyzer/prune_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func pruneSubqueryColumns(
columns[table][col] = struct{}{}
}

findUsedColumns(columns, n)
findUsedColumns(columns, n.Child)

node, err := addSubqueryBarriers(n.Child)
if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions sql/analyzer/prune_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,70 @@ func TestPruneColumns(t *testing.T) {
),
),
},

{
"used inside subquery and not outside",
plan.NewProject(
[]sql.Expression{
gf(0, "sq", "foo"),
},
plan.NewSubqueryAlias("sq",
plan.NewProject(
[]sql.Expression{gf(0, "t1", "foo")},
plan.NewInnerJoin(
plan.NewProject(
[]sql.Expression{
gf(0, "t1", "foo"),
gf(1, "t1", "bar"),
gf(2, "t1", "bax"),
},
t1,
),
plan.NewProject(
[]sql.Expression{
gf(0, "t2", "foo"),
gf(1, "t2", "baz"),
gf(2, "t2", "bux"),
},
t2,
),
expression.NewEquals(
gf(0, "t1", "foo"),
gf(3, "t2", "foo"),
),
),
),
),
),
plan.NewProject(
[]sql.Expression{
gf(0, "sq", "foo"),
},
plan.NewSubqueryAlias("sq",
plan.NewProject(
[]sql.Expression{gf(0, "t1", "foo")},
plan.NewInnerJoin(
plan.NewProject(
[]sql.Expression{
gf(0, "t1", "foo"),
},
t1,
),
plan.NewProject(
[]sql.Expression{
gf(0, "t2", "foo"),
},
t2,
),
expression.NewEquals(
gf(0, "t1", "foo"),
gf(3, "t2", "foo"),
),
),
),
),
),
},
}

for _, tt := range testCases {
Expand Down