[MCOL-5613] Combining CTE, cast, and aggregate results in 0 Created: 2023-11-15  Updated: 2023-11-16

Status: Open
Project: MariaDB ColumnStore
Component/s: None
Affects Version/s: 23.02
Fix Version/s: None

Type: Bug Priority: Major
Reporter: suresh ramagiri Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None


 Description   

The query below does not work in CS 23.10.0 or CS 5.6.8 compared to innodb. Both show no warnings when it should

with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t group by c, cc) select c, sum(ii) from a group by c order by c;

However in CS 23.10.0 it returns all 0's, where as in CS 5.6.8 its bogus out of range numbers

Work Around - columnstore_select_handler=OFF

set session columnstore_select_handler=OFF; with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t group by c, cc) select c, sum(ii) from a group by c order by c;

We noticed in CS 23.10.0 when adding the CAST function as used, we get the 0's result, which is not valid.

# Behaves the same in CS 5.8 & CS 23.10.0 without the CAST( as Decimal) that columnstore and innodb produce the same result
mariadb -e 'with a as (select c, cc, sum(i) as ii from test.t group by c, cc) select c, sum(ii) from a group by c order by c;'
mariadb -e 'with a as (select c, cc, sum(i) as ii from test.ti group by c, cc) select c, sum(ii) from a group by c order by c;'

REPRODUCTION
Setup

yum install wget jq -y
wget https://raw.githubusercontent.com/mariadb-corporation/mariadb-columnstore-engine/develop/extra/cs_package_manager.sh; chmod +x cs_package_manager.sh;
bash cs_package_manager.sh install enterprise 10.6.15-10 --token XXXXXXXXXX
 
mariadb -e 'create database if not exists test; use test; drop table if exists test.t; drop table if exists test.ti; create table if not exists test.t (c char(1), cc char(1), i int) engine columnstore;create table if not exists test.ti (c char(1), cc char(1), i int) engine innodb;'
dnf install -y golang
go install github.com/StabbyCutyou/moldova/cmd/moldova@latest
~/go/bin/moldova -t "{ascii:length:1},{ascii:length:1},{int:max:9999999}" -n 10000 | cpimport -s, test t
mariadb test -e 'insert into ti select * from t;'

Query Columnstore 23.10.0 - notice 0's for every result

mariadb --show-warnings -e 'with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t group by c, cc) select c, sum(ii) from a group by c order by c;'
.....
| v    |   0.000 |
| w    |   0.000 |
| x    |   0.000 |
| y    |   0.000 |
+------+---------+

Query InnoDB - also notice all the warnings thrown alerting users of the overflow

mariadb --show-warnings -e 'with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.ti group by c, cc) select c, sum(ii) from a group by c order by c;'
.....
| v    | 346902230.967 |
| w    | 349354321.966 |
| x    | 346987098.966 |
| y    | 349999999.965 |
+------+---------------+
Warning (Code 1264): Out of range value for column 'ii' at row 1
Warning (Code 1264): Out of range value for column 'ii' at row 1

In CS 5.6.8 - no all 0's but still wrong result

mariadb --show-warnings -e 'with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t group by c, cc) select c, sum(ii) from a group by c order by c;'
.....
| v    | 1541954575.000 |
| n    | 1400597401.000 |
| s    | 1526268264.000 |
| e    | 1296676521.000 |
+------+----------------+



 Comments   
Comment by suresh ramagiri [ 2023-11-16 ]

Just want to clarify, I tested for the data without overflowing, i.e., while importing the data restricted the max value to 9999

This is at Columnstore 5, MariaDB 10.5.22-16

REPRODUCTION SETUP

mariadb -e 'drop table test.t; create table test.t (c char(1), cc char(1), i int) engine columnstore;'
dnf install -y golang
go install github.com/StabbyCutyou/moldova/cmd/moldova@latest
~/go/bin/moldova -t "{ascii:length:1},{ascii:length:1},{int:max:999}" -n 10000 | cpimport -s, test t
mariadb -e "create table test.t_innodb1 as select * from t;" # create innodb table

I can see, both at the Columnstore 5 table and the innoDB Table, result is same for the noted query of cast, int to decimal conversion type.
It indicates regression, working fine at Columnstore 5, where as at Columnstore 23.08 getting us result as 0, for that noted query.

Result:

MariaDB [test]> select @@version;
+-----------------------------------+
| @@version                         |
+-----------------------------------+
| 10.5.22-16-MariaDB-enterprise-log |
+-----------------------------------+
1 row in set (0.000 sec)
 
MariaDB [test]> select calgetversion();
+-----------------+
| calgetversion() |
+-----------------+
| 5.6.8           |
+-----------------+
1 row in set (0.000 sec)
 
MariaDB [test]> with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t group by c, cc) select c, sum(ii) from a group by c;
+------+------------+
| c    | sum(ii)    |
+------+------------+
| x    | 132340.000 |
| b    | 140425.000 |
| i    | 154261.000 |
| f    | 143061.000 |
| g    | 151457.000 |
| k    | 136777.000 |
| y    | 132906.000 |
| t    | 143604.000 |
| 2    | 135815.000 |
| p    | 145705.000 |
| 9    | 151430.000 |
| q    | 154360.000 |
| 6    | 159708.000 |
| 0    | 142415.000 |
| r    | 140907.000 |
| l    | 139133.000 |
| u    | 134795.000 |
| 4    | 136558.000 |
| 8    | 142249.000 |
| h    | 142498.000 |
| o    | 134150.000 |
| 1    | 145282.000 |
| a    | 150699.000 |
| n    | 138317.000 |
| c    | 115768.000 |
| 5    | 164846.000 |
| v    | 115057.000 |
| s    | 143960.000 |
| j    | 140053.000 |
| 7    | 152186.000 |
| w    | 141957.000 |
| 3    | 144300.000 |
| e    | 147712.000 |
| d    | 147721.000 |
| m    | 131516.000 |
+------+------------+
35 rows in set (0.016 sec)
 
MariaDB [test]> insert into test.t_innodb1 select * from test.t;
Query OK, 10000 rows affected (0.089 sec)
Records: 10000  Duplicates: 0  Warnings: 0
 
MariaDB [test]> with a as (select c, cc, cast(sum(i) as decimal(10,3)) as ii from test.t_innodb1 group by c, cc) select c, sum(ii) from a group by c;
+------+------------+
| c    | sum(ii)    |
+------+------------+
| 0    | 142415.000 |
| 1    | 145282.000 |
| 2    | 135815.000 |
| 3    | 144300.000 |
| 4    | 136558.000 |
| 5    | 164846.000 |
| 6    | 159708.000 |
| 7    | 152186.000 |
| 8    | 142249.000 |
| 9    | 151430.000 |
| a    | 150699.000 |
| b    | 140425.000 |
| c    | 115768.000 |
| d    | 147721.000 |
| e    | 147712.000 |
| f    | 143061.000 |
| g    | 151457.000 |
| h    | 142498.000 |
| i    | 154261.000 |
| j    | 140053.000 |
| k    | 136777.000 |
| l    | 139133.000 |
| m    | 131516.000 |
| n    | 138317.000 |
| o    | 134150.000 |
| p    | 145705.000 |
| q    | 154360.000 |
| r    | 140907.000 |
| s    | 143960.000 |
| t    | 143604.000 |
| u    | 134795.000 |
| v    | 115057.000 |
| w    | 141957.000 |
| x    | 132340.000 |
| y    | 132906.000 |
+------+------------+
35 rows in set (0.012 sec)
 
 

Generated at Thu Feb 08 02:59:10 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.