Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LOCATE function optional parameter handling #49666

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/functions.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ SELECT LOCATE('a',"first_name") pos, INSERT("first_name",LOCATE('a',"first_name"
8 |ChirstiAAAn
;

selectLocateWithConditional1
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;

x:s | c:l
---------------+---------------
null |4
false |43
true |53
;

selectLocateWithConditional2
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;

x:s | c:l
---------------+---------------
false |80
true |16
null |4
;

selectLeft
SELECT LEFT("first_name",2) f FROM "test_emp" ORDER BY "first_name" LIMIT 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ SELECT LOCATE('a',"first_name",7) pos, INSERT("first_name",LOCATE('a',"first_nam
selectLocateAndInsertWithLocateWithConditionAndTwoParameters
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;

selectLocateWithConditional
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;

selectLeft
SELECT LEFT("first_name",2) f FROM "test_emp" ORDER BY "first_name" NULLS LAST LIMIT 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public DataType dataType() {

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

return new Locate(source(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
return new Locate(source(), newChildren.get(0), newChildren.get(1), start == null ? null : newChildren.get(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,23 @@ public void testReplaceChildren() {
LocateFunctionPipe b = randomInstance();
Pipe newPattern = pipe(((Expression) randomValueOtherThan(b.pattern(), () -> randomStringLiteral())));
Pipe newSource = pipe(((Expression) randomValueOtherThan(b.source(), () -> randomStringLiteral())));
Pipe newStart;
Pipe newStart = b.start() == null ? null : pipe(((Expression) randomValueOtherThan(b.start(), () -> randomIntLiteral())));

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

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

assertEquals(transformed.pattern(), comb.get(0) ? newPattern : b.pattern());
assertEquals(transformed.src(), comb.get(1) ? newSource : b.src());
assertEquals(transformed.start(), comb.get(2) ? newStart : b.start());
assertEquals(transformed.start(), tempNewStart);
assertEquals(transformed.expression(), b.expression());
assertEquals(transformed.source(), b.source());
}
Expand Down