Skip to content

Commit

Permalink
fix index name recognition. bug #37
Browse files Browse the repository at this point in the history
  • Loading branch information
omershelef committed Feb 28, 2015
1 parent a584ca9 commit 48db9ff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public SQLExprTableSource(SQLExpr expr){
this.tablename = expr.toString().replace(" ", "");
}

public SQLExprTableSource(String tablename){
this.tablename = tablename;
}

public SQLExpr getExpr() {
return this.expr;
}
Expand Down
65 changes: 52 additions & 13 deletions src/main/java/org/durid/sql/parser/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,26 @@ public class Lexer {

private int varIndex = -1;


public Lexer(String input){
this(input, true);
}


public Lexer(String input, boolean skipComment){
this.skipComment = skipComment;

this.text = input;
this.pos = -1;

scanChar();
}


public Lexer(char[] input, int inputLength, boolean skipComment){
this(new String(input, 0, inputLength), skipComment);
}

public final char charAt(int index) {
if (index >= text.length()) {
return EOI;
Expand Down Expand Up @@ -145,19 +161,6 @@ public void reset() {
this.token = savePoint.token;
}

public Lexer(String input, boolean skipComment){
this.skipComment = skipComment;

this.text = input;
this.pos = -1;

scanChar();
}

public Lexer(char[] input, int inputLength, boolean skipComment){
this(new String(input, 0, inputLength), skipComment);
}

protected final void scanChar() {
ch = charAt(++pos);
}
Expand Down Expand Up @@ -375,6 +378,42 @@ public final void nextToken() {

}


/**
* Scan all values until first whitespace, spaces near ',' are ignored,
* so values like 'hello , world' will capture as one token.
* @return all names as string.
*/
public String scanNames() {
String restOfText = text.substring(pos);
String[] splittedText = restOfText.split(",");

StringBuilder names = new StringBuilder();
for (String textPart : splittedText) {
String trimmedTextPart = textPart.trim();

// is last part?
if(trimmedTextPart.contains(" ")) {
int whitespaceIndex = trimmedTextPart.indexOf(" ");
if(whitespaceIndex != -1) {
trimmedTextPart = trimmedTextPart.substring(0, whitespaceIndex);
}
names.append(trimmedTextPart);
while(isWhitespace(charAt(pos))) {
scanChar();
}
pos += whitespaceIndex + 1;
break;
}

names.append(trimmedTextPart + ",");
pos += textPart.length() + 1;
}

ch = charAt(pos);
return names.toString();
}

private final void scanOperator() {
switch (ch) {
case '+':
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/durid/sql/parser/SQLSelectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ public void parseFrom(SQLSelectQueryBlock queryBlock) {
return;
}

lexer.nextToken();

queryBlock.setFrom(parseTableSource());
SQLTableSource source = new SQLExprTableSource(lexer.scanNames());
queryBlock.setFrom(source);
lexer.nextToken();
}

public SQLTableSource parseTableSource() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/nlpcn/es4sql/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void selectAliases() throws IOException, SqlParseException, SQLFeatureNot

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

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

0 comments on commit 48db9ff

Please sign in to comment.