Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de72b2f

Browse files
committed
Merge commit '6d01f2ed24d198d040e767a840bfd97141bda821' into elastic2.0
Conflicts: src/main/java/org/nlpcn/es4sql/domain/Condition.java
2 parents a3c15b8 + 6d01f2e commit de72b2f

File tree

12 files changed

+131
-33
lines changed

12 files changed

+131
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ http://localhost:9200/_sql/_explain?sql=select * from indexName limit 10
5555

5656

5757

58-
## SQL Usuage
58+
## SQL Usage
5959

6060
* Query
6161

src/main/java/org/nlpcn/es4sql/Util.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ public static double[] KV2DoubleArr(List<KVValue> params) {
7272
return ds;
7373
}
7474

75+
76+
public static String extendedToString(SQLExpr sqlExpr) {
77+
if(sqlExpr instanceof SQLTextLiteralExpr){
78+
return ((SQLTextLiteralExpr) sqlExpr).getText();
79+
}
80+
return sqlExpr.toString();
81+
}
7582
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
public class Condition extends Where {
1717

18-
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;
18+
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;
2020

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

@@ -63,8 +63,11 @@ public OPEAR negative() throws SqlParseException {
6363

6464
private String nestedPath;
6565

66-
public Condition(CONN conn, String name, OPEAR oper, Object value) throws SqlParseException {
67-
this(conn,name,oper,value,false,null);
66+
public Condition(CONN conn, String field, String condition, Object obj) throws SqlParseException {
67+
this(conn, field, condition, obj, false, null);
68+
}
69+
public Condition(CONN conn, String field, OPEAR condition, Object obj) throws SqlParseException {
70+
this(conn, field, condition, obj, false, null);
6871
}
6972

7073
public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNested , String nestedPath) throws SqlParseException {
@@ -82,10 +85,6 @@ public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNest
8285
this.nestedPath = nestedPath;
8386
}
8487

85-
public Condition(CONN conn, String name, String oper, Object value) throws SqlParseException {
86-
this(conn,name,oper,value,false,null);
87-
}
88-
8988
public Condition(CONN conn, String name, String oper, Object value,boolean isNested,String nestedPath) throws SqlParseException {
9089
super(conn);
9190

@@ -164,6 +163,9 @@ public Condition(CONN conn, String name, String oper, Object value,boolean isNes
164163
case "GEO_CELL":
165164
this.opear = OPEAR.GEO_CELL;
166165
break;
166+
case "NESTED":
167+
this.opear = OPEAR.NESTED_COMPLEX;
168+
break;
167169
default:
168170
throw new SqlParseException(oper + " is err!");
169171
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static Field makeField(SQLExpr expr, String alias,String tableAlias) thro
3535
if(methodName.toLowerCase().equals("nested")){
3636
NestedType nestedType = new NestedType();
3737
if(nestedType.tryFillFromExpr(mExpr)){
38-
return handleIdentifier(nestedType,alias,tableAlias);
38+
return handleIdentifier(nestedType, alias, tableAlias);
3939
}
4040
}
4141
else if (methodName.toLowerCase().equals("filter")){
@@ -46,11 +46,12 @@ else if (methodName.toLowerCase().equals("filter")){
4646
SQLAggregateExpr sExpr = (SQLAggregateExpr) expr;
4747
return makeMethodField(sExpr.getMethodName(), sExpr.getArguments(), sExpr.getOption(), alias);
4848
} else {
49-
throw new SqlParseException("unknow field name : " + expr);
49+
throw new SqlParseException("unknown field name : " + expr);
5050
}
5151
return null;
5252
}
5353

54+
5455
private static Field makeFilterMethodField(SQLMethodInvokeExpr filterMethod,String alias) throws SqlParseException {
5556
List<SQLExpr> parameters = filterMethod.getParameters();
5657
int parametersSize = parameters.size();
@@ -64,8 +65,7 @@ private static Field makeFilterMethodField(SQLMethodInvokeExpr filterMethod,Stri
6465
filterAlias = "filter(" + exprToCheck.toString().replaceAll("\n"," ") +")";
6566
}
6667
if(parametersSize == 2){
67-
//todo: function extendedToString - if sqlString remove ''
68-
filterAlias = extendedToString(parameters.get(0));
68+
filterAlias = Util.extendedToString(parameters.get(0));
6969
exprToCheck = parameters.get(1);
7070
}
7171
Where where = Where.newInstance();
@@ -78,12 +78,7 @@ private static Field makeFilterMethodField(SQLMethodInvokeExpr filterMethod,Stri
7878
return new MethodField("filter", methodParameters, null, alias);
7979
}
8080

81-
private static String extendedToString(SQLExpr sqlExpr) {
82-
if(sqlExpr instanceof SQLTextLiteralExpr){
83-
return ((SQLTextLiteralExpr) sqlExpr).getText();
84-
}
85-
return sqlExpr.toString();
86-
}
81+
8782

8883
private static Field handleIdentifier(NestedType nestedType, String alias, String tableAlias) {
8984
Field field = handleIdentifier(new SQLIdentifierExpr(nestedType.field), alias, tableAlias);

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.nlpcn.es4sql.parse;
22

33
import com.alibaba.druid.sql.ast.SQLExpr;
4+
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
45
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
6+
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
7+
import com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr;
8+
import org.nlpcn.es4sql.Util;
9+
import org.nlpcn.es4sql.domain.Where;
510
import org.nlpcn.es4sql.exception.SqlParseException;
611

712
import java.util.List;
@@ -12,6 +17,8 @@
1217
public class NestedType {
1318
public String field;
1419
public String path;
20+
public Where where;
21+
private boolean simple;
1522

1623
public boolean tryFillFromExpr(SQLExpr expr) throws SqlParseException {
1724
if (!(expr instanceof SQLMethodInvokeExpr)) return false;
@@ -20,20 +27,38 @@ public boolean tryFillFromExpr(SQLExpr expr) throws SqlParseException {
2027

2128
List<SQLExpr> parameters = method.getParameters();
2229
if (parameters.size() != 2 && parameters.size() != 1)
23-
throw new SqlParseException("on nested object only allowed 2 parameters (field,path) or 1 parameter (field) ");
30+
throw new SqlParseException("on nested object only allowed 2 parameters (field,path)/(path,conditions..) or 1 parameter (field) ");
2431

25-
String field = parameters.get(0).toString();
32+
String field = Util.extendedToString(parameters.get(0));
2633
this.field = field;
2734
if (parameters.size() == 1) {
2835
//calc path myself..
2936
if (!field.contains("."))
3037
throw new SqlParseException("nested should contain . on their field name");
3138
int lastDot = field.lastIndexOf(".");
3239
this.path = field.substring(0, lastDot);
40+
this.simple = true;
3341
} else if (parameters.size() == 2) {
34-
this.path = parameters.get(1).toString();
42+
SQLExpr secondParameter = parameters.get(1);
43+
if(secondParameter instanceof SQLTextLiteralExpr || secondParameter instanceof SQLIdentifierExpr || secondParameter instanceof SQLPropertyExpr) {
44+
this.path = Util.extendedToString(secondParameter);
45+
this.simple = true;
46+
}
47+
else {
48+
this.path = field;
49+
Where where = Where.newInstance();
50+
new SqlParser().parseWhere(secondParameter,where);
51+
if(where.getWheres().size() == 0)
52+
throw new SqlParseException("unable to parse filter where.");
53+
this.where = where;
54+
simple = false;
55+
}
3556
}
3657

3758
return true;
3859
}
60+
61+
public boolean isSimple() {
62+
return simple;
63+
}
3964
}

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,37 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse
173173
where.addWhere(condition);
174174
}
175175
else if (expr instanceof SQLMethodInvokeExpr) {
176+
176177
SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr;
177178
List<SQLExpr> methodParameters = methodExpr.getParameters();
178179

179180
String methodName = methodExpr.getMethodName();
180-
String fieldName = methodParameters.get(0).toString();
181-
NestedType nestedType = new NestedType();
182-
if(nestedType.tryFillFromExpr(methodParameters.get(0))){
183-
fieldName = nestedType.field;
181+
if(SpatialParamsFactory.isAllowedMethod(methodName)){
182+
183+
String fieldName = methodParameters.get(0).toString();
184+
NestedType nestedType = new NestedType();
185+
if (nestedType.tryFillFromExpr(methodParameters.get(0))) {
186+
fieldName = nestedType.field;
187+
}
188+
189+
Object spatialParamsObject = SpatialParamsFactory.generateSpatialParamsObject(methodName, methodParameters);
190+
191+
Condition condition = new Condition(CONN.valueOf(opear), fieldName, methodName, spatialParamsObject, nestedType.field != null, nestedType.path);
192+
where.addWhere(condition);
184193
}
185194

186-
Object spatialParamsObject = SpatialParamsFactory.generateSpatialParamsObject(methodName, methodParameters);
195+
else if (methodName.toLowerCase().equals("nested")){
196+
NestedType nestedType = new NestedType();
197+
if(!nestedType.tryFillFromExpr(expr)){
198+
throw new SqlParseException("could not fill nested from expr:"+expr);
199+
}
200+
Condition condition = new Condition(CONN.valueOf(opear),nestedType.path,methodName.toUpperCase(),nestedType.where);
201+
where.addWhere(condition);
187202

188-
Condition condition = new Condition(CONN.valueOf(opear), fieldName, methodName, spatialParamsObject,nestedType.field!=null,nestedType.path);
189-
where.addWhere(condition);
203+
}
204+
else {
205+
throw new SqlParseException("unsupported method: " + methodName);
206+
}
190207
} else if (expr instanceof SQLInSubQueryExpr){
191208
SQLInSubQueryExpr sqlIn = (SQLInSubQueryExpr) expr;
192209
Select innerSelect = parseSelect((MySqlSelectQueryBlock) sqlIn.getSubQuery().getQuery());

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.nlpcn.es4sql.domain.Condition;
1616
import org.nlpcn.es4sql.domain.Condition.OPEAR;
1717
import org.nlpcn.es4sql.domain.Paramer;
18+
import org.nlpcn.es4sql.domain.Where;
1819
import org.nlpcn.es4sql.exception.SqlParseException;
1920

2021

@@ -228,6 +229,15 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
228229
}
229230
x = QueryBuilders.idsQuery(type).addIds(ids);
230231
break;
232+
case NESTED_COMPLEX:
233+
if(value == null || ! (value instanceof Where) )
234+
throw new SqlParseException("unsupported nested condition");
235+
Where where = (Where) value;
236+
BoolQueryBuilder nestedFilter = QueryMaker.explan(where);
237+
238+
x = QueryBuilders.nestedQuery(name,nestedFilter);
239+
240+
break;
231241
default:
232242
throw new SqlParseException("not define type " + cond.getName());
233243
}

src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44
import com.alibaba.druid.sql.ast.SQLExpr;
55
import com.alibaba.druid.sql.parser.SQLParseException;
66

7+
import java.util.HashSet;
78
import java.util.LinkedList;
89
import java.util.List;
10+
import java.util.Set;
911

1012
/**
1113
* Created by Eliran on 1/8/2015.
1214
*/
1315
public class SpatialParamsFactory {
16+
public static Set<String> allowedMethods ;
17+
static {
18+
allowedMethods = new HashSet<>();
19+
allowedMethods.add("GEO_INTERSECTS");
20+
allowedMethods.add("GEO_BOUNDING_BOX");
21+
allowedMethods.add("GEO_DISTANCE");
22+
allowedMethods.add("GEO_DISTANCE_RANGE");
23+
allowedMethods.add("GEO_POLYGON");
24+
allowedMethods.add("GEO_CELL");
25+
}
26+
public static boolean isAllowedMethod(String name){
27+
return allowedMethods.contains(name);
28+
}
1429
public static Object generateSpatialParamsObject(String methodName,List<SQLExpr> params){
1530
switch(methodName){
1631
case "GEO_INTERSECTS":

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ private static void prepareNestedTypeIndex() {
9797
" \"info\": {\n" +
9898
" \"type\": \"string\",\n" +
9999
" \"index\": \"not_analyzed\"\n" +
100+
" },\n" +
101+
" \"author\": {\n" +
102+
" \"type\": \"string\",\n" +
103+
" \"index\": \"not_analyzed\"\n" +
100104
" }\n" +
101105
" }\n" +
102106
" },\n" +

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ public void nestedOnInQuery() throws IOException, SqlParseException, SQLFeatureN
792792
Assert.assertEquals(3, response.getTotalHits());
793793
}
794794

795+
@Test
796+
public void complexNestedQueryBothOnSameObject() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
797+
SearchHits response = query(String.format("SELECT * FROM %s/nestedType where nested('message',message.info = 'a' and message.author ='i' ) ", TEST_INDEX));
798+
Assert.assertEquals(1, response.getTotalHits());
799+
}
800+
@Test
801+
public void complexNestedQueryNotBothOnSameObject() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
802+
SearchHits response = query(String.format("SELECT * FROM %s/nestedType where nested('message',message.info = 'a' and message.author ='h' ) ", TEST_INDEX));
803+
Assert.assertEquals(0, response.getTotalHits());
804+
}
805+
795806
@Test
796807
public void nestedOnInTermsQuery() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
797808
SearchHits response = query(String.format("SELECT * FROM %s/nestedType where nested(message.info) = IN_TERMS(a,b)", TEST_INDEX));

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,18 @@ public void termWithStringTest() throws SqlParseException {
711711
Assert.assertEquals("a",values[0]);
712712
}
713713

714+
@Test
715+
public void complexNestedTest() throws SqlParseException {
716+
String query = "select * from x where nested('y',y.b = 'a' and y.c = 'd') ";
717+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
718+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
719+
Assert.assertEquals(Condition.OPEAR.NESTED_COMPLEX,condition.getOpear());
720+
Assert.assertEquals("y",condition.getName());
721+
Assert.assertTrue(condition.getValue() instanceof Where);
722+
Where where = (Where) condition.getValue();
723+
Assert.assertEquals(2,where.getWheres().size());
724+
}
725+
714726

715727
private SQLExpr queryToExpr(String query) {
716728
return new ElasticSqlExprParser(query).expr();
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{"index":{"_type": "nestedType", "_id":"1"}}
2-
{"message":{"info":"a"},"myNum":1}
2+
{"message":{"info":"a","author":"e"},"myNum":1}
33
{"index":{"_type": "nestedType", "_id":"2"}}
4-
{"message":{"info":"b"},"myNum":2}
4+
{"message":{"info":"b","author":"f"},"myNum":2}
55
{"index":{"_type": "nestedType", "_id":"3"}}
6-
{"message":{"info":"c"},"myNum":3}
6+
{"message":{"info":"c","author":"g"},"myNum":3}
77
{"index":{"_type": "nestedType", "_id":"4"}}
8-
{"message":[{"info":"c"},{"info":"a"}],"myNum":4}
8+
{"message":[{"info":"c","author":"h"},{"info":"a","author":"i"}],"myNum":4}

0 commit comments

Comments
 (0)