1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-29 08:21:15 +03:00

MCOL-4234 and MCOL 5772 cherry-picked into [stable 23.10] (#3226)

* MCOL-4234: improve GROUP BY and ORDER BY interaction (#3194)

This patch fixes the problem in MCOL-4234 and also generally improves
behavior of GROUP BY.

It does so by introducing a "dummy" aggregate and by wrapping columns
into it. This allows for columns that are not in GROUP BY to be used
more freely, for example, in SELECT * FROM tbl GROUP BY col - all
columns that are not "col" will be wrapped into an aggregate and query
will proceed to execution.

The dummy aggregate itself does nothing more than remember last value
passed into it.

There also an additional error message that tries to explain what types
of expressions can be wrapped into an aggregate.

* MCOL-5772: incorrect ORDER BY ordering for a columns not in GROUP BY (#3214)

When ORDER BY column is not in GROUP BY, is not an aggregate and there
is a SELECT column that is also not an aggregate, there was a problem:
ordering happened on the SELECTed column, not ORDERed one.

This patch fixes that particular problem and also performs some tidying
around newly added aggregate.

---------

Co-authored-by: Leonid Fedorov <79837786+mariadb-LeonidFedorov@users.noreply.github.com>
This commit is contained in:
Sergey Zefirov
2024-06-27 23:31:53 +03:00
committed by GitHub
parent 9f4231f87f
commit db4cb1d657
18 changed files with 370 additions and 24 deletions

View File

@ -0,0 +1,23 @@
DROP DATABASE IF EXISTS MCOL5772;
CREATE DATABASE MCOL5772;
USE MCOL5772;
CREATE TABLE products (
product_id INT,
product_name VARCHAR(100),
category VARCHAR(50),
unit_price DECIMAL(10, 2),
stock_quantity INT
) ENGINE=Columnstore;
INSERT INTO products VALUES
(1, 'Laptop', 'Electronics', 1200.00, 50),
(2, 'Smartphone', 'Electronics', 800.00, 100),
(3, 'Coffee Maker', 'Appliances', 50.00, 30),
(4, 'Backpack', 'Fashion', 40.00, 80),
(5, 'Desk Chair', 'Furniture', 150.00, 20);
SELECT product_name, SUM(stock_quantity) AS total_stock FROM products GROUP BY category ORDER BY stock_quantity;
product_name total_stock
Desk Chair 20
Coffee Maker 30
Backpack 80
Smartphone 150
DROP DATABASE MCOL5772;

View File

@ -80,7 +80,8 @@ SET columnstore_select_handler=ON;
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2;
ERROR 42000: The storage engine for the table doesn't support MCS-2016: Non supported item 'col2' on the GROUP BY list.
SELECT col1 c FROM t1 ORDER BY AVG(col1);
ERROR HY000: Internal error: MCS-2021: 'c' is not in GROUP BY clause. All non-aggregate columns in the SELECT and ORDER BY clause must be included in the GROUP BY clause.
c
10
SET columnstore_select_handler=AUTO;
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2;
col2

View File

@ -56,7 +56,9 @@ NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
SELECT * FROM t1 GROUP BY t1_tinyint;
ERROR HY000: Internal error: MCS-2021: '`mcs35_db1`.`t1`.`t1_int`' is not in GROUP BY clause. All non-aggregate columns in the SELECT and ORDER BY clause must be included in the GROUP BY clause.
t1_tinyint t1_int t1_bigint t1_double t1_float t1_blob t1_text t1_char t1_varchar t1_datetime
0 NULL 403685477580676 54.797693231 8.40287 222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd d hello world! 4387-11-08 11:22:30
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
SELECT COUNT(*) FROM t1 GROUP BY t1_tinyint;
COUNT(*)
11

View File

@ -69,7 +69,11 @@ spID userid MIN(t1.score)
3 3 3
SELECT t1.spID, t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid, t1.spID ORDER BY t1.date;
ERROR HY000: Internal error: MCS-2021: 'mcs36_db1.t1.date' is not in GROUP BY clause. All non-aggregate columns in the SELECT and ORDER BY clause must be included in the GROUP BY clause.
spID userid MIN(t1.score)
1 1 1
2 1 1
2 2 2
3 3 3
SELECT t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY unknown ORDER BY t2.userid;
ERROR 42S22: Unknown column 'unknown' in 'group statement'
@ -78,5 +82,20 @@ FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY unknown;
ERROR 42S22: Unknown column 'unknown' in 'order clause'
SELECT t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY NULL;
ERROR HY000: Internal error: MCS-2021: 'unknown db.unknown table.unknown field' is not in GROUP BY clause. All non-aggregate columns in the SELECT and ORDER BY clause must be included in the GROUP BY clause.
userid MIN(t1.score)
1 1
2 2
3 3
SELECT * FROM t1 GROUP BY spID, userID ORDER BY score ASC, spID, userID;
spID userID score lsg date
1 1 1 0000-00-00
2 1 1 0000-00-00
2 2 2 0000-00-00
3 3 3 0000-00-00
SELECT * FROM t1 GROUP BY spID, userID ORDER BY score DESC, spID, userID;
spID userID score lsg date
3 3 3 0000-00-00
2 2 2 0000-00-00
1 1 1 0000-00-00
2 1 1 0000-00-00
DROP DATABASE IF EXISTS mcs36_db1;

View File

@ -23,7 +23,9 @@ col1 col2
3 sss
4 ooo
SELECT col1, col2, SUM(LENGTH(col2)) FROM t1 GROUP BY col1 HAVING col1 > 1 AND col2 LIKE '%o%' ORDER BY col1;
ERROR HY000: Internal error: MCS-2021: '`mcs76_db`.`t1`.`col2`' is not in GROUP BY clause. All non-aggregate columns in the SELECT and ORDER BY clause must be included in the GROUP BY clause.
col1 col2 SUM(LENGTH(col2))
2 oooooooooooooooooooo 40
4 ooo 6
CREATE TABLE t2(col1 INT, col2 DATETIME)ENGINE=Columnstore;
INSERT INTO t2 VALUES(1, '2020-2-2'),(2, '2020-3-3'),(5,'2020-6-6'),(6, '2020-7-7');
SELECT t1.col1, SUM(t1.col1*t2.col1) AS a FROM t1 JOIN t2 ON t1.col1 = t2.col1 GROUP BY t1.col1 HAVING a>1 ORDER BY t1.col1;

View File

@ -0,0 +1,28 @@
# Order by a column that is not in GROUP BY and SELECT parts
# should be correct.
--disable_warnings
DROP DATABASE IF EXISTS MCOL5772;
--enable_warnings
CREATE DATABASE MCOL5772;
USE MCOL5772;
CREATE TABLE products (
product_id INT,
product_name VARCHAR(100),
category VARCHAR(50),
unit_price DECIMAL(10, 2),
stock_quantity INT
) ENGINE=Columnstore;
INSERT INTO products VALUES
(1, 'Laptop', 'Electronics', 1200.00, 50),
(2, 'Smartphone', 'Electronics', 800.00, 100),
(3, 'Coffee Maker', 'Appliances', 50.00, 30),
(4, 'Backpack', 'Fashion', 40.00, 80),
(5, 'Desk Chair', 'Furniture', 150.00, 20);
SELECT product_name, SUM(stock_quantity) AS total_stock FROM products GROUP BY category ORDER BY stock_quantity;
DROP DATABASE MCOL5772;

View File

@ -85,7 +85,6 @@ INSERT INTO t1 VALUES(10,'bob',10);
SET columnstore_select_handler=ON;
--error 1178
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2;
--error 1815
SELECT col1 c FROM t1 ORDER BY AVG(col1);
SET columnstore_select_handler=AUTO;
SELECT SUM(col1) AS col2 FROM t1 GROUP BY col2;

View File

@ -52,7 +52,7 @@ INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NUL
--sorted_result
SELECT * FROM t1;
#Without aggregate function
--error 1815
--sorted_result
SELECT * FROM t1 GROUP BY t1_tinyint;
--sorted_result

View File

@ -60,8 +60,7 @@ SELECT t1.spID, t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid, t1.spID ORDER BY t1.spID, t2.userid;
# columns in ORDER BY clause not included in the GROUP BY clause.
#Suspected bug. Innodb succeeds.
--error 1815
--sorted_result
SELECT t1.spID, t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid, t1.spID ORDER BY t1.date;
@ -76,10 +75,13 @@ SELECT t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY unknown;
# order by null
#Suspected bug. Innodb succeeds.
--error 1815
# Innodb succeeds - it is a workaround to speed up GROUP BY.
--sorted_result
SELECT t2.userid, MIN(t1.score)
FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY NULL;
SELECT * FROM t1 GROUP BY spID, userID ORDER BY score ASC, spID, userID;
SELECT * FROM t1 GROUP BY spID, userID ORDER BY score DESC, spID, userID;
#Clean up
DROP DATABASE IF EXISTS mcs36_db1;

View File

@ -18,7 +18,7 @@ SELECT col2 FROM t1 GROUP BY col2 HAVING col2 LIKE '%o%' ORDER BY col2;
SELECT col1 FROM t1 GROUP BY col1 HAVING col1 > 1 ORDER BY col1;
SELECT col1, col2 FROM t1 GROUP BY col1, col2 HAVING col1 > 1 AND col2 LIKE '%o%' ORDER BY col1;
SELECT col1, col2 FROM t1 GROUP BY col1, col2 HAVING col1 > 1 OR col2 LIKE '%o%' ORDER BY col1;
--error ER_INTERNAL_ERROR
--sorted_result
SELECT col1, col2, SUM(LENGTH(col2)) FROM t1 GROUP BY col1 HAVING col1 > 1 AND col2 LIKE '%o%' ORDER BY col1;
CREATE TABLE t2(col1 INT, col2 DATETIME)ENGINE=Columnstore;