|
| 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 | +} |
0 commit comments