Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore](nereids) Added compatibility with mysql alias filter #39738

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,8 +92,19 @@ public List<Slot> computeOutput() {
} else {
columnAlias = originSlot.getName();
}
List<String> originQualifier = originSlot.getQualifier();

ArrayList<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public View getView() {
return view;
}

@Override
public LogicalProperties getLogicalProperties() {
return child().getLogicalProperties();
}

@Override
public Plan withGroupExpression(Optional<GroupExpression> groupExpression) {
return new LogicalView(view, child());
Expand Down Expand Up @@ -125,7 +120,16 @@ public int hashCode() {

@Override
public List<Slot> computeOutput() {
return child().getOutput();
List<Slot> childOutput = child().getOutput();
ImmutableList.Builder<Slot> currentOutput = ImmutableList.builder();
List<String> 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
Expand Down
6 changes: 3 additions & 3 deletions regression-test/data/ddl_p0/test_create_view_nereids.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

24 changes: 24 additions & 0 deletions regression-test/data/nereids_syntax_p0/filter_with_alias.out
Original file line number Diff line number Diff line change
@@ -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

81 changes: 81 additions & 0 deletions regression-test/suites/nereids_syntax_p0/filter_with_alias.groovy
Original file line number Diff line number Diff line change
@@ -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 'filter_alias_test.test'"
}

// 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'"
}

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;"

}
Loading