Skip to content

Commit ddc70f2

Browse files
Marios Trivyzasmatriv
authored andcommitted
SQL: [tests] Fix select csv spec and enable it (#35239)
Also, replace `||` and `&&` with `OR` and `AND` as the former are not SQL standard operators.
1 parent 54365fc commit ddc70f2

File tree

5 files changed

+73
-63
lines changed

5 files changed

+73
-63
lines changed

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/CsvSpecTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public abstract class CsvSpecTestCase extends SpecBaseIntegrationTestCase {
2929
public static List<Object[]> readScriptSpec() throws Exception {
3030
Parser parser = specParser();
3131
List<Object[]> tests = new ArrayList<>();
32+
tests.addAll(readScriptSpec("/select.csv-spec", parser));
3233
tests.addAll(readScriptSpec("/command.csv-spec", parser));
3334
tests.addAll(readScriptSpec("/fulltext.csv-spec", parser));
3435
tests.addAll(readScriptSpec("/agg.csv-spec", parser));

x-pack/plugin/sql/qa/src/main/resources/select.csv-spec

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,110 @@
22
inWithLiterals
33
SELECT 1 IN (1, 2, 3), 1 IN (2, 3);
44

5-
1 IN (1, 2, 3) | 1 IN (2, 3)
6-
-----------------+-------------
7-
true |false
5+
1 IN (1, 2, 3):b | 1 IN (2, 3):b
6+
-------------------+-------------
7+
true |false
88
;
99

1010
inWithLiteralsAndFunctions
1111
SELECT 1 IN (2 - 1, 2, 3), abs(-1) IN (2, 3, abs(4 - 5));
1212

13-
1 IN (1, 2, 3) | 1 IN (2, 3)
14-
-----------------+-------------
15-
true |false
13+
1 IN (2 - 1, 2, 3) | ABS(-1) IN (2, 3, ABS(4 - 5))
14+
---------------------+------------------------------
15+
true |true
1616
;
1717

1818

1919
inWithLiteralsAndNegation
20-
SELECT NOT 1 IN (1, 1 + 1, 3), NOT 1 IN (2, 3);
20+
SELECT 1 NOT IN (1, 1 + 1, 3), 1 NOT IN (2, 3);
2121

22-
1 IN (1, 2, 3) | 1 IN (2, 3)
23-
-----------------+-------------
24-
false |true
22+
NOT(1 IN (1, 1 + 1, 3)) | NOT(1 IN (2, 3))
23+
--------------------------+-----------------
24+
false |true
2525
;
2626

27-
27+
// Need to CAST as STRING since for boolean types the jdbc CSV translates null -> false
2828
inWithNullHandling
29-
SELECT 2 IN (1, null, 3), 3 IN (1, null, 3), null IN (1, null, 3), null IN (1, 2, 3);
29+
SELECT CAST(2 IN (1, null, 3) AS STRING), CAST(3 IN (1, null, 3) AS STRING), CAST(null IN (1, null, 3) AS STRING), CAST(null IN (1, 2, 3) AS STRING);
3030

31-
2 IN (1, null, 3) | 3 IN (1, null, 3) | null IN (1, null, 3) | null IN (1, 2, 3)
32-
--------------------+--------------------+-----------------------+-------------------
33-
null |true |null | null
31+
CAST(2 IN (1, null, 3) AS VARCHAR):s | CAST(3 IN (1, null, 3) AS VARCHAR):s | CAST(null IN (1, null, 3) AS VARCHAR):s | CAST(null IN (1, 2, 3) AS VARCHAR):s
32+
---------------------------------------+--------------------------------------+------------------------------------------+--------------------------------------
33+
null |true |null |null
3434
;
3535

3636
inWithNullHandlingAndNegation
37-
SELECT NOT 2 IN (1, null, 3), NOT 3 IN (1, null, 3), NOT null IN (1, null, 3), NOT null IN (1, 2, 3);
37+
SELECT CAST(NOT 2 IN (1, null, 3) AS STRING), CAST(3 NOT IN (1, null, 3) AS STRING), CAST(NOT null IN (1, null, 3) AS STRING), CAST(null NOT IN (1, 2, 3) AS STRING);
3838

39-
NOT 2 IN (1, null, 3) | NOT 3 IN (1, null, 3) | NOT null IN (1, null, 3) | null IN (1, 2, 3)
40-
------------------------+------------------------+---------------------------+--------------------
41-
null |false |null | null
39+
CAST(NOT(2 IN (1, null, 3)) AS VARCHAR):s | CAST(NOT(3 IN (1, null, 3)) AS VARCHAR):s | CAST(NOT(null IN (1, null, 3)) AS VARCHAR):s | CAST(NOT(null IN (1, 2, 3)) AS VARCHAR):s
40+
--------------------------------------------+--------------------------------------------+-----------------------------------------------+-------------------------------------------
41+
null |false |null |null
4242
;
4343

4444
//
4545
// SELECT with IN and table columns
4646
//
4747
inWithTableColumn
48-
SELECT emp_no IN (10000, 10001, 10002) FROM test_emp ORDER BY 1;
49-
50-
emp_no
51-
-------
52-
10001
53-
10002
48+
SELECT emp_no IN (10000, 10001, 10002) FROM test_emp WHERE emp_no BETWEEN 10001 AND 10004 ORDER BY emp_no;
49+
50+
emp_no IN (10000, 10001, 10002):b
51+
----------------------------------
52+
true
53+
true
54+
false
55+
false
5456
;
5557

5658
inWithTableColumnAndFunction
57-
SELECT emp_no IN (10000, 10000 + 1, abs(-10000 - 2)) FROM test_emp;
58-
59-
emp_no
60-
-------
61-
10001
62-
10002
59+
SELECT emp_no IN (10000, 10000 + 1, abs(-10000 - 2)) FROM test_emp WHERE emp_no BETWEEN 10001 AND 10004 ORDER BY emp_no;
60+
61+
emp_no IN (10000, 10000 + 1, ABS(-10000 - 2)):b
62+
------------------------------------------------
63+
true
64+
true
65+
false
66+
false
6367
;
6468

6569
inWithTableColumnAndNegation
66-
SELECT emp_no NOT IN (10000, 10000 + 1, 10002) FROM test_emp ORDER BY 1 LIMIT 3;
67-
68-
emp_no
69-
-------
70-
10003
71-
10004
72-
10005
70+
SELECT emp_no NOT IN (10000, 10000 + 1, 10002) FROM test_emp WHERE emp_no BETWEEN 10001 AND 10004 ORDER BY emp_no;
71+
72+
NOT(emp_no IN (10000, 10000 + 1, 10002)):b
73+
-------------------------------------------
74+
false
75+
false
76+
true
77+
true
7378
;
7479

7580
inWithTableColumnAndComplexFunctions
76-
SELECT 1 IN (1, abs(2 - 4), 3) OR emp_no NOT IN (10000, 10000 + 1, 10002) FROM test_emp ORDER BY 1 LIMIT 3;
77-
78-
emp_no
79-
-------
80-
10003
81-
10004
82-
10005
81+
SELECT emp_no IN (1, abs(1 - 10002), 3) OR emp_no NOT IN (10000, 10000 + 2, 10003) FROM test_emp WHERE emp_no BETWEEN 10001 AND 10004 ORDER BY emp_no;
82+
83+
(emp_no IN (1, ABS(1 - 10002), 3)) OR (NOT(emp_no IN (10000, 10000 + 2, 10003))):b
84+
----------------------------------------------------------------------------------
85+
true
86+
false
87+
false
88+
true
8389
;
8490

85-
inWithTableColumnAndNullHandling
86-
SELECT emp_no, birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)), birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), null, CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)) FROM test_emp WHERE emp_no = 10038 OR emp_no = 10039 OR emp_no = 10040 ORDER BY 1;
8791

