Skip to content

Commit 509a45c

Browse files
committed
SQL: Introduce Coalesce function (#35253)
Add Coalesce conditional for replacing null values Fix #35060 (cherry picked from commit 75e9a63)
1 parent fd6dba7 commit 509a45c

File tree

31 files changed

+560
-71
lines changed

31 files changed

+560
-71
lines changed

x-pack/plugin/sql/qa/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/RestSqlIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
import org.elasticsearch.test.rest.ESRestTestCase;
1414
import org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase;
1515

16-
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
17-
1816
import java.net.URISyntaxException;
1917
import java.nio.file.Files;
2018
import java.nio.file.Path;
2119

20+
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
21+
2222
/**
2323
* Integration test for the rest sql action. The one that speaks json directly to a
2424
* user rather than to the JDBC driver or CLI.
2525
*/
2626
public class RestSqlIT extends RestSqlTestCase {
27-
static final boolean SSL_ENABLED = Booleans.parseBoolean(System.getProperty("tests.ssl.enabled"));
27+
static final boolean SSL_ENABLED = Booleans.parseBoolean(System.getProperty("tests.ssl.enabled"), false);
2828

2929
static Settings securitySettings() {
3030
String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray()));

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ShowTestCase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public void testShowFunctions() throws IOException {
3535
while (aggregateFunction.matcher(line).matches()) {
3636
line = readLine();
3737
}
38+
Pattern conditionalFunction = Pattern.compile("\\s*[A-Z0-9_~]+\\s*\\|\\s*CONDITIONAL\\s*");
39+
while (conditionalFunction.matcher(line).matches()) {
40+
line = readLine();
41+
}
3842
Pattern scalarFunction = Pattern.compile("\\s*[A-Z0-9_~]+\\s*\\|\\s*SCALAR\\s*");
3943
while (scalarFunction.matcher(line).matches()) {
4044
line = readLine();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static List<Object[]> readScriptSpec() throws Exception {
3636
tests.addAll(readScriptSpec("/columns.csv-spec", parser));
3737
tests.addAll(readScriptSpec("/datetime.csv-spec", parser));
3838
tests.addAll(readScriptSpec("/alias.csv-spec", parser));
39-
tests.addAll(readScriptSpec("/nulls.csv-spec", parser));
39+
tests.addAll(readScriptSpec("/null.csv-spec", parser));
4040
tests.addAll(readScriptSpec("/nested.csv-spec", parser));
4141
tests.addAll(readScriptSpec("/functions.csv-spec", parser));
4242
tests.addAll(readScriptSpec("/math.csv-spec", parser));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public static void assertResultSetMetadata(ResultSet expected, ResultSet actual,
118118
if (expectedType == Types.FLOAT && expected instanceof CsvResultSet) {
119119
expectedType = Types.REAL;
120120
}
121+
// csv doesn't support NULL type so skip type checking
122+
if (actualType == Types.NULL && expected instanceof CsvResultSet) {
123+
expectedType = Types.NULL;
124+
}
125+
121126
// when lenient is used, an int is equivalent to a short, etc...
122127
assertEquals("Different column type for column [" + expectedName + "] (" + JDBCType.valueOf(expectedType) + " != "
123128
+ JDBCType.valueOf(actualType) + ")", expectedType, actualType);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static List<Object[]> readScriptSpec() throws Exception {
4141
tests.addAll(readScriptSpec("/arithmetic.sql-spec", parser));
4242
tests.addAll(readScriptSpec("/string-functions.sql-spec", parser));
4343
tests.addAll(readScriptSpec("/case-functions.sql-spec", parser));
44+
tests.addAll(readScriptSpec("/null.sql-spec", parser));
4445
return tests;
4546
}
4647

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SKEWNESS |AGGREGATE
1919
STDDEV_POP |AGGREGATE
2020
SUM_OF_SQUARES |AGGREGATE
2121
VAR_POP |AGGREGATE
22+
COALESCE |CONDITIONAL
2223
DAY |SCALAR
2324
DAYNAME |SCALAR
2425
DAYOFMONTH |SCALAR

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ SKEWNESS |AGGREGATE
196196
STDDEV_POP |AGGREGATE
197197
SUM_OF_SQUARES |AGGREGATE
198198
VAR_POP |AGGREGATE
199+
COALESCE |CONDITIONAL
199200
DAY |SCALAR
200201
DAYNAME |SCALAR
201202
DAYOFMONTH |SCALAR
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// Null expressions
3+
//
4+
5+
dateTimeOverNull
6+
SELECT YEAR(CAST(NULL AS DATE)) d;
7+
8+
d:i
9+
null
10+
;
11+
12+
addOfNull
13+
SELECT CAST(NULL AS INT) + CAST(NULL AS FLOAT) AS n;
14+
15+
n:d
16+
null
17+
;
18+
19+
20+
divOfCastedNull
21+
SELECT 5 / CAST(NULL AS FLOAT) + 10 AS n;
22+
23+
n:d
24+
null
25+
;
26+
27+
divNoNull
28+
SELECT 5 / null + 1 AS n;
29+
30+
n:i
31+
null
32+
;
33+
34+
coalesceJustWithNull
35+
SELECT COALESCE(null, null, null) AS c;
36+
37+
c
38+
null
39+
;
40+
41+
coalesceFirstNotNull
42+
SELECT COALESCE(123) AS c;
43+
44+
c
45+
123
46+
;
47+
48+
49+
coalesceWithFirstNullOfString
50+
SELECT COALESCE(null, 'first') AS c;
51+
52+
c:s
53+
first
54+
;
55+
56+
coalesceWithFirstNullOfNumber
57+
SELECT COALESCE(null, 123) AS c;
58+
59+
c:i
60+
123
61+
;
62+
63+
coalesceMixed
64+
SELECT COALESCE(null, 123, null, 321) AS c;
65+
66+
c:i
67+
123
68+
;
69+
70+
coalesceScalar
71+
SELECT COALESCE(null, ABS(123) + 1) AS c;
72+
73+
c:i
74+
124
75+
;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Null expressions
3+
//
4+
5+
coalesceField
6+
SELECT COALESCE(null, ABS(emp_no) + 1) AS c FROM test_emp ORDER BY emp_no LIMIT 5;
7+
8+
coalesceHaving
9+
SELECT COALESCE(null, ABS(MAX(emp_no)) + 1, 123) AS c FROM test_emp GROUP BY languages HAVING c > 100 ORDER BY languages LIMIT 5;
10+
11+
coalesceWhere
12+
SELECT COALESCE(null, ABS(emp_no) + 1, 123) AS c FROM test_emp WHERE COALESCE(null, ABS(emp_no) + 1, 123, 321) > 100 ORDER BY emp_no NULLS FIRST LIMIT 5;

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)