Skip to content

Commit b7124a9

Browse files
committed
Merge commit 'bd4d1c137583ccb10563e05e76c80db15fcdc4fa' into elastic2.0
Conflicts: src/main/java/org/nlpcn/es4sql/domain/Condition.java src/main/java/org/nlpcn/es4sql/query/maker/Maker.java
2 parents 2944743 + bd4d1c1 commit b7124a9

File tree

7 files changed

+72
-31
lines changed

7 files changed

+72
-31
lines changed

src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ public List<InternalSearchHit> innerRun() throws IOException, SqlParseException
7676
t2Alias);
7777
}
7878
if(firstTableRequest.getOriginalSelect().isOrderdSelect()){
79-
combinedResult.sort(new Comparator<InternalSearchHit>() {
79+
Collections.sort(combinedResult,new Comparator<InternalSearchHit>() {
8080
@Override
8181
public int compare(InternalSearchHit o1, InternalSearchHit o2) {
8282
return o1.docId() - o2.docId();
8383
}
8484
});
85+
8586
}
8687
return combinedResult;
8788
}

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.nlpcn.es4sql.domain;
22

33
import java.util.Arrays;
4+
import java.util.HashMap;
45
import java.util.Map;
56

6-
import com.alibaba.druid.sql.ast.SQLExpr;
77
import com.google.common.collect.BiMap;
88
import com.google.common.collect.HashBiMap;
9-
10-
import com.google.common.collect.ImmutableMap;
119
import org.nlpcn.es4sql.exception.SqlParseException;
1210

1311
/**
@@ -17,14 +15,23 @@
1715
*/
1816
public class Condition extends Where {
1917

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

21-
public enum OPEAR {
22-
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 , IDS_QUERY;
21+
public static Map<String,OPEAR> methodNameToOpear;
2322

24-
public static Map<String,OPEAR> methodNameToOpear = ImmutableMap.of("in_terms", IN_TERMS, "terms", IN_TERMS, "ids", IDS_QUERY, "ids_query", IDS_QUERY);
2523
private static BiMap<OPEAR, OPEAR> negatives;
2624

27-
25+
static {
26+
methodNameToOpear = new HashMap<>();
27+
methodNameToOpear.put("term",TERM);
28+
methodNameToOpear.put("matchterm",TERM);
29+
methodNameToOpear.put("match_term",TERM);
30+
methodNameToOpear.put("terms",IN_TERMS);
31+
methodNameToOpear.put("in_terms",IN_TERMS);
32+
methodNameToOpear.put("ids",IDS_QUERY);
33+
methodNameToOpear.put("ids_query",IDS_QUERY);
34+
}
2835
static {
2936
negatives = HashBiMap.create(7);
3037
negatives.put(EQ, N);
@@ -56,15 +63,11 @@ public OPEAR negative() throws SqlParseException {
5663

5764
private String nestedPath;
5865

59-
public Condition(CONN conn, String name, OPEAR opear, SQLExpr sqlExpr) throws SqlParseException {
60-
this(conn, name, opear, sqlExpr, false, null);
66+
public Condition(CONN conn, String name, OPEAR oper, Object value) throws SqlParseException {
67+
this(conn,name,oper,value,false,null);
6168
}
6269

63-
public Condition(CONN conn, String name, String opear, SQLExpr sqlExpr) throws SqlParseException {
64-
this(conn, name, opear, sqlExpr, false, null);
65-
}
66-
67-
public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNested , String nestedPath) throws SqlParseException {
70+
public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNested , String nestedPath) throws SqlParseException {
6871
super(conn);
6972
this.opear = null;
7073

@@ -79,6 +82,10 @@ public Condition(CONN conn, String name, OPEAR oper, Object value,boolean isNest
7982
this.nestedPath = nestedPath;
8083
}
8184

85+
public Condition(CONN conn, String name, String oper, Object value) throws SqlParseException {
86+
this(conn,name,oper,value,false,null);
87+
}
88+
8289
public Condition(CONN conn, String name, String oper, Object value,boolean isNested,String nestedPath) throws SqlParseException {
8390
super(conn);
8491

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,20 @@ else if (expr instanceof SQLMethodInvokeExpr) {
209209

210210
private Object[] getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException {
211211
List<Object> values = new ArrayList<>();
212-
boolean foundSubQuery = false;
213212
for(SQLExpr innerExpr : method.getParameters()){
214213
if(innerExpr instanceof SQLQueryExpr){
215-
foundSubQuery = true;
216214
Select select = parseSelect((MySqlSelectQueryBlock) ((SQLQueryExpr) innerExpr).getSubQuery().getQuery());
217215
values.add(new SubQueryExpression(select));
218216
}
217+
else if(innerExpr instanceof SQLTextLiteralExpr){
218+
values.add(((SQLTextLiteralExpr)innerExpr).getText());
219+
}
219220
else {
220221
values.add(innerExpr);
221222
}
222223

223224
}
224-
Object[] conditionValues ;
225-
if(foundSubQuery)
226-
conditionValues = values.toArray();
227-
else
228-
conditionValues = method.getParameters().toArray();
229-
return conditionValues;
225+
return values.toArray();
230226
}
231227

232228
private Object[] parseValue(List<SQLExpr> targetList) throws SqlParseException {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value)
9797
MatchQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery(name, paramer.value);
9898
bqb = Paramer.fullParamer(matchPhraseQuery, paramer);
9999
break;
100-
case "match_term":
101-
case "matchterm":
102-
case "term":
103-
bqb = QueryBuilders.termQuery(name,value.getParameters().get(0));
104-
break;
105100
default:
106101
throw new SqlParseException("it did not support this query method " + value.getMethodName());
107102

@@ -216,6 +211,10 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
216211
termValues = ((SubQueryExpression) termValues[0]).getValues();
217212
x = QueryBuilders.termsQuery(name,termValues);
218213
break;
214+
case TERM:
215+
Object term =( (Object[]) value)[0];
216+
x = QueryBuilders.termQuery(name,term);
217+
break;
219218
case IDS_QUERY:
220219
Object[] idsParameters = (Object[]) value;
221220
String[] ids;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
})
4343
public class MainTestSuite {
4444

45-
45+
4646
private static TransportClient client;
4747
private static SearchDao searchDao;
4848

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void inTestWithStrings() throws IOException, SqlParseException, SQLFeatur
304304
}
305305

306306
@Test
307-
public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
307+
public void inTermsTestWithIdentifiersTreatLikeStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
308308
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS(daenerys,eddard) LIMIT 1000", TEST_INDEX));
309309
SearchHit[] hits = response.getHits();
310310
Assert.assertEquals(2, response.getTotalHits());
@@ -313,6 +313,16 @@ public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLF
313313
assertThat(firstname, isOneOf("Daenerys", "Eddard"));
314314
}
315315
}
316+
@Test
317+
public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
318+
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS('daenerys','eddard') LIMIT 1000", TEST_INDEX));
319+
SearchHit[] hits = response.getHits();
320+
Assert.assertEquals(2, response.getTotalHits());
321+
for(SearchHit hit : hits) {
322+
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
323+
assertThat(firstname, isOneOf("Daenerys", "Eddard"));
324+
}
325+
}
316326

317327
@Test
318328
public void inTermsTestWithNumbers() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
@@ -336,7 +346,7 @@ public void termQueryWithNumber() throws IOException, SqlParseException, SQLFeat
336346
}
337347

338348
@Test
339-
public void termQueryWithString() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
349+
public void termQueryWithStringIdentifier() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
340350
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = term(brandon) LIMIT 1000", TEST_INDEX));
341351
SearchHit[] hits = response.getHits();
342352
Assert.assertEquals(1, response.getTotalHits());
@@ -345,6 +355,16 @@ public void termQueryWithString() throws IOException, SqlParseException, SQLFeat
345355
Assert.assertEquals("Brandon",firstname);
346356
}
347357

358+
@Test
359+
public void termQueryWithStringLiteral() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
360+
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = term('brandon') LIMIT 1000", TEST_INDEX));
361+
SearchHit[] hits = response.getHits();
362+
Assert.assertEquals(1, response.getTotalHits());
363+
SearchHit hit = hits[0];
364+
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
365+
Assert.assertEquals("Brandon",firstname);
366+
}
367+
348368

349369
/* TODO when using not in on some field, documents that not contains this
350370
field will return as well, That may considered a Wrong behaivor.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,24 @@ public void parseJoinWithOneTableOrderByRemoveAlias() throws SqlParseException {
692692

693693
}
694694

695+
@Test
696+
public void termsWithStringTest() throws SqlParseException {
697+
String query = "select * from x where y = IN_TERMS('a','b')";
698+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
699+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
700+
Object[] values = (Object[]) condition.getValue();
701+
Assert.assertEquals("a",values[0]);
702+
Assert.assertEquals("b",values[1]);
703+
}
704+
705+
@Test
706+
public void termWithStringTest() throws SqlParseException {
707+
String query = "select * from x where y = TERM('a')";
708+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
709+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
710+
Object[] values = (Object[]) condition.getValue();
711+
Assert.assertEquals("a",values[0]);
712+
}
695713

696714

697715
private SQLExpr queryToExpr(String query) {

0 commit comments

Comments
 (0)