Skip to content

Commit 7a6d98e

Browse files
committed
Merge branch 'develop'
2 parents 76ba4c2 + a544c60 commit 7a6d98e

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

src/main/java/org/durid/sql/ast/statement/SQLExprTableSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public SQLExprTableSource(SQLExpr expr){
3434
this.tablename = expr.toString().replace(" ", "");
3535
}
3636

37+
public SQLExprTableSource(String tablename){
38+
this.tablename = tablename;
39+
}
40+
3741
public SQLExpr getExpr() {
3842
return this.expr;
3943
}

src/main/java/org/durid/sql/parser/Lexer.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,26 @@ public class Lexer {
6767

6868
private int varIndex = -1;
6969

70+
7071
public Lexer(String input){
7172
this(input, true);
7273
}
7374

75+
76+
public Lexer(String input, boolean skipComment){
77+
this.skipComment = skipComment;
78+
79+
this.text = input;
80+
this.pos = -1;
81+
82+
scanChar();
83+
}
84+
85+
86+
public Lexer(char[] input, int inputLength, boolean skipComment){
87+
this(new String(input, 0, inputLength), skipComment);
88+
}
89+
7490
public final char charAt(int index) {
7591
if (index >= text.length()) {
7692
return EOI;
@@ -145,19 +161,6 @@ public void reset() {
145161
this.token = savePoint.token;
146162
}
147163

148-
public Lexer(String input, boolean skipComment){
149-
this.skipComment = skipComment;
150-
151-
this.text = input;
152-
this.pos = -1;
153-
154-
scanChar();
155-
}
156-
157-
public Lexer(char[] input, int inputLength, boolean skipComment){
158-
this(new String(input, 0, inputLength), skipComment);
159-
}
160-
161164
protected final void scanChar() {
162165
ch = charAt(++pos);
163166
}
@@ -375,6 +378,42 @@ public final void nextToken() {
375378

376379
}
377380

381+
382+
/**
383+
* Scan all values until first whitespace, spaces near ',' are ignored,
384+
* so values like 'hello , world' will capture as one token.
385+
* @return all names as string.
386+
*/
387+
public String scanNames() {
388+
String restOfText = text.substring(pos);
389+
String[] splittedText = restOfText.split(",");
390+
391+
StringBuilder names = new StringBuilder();
392+
for (String textPart : splittedText) {
393+
String trimmedTextPart = textPart.trim();
394+
395+
// is last part?
396+
if(trimmedTextPart.contains(" ")) {
397+
int whitespaceIndex = trimmedTextPart.indexOf(" ");
398+
if(whitespaceIndex != -1) {
399+
trimmedTextPart = trimmedTextPart.substring(0, whitespaceIndex);
400+
}
401+
names.append(trimmedTextPart);
402+
while(isWhitespace(charAt(pos))) {
403+
scanChar();
404+
}
405+
pos += whitespaceIndex + 1;
406+
break;
407+
}
408+
409+
names.append(trimmedTextPart + ",");
410+
pos += textPart.length() + 1;
411+
}
412+
413+
ch = charAt(pos);
414+
return names.toString();
415+
}
416+
378417
private final void scanOperator() {
379418
switch (ch) {
380419
case '+':

src/main/java/org/durid/sql/parser/SQLSelectParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ public void parseFrom(SQLSelectQueryBlock queryBlock) {
212212
return;
213213
}
214214

215-
lexer.nextToken();
216-
217-
queryBlock.setFrom(parseTableSource());
215+
SQLTableSource source = new SQLExprTableSource(lexer.scanNames());
216+
queryBlock.setFrom(source);
217+
lexer.nextToken();
218218
}
219219

220220
public SQLTableSource parseTableSource() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void setUp() throws Exception {
5252
loadBulk("src/test/resources/phrases.json");
5353
loadBulk("src/test/resources/online.json");
5454

55+
5556
searchDao = new SearchDao(client);
5657
System.out.println("Finished the setup process...");
5758
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public void multipleFromTest() throws IOException, SqlParseException, SQLFeature
3535
Assert.assertEquals(1004, response.getTotalHits());
3636
}
3737

38+
@Test
39+
public void indexWithWildcardTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
40+
SearchHits response = query("SELECT * FROM elasticsearch-* LIMIT 1000");
41+
assertThat(response.getTotalHits(), greaterThan(0L));
42+
}
43+
3844

3945
@Test
4046
public void selectSpecificFields() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
@@ -67,7 +73,7 @@ public void selectAliases() throws IOException, SqlParseException, SQLFeatureNot
6773

6874
@Test
6975
public void equallityTest() throws SqlParseException, SQLFeatureNotSupportedException {
70-
SearchHits response = query(String.format("select * from %s where city = 'Nogal' LIMIT 1000", TEST_INDEX));
76+
SearchHits response = query(String.format("select * from %s/account where city = 'Nogal' LIMIT 1000", TEST_INDEX));
7177
SearchHit[] hits = response.getHits();
7278

7379
// assert the results is correct according to accounts.json data.
@@ -80,7 +86,7 @@ public void equallityTest() throws SqlParseException, SQLFeatureNotSupportedExce
8086
// in some cases, depends on the analasis, we might want choose better behavior for equallity.
8187
@Test
8288
public void equallityTest_phrase() throws SqlParseException, SQLFeatureNotSupportedException {
83-
SearchHits response = query(String.format("SELECT * FROM %s WHERE phrase = 'quick fox here' LIMIT 1000", TEST_INDEX));
89+
SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE phrase = 'quick fox here' LIMIT 1000", TEST_INDEX));
8490
SearchHit[] hits = response.getHits();
8591

8692
// assert the results is correct according to accounts.json data.
@@ -228,7 +234,7 @@ public void notBetweenTest() throws IOException, SqlParseException, SQLFeatureNo
228234

229235
@Test
230236
public void inTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
231-
SearchHits response = query(String.format("SELECT age FROM %s WHERE age IN (20, 22) LIMIT 1000", TEST_INDEX));
237+
SearchHits response = query(String.format("SELECT age FROM %s/phrase WHERE age IN (20, 22) LIMIT 1000", TEST_INDEX));
232238
SearchHit[] hits = response.getHits();
233239
for(SearchHit hit : hits) {
234240
int age = (int) hit.getSource().get("age");
@@ -239,7 +245,7 @@ public void inTest() throws IOException, SqlParseException, SQLFeatureNotSupport
239245

240246
@Test
241247
public void inTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
242-
SearchHits response = query(String.format("SELECT phrase FROM %s WHERE phrase IN ('quick fox here', 'fox brown') LIMIT 1000", TEST_INDEX));
248+
SearchHits response = query(String.format("SELECT phrase FROM %s/phrase WHERE phrase IN ('quick fox here', 'fox brown') LIMIT 1000", TEST_INDEX));
243249
SearchHit[] hits = response.getHits();
244250
Assert.assertEquals(2, response.getTotalHits());
245251
for(SearchHit hit : hits) {

0 commit comments

Comments
 (0)