improved odps sql parser. bug fixed for issue #1375

This commit is contained in:
wenshao
2016-08-21 13:58:38 +08:00
parent 0565f9c1a9
commit 103ee3e1a3
8 changed files with 381 additions and 28 deletions
@@ -352,4 +352,8 @@ public class OdpsLexer extends Lexer {
protected final void scanString() {
scanString2();
}
protected final void scanAlias() {
scanAlias2();
}
}
@@ -881,10 +881,31 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (text == null) {
print0(ucase ? "NULL" : "null");
} else {
print('\'');
String text2 = text.replaceAll("\\'", "\\\\'");
print0(text2);
print('\'');
StringBuilder buf = new StringBuilder(text.length() + 2);
buf.append('\'');
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
switch (ch) {
case '\\':
buf.append("\\\\");
break;
case '\'':
buf.append("\\'");
break;
case '\0':
buf.append("\\0");
break;
case '\n':
buf.append("\\n");
break;
default:
buf.append(ch);
break;
}
}
buf.append('\'');
print0(buf.toString());
}
return false;
@@ -809,7 +809,7 @@ public class Lexer {
}
}
private final void scanAlias() {
protected void scanAlias() {
mark = pos;
if (buf == null) {
@@ -856,6 +856,121 @@ public class Lexer {
//stringVal = subString(mark + 1, bufPos);
}
protected final void scanAlias2() {
{
boolean hasSpecial = false;
int startIndex = pos + 1;
int endIndex = -1; // text.indexOf('\'', startIndex);
for (int i = startIndex; i < text.length(); ++i) {
final char ch = text.charAt(i);
if (ch == '\\') {
hasSpecial = true;
continue;
}
if (ch == '"') {
endIndex = i;
break;
}
}
if (endIndex == -1) {
throw new ParserException("unclosed str");
}
String stringVal = subString(startIndex, endIndex - startIndex);
// hasSpecial = stringVal.indexOf('\\') != -1;
if (!hasSpecial) {
this.stringVal = stringVal;
int pos = endIndex + 1;
char ch = charAt(pos);
if (ch != '\'') {
this.pos = pos;
this.ch = ch;
token = LITERAL_CHARS;
return;
}
}
}
mark = pos;
boolean hasSpecial = false;
for (;;) {
if (isEOF()) {
lexError("unclosed.str.lit");
return;
}
ch = charAt(++pos);
if (ch == '\\') {
scanChar();
if (!hasSpecial) {
initBuff(bufPos);
arraycopy(mark + 1, buf, 0, bufPos);
hasSpecial = true;
}
switch (ch) {
case '0':
putChar('\0');
break;
case '\'':
putChar('\'');
break;
case '"':
putChar('"');
break;
case 'b':
putChar('\b');
break;
case 'n':
putChar('\n');
break;
case 'r':
putChar('\r');
break;
case 't':
putChar('\t');
break;
case '\\':
putChar('\\');
break;
case 'Z':
putChar((char) 0x1A); // ctrl + Z
break;
default:
putChar(ch);
break;
}
continue;
}
if (ch == '\"') {
scanChar();
token = LITERAL_CHARS;
break;
}
if (!hasSpecial) {
bufPos++;
continue;
}
if (bufPos == buf.length) {
putChar(ch);
} else {
buf[bufPos++] = ch;
}
}
if (!hasSpecial) {
stringVal = subString(mark + 1, bufPos);
} else {
stringVal = new String(buf, 0, bufPos);
}
}
public void scanSharp() {
scanVariable();
}
@@ -757,11 +757,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
SQLExpr where = x.getWhere();
if (where != null) {
println();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
where.setParent(x);
where.accept(this);
}
if (x.getGroupBy() != null) {