Skip to content

Commit 1c44587

Browse files
author
石源
committed
fix #877 Cast not used in the where condition
1 parent f8ee5b6 commit 1c44587

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

src/main/java/org/nlpcn/es4sql/parse/CastParser.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public String parse(boolean isReturn) throws SqlParseException {
5959
}
6060
if(isReturn) {
6161
result.add("return " + name);
62-
}else{
63-
result.add(name);
6462
}
6563

6664
return Joiner.on("; ").join(result);

src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public static MethodField makeMethodField(String name, List<SQLExpr> arguments,
236236

237237
if (SQLFunctions.buildInFunctions.contains(binaryOpExpr.getOperator().toString().toLowerCase())) {
238238
SQLMethodInvokeExpr mExpr = makeBinaryMethodField(binaryOpExpr, alias, first);
239-
MethodField abc = makeMethodField(mExpr.getMethodName(), mExpr.getParameters(), null, null, tableAlias, false);
240-
String key = abc.getParams().get(0).toString(), value = abc.getParams().get(1).toString();
239+
MethodField mf = makeMethodField(mExpr.getMethodName(), mExpr.getParameters(), null, null, tableAlias, false);
240+
String key = mf.getParams().get(0).toString(), value = mf.getParams().get(1).toString();
241241
paramers.add(new KVValue(key, new SQLCharExpr(first && !SQLFunctions.buildInFunctions.contains(finalMethodName) ? String.format("%s;return %s;", value, key) : value)));
242242
} else {
243243
if (!binaryOpExpr.getOperator().getName().equals("=")) {
@@ -273,8 +273,8 @@ public static MethodField makeMethodField(String name, List<SQLExpr> arguments,
273273
paramers.add(new KVValue("children", childrenType));
274274
} else if (SQLFunctions.buildInFunctions.contains(methodName)) {
275275
//throw new SqlParseException("only support script/nested as inner functions");
276-
MethodField abc = makeMethodField(methodName, mExpr.getParameters(), null, null, tableAlias, false);
277-
String key = abc.getParams().get(0).toString(), value = abc.getParams().get(1).toString();
276+
MethodField mf = makeMethodField(methodName, mExpr.getParameters(), null, null, tableAlias, false);
277+
String key = mf.getParams().get(0).toString(), value = mf.getParams().get(1).toString();
278278
paramers.add(new KVValue(key, new SQLCharExpr(first && !SQLFunctions.buildInFunctions.contains(finalMethodName) ? String.format("%s;return %s;", value, key) : value)));
279279
} else throw new SqlParseException("only support script/nested/children as inner functions");
280280
} else if (object instanceof SQLCaseExpr) {

src/main/java/org/nlpcn/es4sql/parse/WhereParser.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
2323
import java.util.ArrayList;
24+
import java.util.Collections;
2425
import java.util.List;
2526

2627
/**
@@ -173,7 +174,8 @@ private boolean isCond(SQLBinaryOpExpr expr) {
173174
}
174175
return leftSide instanceof SQLIdentifierExpr ||
175176
leftSide instanceof SQLPropertyExpr ||
176-
leftSide instanceof SQLVariantRefExpr;
177+
leftSide instanceof SQLVariantRefExpr ||
178+
leftSide instanceof SQLCastExpr;
177179
}
178180

179181
private boolean isAllowedMethodOnConditionLeft(SQLMethodInvokeExpr method, SQLBinaryOperator operator) {
@@ -480,48 +482,63 @@ private MethodField parseSQLMethodInvokeExprWithFunctionInWhere(SQLMethodInvokeE
480482
return methodField;
481483
}
482484

485+
private MethodField parseSQLCastExprInWhere(SQLCastExpr soExpr) throws SqlParseException {
486+
MethodField methodField = FieldMaker.makeMethodField("cast",
487+
Collections.singletonList(soExpr),
488+
null,
489+
null,
490+
query != null ? query.getFrom().getAlias() : null,
491+
true);
492+
List<KVValue> params = methodField.getParams();
493+
KVValue param = params.get(0);
494+
params.clear();
495+
params.add(new KVValue(param.key));
496+
params.add(new KVValue(param.value));
497+
return methodField;
498+
}
499+
483500
private SQLMethodInvokeExpr parseSQLBinaryOpExprWhoIsConditionInWhere(SQLBinaryOpExpr soExpr) throws SqlParseException {
484501

485-
if (!(soExpr.getLeft() instanceof SQLMethodInvokeExpr ||
502+
if (!(soExpr.getLeft() instanceof SQLCastExpr || soExpr.getRight() instanceof SQLCastExpr)) {
503+
if (!(soExpr.getLeft() instanceof SQLMethodInvokeExpr ||
486504
soExpr.getRight() instanceof SQLMethodInvokeExpr)) {
487-
return null;
488-
}
489-
490-
if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
491-
if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getLeft()).getMethodName())) {
492505
return null;
493506
}
494-
}
495507

496-
if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
497-
if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getRight()).getMethodName())) {
498-
return null;
508+
if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
509+
if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getLeft()).getMethodName())) {
510+
return null;
511+
}
499512
}
500-
}
501513

514+
if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
515+
if (!SQLFunctions.buildInFunctions.contains(((SQLMethodInvokeExpr) soExpr.getRight()).getMethodName())) {
516+
return null;
517+
}
518+
}
519+
}
502520

503521
MethodField leftMethod = new MethodField(null, Lists.newArrayList(new KVValue("", Util.expr2Object(soExpr.getLeft(), "'"))), null, null);
504-
MethodField rightMethod = new MethodField(null, Lists.newArrayList(new KVValue("", Util.expr2Object(soExpr.getRight(), "'"))), null, null);
505-
506522
if (soExpr.getLeft() instanceof SQLIdentifierExpr || soExpr.getLeft() instanceof SQLPropertyExpr) {
507523
leftMethod = new MethodField(null, Lists.newArrayList(new KVValue("", "doc['" + Util.expr2Object(soExpr.getLeft(), "'") + "'].value")), null, null);
524+
} else if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
525+
leftMethod = parseSQLMethodInvokeExprWithFunctionInWhere((SQLMethodInvokeExpr) soExpr.getLeft());
526+
} else if (soExpr.getLeft() instanceof SQLCastExpr) {
527+
leftMethod = parseSQLCastExprInWhere((SQLCastExpr) soExpr.getLeft());
508528
}
509529

530+
MethodField rightMethod = new MethodField(null, Lists.newArrayList(new KVValue("", Util.expr2Object(soExpr.getRight(), "'"))), null, null);
510531
if (soExpr.getRight() instanceof SQLIdentifierExpr || soExpr.getRight() instanceof SQLPropertyExpr) {
511532
rightMethod = new MethodField(null, Lists.newArrayList(new KVValue("", "doc['" + Util.expr2Object(soExpr.getRight(), "'") + "'].value")), null, null);
512-
}
513-
514-
if (soExpr.getLeft() instanceof SQLMethodInvokeExpr) {
515-
leftMethod = parseSQLMethodInvokeExprWithFunctionInWhere((SQLMethodInvokeExpr) soExpr.getLeft());
516-
}
517-
if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
533+
} else if (soExpr.getRight() instanceof SQLMethodInvokeExpr) {
518534
rightMethod = parseSQLMethodInvokeExprWithFunctionInWhere((SQLMethodInvokeExpr) soExpr.getRight());
535+
} else if (soExpr.getRight() instanceof SQLCastExpr) {
536+
rightMethod = parseSQLCastExprInWhere((SQLCastExpr) soExpr.getRight());
519537
}
520538

521539
String v1 = leftMethod.getParams().get(0).value.toString();
522540
String v1Dec = leftMethod.getParams().size() == 2 ? leftMethod.getParams().get(1).value.toString() + ";" : "";
523541

524-
525542
String v2 = rightMethod.getParams().get(0).value.toString();
526543
String v2Dec = rightMethod.getParams().size() == 2 ? rightMethod.getParams().get(1).value.toString() + ";" : "";
527544

src/test/java/org/nlpcn/es4sql/ExplainTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public void testStatsGroupsExplain() throws SqlParseException, SQLFeatureNotSupp
109109
assertThat(map.get("stats").toString(), equalTo("[group1, group2]"));
110110
}
111111

112+
@Test
113+
public void testCastInWhereExplain() throws SqlParseException, SQLFeatureNotSupportedException {
114+
System.out.println(explain("select * from file1 where cast(offset as int) > 20"));
115+
}
116+
112117
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException {
113118
SearchDao searchDao = MainTestSuite.getSearchDao();
114119
SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql).explain();

0 commit comments

Comments
 (0)