Skip to content

Commit 840a6e3

Browse files
Rustin170506ti-chi-bot
authored andcommitted
planner: add newly created col for window projection (pingcap#52378)
close pingcap#42734
1 parent 2ecc47d commit 840a6e3

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

pkg/planner/core/casetest/windows/BUILD.bazel

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ go_test(
55
timeout = "short",
66
srcs = [
77
"main_test.go",
8+
"widow_with_exist_subquery_test.go",
89
"window_push_down_test.go",
910
],
1011
data = glob(["testdata/**"]),
1112
flaky = True,
12-
shard_count = 4,
13+
shard_count = 5,
1314
deps = [
1415
"//pkg/domain",
1516
"//pkg/planner/core/internal",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package windows
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/testkit"
21+
)
22+
23+
func TestWindowWithCorrelatedSubQuery(t *testing.T) {
24+
store := testkit.CreateMockStore(t)
25+
tk := testkit.NewTestKit(t, store)
26+
27+
tk.MustExec("use test")
28+
tk.MustExec("CREATE TABLE temperature_data (temperature double);")
29+
tk.MustExec("CREATE TABLE humidity_data (humidity double);")
30+
tk.MustExec("CREATE TABLE weather_report (report_id double, report_date varchar(100));")
31+
32+
tk.MustExec("INSERT INTO temperature_data VALUES (1.0);")
33+
tk.MustExec("INSERT INTO humidity_data VALUES (0.5);")
34+
tk.MustExec("INSERT INTO weather_report VALUES (2.0, 'test');")
35+
36+
result := tk.MustQuery(`
37+
SELECT
38+
EXISTS (
39+
SELECT
40+
FIRST_VALUE(temp_data.temperature) OVER weather_window AS first_temperature,
41+
MIN(report_data.report_id) OVER weather_window AS min_report_id
42+
FROM
43+
temperature_data AS temp_data
44+
WINDOW weather_window AS (
45+
PARTITION BY EXISTS (
46+
SELECT
47+
report_data.report_date AS report_date
48+
FROM
49+
humidity_data AS humidity_data
50+
WHERE temp_data.temperature >= humidity_data.humidity
51+
)
52+
)
53+
) AS is_exist
54+
FROM
55+
weather_report AS report_data;
56+
`)
57+
58+
result.Check(testkit.Rows("1"))
59+
60+
result = tk.MustQuery(`
61+
SELECT
62+
EXISTS (
63+
SELECT
64+
FIRST_VALUE(temp_data.temperature) OVER weather_window AS first_temperature,
65+
MIN(report_data.report_id) OVER weather_window AS min_report_id
66+
FROM
67+
temperature_data AS temp_data
68+
WINDOW weather_window AS (
69+
PARTITION BY temp_data.temperature
70+
)
71+
) AS is_exist
72+
FROM
73+
weather_report AS report_data;
74+
`)
75+
76+
result.Check(testkit.Rows("1"))
77+
}

pkg/planner/core/logical_plan_builder.go

+8
Original file line numberDiff line numberDiff line change
@@ -6843,6 +6843,14 @@ func (b *PlanBuilder) buildByItemsForWindow(
68436843
}
68446844
if col, ok := it.(*expression.Column); ok {
68456845
retItems = append(retItems, property.SortItem{Col: col, Desc: item.Desc})
6846+
// We need to attempt to add this column because a subquery may be created during the expression rewrite process.
6847+
// Therefore, we need to ensure that the column from the newly created query plan is added.
6848+
// If the column is already in the schema, we don't need to add it again.
6849+
if !proj.schema.Contains(col) {
6850+
proj.Exprs = append(proj.Exprs, col)
6851+
proj.names = append(proj.names, types.EmptyName)
6852+
proj.schema.Append(col)
6853+
}
68466854
continue
68476855
}
68486856
proj.Exprs = append(proj.Exprs, it)

0 commit comments

Comments
 (0)