mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.odps.ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSourceImpl;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class OdpsLateralViewTableSource extends SQLTableSourceImpl {
|
||||
|
||||
private SQLTableSource tableSource;
|
||||
|
||||
private SQLMethodInvokeExpr method;
|
||||
|
||||
private List<SQLName> columns = new ArrayList<SQLName>(2);
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((OdpsASTVisitor) visitor);
|
||||
}
|
||||
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, method);
|
||||
acceptChild(visitor, columns);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLTableSource tableSource) {
|
||||
if (tableSource != null) {
|
||||
tableSource.setParent(this);
|
||||
}
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(SQLMethodInvokeExpr method) {
|
||||
if (method != null) {
|
||||
method.setParent(this);
|
||||
}
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<SQLName> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSource(long alias_hash) {
|
||||
long hash = this.aliasHashCode64();
|
||||
if (hash != 0 && hash == alias_hash) {
|
||||
return this;
|
||||
}
|
||||
|
||||
for (SQLName column : columns) {
|
||||
if (column.nameHashCode64() == alias_hash) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
if (tableSource != null) {
|
||||
return tableSource.findTableSource(alias_hash);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
|
||||
for (SQLName column : columns) {
|
||||
if (column.nameHashCode64() == columnNameHash) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
if (tableSource != null) {
|
||||
return tableSource.findTableSourceWithColumn(columnNameHash);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsLateralViewTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLLateralViewTableSource;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsValuesTableSource;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
@@ -167,41 +167,4 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
|
||||
return super.parseTableSource();
|
||||
}
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
|
||||
tableSource = super.parseTableSourceRest(tableSource);
|
||||
|
||||
if ("LATERAL".equalsIgnoreCase(tableSource.getAlias()) && lexer.token() == Token.VIEW) {
|
||||
return parseLateralView(tableSource);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("LATERAL")) {
|
||||
lexer.nextToken();
|
||||
return parseLateralView(tableSource);
|
||||
}
|
||||
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
protected SQLTableSource parseLateralView(SQLTableSource tableSource) {
|
||||
accept(Token.VIEW);
|
||||
if ("LATERAL".equalsIgnoreCase(tableSource.getAlias())) {
|
||||
tableSource.setAlias(null);
|
||||
}
|
||||
OdpsLateralViewTableSource lateralViewTabSrc = new OdpsLateralViewTableSource();
|
||||
lateralViewTabSrc.setTableSource(tableSource);
|
||||
|
||||
SQLMethodInvokeExpr udtf = (SQLMethodInvokeExpr) this.exprParser.expr();
|
||||
lateralViewTabSrc.setMethod(udtf);
|
||||
|
||||
String alias = as();
|
||||
lateralViewTabSrc.setAlias(alias);
|
||||
|
||||
accept(Token.AS);
|
||||
|
||||
this.exprParser.names(lateralViewTabSrc.getColumns());
|
||||
|
||||
return parseTableSourceRest(lateralViewTabSrc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.odps.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLLateralViewTableSource;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -103,10 +104,6 @@ public interface OdpsASTVisitor extends SQLASTVisitor {
|
||||
void endVisit(OdpsGrantStmt x);
|
||||
|
||||
boolean visit(OdpsGrantStmt x);
|
||||
|
||||
void endVisit(OdpsLateralViewTableSource x);
|
||||
|
||||
boolean visit(OdpsLateralViewTableSource x);
|
||||
|
||||
void endVisit(OdpsValuesTableSource x);
|
||||
|
||||
|
||||
@@ -231,16 +231,6 @@ public class OdpsASTVisitorAdapter extends SQLASTVisitorAdapter implements OdpsA
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsLateralViewTableSource x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsValuesTableSource x) {
|
||||
|
||||
|
||||
@@ -798,26 +798,6 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsLateralViewTableSource x) {
|
||||
x.getTableSource().accept(this);
|
||||
this.indentCount++;
|
||||
println();
|
||||
print0(ucase ? "LATERAL VIEW " : "lateral view ");
|
||||
x.getMethod().accept(this);
|
||||
print(' ');
|
||||
print0(x.getAlias());
|
||||
print0(ucase ? " AS " : " as ");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
this.indentCount--;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCharExpr x) {
|
||||
String text = x.getText();
|
||||
|
||||
@@ -23,8 +23,6 @@ import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class OdpsSchemaStatVisitor extends SchemaStatVisitor implements OdpsASTVisitor {
|
||||
|
||||
public OdpsSchemaStatVisitor() {
|
||||
@@ -265,16 +263,6 @@ public class OdpsSchemaStatVisitor extends SchemaStatVisitor implements OdpsASTV
|
||||
return super.visit((SQLGrantStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsLateralViewTableSource x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsValuesTableSource x) {
|
||||
|
||||
|
||||
@@ -557,11 +557,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
public void endVisit(PGLineSegmentsExpr x) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryExpr x) {
|
||||
print0(ucase ? "B'" : "b'");
|
||||
print0(x.getValue());
|
||||
print0(x.getText());
|
||||
print('\'');
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user