Skip to content

Commit

Permalink
[BugFix] fix mysql external table clause (backport #50901) (#51167)
Browse files Browse the repository at this point in the history
Co-authored-by: RyanZ <[email protected]>
  • Loading branch information
mergify[bot] and dirtysalt committed Sep 19, 2024
1 parent f4c3bcd commit 480608d
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 25 deletions.
54 changes: 29 additions & 25 deletions be/src/exec/mysql_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,39 @@ Status MysqlScanner::query(const std::string& table, const std::vector<std::stri

// In Filter part.
if (filters_in.size() > 0) {
if (!is_filter_initial) {
is_filter_initial = true;
_sql_str += " WHERE (";
} else {
_sql_str += " AND (";
}

bool is_first_conjunct = true;
std::vector<std::string> conjuncts;
for (auto& iter : filters_in) {
if (!is_first_conjunct) {
_sql_str += " AND (";
}
is_first_conjunct = false;
if (iter.second.size() > 0) {
auto curr = iter.second.begin();
auto end = iter.second.end();
_sql_str += iter.first + " in (";
_sql_str += *curr;
++curr;

// collect optional values.
while (curr != end) {
_sql_str += ", " + *curr;
++curr;
if (iter.second.size() > 0 || filters_null_in_set[iter.first]) {
std::string tmp;
tmp += "(" + iter.first + " in (";
for (auto& curr : iter.second) {
tmp += curr + ",";
}
if (filters_null_in_set[iter.first]) {
_sql_str += ", null";
tmp += "null";
}
if (tmp.back() == ',') {
tmp.pop_back();
}
_sql_str += ")) ";
tmp += "))";
conjuncts.emplace_back(tmp);
} else {
// there is in predicate, but no value in it
// so there is no row from mysql node.
conjuncts.emplace_back("(0 = 1)");
}
}

if (conjuncts.size() > 0) {
if (!is_filter_initial) {
is_filter_initial = true;
_sql_str += " WHERE ";
} else {
_sql_str += " AND ";
}
_sql_str += conjuncts.at(0);
for (size_t i = 1; i < conjuncts.size(); i++) {
_sql_str += " AND " + conjuncts.at(i);
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions test/sql/test_external_mysql/R/test_external_mysql_clause
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
-- name: testExternalMysqlCaluse
create database db_${uuid0};
-- result:
-- !result
use db_${uuid0};
-- result:
-- !result
CREATE TABLE `t00` (
`id` int,
`s` string
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"enable_persistent_index" = "false",
"replicated_storage" = "false",
"compression" = "LZ4"
);
-- result:
-- !result
insert into t00 values(1, "hello"), (2, "world"), (3, null);
-- result:
-- !result
CREATE EXTERNAL TABLE mysql_ext
(
`id` int,
`s` string
)
ENGINE=mysql
PROPERTIES
(
"host" = "${mysql_host}",
"port" = "${mysql_port}",
"user" = "${mysql_user}",
"password" = "${mysql_password}",
"database" = "db_${uuid0}",
"table" = "t00"
);
-- result:
-- !result
set cbo_derive_join_is_null_predicate = true;
-- result:
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id = 3;
-- result:
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2, 3);
-- result:
1 hello 1 hello
2 world 2 world
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2);
-- result:
1 hello 1 hello
2 world 2 world
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1);
-- result:
1 hello 1 hello
-- !result
set cbo_derive_join_is_null_predicate = false;
-- result:
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id = 3;
-- result:
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2, 3);
-- result:
1 hello 1 hello
2 world 2 world
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2);
-- result:
1 hello 1 hello
2 world 2 world
-- !result
select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1);
-- result:
1 hello 1 hello
-- !result
58 changes: 58 additions & 0 deletions test/sql/test_external_mysql/T/test_external_mysql_clause
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- name: testExternalMysqlCaluse

create database db_${uuid0};
use db_${uuid0};

CREATE TABLE `t00` (
`id` int,
`s` string
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"enable_persistent_index" = "false",
"replicated_storage" = "false",
"compression" = "LZ4"
);

insert into t00 values(1, "hello"), (2, "world"), (3, null);

CREATE EXTERNAL TABLE mysql_ext
(
`id` int,
`s` string
)
ENGINE=mysql
PROPERTIES
(
"host" = "${mysql_host}",
"port" = "${mysql_port}",
"user" = "${mysql_user}",
"password" = "${mysql_password}",
"database" = "db_${uuid0}",
"table" = "t00"
);

set cbo_derive_join_is_null_predicate = true;

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id = 3;

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2, 3);

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2);

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1);


set cbo_derive_join_is_null_predicate = false;

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id = 3;

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2, 3);

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1, 2);

select * from mysql_ext x inner join t00 on x.s = t00.s where t00.id in (1);

0 comments on commit 480608d

Please sign in to comment.