88-
emp_no | birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)) | birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), null, CAST('1959-10-01T00:00:00Z' AS TIMESTAMP))
89-
--------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------
90-
10038 | true | true
91-
10039 | null | null
92-
10040 | false | null
92+
// Need to CAST as STRING since for boolean types the jdbc CSV translates null -> false
93+
inWithTableColumnAndNullHandling
94+
SELECT emp_no, CAST(languages IN (2, 3) AS STRING), CAST(languages IN (2, null, 3) AS STRING) FROM test_emp WHERE emp_no BETWEEN 10018 AND 10020 ORDER BY emp_no;
9395

96+
emp_no:i | CAST(languages IN (2, 3) AS VARCHAR):s | CAST(languages IN (2, null, 3) AS VARCHAR):s
97+
----------+-----------------------------------------+----------------------------------------------
98+
10018 |true |true
99+
10019 |false |null
100+
10020 |null |null
101+
;
94102

95103
inWithTableColumnAndNullHandlingAndNegation
96-
SELECT emp_no, NOT birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)), NOT birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), null, CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)) FROM test_emp WHERE emp_no = 10038 OR emp_no = 10039 OR emp_no = 10040 ORDER BY 1;
104+
SELECT emp_no, CAST(languages NOT IN (2, 3) AS STRING), CAST(NOT languages IN (2, null, 3) AS STRING) FROM test_emp WHERE emp_no BETWEEN 10018 AND 10020 ORDER BY emp_no;
97105

