From 7cb3c68d1e0f7dd02bb664b2a3625ca1cf44f60a Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 31 Aug 2017 13:23:47 +0300 Subject: [PATCH] MXS-1380 Parse UNIONs completely Before this change, only db2.t2 was reported as table name for a select like select * from db1.t1 union select * from db2.t2 With this change, db1.t1 and db2.t2 are reported. --- query_classifier/qc_sqlite/qc_sqlite.c | 40 ++++++++++++++++++++------ query_classifier/test/insert.test | 4 ++- query_classifier/test/maxscale.test | 2 ++ query_classifier/test/select.test | 10 ++++--- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/query_classifier/qc_sqlite/qc_sqlite.c b/query_classifier/qc_sqlite/qc_sqlite.c index 1fbb4981e..ab9e84fc5 100644 --- a/query_classifier/qc_sqlite/qc_sqlite.c +++ b/query_classifier/qc_sqlite/qc_sqlite.c @@ -167,10 +167,18 @@ static void update_field_infos_from_idlist(QC_SQLITE_INFO* info, const IdList* pIds, uint32_t usage, const ExprList* pExclude); -static void update_field_infos_from_select(QC_SQLITE_INFO* info, - const Select* pSelect, - uint32_t usage, - const ExprList* pExclude); +typedef enum compound_approach +{ + ANALYZE_COMPOUND_SELECTS, + IGNORE_COMPOUND_SELECTS +} compound_approach_t; +static void update_field_infos_from_select_compound(QC_SQLITE_INFO* info, + const Select* pSelect, + uint32_t usage, + const ExprList* pExclude, + compound_approach_t compound_approach); +#define update_field_infos_from_select(i, s, u, e)\ + update_field_infos_from_select_compound(i, s, u, e, ANALYZE_COMPOUND_SELECTS) static void update_function_info(QC_SQLITE_INFO* info, const char* name, uint32_t usage); @@ -1193,10 +1201,11 @@ static void update_field_infos_from_idlist(QC_SQLITE_INFO* info, } } -static void update_field_infos_from_select(QC_SQLITE_INFO* info, - const Select* pSelect, - uint32_t usage, - const ExprList* pExclude) +static void update_field_infos_from_select_compound(QC_SQLITE_INFO* info, + const Select* pSelect, + uint32_t usage, + const ExprList* pExclude, + compound_approach_t compound_approach) { if (pSelect->pSrc) { @@ -1257,6 +1266,21 @@ static void update_field_infos_from_select(QC_SQLITE_INFO* info, update_field_infos(info, 0, pSelect->pHaving, 0, QC_TOKEN_MIDDLE, pSelect->pEList); #endif } + + if (compound_approach == ANALYZE_COMPOUND_SELECTS) + { + if (((pSelect->op == TK_UNION) || (pSelect->op == TK_ALL)) && pSelect->pPrior) + { + const Select* pPrior = pSelect->pPrior; + + while (pPrior) + { + update_field_infos_from_select_compound(info, pPrior, usage, pExclude, + IGNORE_COMPOUND_SELECTS); + pPrior = pPrior->pPrior; + } + } + } } static void update_database_names(QC_SQLITE_INFO* info, const char* zDatabase) diff --git a/query_classifier/test/insert.test b/query_classifier/test/insert.test index 04a5aaa73..6bf0eb805 100644 --- a/query_classifier/test/insert.test +++ b/query_classifier/test/insert.test @@ -523,7 +523,9 @@ insert t1 (data) values ('letter'), (1/0); --disable_ps_protocol update t1 set data='envelope' where 1/0 or 1; --enable_ps_protocol -insert t1 (data) values (default), (1/0), ('dead beef'); +#MXS qc_mysqlembedded does not return all functions +#MXS qc_get_function_info : ERR: != /() +#MXS insert t1 (data) values (default), (1/0), ('dead beef'); --disable_info set sql_mode=default; diff --git a/query_classifier/test/maxscale.test b/query_classifier/test/maxscale.test index 52a10478c..960149b62 100644 --- a/query_classifier/test/maxscale.test +++ b/query_classifier/test/maxscale.test @@ -79,3 +79,5 @@ ROLLBACK TO id; RELEASE SAVEPOINT id; SELECT her FROM (SELECT @@server_id as her) as t WHERE her REGEXP '.*'; + +select * from db1.t1 union select * from db2.t2 diff --git a/query_classifier/test/select.test b/query_classifier/test/select.test index ae8787224..451859562 100644 --- a/query_classifier/test/select.test +++ b/query_classifier/test/select.test @@ -3296,10 +3296,12 @@ DROP TABLE t1,t2; # create table t1 (a bigint unsigned); -insert into t1 values - (if(1, 9223372036854775808, 1)), - (case when 1 then 9223372036854775808 else 1 end), - (coalesce(9223372036854775808, 1)); +#MXS qc_mysqlembedded does not return all functions +#MXS qc_get_function_info : ERR: coalesce() != case() coalesce() if() +#insert into t1 values +# (if(1, 9223372036854775808, 1)), +# (case when 1 then 9223372036854775808 else 1 end), +# (coalesce(9223372036854775808, 1)); select * from t1; drop table t1; create table t1 select