Skip to content

Commit d42fd71

Browse files
committed
Merge commit '777731da79de7676e2ad7c9c2e8b86aff93bab13' into elastic2.0
Conflicts: src/test/java/org/nlpcn/es4sql/SqlParserTests.java
2 parents dee8421 + 777731d commit d42fd71

File tree

6 files changed

+163
-2
lines changed

6 files changed

+163
-2
lines changed

src/main/java/org/nlpcn/es4sql/domain/Condition.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class Condition extends Where {
1717

1818
public enum OPEAR {
19-
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , TERM , IDS_QUERY,NESTED_COMPLEX;
19+
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , TERM , IDS_QUERY,NESTED_COMPLEX , SCRIPT;
2020

2121
public static Map<String,OPEAR> methodNameToOpear;
2222

@@ -166,6 +166,9 @@ public Condition(CONN conn, String name, String oper, Object value,boolean isNes
166166
case "NESTED":
167167
this.opear = OPEAR.NESTED_COMPLEX;
168168
break;
169+
case "SCRIPT":
170+
this.opear = OPEAR.SCRIPT;
171+
break;
169172
default:
170173
throw new SqlParseException(oper + " is err!");
171174
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.nlpcn.es4sql.parse;
2+
3+
import com.alibaba.druid.sql.ast.SQLExpr;
4+
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
5+
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
6+
import org.nlpcn.es4sql.Util;
7+
import org.nlpcn.es4sql.domain.Field;
8+
import org.nlpcn.es4sql.domain.KVValue;
9+
import org.nlpcn.es4sql.exception.SqlParseException;
10+
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* Created by Eliran on 11/12/2015.
18+
*/
19+
public class ScriptFilter {
20+
private String script;
21+
private Map<String,Object> args;
22+
23+
public ScriptFilter() {
24+
args = null;
25+
}
26+
27+
public ScriptFilter(String script, Map<String, Object> args) {
28+
this.script = script;
29+
this.args = args;
30+
}
31+
32+
public boolean tryParseFromMethodExpr(SQLMethodInvokeExpr expr) throws SqlParseException {
33+
if (!expr.getMethodName().toLowerCase().equals("script")) {
34+
return false;
35+
}
36+
List<SQLExpr> methodParameters = expr.getParameters();
37+
if (methodParameters.size() == 0) {
38+
return false;
39+
}
40+
script = Util.extendedToString(methodParameters.get(0));
41+
42+
if (methodParameters.size() == 1) {
43+
return true;
44+
}
45+
46+
args = new HashMap<>();
47+
for (int i = 1; i < methodParameters.size(); i++) {
48+
49+
SQLExpr innerExpr = methodParameters.get(i);
50+
if (!(innerExpr instanceof SQLBinaryOpExpr)) {
51+
return false;
52+
}
53+
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) innerExpr;
54+
if (!binaryOpExpr.getOperator().getName().equals("=")) {
55+
return false;
56+
}
57+
58+
SQLExpr right = binaryOpExpr.getRight();
59+
Object value = Util.expr2Object(right);
60+
String key = Util.extendedToString(binaryOpExpr.getLeft());
61+
args.put(key, value);
62+
63+
}
64+
return true;
65+
}
66+
67+
public boolean containsParameters(){
68+
return args!=null && args.size() > 0;
69+
}
70+
71+
public String getScript() {
72+
return script;
73+
}
74+
75+
76+
public Map<String, Object> getArgs() {
77+
return args;
78+
}
79+
80+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
1010

1111

12+
import org.nlpcn.es4sql.Util;
1213
import org.nlpcn.es4sql.domain.*;
1314
import org.nlpcn.es4sql.domain.Where.CONN;
1415
import org.nlpcn.es4sql.domain.hints.Hint;
@@ -201,6 +202,14 @@ else if (methodName.toLowerCase().equals("nested")){
201202
where.addWhere(condition);
202203

203204
}
205+
else if (methodName.toLowerCase().equals("script")){
206+
ScriptFilter scriptFilter = new ScriptFilter();
207+
if(!scriptFilter.tryParseFromMethodExpr(methodExpr)){
208+
throw new SqlParseException("could not parse script filter");
209+
}
210+
Condition condition = new Condition(CONN.valueOf(opear),null,"SCRIPT",scriptFilter);
211+
where.addWhere(condition);
212+
}
204213
else {
205214
throw new SqlParseException("unsupported method: " + methodName);
206215
}

src/main/java/org/nlpcn/es4sql/query/maker/Maker.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.nlpcn.es4sql.query.maker;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Map;
47
import java.util.Set;
58

9+
import com.alibaba.druid.sql.ast.SQLExpr;
610
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
711
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
812
import com.google.common.collect.ImmutableSet;
@@ -12,13 +16,18 @@
1216
import org.elasticsearch.common.xcontent.XContentParser;
1317
import org.elasticsearch.common.xcontent.json.JsonXContent;
1418
import org.elasticsearch.index.query.*;
19+
import org.elasticsearch.script.Script;
20+
import org.elasticsearch.script.ScriptService;
21+
import org.nlpcn.es4sql.Util;
1522
import org.nlpcn.es4sql.domain.Condition;
1623
import org.nlpcn.es4sql.domain.Condition.OPEAR;
1724
import org.nlpcn.es4sql.domain.Paramer;
25+
import org.nlpcn.es4sql.domain.Query;
1826
import org.nlpcn.es4sql.domain.Where;
1927
import org.nlpcn.es4sql.exception.SqlParseException;
2028

2129

30+
import org.nlpcn.es4sql.parse.ScriptFilter;
2231
import org.nlpcn.es4sql.parse.SubQueryExpression;
2332
import org.nlpcn.es4sql.spatial.*;
2433

@@ -238,7 +247,15 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
238247
x = QueryBuilders.nestedQuery(name,nestedFilter);
239248

240249
break;
241-
default:
250+
case SCRIPT:
251+
ScriptFilter scriptFilter = (ScriptFilter) value;
252+
Map<String, Object> params = null;
253+
if(scriptFilter.containsParameters()){
254+
params = scriptFilter.getArgs();
255+
}
256+
x = QueryBuilders.scriptQuery(new Script(scriptFilter.getScript(), ScriptService.ScriptType.INLINE, null, params));
257+
break;
258+
default:
242259
throw new SqlParseException("not define type " + cond.getName());
243260
}
244261

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,24 @@ public void multipleIndicesOneNotExistWithoutHint() throws IOException, SqlParse
827827
Assert.assertTrue(response.getTotalHits() > 0);
828828
}
829829

830+
//todo: find a way to check if scripts are enabled , uncomment before deploy.
831+
// @Test
832+
// public void scriptFilterNoParams() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
833+
// SearchHits response = query(String.format("SELECT insert_time FROM %s/online where script('doc[\\'insert_time\''].date.hourOfDay==16') " +
834+
// "and insert_time <'2014-08-21T00:00:00.000Z'", TEST_INDEX));
835+
// Assert.assertEquals(237,response.getTotalHits() );
836+
//
837+
// }
838+
//
839+
// @Test
840+
// public void scriptFilterWithParams() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
841+
// SearchHits response = query(String.format("SELECT insert_time FROM %s/online where script('doc[\\'insert_time\''].date.hourOfDay==x','x'=16) " +
842+
// "and insert_time <'2014-08-21T00:00:00.000Z'", TEST_INDEX));
843+
// Assert.assertEquals(237,response.getTotalHits() );
844+
//
845+
// }
846+
847+
830848
private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException {
831849
SearchDao searchDao = MainTestSuite.getSearchDao();
832850
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.nlpcn.es4sql.domain.hints.HintType;
1313
import org.nlpcn.es4sql.exception.SqlParseException;
1414
import org.nlpcn.es4sql.parse.ElasticSqlExprParser;
15+
import org.nlpcn.es4sql.parse.FieldMaker;
16+
import org.nlpcn.es4sql.parse.ScriptFilter;
1517
import org.nlpcn.es4sql.parse.SqlParser;
1618

1719
import java.io.IOException;
@@ -723,6 +725,38 @@ public void complexNestedTest() throws SqlParseException {
723725
Assert.assertEquals(2,where.getWheres().size());
724726
}
725727

728+
@Test
729+
public void scriptOnFilterNoParams() throws SqlParseException {
730+
String query = "select * from x where script('doc[\\'field\\'].date.hourOfDay == 3') ";
731+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
732+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
733+
Assert.assertEquals(Condition.OPEAR.SCRIPT,condition.getOpear());
734+
Assert.assertEquals(null,condition.getName());
735+
Assert.assertTrue(condition.getValue() instanceof ScriptFilter);
736+
ScriptFilter scriptFilter = (ScriptFilter) condition.getValue();
737+
Assert.assertEquals("doc['field'].date.hourOfDay == 3",scriptFilter.getScript());
738+
Assert.assertFalse(scriptFilter.containsParameters());
739+
}
740+
741+
@Test
742+
public void scriptOnFilterWithParams() throws SqlParseException {
743+
String query = "select * from x where script('doc[\\'field\\'].date.hourOfDay == x','x'=3) ";
744+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
745+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
746+
Assert.assertEquals(Condition.OPEAR.SCRIPT,condition.getOpear());
747+
Assert.assertEquals(null,condition.getName());
748+
Assert.assertTrue(condition.getValue() instanceof ScriptFilter);
749+
ScriptFilter scriptFilter = (ScriptFilter) condition.getValue();
750+
Assert.assertEquals("doc['field'].date.hourOfDay == x",scriptFilter.getScript());
751+
Assert.assertTrue(scriptFilter.containsParameters());
752+
Map<String, Object> args = scriptFilter.getArgs();
753+
Assert.assertEquals(1, args.size());
754+
Assert.assertTrue(args.containsKey("x"));
755+
Assert.assertEquals(3, args.get("x"));
756+
757+
}
758+
759+
726760

727761
private SQLExpr queryToExpr(String query) {
728762
return new ElasticSqlExprParser(query).expr();

0 commit comments

Comments
 (0)