98-
emp_no | NOT birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), CAST('1959-10-01T00:00:00Z' AS TIMESTAMP)) | NOT birth_date in (CAST('2018-10-01T00:00:00Z' AS TIMESTAMP), null, CAST('1959-10-01T00:00:00Z' AS TIMESTAMP))
99-
--------+-----------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------
100-
10038 | false | false
101-
10039 | null | null
102-
10040 | true | null
106+
emp_no:i | CAST(NOT(languages IN (2, 3)) AS VARCHAR):s | CAST(NOT(languages IN (2, null, 3)) AS VARCHAR):s
107+
----------+----------------------------------------------+---------------------------------------------------
108+
10018 |false |false
109+
10019 |true |null
110+
10020 |null |null
111+
;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/logical/BinaryLogicProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum BinaryLogicOperation implements PredicateBiFunction<Boolean, Boolean
2727
return null;
2828
}
2929
return Boolean.logicalAnd(l.booleanValue(), r.booleanValue());
30-
}, "&&"),
30+
}, "AND"),
3131
OR((l, r) -> {
3232
if (Boolean.TRUE.equals(l) || Boolean.TRUE.equals(r)) {
3333
return Boolean.TRUE;
@@ -36,7 +36,7 @@ public enum BinaryLogicOperation implements PredicateBiFunction<Boolean, Boolean
3636
return null;
3737
}
3838
return Boolean.logicalOr(l.booleanValue(), r.booleanValue());
39-
}, "||");
39+
}, "OR");
4040

4141
private final BiFunction<Boolean, Boolean, Boolean> process;
4242
private final String symbol;
@@ -88,4 +88,4 @@ protected void checkParameter(Object param) {
8888
throw new SqlIllegalArgumentException("A boolean is required; received {}", param);
8989
}
9090
}
91-
}
91+
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/comparison/In.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Boolean fold() {
9090

9191
@Override
9292
public String name() {
93-
StringJoiner sj = new StringJoiner(", ", " IN(", ")");
93+
StringJoiner sj = new StringJoiner(", ", " IN (", ")");
9494
list.forEach(e -> sj.add(Expressions.name(e)));
9595
return Expressions.name(value) + sj.toString();
9696
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void testTranslateInExpressionInvalidValues_WhereClause() {
197197
assertFalse(condition.foldable());
198198
SqlIllegalArgumentException ex = expectThrows(SqlIllegalArgumentException.class, () -> QueryTranslator.toQuery(condition, false));
199199
assertEquals("Line 1:52: Comparisons against variables are not (currently) supported; " +
200-
"offender [keyword] in [keyword IN(foo, bar, keyword)]", ex.getMessage());
200+
"offender [keyword] in [keyword IN (foo, bar, keyword)]", ex.getMessage());
201201
}
202202

203203
public void testTranslateInExpression_WhereClause_Painless() {

0 commit comments

Comments
 (0)