From aba90cb56cbe2ed0479ac7dc92396c68011c749e Mon Sep 17 00:00:00 2001 From: totaj Date: Fri, 22 Sep 2023 19:00:58 +0800 Subject: [PATCH] Fix only full group by. --- .../dolphin/expected/mysqlmode_fullgroup.out | 35 +++++++++++++++++++ contrib/dolphin/include/plugin_postgres.h | 1 + .../dolphin/plugin_optimizer/plan/planner.cpp | 9 ++++- contrib/dolphin/plugin_parser/parse_agg.cpp | 6 +++- contrib/dolphin/plugin_postgres.cpp | 1 + contrib/dolphin/sql/mysqlmode_fullgroup.sql | 11 ++++++ 6 files changed, 61 insertions(+), 2 deletions(-) diff --git a/contrib/dolphin/expected/mysqlmode_fullgroup.out b/contrib/dolphin/expected/mysqlmode_fullgroup.out index 85024355b..53bb66e9f 100644 --- a/contrib/dolphin/expected/mysqlmode_fullgroup.out +++ b/contrib/dolphin/expected/mysqlmode_fullgroup.out @@ -24,6 +24,41 @@ ERROR: subquery uses ungrouped column "t.b" from outer query LINE 1: ...t.a, (select sum(b) from test_group i where i.b = t.b ) from... ^ set dolphin.sql_mode = ''; +--min/max without optimize +create table t1(a int, b int, primary key(b)); +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" +insert into t1 values(1,10); +--can't use index scan +explain(costs off) select a, max(b) from t1; + QUERY PLAN +---------------------- + Aggregate + -> Seq Scan on t1 +(2 rows) + +select a, max(b) from t1; + a | max +---+----- + 1 | 10 +(1 row) + +--use index scan +explain(costs off) select max(b) from t1; + QUERY PLAN +------------------------------------------------------------ + Result + InitPlan 1 (returns $0) + -> Limit + -> Index Only Scan Backward using t1_pkey on t1 +(4 rows) + +select max(b) from t1; + max +----- + 10 +(1 row) + +drop table t1; select a, b from test_group group by a; a | b ---+--- diff --git a/contrib/dolphin/include/plugin_postgres.h b/contrib/dolphin/include/plugin_postgres.h index a448dad4b..99e4757c2 100644 --- a/contrib/dolphin/include/plugin_postgres.h +++ b/contrib/dolphin/include/plugin_postgres.h @@ -164,6 +164,7 @@ typedef struct BSqlPluginContext { bool is_dolphin_call_stmt; bool is_binary_proto; bool is_ast_stmt; + bool group_by_error; #endif } bSqlPluginContext; diff --git a/contrib/dolphin/plugin_optimizer/plan/planner.cpp b/contrib/dolphin/plugin_optimizer/plan/planner.cpp index 8ff4038fa..3b4f5b2d4 100644 --- a/contrib/dolphin/plugin_optimizer/plan/planner.cpp +++ b/contrib/dolphin/plugin_optimizer/plan/planner.cpp @@ -3048,7 +3048,14 @@ static Plan* grouping_planner(PlannerInfo* root, double tuple_fraction) /* Set u_sess->opt_cxt.query_dop to forbidden the parallel of subplan. */ int dop_tmp = u_sess->opt_cxt.query_dop; u_sess->opt_cxt.query_dop = 1; - preprocess_minmax_aggregates(root, tlist); +#ifdef DOLPHIN + /* can't optimize min/max if we have group by error */ + if (!GetSessionContext()->group_by_error) { +#endif + preprocess_minmax_aggregates(root, tlist); +#ifdef DOLPHIN + } +#endif /* Reset u_sess->opt_cxt.query_dop. */ u_sess->opt_cxt.query_dop = dop_tmp; } diff --git a/contrib/dolphin/plugin_parser/parse_agg.cpp b/contrib/dolphin/plugin_parser/parse_agg.cpp index d13256a08..b706ac773 100644 --- a/contrib/dolphin/plugin_parser/parse_agg.cpp +++ b/contrib/dolphin/plugin_parser/parse_agg.cpp @@ -418,7 +418,10 @@ void parseCheckAggregates(ParseState* pstate, Query* qry) bool hasSelfRefRTEs = false; PlannerInfo* root = NULL; Node* clause = NULL; - +#ifdef DOLPHIN + /* reset value */ + GetSessionContext()->group_by_error = false; +#endif /* This should only be called if we found aggregates or grouping */ AssertEreport(pstate->p_hasAggs || qry->groupClause || qry->havingQual || qry->groupingSets, MOD_OPT, @@ -1403,6 +1406,7 @@ static bool check_ungrouped_columns_walker(Node* node, check_ungrouped_columns_c } #ifdef DOLPHIN } + GetSessionContext()->group_by_error = true; #endif if (attname != NULL) { pfree_ext(attname); diff --git a/contrib/dolphin/plugin_postgres.cpp b/contrib/dolphin/plugin_postgres.cpp index 56726b864..3f72ec041 100644 --- a/contrib/dolphin/plugin_postgres.cpp +++ b/contrib/dolphin/plugin_postgres.cpp @@ -918,6 +918,7 @@ void init_session_vars(void) cxt->is_dolphin_call_stmt = false; cxt->is_binary_proto = false; cxt->is_ast_stmt = false; + cxt->group_by_error = false; DefineCustomBoolVariable("dolphin.b_compatibility_mode", "Enable mysql behavior override opengauss's when collision happens.", diff --git a/contrib/dolphin/sql/mysqlmode_fullgroup.sql b/contrib/dolphin/sql/mysqlmode_fullgroup.sql index 274e7c968..ff4ec7504 100644 --- a/contrib/dolphin/sql/mysqlmode_fullgroup.sql +++ b/contrib/dolphin/sql/mysqlmode_fullgroup.sql @@ -15,6 +15,17 @@ select a, b from test_group group by a; select a, d as items, (select count(*) from test_group t where t.a = i.a and b in (select b from test_group1 where c = 4)) as third from test_group i group by a; select t.a, (select sum(b) from test_group i where i.b = t.b ) from test_group t where t.a > 1+1 or (t.b < 8 and t.b > 1) group by t.a; set dolphin.sql_mode = ''; +--min/max without optimize +create table t1(a int, b int, primary key(b)); +insert into t1 values(1,10); +--can't use index scan +explain(costs off) select a, max(b) from t1; +select a, max(b) from t1; +--use index scan +explain(costs off) select max(b) from t1; +select max(b) from t1; +drop table t1; + select a, b from test_group group by a; select a, d as items, (select count(*) from test_group t where t.a = i.a and b in (select b from test_group1 where c = 4)) as third from test_group i group by a; select t.a, (select sum(b) from test_group i where i.b = t.b ) from test_group t where t.a > 1+1 or (t.b < 8 and t.b > 1) group by t.a; -- Gitee