From d7dcdfcba9347d0959ca5cdc003c8e011841184a Mon Sep 17 00:00:00 2001 From: Tiewei Fang <43782773+BePPPower@users.noreply.github.com> Date: Tue, 28 Mar 2023 15:07:32 +0800 Subject: [PATCH] [Fix](Create View) support create view from tvf (#18087) Support create view as select * from tvf() --- .../analysis/TableValuedFunctionRef.java | 24 ++++ .../test_create_view_from_tvf.out | 87 ++++++++++++++ .../test_create_view_from_tvf.groovy | 108 ++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 regression-test/data/correctness_p0/table_valued_function/test_create_view_from_tvf.out create mode 100644 regression-test/suites/correctness_p0/table_valued_function/test_create_view_from_tvf.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/TableValuedFunctionRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/TableValuedFunctionRef.java index 4871baf889..294e18665d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/TableValuedFunctionRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/TableValuedFunctionRef.java @@ -24,6 +24,7 @@ import org.apache.doris.planner.ScanNode; import org.apache.doris.tablefunction.TableValuedFunctionIf; import java.util.Map; +import java.util.stream.Collectors; public class TableValuedFunctionRef extends TableRef { @@ -64,6 +65,29 @@ public class TableValuedFunctionRef extends TableRef { return result; } + @Override + protected String tableNameToSql() { + String aliasSql = null; + String alias = getExplicitAlias(); + if (alias != null) { + aliasSql = ToSqlUtils.getIdentSql(alias); + } + + // set tableName + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(funcName); + stringBuilder.append('('); + String paramsString = params.entrySet().stream().map(kv -> "\"" + kv.getKey() + "\"" + + " = " + "\"" + kv.getValue() + "\"") + .collect(Collectors.joining(",")); + stringBuilder.append(paramsString); + stringBuilder.append(')'); + + // set alias + stringBuilder.append((aliasSql != null) ? " " + aliasSql : ""); + return stringBuilder.toString(); + } + /** * Register this table ref and then analyze the Join clause. */ diff --git a/regression-test/data/correctness_p0/table_valued_function/test_create_view_from_tvf.out b/regression-test/data/correctness_p0/table_valued_function/test_create_view_from_tvf.out new file mode 100644 index 0000000000..b0b9a04193 --- /dev/null +++ b/regression-test/data/correctness_p0/table_valued_function/test_create_view_from_tvf.out @@ -0,0 +1,87 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !view1 -- +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 + +-- !view2 -- +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 + +-- !count -- +100 + +-- !inner_join -- +0 0 +1 1 +2 2 +3 3 +4 4 +5 5 +6 6 +7 7 +8 8 +9 9 + +-- !left_join -- +0 0 +1 1 +2 2 +3 3 +4 4 +5 \N +6 \N +7 \N +8 \N +9 \N + +-- !where_equal -- +1 +3 +5 +7 +9 + +-- !where_lt -- +0 +1 +2 +3 +4 +5 +6 +7 + +-- !join_where -- +5 5 +6 6 +7 7 +8 8 +9 9 + +-- !groupby -- +4 +5 +6 +7 +8 +9 + +-- !subquery1 -- +1 + diff --git a/regression-test/suites/correctness_p0/table_valued_function/test_create_view_from_tvf.groovy b/regression-test/suites/correctness_p0/table_valued_function/test_create_view_from_tvf.groovy new file mode 100644 index 0000000000..c98cfb328e --- /dev/null +++ b/regression-test/suites/correctness_p0/table_valued_function/test_create_view_from_tvf.groovy @@ -0,0 +1,108 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + + suite("test_create_view_from_tvf") { + String testViewName = "test_view_from_number" + + def create_view = {createViewSql -> + sql """ drop view if exists ${testViewName} """ + sql createViewSql + } + + def sql1 = """ CREATE VIEW ${testViewName} + ( + k1 COMMENT "number" + ) + COMMENT "my first view" + AS + SELECT number as k1 FROM numbers("number" = "10"); + """ + create_view(sql1) + order_qt_view1 """ select * from ${testViewName} """ + + + def sql2 = """ CREATE VIEW ${testViewName} + AS + SELECT * FROM numbers("number" = "10"); + """ + create_view(sql2) + order_qt_view2 """ select * from ${testViewName} """ + + def sql_count = """ CREATE VIEW ${testViewName} + AS + select count(*) from numbers("number" = "100"); + """ + create_view(sql_count) + order_qt_count """ select * from ${testViewName} """ + + def sql_inner_join = """ CREATE VIEW ${testViewName} + AS + select a.number as num1, b.number as num2 + from numbers("number" = "10") a inner join numbers("number" = "10") b + on a.number=b.number; + """ + create_view(sql_inner_join) + order_qt_inner_join """ select * from ${testViewName} """ + + def sql_left_join = """ CREATE VIEW ${testViewName} + AS + select a.number as num1, b.number as num2 + from numbers("number" = "10") a left join numbers("number" = "5") b + on a.number=b.number order by num1; + """ + create_view(sql_left_join) + order_qt_left_join """ select * from ${testViewName} """ + + def sql_where_equal = """ CREATE VIEW ${testViewName} + AS + select * from numbers("number" = "10") where number%2 = 1; + """ + create_view(sql_where_equal) + order_qt_where_equal """ select * from ${testViewName} """ + + def sql_where_lt = """ CREATE VIEW ${testViewName} + AS + select * from numbers("number" = "10") where number+1 < 9; + """ + create_view(sql_where_lt) + order_qt_where_lt """ select * from ${testViewName} """ + + def sql_join_where = """ CREATE VIEW ${testViewName} + AS + select a.number as num1, b.number as num2 + from numbers("number" = "10") a inner join numbers("number" = "10") b + on a.number=b.number where a.number>4; + """ + create_view(sql_join_where) + order_qt_join_where """ select * from ${testViewName} """ + + def sql_groupby = """ CREATE VIEW ${testViewName} + AS + select number from numbers("number" = "10") where number>=4 group by number order by number; + """ + create_view(sql_groupby) + order_qt_groupby """ select * from ${testViewName} """ + + def sql_subquery1 = """ CREATE VIEW ${testViewName} + AS + select * from numbers("number" = "10") where number = (select number from numbers("number" = "10") where number=1); + """ + create_view(sql_subquery1) + order_qt_subquery1 """ select * from ${testViewName} """ + + } + \ No newline at end of file