Skip to content

Commit

Permalink
[feat](nereids) support create view in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei committed Mar 25, 2024
1 parent a3c470d commit 4cf6fdd
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append(frameUnits + " ");
if (rightBoundary != null) {
sb.append("BETWEEN " + leftBoundary + " AND " + rightBoundary);
sb.append("BETWEEN " + leftBoundary.toSql() + " AND " + rightBoundary.toSql());
} else {
sb.append(leftBoundary);
}
Expand Down Expand Up @@ -215,6 +215,31 @@ public String toString() {
return sb.toString();
}

public String toSql() {
StringBuilder sb = new StringBuilder();
boundOffset.ifPresent(value -> sb.append(value + " "));
switch (frameBoundType) {
case UNBOUNDED_PRECEDING:
sb.append("UNBOUNDED PRECEDING");
break;
case UNBOUNDED_FOLLOWING:
sb.append("UNBOUNDED FOLLOWING");
break;
case CURRENT_ROW:
sb.append("CURRENT ROW");
break;
case PRECEDING:
sb.append("PRECEDING");
break;
case FOLLOWING:
sb.append("FOLLOWING");
break;
default:
break;
}
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ private String rewriteProjectsToUserDefineAlias(String resSql) {
for (int i = 0; i < projectExprs.size(); ++i) {
replaceWithColsBuilder.append(projectExprs.get(i).toSql());
replaceWithColsBuilder.append(" AS `");
replaceWithColsBuilder.append(finalCols.get(i).getName());
String escapeBacktick = finalCols.get(i).getName().replace("`", "``");
replaceWithColsBuilder.append(escapeBacktick);
replaceWithColsBuilder.append('`');
if (i != projectExprs.size() - 1) {
replaceWithColsBuilder.append(", ");
Expand Down
140 changes: 140 additions & 0 deletions regression-test/data/ddl_p0/test_create_view_nereids.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !test_view_1 --
1 [1, 2, 3]
2 [10, -2, 8]
3 [-1, 20, 0]

-- !test_view_2 --
1 [1, 2, 3] [1, 1, 1]
2 [10, -2, 8] [1, 0, 1]
3 [-1, 20, 0] [0, 1, 0]

-- !test_view_3 --
1 [1, 2, 3] [1, 2, 3] [1, 2, 3]
2 [10, -2, 8] [10, 8] [10, 8]
3 [-1, 20, 0] [20] [20]

-- !test_view_4 --
1 [1, 2, 3] [1, 2, 3] [1, 2, 3]
2 [10, -2, 8] [10, 8] [10, 8]
3 [-1, 20, 0] [20] [20]

-- !test_view_5 --
1 [1, 2, 3] [1, 1, 1]
2 [10, -2, 8] [1, 0, 1]
3 [-1, 20, 0] [0, 1, 0]

-- !test_view_6 --
v1 CREATE VIEW `v1` COMMENT 'VIEW' AS SELECT\n `regression_test_ddl_p0`.`view_column_name_test_nereids`.`error_code` AS `error_code`, 1 AS `1`, 'string' AS `'string'`, now() AS `now()`, dayofyear(`regression_test_ddl_p0`.`view_column_name_test_nereids`.`op_time`) AS `dayofyear(``regression_test_ddl_p0``.``view_column_name_test_nereids``.``op_time``)`, cast(`regression_test_ddl_p0`.`view_column_name_test_nereids`.`source` as BIGINT) AS `cast(``regression_test_ddl_p0``.``view_column_name_test_nereids``.``source`` as BIGINT)`, min(`regression_test_ddl_p0`.`view_column_name_test_nereids`.`timestamp`) OVER(ORDER BY `regression_test_ddl_p0`.`view_column_name_test_nereids`.`op_time` desc ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS `min(``regression_test_ddl_p0``.``view_column_name_test_nereids``.``timestamp``) OVER(ORDER BY ``regression_test_ddl_p0``.``view_column_name_test_nereids``.``op_time`` desc ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)`, (1 > 2) AS `(1 > 2)`, (2 + 3) AS `(2 + 3)`, 1 IN (1, 2, 3, 4) AS `1 IN (1, 2, 3, 4)`, (`regression_test_ddl_p0`.`view_column_name_test_nereids`.`remark` like '%like') AS `(``regression_test_ddl_p0``.``view_column_name_test_nereids``.``remark`` like '%like')`, CASE WHEN (`regression_test_ddl_p0`.`view_column_name_test_nereids`.`remark` = 's') THEN 1 ELSE 2 END AS `CASE WHEN (``regression_test_ddl_p0``.``view_column_name_test_nereids``.``remark`` = 's') THEN 1 ELSE 2 END`, (cast(TRUE as TINYINT) | cast(FALSE as TINYINT)) AS `(cast(TRUE as TINYINT) | cast(FALSE as TINYINT))` \n FROM \n view_column_name_test_nereids;

-- !test_with_as --
1 1 2
1 1 4
1 3 6
2 1 3
2 1 4
2 1 7
2 3 5
2 3 9
2 4 2
3 2 8
3 5 \N
3 5 6
3 5 6
3 5 8
4 5 6
6 \N 6
6 7 1

-- !test_with_as_sql --
test_view_with_as CREATE VIEW `test_view_with_as` COMMENT 'VIEW' AS (\n with t1 as (select `regression_test_ddl_p0`.`mal_test_view`.`pk`, `regression_test_ddl_p0`.`mal_test_view`.`a`, `regression_test_ddl_p0`.`mal_test_view`.`b` from mal_test_view), t2 as (select `regression_test_ddl_p0`.`mal_test_view`.`pk`, `regression_test_ddl_p0`.`mal_test_view`.`a`, `regression_test_ddl_p0`.`mal_test_view`.`b` from mal_test_view), \n t3 as (select `regression_test_ddl_p0`.`mal_test_view`.`pk`, `regression_test_ddl_p0`.`mal_test_view`.`a`, `regression_test_ddl_p0`.`mal_test_view`.`b` from mal_test_view) SELECT `t1`.`pk` AS `pk`, `t1`.`a` AS `a`, `t1`.`b` AS `b` FROM t1); utf8mb4 utf8mb4_0900_bin

-- !test_union --
1 1 2
1 1 2
1 1 4
1 1 4
1 3 6
1 3 6
2 1 3
2 1 3
2 1 4
2 1 4
2 1 7
2 1 7
2 3 5
2 3 5
2 3 9
2 3 9
2 4 2
2 4 2
3 2 8
3 2 8
3 5 \N
3 5 \N
3 5 6
3 5 6
3 5 6
3 5 6
3 5 8
3 5 8
4 5 6
4 5 6
6 \N 6
6 \N 6
6 7 1
6 7 1

-- !test_union_sql --
test_view_union CREATE VIEW `test_view_union` COMMENT 'VIEW' AS (\nselect `regression_test_ddl_p0`.`mal_test_view`.`pk` AS `c1`, `regression_test_ddl_p0`.`mal_test_view`.`a` AS `c2`, `regression_test_ddl_p0`.`mal_test_view`.`b` AS `c3` from mal_test_view Union all SELECT * FROM mal_test_view); utf8mb4 utf8mb4_0900_bin

-- !test_count_star --
17

-- !test_count_star_sql --
test_view_count_star CREATE VIEW `test_view_count_star` COMMENT 'VIEW' AS (select count(*) AS `c1` from mal_test_view having count(*) > 0); utf8mb4 utf8mb4_0900_bin

-- !test_expression --
\N \N 6
2 4 2
2 4 3
2 4 4
2 4 4
2 4 7
3 5 8
4 6 5
4 6 6
4 6 9
5 7 2
6 8 \N
6 8 6
6 8 6
6 8 6
6 8 8
8 10 1

-- !test_expression_sql --
test_view_expression CREATE VIEW `test_view_expression` COMMENT 'VIEW' AS (select (`regression_test_ddl_p0`.`mal_test_view`.`a` + 1) AS `c1`, ((abs(`regression_test_ddl_p0`.`mal_test_view`.`a`) + 2) + 1) AS `c2`, substring(cast(`regression_test_ddl_p0`.`mal_test_view`.`b` as VARCHAR(10)), 1, 10) AS `c3` from mal_test_view); utf8mb4 utf8mb4_0900_bin

-- !test_alias --
\N \N 6
2 4 2
2 4 3
2 4 4
2 4 4
2 4 7
3 5 8
4 6 5
4 6 6
4 6 9
5 7 2
6 8 \N
6 8 6
6 8 6
6 8 6
6 8 8
8 10 1

-- !test_alias_sql --
test_view_alias CREATE VIEW `test_view_alias` COMMENT 'VIEW' AS (\n select `t`.`c8` AS `c1`, `t`.`c2` AS `c2`, `t`.`c1` AS `c3` from (select a+1 c8,abs(a)+2+1 as c2, cast(b as varchar(10)) as c1 from mal_test_view) t); utf8mb4 utf8mb4_0900_bin

3 changes: 1 addition & 2 deletions regression-test/suites/ddl_p0/test_create_view.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
// under the License.

suite("test_create_view") {
sql "SET enable_nereids_planner=true;"
sql "SET enable_fallback_to_original_planner=false;"
sql "SET enable_nereids_planner=false;"
sql """DROP TABLE IF EXISTS count_distinct"""
sql """
CREATE TABLE IF NOT EXISTS count_distinct
Expand Down
Loading

0 comments on commit 4cf6fdd

Please sign in to comment.