Apache DataFusion SQL Query Engine
[sqllogictest] Apply rowsort when there is no explicit order by (#5110)
Yevhenii Melnyk71353bb9ad99
diff --git a/datafusion/core/tests/sqllogictests/README.md b/datafusion/core/tests/sqllogictests/README.mdindex e3cfbde2e6..afd6f7d3c1 100644--- a/datafusion/core/tests/sqllogictests/README.md+++ b/datafusion/core/tests/sqllogictests/README.md@@ -97,17 +97,27 @@ Query records follow the format:```sql# <test_name>-query <type_string> <sort_mode> <label>+query <type_string> <sort_mode><sql_query>----<expected_result>```- `test_name`: Uniquely identify the test name (arrow-datafusion only)-- `type_string`: A short string that specifies the number of result columns and the expected datatype of each result column. There is one character in the <type_string> for each result column. The characters codes are "T" for a text result, "I" for an integer result, and "R" for a floating-point result.-- (Optional) `label`: sqllogictest stores a hash of the results of this query under the given label. If the label is reused, then sqllogictest verifies that the results are the same. This can be used to verify that two or more queries in the same test script that are logically equivalent always generate the same output.-- `expected_result`: In the results section, integer values are rendered as if by printf("%d"). Floating point values are rendered as if by printf("%.3f"). NULL values are rendered as "NULL". Empty strings are rendered as "(empty)". Within non-empty strings, all control characters and unprintable characters are rendered as "@".-- `sort_mode`: If included, it must be one of "nosort", "rowsort", or "valuesort". The default is "nosort". In nosort mode, the results appear in exactly the order in which they were received from the database engine. The nosort mode should only be used on queries that have an ORDER BY clause or which only have a single row of result, since otherwise the order of results is undefined and might vary from one database engine to another. The "rowsort" mode gathers all output from the database engine then sorts it by rows on the client side. Sort comparisons use strcmp() on the rendered ASCII text representation of the values. Hence, "9" sorts after "10", not before. The "valuesort" mode works like rowsort except that it does not honor row groupings. Each individual result value is sorted on its own.+- `type_string`: A short string that specifies the number of result columns and the expected datatype of each result column. There is one character in the <type_string> for each result column. The characters codes are:+ - "T" for a text result,+ - "I" for an integer result,+ - "R" for a floating-point result,+ - "?" for any other type.+- `expected_result`: In the results section, some values are converted according to some rules:+ - floating point values are rounded to the scale of "12",+ - NULL values are rendered as `NULL`,+ - empty strings are rendered as `(empty)`,+ - boolean values are rendered as `true`/`false`,+ - this list can be not exhaustive, check the `datafusion/core/tests/sqllogictests/src/engines/conversion.rs` for details.+- `sort_mode`: If included, it must be one of `nosort` (**default**), `rowsort`, or `valuesort`. In `nosort` mode, the results appear in exactly the order in which they were received from the database engine. The `nosort` mode should only be used on queries that have an `ORDER BY` clause or which only have a single row of result, since otherwise the order of results is undefined and might vary from one database engine to another. The `rowsort` mode gathers all output from the database engine then sorts it by rows on the client side. Sort comparisons use [sort_unstable](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable) on the rendered text representation of the values. Hence, "9" sorts after "10", not before. The `valuesort` mode works like `rowsort` except that it does not honor row groupings. Each individual result value is sorted on its own.++> :warning: It is encouraged to either apply `order by`, or use `rowsort` for queries without explicit `order by` clauses.##### Examplediff --git a/datafusion/core/tests/sqllogictests/test_files/decimal.slt b/datafusion/core/tests/sqllogictests/test_files/decimal.sltindex 7b9eee8726..10c11f574d 100644--- a/datafusion/core/tests/sqllogictests/test_files/decimal.slt+++ b/datafusion/core/tests/sqllogictests/test_files/decimal.slt@@ -54,7 +54,7 @@ select arrow_typeof(c1), arrow_typeof(c5) from decimal_simple where c1 > c5 limiDecimal128(10, 6) Decimal128(12, 7)-query R+query R rowsortSELECT c1 from decimal_simple;----0.00001@@ -74,7 +74,7 @@ SELECT c1 from decimal_simple;0.00005-query R+query R rowsortselect c1 from decimal_simple where c1 > 0.000030;----0.00004@@ -88,7 +88,7 @@ select c1 from decimal_simple where c1 > 0.000030;0.00005-query RRI?R+query RRI?R rowsortselect * from decimal_simple where c1 > c5;----0.00002 0.000000000002 3 false 0.000019@@ -124,29 +124,29 @@ select arrow_typeof(avg(c1)), avg(c1) from decimal_simple;Decimal128(14, 10) 0.0000366666-query RRI?R+query RRI?R rowsortselect * from decimal_simple where c1=CAST(0.00002 as Decimal(10,8));----0.00002 0.000000000002 2 true 0.0000250.00002 0.000000000002 3 false 0.000019-query RI+query RI rowsortselect c2,c3 from decimal_simple where c1!=0.00002;----0.000000000001 10.000000000003 40.000000000003 50.000000000003 5-0.000000000004 50.000000000004 120.000000000004 14+0.000000000004 50.000000000004 8-0.000000000005 9+0.000000000005 1+0.000000000005 1000.000000000005 40.000000000005 8-0.000000000005 100-0.000000000005 1+0.000000000005 9query RRI?R@@ -155,7 +155,7 @@ select * from decimal_simple where 0.00002 > c1;0.00001 0.000000000001 1 true 0.000014-query RRI?R+query RRI?R rowsortselect * from decimal_simple where c1 <= 0.00002;----0.00001 0.000000000001 1 true 0.000014@@ -163,24 +163,24 @@ select * from decimal_simple where c1 <= 0.00002;0.00002 0.000000000002 3 false 0.000019-query RRI?R+query RRI?R rowsortselect * from decimal_simple where c1 > 0.00002;----0.00003 0.000000000003 4 true 0.0000320.00003 0.000000000003 5 false 0.0000350.00003 0.000000000003 5 true 0.000011-0.00004 0.000000000004 5 true 0.0000440.00004 0.000000000004 12 false 0.000040.00004 0.000000000004 14 true 0.00004+0.00004 0.000000000004 5 true 0.0000440.00004 0.000000000004 8 false 0.000044-0.00005 0.000000000005 9 true 0.000052+0.00005 0.000000000005 1 false 0.0001+0.00005 0.000000000005 100 true 0.0000680.00005 0.000000000005 4 true 0.0000780.00005 0.000000000005 8 false 0.000033-0.00005 0.000000000005 100 true 0.000068-0.00005 0.000000000005 1 false 0.0001+0.00005 0.000000000005 9 true 0.000052-query RRI?R+query RRI?R rowsortselect * from decimal_simple where c1 >= 0.00002;----0.00002 0.000000000002 2 true 0.000025@@ -188,15 +188,15 @@ select * from decimal_simple where c1 >= 0.00002;0.00003 0.000000000003 4 true 0.0000320.00003 0.000000000003 5 false 0.0000350.00003 0.000000000003 5 true 0.000011-0.00004 0.000000000004 5 true 0.0000440.00004 0.000000000004 12 false 0.000040.00004 0.000000000004 14 true 0.00004+0.00004 0.000000000004 5 true 0.0000440.00004 0.000000000004 8 false 0.000044-0.00005 0.000000000005 9 true 0.000052+0.00005 0.000000000005 1 false 0.0001+0.00005 0.000000000005 100 true 0.0000680.00005 0.000000000005 4 true 0.0000780.00005 0.000000000005 8 false 0.000033-0.00005 0.000000000005 100 true 0.000068-0.00005 0.000000000005 1 false 0.0001+0.00005 0.000000000005 9 true 0.000052query ?@@ -205,7 +205,7 @@ select arrow_typeof(c1+1) from decimal_simple limit 1;Decimal128(27, 6)-query R+query R rowsortselect c1+1 from decimal_simple;----1.00001@@ -232,23 +232,23 @@ select arrow_typeof(c1+c5) from decimal_simple limit 1;Decimal128(13, 7)-query R+query R rowsortselect c1+c5 from decimal_simple;----0.000024-0.0000450.000039+0.000041+0.0000450.0000620.000065-0.000041-0.0000840.000080.00008+0.000083+0.0000840.0000840.000102-0.000128-0.0000830.000118+0.0001280.00015@@ -258,24 +258,24 @@ select arrow_typeof(c1-1) from decimal_simple limit 1;Decimal128(27, 6)-query R+query R rowsortselect c1-1 from decimal_simple;------0.99999--0.99998--0.99998--0.99997--0.99997--0.99997--0.99996--0.99996--0.99996--0.99996-0.99995-0.99995-0.99995-0.99995-0.99995+-0.99996+-0.99996+-0.99996+-0.99996+-0.99997+-0.99997+-0.99997+-0.99998+-0.99998+-0.99999query ?@@ -284,24 +284,24 @@ select arrow_typeof(c1-c5) from decimal_simple limit 1;Decimal128(13, 7)-query R+query R rowsortselect c1-c5 from decimal_simple;----+-0.000002+-0.000002+-0.000004+-0.000004-0.000004-0.000005-0.000001--0.000002-0.000005-0.000019--0.000004+-0.000018+-0.000028+-0.0000500--0.000004--0.000002--0.000028+0.0000010.000017--0.000018--0.00005+0.000019query ?@@ -310,7 +310,7 @@ select arrow_typeof(c1*20) from decimal_simple limit 1;Decimal128(31, 6)-query R+query R rowsortselect c1*20 from decimal_simple;----0.0002@@ -336,23 +336,23 @@ select arrow_typeof(c1*c5) from decimal_simple limit 1;Decimal128(23, 13)-query R+query R rowsortselect c1*c5 from decimal_simple;----0.00000000014-0.0000000005+0.000000000330.00000000038+0.00000000050.000000000960.00000000105-0.00000000033-0.000000001760.00000000160.0000000016+0.00000000165+0.000000001760.000000001760.0000000026-0.0000000039-0.000000001650.0000000034+0.00000000390.000000005@@ -362,7 +362,7 @@ select arrow_typeof(c1/cast(0.00001 as decimal(5,5))) from decimal_simple limitDecimal128(21, 12)-query R+query R rowsortselect c1/cast(0.00001 as decimal(5,5)) from decimal_simple;----1@@ -388,24 +388,24 @@ select arrow_typeof(c1/c5) from decimal_simple limit 1;Decimal128(30, 19)-query R+query R rowsortselect c1/c5 from decimal_simple;----+0.5+0.6410256410260.714285714286+0.7352941176470.8-1.052631578947-0.93750.857142857143-2.7272727272730.909090909091-1-10.909090909091+0.93750.961538461538-0.641025641026+1+1+1.0526315789471.515151515152-0.735294117647-0.5+2.727272727273query ?@@ -414,24 +414,24 @@ select arrow_typeof(c5%cast(0.00001 as decimal(5,5))) from decimal_simple limitDecimal128(7, 7)-query R+query R rowsortselect c5%cast(0.00001 as decimal(5,5)) from decimal_simple;-----0.000004-0.000005-0.000009-0.000002-0.000005-0.000001-0.00000400-0.000004+0+0.000001+0.0000020.000002-0.0000080.000003+0.000004+0.000004+0.000004+0.000005+0.0000050.000008-0+0.000008+0.000009query ?@@ -440,22 +440,22 @@ select arrow_typeof(c1%c5) from decimal_simple limit 1;Decimal128(11, 7)-query R+query R rowsortselect c1%c5 from decimal_simple;----+0+0+0.000001+0.0000080.00001+0.0000170.00002-0.0000010.000030.00003-0.0000080.00004-0-00.000040.000050.00005-0.0000170.000050.00005@@ -466,7 +466,7 @@ select arrow_typeof(abs(c1)) from decimal_simple limit 1;Float64-query R+query R rowsortSELECT abs(c1) from decimal_simple;----0.00001diff --git a/datafusion/core/tests/sqllogictests/test_files/describe.slt b/datafusion/core/tests/sqllogictests/test_files/describe.sltindex bb5fdeb596..2b7f118a48 100644--- a/datafusion/core/tests/sqllogictests/test_files/describe.slt+++ b/datafusion/core/tests/sqllogictests/test_files/describe.slt@@ -26,7 +26,7 @@ set datafusion.catalog.information_schema = truestatement okCREATE external table aggregate_simple(c1 real, c2 double, c3 boolean) STORED as CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv';-query C1+query C1 rowsortDESCRIBE aggregate_simple;----c1 Float32 NO@@ -46,7 +46,7 @@ set datafusion.catalog.information_schema = falsestatement okCREATE external table aggregate_simple(c1 real, c2 double, c3 boolean) STORED as CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv';-query C2+query C2 rowsortDESCRIBE aggregate_simple;----c1 Float32 NOdiff --git a/datafusion/core/tests/sqllogictests/test_files/information_schema.slt b/datafusion/core/tests/sqllogictests/test_files/information_schema.sltindex bd796ed71b..99b096de12 100644--- a/datafusion/core/tests/sqllogictests/test_files/information_schema.slt+++ b/datafusion/core/tests/sqllogictests/test_files/information_schema.slt@@ -102,7 +102,7 @@ statement okSET datafusion.execution.target_partitions=7# show all variables-query R+query R rowsortSHOW ALL----datafusion.catalog.create_default_catalog_and_schema true@@ -165,7 +165,7 @@ datafusion.execution.time_zone +00:00statement okCREATE OR REPLACE TABLE some_table AS VALUES (1,2),(3,4);-query R+query R rowsortDESCRIBE some_table----column1 Int64 YES@@ -179,7 +179,7 @@ DROP TABLE public.some_table;statement okCREATE OR REPLACE TABLE public.some_table AS VALUES (1,2),(3,4);-query R+query R rowsortDESCRIBE public.some_table----column1 Int64 YES@@ -193,7 +193,7 @@ DROP TABLE public.some_table;statement okCREATE OR REPLACE TABLE datafusion.public.some_table AS VALUES (1,2),(3,4);-query R+query R rowsortDESCRIBE datafusion.public.some_table----column1 Int64 YES@@ -209,13 +209,13 @@ describe table;# information_schema_show_tables-query CCCC+query CCCC rowsortSHOW TABLES-----datafusion information_schema tables VIEW-datafusion information_schema views VIEWdatafusion information_schema columns VIEWdatafusion information_schema df_settings VIEW+datafusion information_schema tables VIEW+datafusion information_schema views VIEW# information_schema_show_tables_no_information_schemadiff --git a/datafusion/core/tests/sqllogictests/test_files/nullif.slt b/datafusion/core/tests/sqllogictests/test_files/nullif.sltindex d6dd92d33e..9db3b5c01b 100644--- a/datafusion/core/tests/sqllogictests/test_files/nullif.slt+++ b/datafusion/core/tests/sqllogictests/test_files/nullif.slt@@ -31,55 +31,60 @@ CREATE TABLE test(;# Arrays tests-query T+query T rowsortSELECT NULLIF(int_field, 2) FROM test;----1-NULL3-NULL4NULL+NULL+NULL-query T++query T rowsortSELECT NULLIF(bool_field, false) FROM test;-----trueNULLNULLNULLNULLtrue+true-query T++query T rowsortSELECT NULLIF(text_field, 'zxc') FROM test;-----abc-def-ghijNULLNULLNULL+abc+def+ghij-query T++query T rowsortSELECT NULLIF(int_field, more_ints) FROM test;----1+4NULLNULLNULL-4NULL-query T++query T rowsortSELECT NULLIF(3, int_field) FROM test;----33-NULL333+NULL+# Scalar values testsquery Tdiff --git a/datafusion/core/tests/sqllogictests/test_files/pg_compat/pg_compat_simple.slt b/datafusion/core/tests/sqllogictests/test_files/pg_compat/pg_compat_simple.sltindex 68194fcec7..344fe09d82 100644--- a/datafusion/core/tests/sqllogictests/test_files/pg_compat/pg_compat_simple.slt+++ b/datafusion/core/tests/sqllogictests/test_files/pg_compat/pg_compat_simple.slt@@ -316,7 +316,7 @@ ORDER BY c5 ASC, c4 DESC, c10;2143473091 -14704 4602675983996931623-query IRII+query IRII rowsortSELECT * FROM(VALUES (1,2.0,-3,1+1),(10,20.0,-30,2+2))AS tbl(int_col, float_col, negative_col, summation);diff --git a/datafusion/core/tests/sqllogictests/test_files/select.slt b/datafusion/core/tests/sqllogictests/test_files/select.sltindex 2d8209fb7b..1b5ede066c 100644--- a/datafusion/core/tests/sqllogictests/test_files/select.slt+++ b/datafusion/core/tests/sqllogictests/test_files/select.slt@@ -44,14 +44,14 @@ VALUES (2+1,2-1,2>1)3 1 true# multiple rows values-query I+query I rowsortVALUES (1),(2)----12# multiple rows and columns from VALUES-query IC+query IC rowsortVALUES (1,'a'),(2,'b')----1 adiff --git a/datafusion/core/tests/sqllogictests/test_files/strings.slt b/datafusion/core/tests/sqllogictests/test_files/strings.sltindex 464aef6abe..27ed0e2d09 100644--- a/datafusion/core/tests/sqllogictests/test_files/strings.slt+++ b/datafusion/core/tests/sqllogictests/test_files/strings.slt@@ -32,22 +32,22 @@ CREATE TABLE test(;# LIKE-query T+query T rowsortSELECT s FROM test WHERE s LIKE 'p1%';----p1p1e1p1m1e1-query T+query T rowsortSELECT s FROM test WHERE s LIKE '%m1%';-----p1m1e1P1m1e1+p1m1e1p2m1e1# NOT LIKE-query T+query T rowsortSELECT s FROM test WHERE s NOT LIKE 'p1%';----P1@@ -60,18 +60,18 @@ p2m1e1# ILIKE-query T+query T rowsortSELECT s FROM test WHERE s ILIKE 'p1%';-----p1-p1e1-p1m1e1P1P1e1P1m1e1+p1+p1e1+p1m1e1# NOT ILIKE-query T+query T rowsortSELECT s FROM test WHERE s NOT ILIKE 'p1%';----e1