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

planner: add newly created col for window projection #52378

Merged
merged 9 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6502,6 +6502,14 @@ func (b *PlanBuilder) buildByItemsForWindow(
}
if col, ok := it.(*expression.Column); ok {
retItems = append(retItems, property.SortItem{Col: col, Desc: item.Desc})
// We need to attempt to add this column because a subquery may be created during the expression rewrite process.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 3 lines is same as the under 3 lines from 6515 to 6521.
So should you please change those code more unified ~

Copy link
Member Author

@Rustin170506 Rustin170506 Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I have reordered the code to make them have the same order.

// Therefore, we need to ensure that the column from the newly created query plan is added.
// If the column is already in the schema, we don't need to add it again.
if !proj.schema.Contains(col) {
proj.Exprs = append(proj.Exprs, col)
proj.schema.Append(col)
proj.names = append(proj.names, types.EmptyName)
}
continue
}
proj.Exprs = append(proj.Exprs, it)
Expand Down
56 changes: 56 additions & 0 deletions pkg/planner/core/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,59 @@ func TestImportIntoBuildPlan(t *testing.T) {
require.ErrorIs(t, tk.ExecToErr("IMPORT INTO t3 FROM select * from t2"),
infoschema.ErrTableNotExists)
}

func TestIssue42734(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("CREATE TABLE temperature_data (temperature double);")
tk.MustExec("CREATE TABLE humidity_data (humidity double);")
tk.MustExec("CREATE TABLE weather_report (report_id double, report_date varchar(100));")

tk.MustExec("INSERT INTO temperature_data VALUES (1.0);")
tk.MustExec("INSERT INTO humidity_data VALUES (0.5);")
tk.MustExec("INSERT INTO weather_report VALUES (2.0, 'test');")

result := tk.MustQuery(`
SELECT
EXISTS (
SELECT
FIRST_VALUE(temp_data.temperature) OVER weather_window AS first_temperature,
MIN(report_data.report_id) OVER weather_window AS min_report_id
FROM
temperature_data AS temp_data
WINDOW weather_window AS (
PARTITION BY EXISTS (
SELECT
report_data.report_date AS report_date
FROM
humidity_data AS humidity_data
WHERE temp_data.temperature >= humidity_data.humidity
)
)
) AS is_exist
FROM
weather_report AS report_data;
`)

result.Check(testkit.Rows("1"))

result = tk.MustQuery(`
SELECT
EXISTS (
SELECT
FIRST_VALUE(temp_data.temperature) OVER weather_window AS first_temperature,
MIN(report_data.report_id) OVER weather_window AS min_report_id
FROM
temperature_data AS temp_data
WINDOW weather_window AS (
PARTITION BY temp_data.temperature
)
) AS is_exist
FROM
weather_report AS report_data;
`)

result.Check(testkit.Rows("1"))
}