Skip to content

Commit dd3aeb8

Browse files
authored
Fix Locate function optional parameter handling (#49666)
1 parent 69e0b1a commit dd3aeb8

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,26 @@ SELECT LOCATE('a',"first_name") pos, INSERT("first_name",LOCATE('a',"first_name"
333333
8 |ChirstiAAAn
334334
;
335335

336+
selectLocateWithConditional1
337+
SELECT CAST(LOCATE('a', CASE WHEN TRUNCATE(salary, 3) > 40000 THEN first_name.keyword ELSE last_name.keyword END) > 0 AS STRING) AS x, COUNT(*) AS c FROM test_emp GROUP BY x ORDER BY c ASC;
338+
339+
x:s | c:l
340+
---------------+---------------
341+
null |4
342+
false |43
343+
true |53
344+
;
345+
346+
selectLocateWithConditional2
347+
SELECT CAST(LOCATE(CASE WHEN languages > 3 THEN 'a' ELSE 'b' END, CASE WHEN TRUNCATE(salary, 3) > 40000 THEN first_name.keyword ELSE last_name.keyword END, CASE WHEN gender IS NOT NULL THEN 3 ELSE NULL END) > 0 AS STRING) AS x, COUNT(*) AS c FROM test_emp GROUP BY x ORDER BY c DESC;
348+
349+
x:s | c:l
350+
---------------+---------------
351+
false |80
352+
true |16
353+
null |4
354+
;
355+
336356
selectLeft
337357
SELECT LEFT("first_name",2) f FROM "test_emp" ORDER BY "first_name" LIMIT 10;
338358

x-pack/plugin/sql/qa/src/main/resources/string-functions.sql-spec

+3
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ SELECT LOCATE('a',"first_name",7) pos, INSERT("first_name",LOCATE('a',"first_nam
253253
selectLocateAndInsertWithLocateWithConditionAndTwoParameters
254254
SELECT LOCATE('a',"first_name") pos, INSERT("first_name",LOCATE('a',"first_name"),1,'AAA') inserted FROM "test_emp" WHERE LOCATE('a',"first_name") > 0 ORDER BY "first_name" LIMIT 10;
255255

256+
selectLocateWithConditional
257+
SELECT LOCATE(CASE WHEN FALSE THEN NULL ELSE 'x' END, "first_name") > 0 AS x, COUNT(*) AS c FROM "test_emp" GROUP BY x ORDER BY c ASC;
258+
256259
selectLeft
257260
SELECT LEFT("first_name",2) f FROM "test_emp" ORDER BY "first_name" NULLS LAST LIMIT 10;
258261

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Locate.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ public DataType dataType() {
133133

134134
@Override
135135
public Expression replaceChildren(List<Expression> newChildren) {
136-
if (newChildren.size() != 3) {
136+
if (start != null && newChildren.size() != 3) {
137137
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]");
138+
} else if (start == null && newChildren.size() != 2) {
139+
throw new IllegalArgumentException("expected [2] children but received [" + newChildren.size() + "]");
138140
}
139141

140-
return new Locate(source(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
142+
return new Locate(source(), newChildren.get(0), newChildren.get(1), start == null ? null : newChildren.get(2));
141143
}
142144
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipeTests.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,23 @@ public void testReplaceChildren() {
7575
LocateFunctionPipe b = randomInstance();
7676
Pipe newPattern = pipe(((Expression) randomValueOtherThan(b.pattern(), () -> randomStringLiteral())));
7777
Pipe newSource = pipe(((Expression) randomValueOtherThan(b.source(), () -> randomStringLiteral())));
78-
Pipe newStart;
78+
Pipe newStart = b.start() == null ? null : pipe(((Expression) randomValueOtherThan(b.start(), () -> randomIntLiteral())));
7979

80-
LocateFunctionPipe newB = new LocateFunctionPipe(
81-
b.source(), b.expression(), b.pattern(), b.src(), b.start());
82-
newStart = pipe(((Expression) randomValueOtherThan(b.start(), () -> randomIntLiteral())));
80+
LocateFunctionPipe newB = new LocateFunctionPipe(b.source(), b.expression(), b.pattern(), b.src(), b.start());
8381
LocateFunctionPipe transformed = null;
8482

8583
// generate all the combinations of possible children modifications and test all of them
8684
for(int i = 1; i < 4; i++) {
8785
for(BitSet comb : new Combinations(3, i)) {
86+
Pipe tempNewStart = b.start() == null ? b.start() : (comb.get(2) ? newStart : b.start());
8887
transformed = (LocateFunctionPipe) newB.replaceChildren(
8988
comb.get(0) ? newPattern : b.pattern(),
9089
comb.get(1) ? newSource : b.src(),
91-
comb.get(2) ? newStart : b.start());
90+
tempNewStart);
9291

9392
assertEquals(transformed.pattern(), comb.get(0) ? newPattern : b.pattern());
9493
assertEquals(transformed.src(), comb.get(1) ? newSource : b.src());
95-
assertEquals(transformed.start(), comb.get(2) ? newStart : b.start());
94+
assertEquals(transformed.start(), tempNewStart);
9695
assertEquals(transformed.expression(), b.expression());
9796
assertEquals(transformed.source(), b.source());
9897
}

0 commit comments

Comments
 (0)