Skip to content

Commit 48db9ff

Browse files
committed
fix index name recognition. bug #37
1 parent a584ca9 commit 48db9ff

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
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/QueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void selectAliases() throws IOException, SqlParseException, SQLFeatureNot
7373

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

7979
// assert the results is correct according to accounts.json data.

0 commit comments

Comments
 (0)