From 871dd425acbf754213ffae6d931afe0aace7228b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E6=89=8B=E6=8E=89=E5=8C=85=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E5=B8=88?= Date: Tue, 9 Apr 2024 15:30:21 +0800 Subject: [PATCH 1/3] This is an automated cherry-pick of #52378 Signed-off-by: ti-chi-bot --- ddl/metadatalocktest/BUILD.bazel | 9 +++ .../widow_with_exist_subquery_test.go | 77 +++++++++++++++++++ planner/core/logical_plan_builder.go | 8 ++ 3 files changed, 94 insertions(+) create mode 100644 planner/core/casetest/widow_with_exist_subquery_test.go diff --git a/ddl/metadatalocktest/BUILD.bazel b/ddl/metadatalocktest/BUILD.bazel index d458d7d592368..ea7acf4d1b42a 100644 --- a/ddl/metadatalocktest/BUILD.bazel +++ b/ddl/metadatalocktest/BUILD.bazel @@ -5,10 +5,19 @@ go_test( timeout = "short", srcs = [ "main_test.go", +<<<<<<< HEAD:ddl/metadatalocktest/BUILD.bazel "mdl_test.go", +======= + "widow_with_exist_subquery_test.go", + "window_push_down_test.go", +>>>>>>> 9b78a2388b3 (planner: add newly created col for window projection (#52378)):pkg/planner/core/casetest/windows/BUILD.bazel ], flaky = True, +<<<<<<< HEAD:ddl/metadatalocktest/BUILD.bazel shard_count = 32, +======= + shard_count = 5, +>>>>>>> 9b78a2388b3 (planner: add newly created col for window projection (#52378)):pkg/planner/core/casetest/windows/BUILD.bazel deps = [ "//config", "//ddl", diff --git a/planner/core/casetest/widow_with_exist_subquery_test.go b/planner/core/casetest/widow_with_exist_subquery_test.go new file mode 100644 index 0000000000000..e91c73f5274cd --- /dev/null +++ b/planner/core/casetest/widow_with_exist_subquery_test.go @@ -0,0 +1,77 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package windows + +import ( + "testing" + + "github.com/pingcap/tidb/pkg/testkit" +) + +func TestWindowWithCorrelatedSubQuery(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")) +} diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 526327586b363..dd6fec22e032d 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -6452,6 +6452,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. + // 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.names = append(proj.names, types.EmptyName) + proj.schema.Append(col) + } continue } proj.Exprs = append(proj.Exprs, it) From e4a078b6918d4d39f45fb17c9c61b58d97b50474 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 16 Apr 2024 17:26:16 +0800 Subject: [PATCH 2/3] fix --- ddl/metadatalocktest/BUILD.bazel | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ddl/metadatalocktest/BUILD.bazel b/ddl/metadatalocktest/BUILD.bazel index ea7acf4d1b42a..d458d7d592368 100644 --- a/ddl/metadatalocktest/BUILD.bazel +++ b/ddl/metadatalocktest/BUILD.bazel @@ -5,19 +5,10 @@ go_test( timeout = "short", srcs = [ "main_test.go", -<<<<<<< HEAD:ddl/metadatalocktest/BUILD.bazel "mdl_test.go", -======= - "widow_with_exist_subquery_test.go", - "window_push_down_test.go", ->>>>>>> 9b78a2388b3 (planner: add newly created col for window projection (#52378)):pkg/planner/core/casetest/windows/BUILD.bazel ], flaky = True, -<<<<<<< HEAD:ddl/metadatalocktest/BUILD.bazel shard_count = 32, -======= - shard_count = 5, ->>>>>>> 9b78a2388b3 (planner: add newly created col for window projection (#52378)):pkg/planner/core/casetest/windows/BUILD.bazel deps = [ "//config", "//ddl", From af88dc4cb6204f5be104b7c511594e292287b6d1 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 16 Apr 2024 17:33:18 +0800 Subject: [PATCH 3/3] fix --- planner/core/casetest/BUILD.bazel | 1 + planner/core/casetest/widow_with_exist_subquery_test.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/planner/core/casetest/BUILD.bazel b/planner/core/casetest/BUILD.bazel index 2b63b59f5b2a6..88eea70a79d8b 100644 --- a/planner/core/casetest/BUILD.bazel +++ b/planner/core/casetest/BUILD.bazel @@ -23,6 +23,7 @@ go_test( "rule_result_reorder_test.go", "stats_test.go", "tiflash_selection_late_materialization_test.go", + "widow_with_exist_subquery_test.go", "window_push_down_test.go", ], data = glob(["testdata/**"]), diff --git a/planner/core/casetest/widow_with_exist_subquery_test.go b/planner/core/casetest/widow_with_exist_subquery_test.go index e91c73f5274cd..6b5be44dce19c 100644 --- a/planner/core/casetest/widow_with_exist_subquery_test.go +++ b/planner/core/casetest/widow_with_exist_subquery_test.go @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package windows +package casetest import ( "testing" - "github.com/pingcap/tidb/pkg/testkit" + "github.com/pingcap/tidb/testkit" ) func TestWindowWithCorrelatedSubQuery(t *testing.T) {