From cf02d277944a27139247443d385158177b05d92d Mon Sep 17 00:00:00 2001 From: toms <94617906+Toms1999@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:58:24 +0800 Subject: [PATCH 1/2] [chore](nereids) Added compatibility with mysql alias filter --- .../plans/logical/LogicalSubQueryAlias.java | 15 +++- .../trees/plans/logical/LogicalView.java | 16 ++-- .../data/ddl_p0/test_create_view_nereids.out | 6 +- .../nereids_syntax_p0/filter_with_alias.out | 24 ++++++ .../filter_with_alias.groovy | 81 +++++++++++++++++++ 5 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 regression-test/data/nereids_syntax_p0/filter_with_alias.out create mode 100644 regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSubQueryAlias.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSubQueryAlias.java index 5d23561b1a954c..a6396f28618aa4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSubQueryAlias.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSubQueryAlias.java @@ -32,9 +32,11 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.apache.commons.lang3.StringUtils; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -90,8 +92,19 @@ public List computeOutput() { } else { columnAlias = originSlot.getName(); } + List originQualifier = originSlot.getQualifier(); + + ArrayList newQualifier = Lists.newArrayList(originQualifier); + if (newQualifier.size() >= qualifier.size()) { + for (int j = 0; j < qualifier.size(); j++) { + newQualifier.set(newQualifier.size() - qualifier.size() + j, qualifier.get(j)); + } + } else if (newQualifier.isEmpty()) { + newQualifier.addAll(qualifier); + } + Slot qualified = originSlot - .withQualifier(qualifier) + .withQualifier(newQualifier) .withName(columnAlias); currentOutput.add(qualified); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalView.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalView.java index 371d3cae43fcff..4f0468da055e5b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalView.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalView.java @@ -81,11 +81,6 @@ public View getView() { return view; } - @Override - public LogicalProperties getLogicalProperties() { - return child().getLogicalProperties(); - } - @Override public Plan withGroupExpression(Optional groupExpression) { return new LogicalView(view, child()); @@ -125,7 +120,16 @@ public int hashCode() { @Override public List computeOutput() { - return child().getOutput(); + List childOutput = child().getOutput(); + ImmutableList.Builder currentOutput = ImmutableList.builder(); + List fullQualifiers = this.view.getFullQualifiers(); + for (int i = 0; i < childOutput.size(); i++) { + Slot originSlot = childOutput.get(i); + Slot qualified = originSlot + .withQualifier(fullQualifiers); + currentOutput.add(qualified); + } + return currentOutput.build(); } @Override diff --git a/regression-test/data/ddl_p0/test_create_view_nereids.out b/regression-test/data/ddl_p0/test_create_view_nereids.out index b6db86eaf62156..8ee98fa7021977 100644 --- a/regression-test/data/ddl_p0/test_create_view_nereids.out +++ b/regression-test/data/ddl_p0/test_create_view_nereids.out @@ -224,7 +224,7 @@ test_backquote_in_view_define CREATE VIEW `test_backquote_in_view_define` AS sel 7 1 -- !test_backquote_in_table_alias_sql -- -test_backquote_in_table_alias CREATE VIEW `test_backquote_in_table_alias` AS select `ab``c`.`a` AS `c1`, `ab``c`.`b` AS `c2` from (select `internal`.`regression_test_ddl_p0`.`mal_test_view`.`a`,`internal`.`regression_test_ddl_p0`.`mal_test_view`.`b` from `internal`.`regression_test_ddl_p0`.`mal_test_view`) `ab``c`; utf8mb4 utf8mb4_0900_bin +test_backquote_in_table_alias CREATE VIEW `test_backquote_in_table_alias` AS select `internal`.`regression_test_ddl_p0`.`ab``c`.`a` AS `c1`, `internal`.`regression_test_ddl_p0`.`ab``c`.`b` AS `c2` from (select `internal`.`regression_test_ddl_p0`.`mal_test_view`.`a`,`internal`.`regression_test_ddl_p0`.`mal_test_view`.`b` from `internal`.`regression_test_ddl_p0`.`mal_test_view`) `ab``c`; utf8mb4 utf8mb4_0900_bin -- !test_generate -- 1 10 A 30 @@ -288,7 +288,7 @@ test_view_table_alias CREATE VIEW `test_view_table_alias` AS select `t`.`c1`, `t 4 40 -- !test_join_table_alias_sql -- -test_view_join_table_alias CREATE VIEW `test_view_join_table_alias` AS select `t`.`c1`, `t`.`c2` from (\n select `t1`.`id` as `c1`, `t1`.`value1` as `c2` from `internal`.`regression_test_ddl_p0`.`create_view_table1` `t1` inner join `internal`.`regression_test_ddl_p0`.`create_view_table2` `t2` on `t1`.`id`=`t2`.`id` limit 10) as `t`; utf8mb4 utf8mb4_0900_bin +test_view_join_table_alias CREATE VIEW `test_view_join_table_alias` AS select `t`.`c1`, `t`.`c2` from (\n select `internal`.`regression_test_ddl_p0`.`t1`.`id` as `c1`, `internal`.`regression_test_ddl_p0`.`t1`.`value1` as `c2` from `internal`.`regression_test_ddl_p0`.`create_view_table1` `t1` inner join `internal`.`regression_test_ddl_p0`.`create_view_table2` `t2` on `internal`.`regression_test_ddl_p0`.`t1`.`id`=`internal`.`regression_test_ddl_p0`.`t2`.`id` limit 10) as `t`; utf8mb4 utf8mb4_0900_bin -- !test_alias_udf -- 1****1 1 @@ -361,5 +361,5 @@ test_having CREATE VIEW `test_having` AS select sum(`internal`.`regression_test_ 2 200 1 -- !complicated_view1_sql -- -test_view_complicated CREATE VIEW `test_view_complicated` AS SELECT `t`.`id`, `t`.`value3`, `t`.`row_num` FROM (\n SELECT `t1`.`id`, `tt`.`value3`, ROW_NUMBER() OVER (PARTITION BY `t1`.`id` ORDER BY `tt`.`value3` DESC) as `row_num`\n FROM (SELECT `internal`.`regression_test_ddl_p0`.`create_view_table1`.`id` FROM `internal`.`regression_test_ddl_p0`.`create_view_table1` GROUP BY `internal`.`regression_test_ddl_p0`.`create_view_table1`.`id`) `t1`\n FULL OUTER JOIN (SELECT `internal`.`regression_test_ddl_p0`.`create_view_table2`.`value3`, `internal`.`regression_test_ddl_p0`.`create_view_table2`.`id`, MAX(`internal`.`regression_test_ddl_p0`.`create_view_table2`.`value4`) FROM `internal`.`regression_test_ddl_p0`.`create_view_table2` GROUP BY `internal`.`regression_test_ddl_p0`.`create_view_table2`.`value3`, `internal`.`regression_test_ddl_p0`.`create_view_table2`.`id`) `tt`\n ON `tt`.`id` = `t1`.`id`\n ORDER BY `t1`.`id`\n ) `t`\n WHERE `t`.`value3` < 280 AND (`t`.`id` < 3 or `t`.`id` >8); utf8mb4 utf8mb4_0900_bin +test_view_complicated CREATE VIEW `test_view_complicated` AS SELECT `internal`.`regression_test_ddl_p0`.`t`.`id`, `internal`.`regression_test_ddl_p0`.`t`.`value3`, `t`.`row_num` FROM (\n SELECT `internal`.`regression_test_ddl_p0`.`t1`.`id`, `internal`.`regression_test_ddl_p0`.`tt`.`value3`, ROW_NUMBER() OVER (PARTITION BY `internal`.`regression_test_ddl_p0`.`t1`.`id` ORDER BY `internal`.`regression_test_ddl_p0`.`tt`.`value3` DESC) as `row_num`\n FROM (SELECT `internal`.`regression_test_ddl_p0`.`create_view_table1`.`id` FROM `internal`.`regression_test_ddl_p0`.`create_view_table1` GROUP BY `internal`.`regression_test_ddl_p0`.`create_view_table1`.`id`) `t1`\n FULL OUTER JOIN (SELECT `internal`.`regression_test_ddl_p0`.`create_view_table2`.`value3`, `internal`.`regression_test_ddl_p0`.`create_view_table2`.`id`, MAX(`internal`.`regression_test_ddl_p0`.`create_view_table2`.`value4`) FROM `internal`.`regression_test_ddl_p0`.`create_view_table2` GROUP BY `internal`.`regression_test_ddl_p0`.`create_view_table2`.`value3`, `internal`.`regression_test_ddl_p0`.`create_view_table2`.`id`) `tt`\n ON `internal`.`regression_test_ddl_p0`.`tt`.`id` = `internal`.`regression_test_ddl_p0`.`t1`.`id`\n ORDER BY `internal`.`regression_test_ddl_p0`.`t1`.`id`\n ) `t`\n WHERE `internal`.`regression_test_ddl_p0`.`t`.`value3` < 280 AND (`internal`.`regression_test_ddl_p0`.`t`.`id` < 3 or `internal`.`regression_test_ddl_p0`.`t`.`id` >8); utf8mb4 utf8mb4_0900_bin diff --git a/regression-test/data/nereids_syntax_p0/filter_with_alias.out b/regression-test/data/nereids_syntax_p0/filter_with_alias.out new file mode 100644 index 00000000000000..9f98ed3a49e7e9 --- /dev/null +++ b/regression-test/data/nereids_syntax_p0/filter_with_alias.out @@ -0,0 +1,24 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !filter_select1 -- +1 Alice + +-- !filter_select2 -- +1 Alice + +-- !filter_select3 -- +1 Alice + +-- !filter_select4 -- +1 Alice + +-- !filter_select5 -- +1 Alice + +-- !filter_select6 -- +1 +2 +3 + +-- !filter_select7 -- +111 + diff --git a/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy b/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy new file mode 100644 index 00000000000000..d6b3c6e0eed892 --- /dev/null +++ b/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy @@ -0,0 +1,81 @@ +// 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("filter_with_alias") { + + sql "drop database if exists filter_alias_test;" + + sql """ CREATE DATABASE IF NOT EXISTS `filter_alias_test` """ + + sql """ + CREATE TABLE `filter_alias_test`.`test` ( + `id` int(11) NOT NULL, + `name` varchar(255) NULL + ) ENGINE = OLAP DUPLICATE KEY(`id`) COMMENT 'OLAP' DISTRIBUTED BY HASH(`id`) BUCKETS 10 PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", "storage_format" = "V2", + "disable_auto_compaction" = "false" + ); + """ + + sql """ + insert into `filter_alias_test`.`test` values (1, 'Alice'), (2, 'Bob'), (3, 'Carol'); + """ + test { + sql " select * from internal.filter_alias_test.test b where internal.filter_alias_test.test.id = 1;" + exception "Unknown column 'id' in 'internal.filter_alias_test.test' in FILTER clause" + } + + // Test using alias in WHERE clause directly + qt_filter_select1 """ + select * from `filter_alias_test`.`test` b where b.id = 1; + """ + + // Test using table name without alias in WHERE clause + qt_filter_select2 """ + select * from `filter_alias_test`.`test` where id = 1; + """ + + + test { + sql " select * from filter_alias_test.test b where filter_alias_test.test.id = 1;" + exception "Unknown column 'id' in 'filter_alias_test.test' in FILTER clause" + } + + qt_filter_select3 """ + select * from filter_alias_test.test where filter_alias_test.test.id = 1; + """ + + qt_filter_select4 """ + select * from filter_alias_test.test b where filter_alias_test.b.id = 1; + """ + + qt_filter_select5 """ + select * from internal.filter_alias_test.test b where internal.filter_alias_test.b.id = 1; + """ + + qt_filter_select6 """ + select * from (select id from filter_alias_test.test as b ) as toms order by id; + """ + + qt_filter_select7 """ + select 111 from (select current_date() as toms) as toms2; + """ + + sql "drop database if exists filter_alias_test;" + +} From 6f79f17fcde98ce5183579ea4cfdc0c1429128b7 Mon Sep 17 00:00:00 2001 From: Toms1999 Date: Thu, 22 Aug 2024 01:36:48 +0800 Subject: [PATCH 2/2] [chore](nereids) Added compatibility with mysql alias filter --- .../suites/nereids_syntax_p0/filter_with_alias.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy b/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy index d6b3c6e0eed892..e40ab3e3ae1a10 100644 --- a/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy +++ b/regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy @@ -37,7 +37,7 @@ suite("filter_with_alias") { """ test { sql " select * from internal.filter_alias_test.test b where internal.filter_alias_test.test.id = 1;" - exception "Unknown column 'id' in 'internal.filter_alias_test.test' in FILTER clause" + exception "Unknown column 'id' in 'filter_alias_test.test'" } // Test using alias in WHERE clause directly @@ -53,7 +53,7 @@ suite("filter_with_alias") { test { sql " select * from filter_alias_test.test b where filter_alias_test.test.id = 1;" - exception "Unknown column 'id' in 'filter_alias_test.test' in FILTER clause" + exception "Unknown column 'id' in 'filter_alias_test.test'" } qt_filter_select3 """