diff --git a/contrib/dolphin/expected/mysqlmode_fullgroup.out b/contrib/dolphin/expected/mysqlmode_fullgroup.out index 85024355bd880ecfe232bf0ed77fb5924876ff0b..53bb66e9f9735d94ac415b9321710ca9087cf3cb 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 a448dad4b448aef15e83c194f416a47a4d66afb5..99e4757c2abf125516c3e2b09ad5282926ace8ba 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 8ff4038fa6fba99d3b4c142ad671335d3c063ac3..3b4f5b2d421b6a87347797465422f712355394b0 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 d13256a082d7adf52f5ffe57f14f19d8e64e498e..b706ac7731f2a99a9e7ce52766a95d279d5ba0ab 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 56726b86463b8a173017976d67e9ab9e3a6e4133..3f72ec041238e01a5d98c435ae447c646019235e 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 274e7c968074b41667b2f1c3bc089c9b2c2604fa..ff4ec75044207612664d6e124019434de8f0b596 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;