gluesql / gluesql

GlueSQL is quite sticky. It attaches to anywhere.
https://gluesql.org/docs
Apache License 2.0
2.68k stars 215 forks source link

Should be able to select the columns not appeared in the GROUP BY clause? #383

Open devgony opened 2 years ago

devgony commented 2 years ago

Currently in gluesql, we can select the columns not appeared in the GROUP BY clause.

GlueSQL 0.8.0

create table Item (
   id int
  ,ratio int
  ,city int
);

SELECT ratio FROM Item GROUP BY id, city; -- works!

In most databases, Above query throws a error.

Oracle 19.13.0.1.0

SELECT ratio FROM Item GROUP BY id, city
       *
ERROR at line 1:
ORA-00979: not a GROUP BY expression

Postgres 13.1

ERROR:  column "item.ratio" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT ratio FROM Item GROUP BY id, city;

Mysql 8.0.26

In Gluesql, should we throw the error or not?
or If there is other opinion, feel free to suggest.

Examples

mysql> select * from t;
+------+
| no   |
+------+
|    1 |
|    2 |
| NULL |
|    4 |
+------+
mysql> select case when no > 1 then sum(no) else 0 end from t;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.t.no'; this is incompatible with sql_mode=only_full_group_by
mysql> select case when no > 1 then sum(no) else 0 end from t group by no;
+------------------------------------------+
| case when no > 1 then sum(no) else 0 end |
+------------------------------------------+
|                                        0 |
|                                        2 |
|                                        0 |
|                                        4 |
+------------------------------------------+
cake-monotone commented 1 year ago

I think it would be better to raise an error in such situations. The value of a column not specified in the GROUP BY clause is unpredictable, which could lead to users relying on incorrect behavior. While SQLite has defined this behavior in its official docs, I believe it's a rather confusing specification. (https://www.sqlite.org/lang_select.html#resultset)

If it's okay, I'd like to work on it. 🙂