aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tests/sql/suites/agg_phases
diff options
context:
space:
mode:
authorudovichenko-r <udovichenko-r@yandex-team.com>2024-11-19 14:58:38 +0300
committerudovichenko-r <udovichenko-r@yandex-team.com>2024-11-19 15:16:27 +0300
commit24521403b1c44303e043ba540c09b1fe991c7474 (patch)
tree341d1e7206bc7c143d04d2d96f05b6dc0655606d /yql/essentials/tests/sql/suites/agg_phases
parent72b3cd51dc3fb9d16975d353ea82fd85701393cc (diff)
downloadydb-24521403b1c44303e043ba540c09b1fe991c7474.tar.gz
YQL-19206 Move contrib/ydb/library/yql/tests/sql/suites -> yql/essentials/tests/sql/suites
commit_hash:d0ef1f92b09c94db7c2408f946d2a4c62b603f00
Diffstat (limited to 'yql/essentials/tests/sql/suites/agg_phases')
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/avg.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/avg_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/avg_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count_all.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count_all_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count_all_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/count_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/default.cfg2
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/max.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/max_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/max_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min_by.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min_by_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min_by_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/min_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/percentile.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/percentile_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/percentile_opt.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/sum.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/sum_null.sql80
-rw-r--r--yql/essentials/tests/sql/suites/agg_phases/sum_opt.sql80
25 files changed, 1922 insertions, 0 deletions
diff --git a/yql/essentials/tests/sql/suites/agg_phases/avg.sql b/yql/essentials/tests/sql/suites/agg_phases/avg.sql
new file mode 100644
index 0000000000..c4ee64fe05
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/avg.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,avg(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/avg_null.sql b/yql/essentials/tests/sql/suites/agg_phases/avg_null.sql
new file mode 100644
index 0000000000..5399da509a
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/avg_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,avg(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/avg_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/avg_opt.sql
new file mode 100644
index 0000000000..4d65617197
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/avg_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,avg(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,avg(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,avg(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count.sql b/yql/essentials/tests/sql/suites/agg_phases/count.sql
new file mode 100644
index 0000000000..56ebee9864
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count_all.sql b/yql/essentials/tests/sql/suites/agg_phases/count_all.sql
new file mode 100644
index 0000000000..b84047ec4d
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count_all.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count_all_null.sql b/yql/essentials/tests/sql/suites/agg_phases/count_all_null.sql
new file mode 100644
index 0000000000..d66d1d3690
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count_all_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count_all_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/count_all_opt.sql
new file mode 100644
index 0000000000..cd818df3fd
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count_all_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(*) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count_null.sql b/yql/essentials/tests/sql/suites/agg_phases/count_null.sql
new file mode 100644
index 0000000000..428e6b1e9b
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/count_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/count_opt.sql
new file mode 100644
index 0000000000..97df3a4e5a
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/count_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,count(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,count(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,count(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/default.cfg b/yql/essentials/tests/sql/suites/agg_phases/default.cfg
new file mode 100644
index 0000000000..800d911a03
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/default.cfg
@@ -0,0 +1,2 @@
+udf stat_udf
+udf set_udf \ No newline at end of file
diff --git a/yql/essentials/tests/sql/suites/agg_phases/max.sql b/yql/essentials/tests/sql/suites/agg_phases/max.sql
new file mode 100644
index 0000000000..bc80374fa6
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/max.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,max(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/max_null.sql b/yql/essentials/tests/sql/suites/agg_phases/max_null.sql
new file mode 100644
index 0000000000..381d745ab5
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/max_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,max(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/max_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/max_opt.sql
new file mode 100644
index 0000000000..00976d77bf
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/max_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,max(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,max(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,max(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min.sql b/yql/essentials/tests/sql/suites/agg_phases/min.sql
new file mode 100644
index 0000000000..3b10505ebf
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min_by.sql b/yql/essentials/tests/sql/suites/agg_phases/min_by.sql
new file mode 100644
index 0000000000..ad1fbc54c4
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min_by.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min_by(a,a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min_by_null.sql b/yql/essentials/tests/sql/suites/agg_phases/min_by_null.sql
new file mode 100644
index 0000000000..d6b2cac34b
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min_by_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min_by(a,a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min_by_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/min_by_opt.sql
new file mode 100644
index 0000000000..6382a1bee9
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min_by_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min_by(a,a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min_by(a,a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min_by(value,value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min_null.sql b/yql/essentials/tests/sql/suites/agg_phases/min_null.sql
new file mode 100644
index 0000000000..3e2185e44f
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/min_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/min_opt.sql
new file mode 100644
index 0000000000..268c0d3347
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/min_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,min(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,min(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,min(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/percentile.sql b/yql/essentials/tests/sql/suites/agg_phases/percentile.sql
new file mode 100644
index 0000000000..2ef5c32855
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/percentile.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,percentile(a, 0.1) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/percentile_null.sql b/yql/essentials/tests/sql/suites/agg_phases/percentile_null.sql
new file mode 100644
index 0000000000..6a94334f4f
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/percentile_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,percentile(a, 0.1) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/percentile_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/percentile_opt.sql
new file mode 100644
index 0000000000..fd7dd6e529
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/percentile_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,percentile(a, 0.1) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,percentile(a, 0.1) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,percentile(value, 0.1) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/sum.sql b/yql/essentials/tests/sql/suites/agg_phases/sum.sql
new file mode 100644
index 0000000000..5d9f78108c
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/sum.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: 2|>,<|key: 1, value: 3|>]);
+
+$p =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,sum(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/sum_null.sql b/yql/essentials/tests/sql/suites/agg_phases/sum_null.sql
new file mode 100644
index 0000000000..d20e645c5b
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/sum_null.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: null|>,<|key: 1, value: null|>]);
+
+$p =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,sum(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+
diff --git a/yql/essentials/tests/sql/suites/agg_phases/sum_opt.sql b/yql/essentials/tests/sql/suites/agg_phases/sum_opt.sql
new file mode 100644
index 0000000000..2759e2c08c
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/agg_phases/sum_opt.sql
@@ -0,0 +1,80 @@
+/* syntax version 1 */
+/* postgres can not */
+
+$t = SELECT * FROM AS_TABLE([<|key: 1, value: just(2)|>,<|key: 1, value: just(3)|>]);
+
+$p =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with combine
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with combinestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+$p = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergestate
+ ;
+
+$p = PROCESS $p;
+--select FormatType(TypeOf($p));
+select * from $p;
+
+
+$p1 = SELECT
+ key,sum(a) as a
+FROM $p
+GROUP BY
+ key
+ with mergefinalize
+ ;
+
+$p1 = PROCESS $p1;
+--select FormatType(TypeOf($p1));
+select * from $p1;
+
+$p2 = SELECT
+ key,sum(a) as a
+FROM (select key, just(a) as a from $p)
+GROUP BY
+ key
+ with mergemanyfinalize
+ ;
+
+$p2 = PROCESS $p2;
+--select FormatType(TypeOf($p2));
+select * from $p2;
+
+$p3 =
+SELECT
+ key,sum(value) as a
+FROM $t
+GROUP BY
+ key
+ with finalize
+ ;
+
+$p3 = PROCESS $p3;
+--select FormatType(TypeOf($p));
+select * from $p3;
+