MCPcopy
hub / github.com/pingcap/tidb / TestPreprocessCTE

Function TestPreprocessCTE

pkg/planner/core/preprocess_test.go:358–416  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

356}
357
358func TestPreprocessCTE(t *testing.T) {
359 store := testkit.CreateMockStore(t)
360 tk := testkit.NewTestKit(t, store)
361 tk.MustExec("use test;")
362 tk.MustExec("drop table if exists t, t1, t2;")
363 tk.MustExec("create table t (c int);insert into t values (1), (2), (3), (4), (5);")
364 tk.MustExec("create table t1 (a int);insert into t1 values (0), (1), (2), (3), (4);")
365 tk.MustExec("create table t2 (b int);insert into t2 values (1), (2), (3), (4), (5);")
366 tk.MustExec("create table t11111 (d int);insert into t11111 values (1), (2), (3), (4), (5);")
367 tk.MustExec("drop table if exists tbl_1;\nCREATE TABLE `tbl_1` (\n `col_2` char(65) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n `col_3` int(11) NOT NULL\n);")
368 testCases := []struct {
369 before string
370 after string
371 }{
372 {
373 "create view v1 as WITH t1 as (select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1;",
374 "CREATE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `test`.`v1` AS WITH `t1` AS (SELECT `a` FROM `test`.`t2` WHERE `t2`.`a`=3 UNION SELECT `t2`.`a`+1 FROM (`test`.`t1`) JOIN `test`.`t2` WHERE `t1`.`a`=`t2`.`a`) SELECT * FROM `t1`",
375 },
376 {
377 "WITH t1 AS ( SELECT(WITH t1 AS ( WITH qn AS ( SELECT 10 * a AS a FROM t1 ) SELECT 10 * a AS a FROM qn ) SELECT * FROM t1 LIMIT 1 ) FROM t2 WHERE t2.b = 3 UNION SELECT t2.b + 1 FROM t1, t2 WHERE t1.a = t2.b) SELECT * FROM t1",
378 "WITH `t1` AS (SELECT (WITH `t1` AS (WITH `qn` AS (SELECT 10*`a` AS `a` FROM `test`.`t1`) SELECT 10*`a` AS `a` FROM `qn`) SELECT * FROM `t1` LIMIT 1) FROM `test`.`t2` WHERE `t2`.`b`=3 UNION SELECT `t2`.`b`+1 FROM (`test`.`t1`) JOIN `test`.`t2` WHERE `t1`.`a`=`t2`.`b`) SELECT * FROM `t1`",
379 },
380 {
381 "with recursive cte_8932 (col_34891,col_34892) AS ( with recursive cte_8932 (col_34893,col_34894,col_34895) AS ( with tbl_1 (col_34896,col_34897,col_34898,col_34899) AS ( select 1, \"2\",3,col_3 from tbl_1 ) select cte_as_8958.col_34896,cte_as_8958.col_34898,cte_as_8958.col_34899 from tbl_1 as cte_as_8958 UNION DISTINCT select col_34893 + 1,concat(col_34894, 1),col_34895 + 1 from cte_8932 where col_34893 < 5 ) select cte_as_8959.col_34893,cte_as_8959.col_34895 from cte_8932 as cte_as_8959 ) select * from cte_8932 as cte_as_8960 order by cte_as_8960.col_34891,cte_as_8960.col_34892;",
382 "WITH RECURSIVE `cte_8932` (`col_34891`, `col_34892`) AS (WITH RECURSIVE `cte_8932` (`col_34893`, `col_34894`, `col_34895`) AS (WITH `tbl_1` (`col_34896`, `col_34897`, `col_34898`, `col_34899`) AS (SELECT 1,_UTF8MB4'2',3,`col_3` FROM `test`.`tbl_1`) SELECT `cte_as_8958`.`col_34896`,`cte_as_8958`.`col_34898`,`cte_as_8958`.`col_34899` FROM `tbl_1` AS `cte_as_8958` UNION SELECT `col_34893`+1,CONCAT(`col_34894`, 1),`col_34895`+1 FROM `cte_8932` WHERE `col_34893`<5) SELECT `cte_as_8959`.`col_34893`,`cte_as_8959`.`col_34895` FROM `cte_8932` AS `cte_as_8959`) SELECT * FROM `cte_8932` AS `cte_as_8960` ORDER BY `cte_as_8960`.`col_34891`,`cte_as_8960`.`col_34892`",
383 },
384 {
385 "with t1 as (with t11 as (select * from t) select * from t1, t2) select * from t1;",
386 "WITH `t1` AS (WITH `t11` AS (SELECT * FROM `test`.`t`) SELECT * FROM (`test`.`t1`) JOIN `test`.`t2`) SELECT * FROM `t1`",
387 },
388 {
389 "with t1 as (with t1 as (select * from t) select * from t1, t2) select * from t1;",
390 "WITH `t1` AS (WITH `t1` AS (SELECT * FROM `test`.`t`) SELECT * FROM (`t1`) JOIN `test`.`t2`) SELECT * FROM `t1`",
391 },
392 {
393 "WITH t1 AS ( WITH t1 AS ( SELECT * FROM t ) SELECT ( WITH t2 AS ( SELECT * FROM t ) SELECT * FROM t limit 1 ) FROM t1, t2 ) \n\nSELECT\n* \nFROM\n\tt1;",
394 "WITH `t1` AS (WITH `t1` AS (SELECT * FROM `test`.`t`) SELECT (WITH `t2` AS (SELECT * FROM `test`.`t`) SELECT * FROM `test`.`t` LIMIT 1) FROM (`t1`) JOIN `test`.`t2`) SELECT * FROM `t1`",
395 },
396 {
397 "WITH t123 AS (WITH t11111 AS ( SELECT * FROM test.t1 ) SELECT ( WITH t2 AS ( SELECT ( WITH t23 AS ( SELECT * FROM t11111 ) SELECT * FROM t23 LIMIT 1 ) FROM t11111 ) SELECT * FROM t2 LIMIT 1 ) FROM t11111, test.t2 ) SELECT * FROM t11111;",
398 "WITH `t123` AS (WITH `t11111` AS (SELECT * FROM `test`.`t1`) SELECT (WITH `t2` AS (SELECT (WITH `t23` AS (SELECT * FROM `t11111`) SELECT * FROM `t23` LIMIT 1) FROM `t11111`) SELECT * FROM `t2` LIMIT 1) FROM (`t11111`) JOIN `test`.`t2`) SELECT * FROM `test`.`t11111`",
399 },
400 }
401 for _, tc := range testCases {
402 stmts, warnings, err := parser.New().ParseSQL(tc.before)
403 require.Len(t, warnings, 0)
404 require.NoError(t, err)
405 require.Len(t, stmts, 1)
406
407 nodeW := resolve.NewNodeW(stmts[0])
408 err = core.Preprocess(context.Background(), tk.Session(), nodeW)
409 require.NoError(t, err)
410
411 var rs strings.Builder
412 err = stmts[0].Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &rs))
413 require.NoError(t, err)
414 require.Equal(t, tc.after, rs.String())
415 }

Callers

nothing calls this directly

Calls 13

MustExecMethod · 0.95
SessionMethod · 0.95
CreateMockStoreFunction · 0.92
NewTestKitFunction · 0.92
NewFunction · 0.92
NewNodeWFunction · 0.92
PreprocessFunction · 0.92
NewRestoreCtxFunction · 0.92
ParseSQLMethod · 0.65
LenMethod · 0.65
RestoreMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected