Skip to content

Commit

Permalink
Merge pull request #5258 from bucketli/master
Browse files Browse the repository at this point in the history
feat(oracle sql parser):support more sql parse
  • Loading branch information
wenshao committed May 7, 2023
2 parents 1616f79 + 215cddf commit ba4abe0
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.oracle.ast.stmt;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;

public class OracleAlterSummaryStatement extends OracleStatementImpl implements OracleAlterStatement {
private SQLName name;

private boolean compile;

@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
}
visitor.endVisit(this);
}

public SQLName getName() {
return name;
}

public void setName(SQLName name) {
this.name = name;
}

public boolean isCompile() {
return compile;
}

public void setCompile(boolean compile) {
this.compile = compile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.druid.sql.parser.Token.TABLE;

public class OracleStatementParser extends SQLStatementParser {
public OracleStatementParser(String sql) {
super(new OracleExprParser(sql));
Expand Down Expand Up @@ -299,6 +301,16 @@ public void parseStatementList(List<SQLStatement> statementList, int max, SQLObj
continue;
}

if (strVal.equalsIgnoreCase("ANALYZE")) {
acceptIdentifier("ANALYZE");
if (lexer.token() == TABLE) {
SQLAnalyzeTableStatement stmt = this.parseAnalyzeTable();
stmt.setParent(parent);
statementList.add(stmt);
continue;
}
}

if (strVal.equalsIgnoreCase("PIPE")) {
Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
Expand Down Expand Up @@ -1426,6 +1438,18 @@ public SQLStatement parserAlter() {
} else if (lexer.identifierEquals(FnvHash.Constants.TYPE)) {
lexer.reset(savePoint);
return parseAlterType();
} else if (lexer.identifierEquals("SUMMARY")) {
lexer.nextToken();

OracleAlterSummaryStatement stmt = new OracleAlterSummaryStatement();
stmt.setName(this.exprParser.name());

if (lexer.identifierEquals("COMPILE")) {
lexer.nextToken();
stmt.setCompile(true);
}

return stmt;
}

throw new ParserException("TODO : " + lexer.info());
Expand Down Expand Up @@ -1676,6 +1700,11 @@ public void parseAlterDrop(SQLAlterTableStatement stmt) {
lexer.nextToken();
SQLAlterTableDropConstraint item = new SQLAlterTableDropConstraint();
item.setConstraintName(this.exprParser.name());
if (lexer.token() == Token.CASCADE) {
lexer.nextToken();
item.setCascade(true);
}

stmt.addItem(item);
} else if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
Expand Down Expand Up @@ -2319,6 +2348,32 @@ public OracleExplainStatement parseExplain() {
return stmt;
}

public SQLAnalyzeTableStatement parseAnalyzeTable() {
accept(Token.TABLE);
SQLAnalyzeTableStatement stmt = new SQLAnalyzeTableStatement();

List<SQLName> names = new ArrayList<SQLName>();
this.exprParser.names(names, stmt);

for (SQLName name : names) {
stmt.setTable(new SQLExprTableSource(name));
}

if (lexer.token() == Token.PARTITION) {
stmt.setPartition(
parsePartitionRef()
);
}

if (lexer.token() == Token.COMPUTE) {
lexer.nextToken();
acceptIdentifier("STATISTICS");
stmt.setComputeStatistics(true);
}

return stmt;
}

public OracleDeleteStatement parseDeleteStatement() {
OracleDeleteStatement deleteStatement = new OracleDeleteStatement();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,13 @@ default boolean visit(OracleAlterTableShrinkSpace x) {
default void endVisit(OracleAlterTableShrinkSpace x) {
}

default boolean visit(OracleAlterSummaryStatement x) {
return true;
}

default void endVisit(OracleAlterSummaryStatement x) {
}

default boolean visit(OracleFileSpecification x) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,19 @@ public boolean visit(OracleAlterTableShrinkSpace x) {
return false;
}

@Override
public boolean visit(OracleAlterSummaryStatement x) {
print0(ucase ? "ALTER SUMMARY " : "alter summary ");

x.getName().accept(this);

if (x.isCompile()) {
print0(ucase ? " COMPILE " : " compile ");
}

return false;
}

@Override
public boolean visit(OracleFileSpecification x) {
printAndAccept(x.getFileNames(), ", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,61 @@ public void test_10() throws Exception {

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("KBS_QUESTION")));
}

public void test_11() throws Exception {
String sql = "analyze table JUNYU_ORCL.EQ_DRAGONCARD_TEMP compute statistics";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

Assert.assertEquals("ANALYZE TABLE JUNYU_ORCL.EQ_DRAGONCARD_TEMP COMPUTE STATISTICS",
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);

Assert.assertEquals(1, visitor.getTables().size());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("JUNYU_ORCL.EQ_DRAGONCARD_TEMP")));
}

public void test_12() throws Exception {
String sql = "alter table \"JUNYU_ORCL\".\"WORKER_STATS\" drop constraint \"SYS_C007550\" cascade;";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

Assert.assertEquals("ALTER TABLE \"JUNYU_ORCL\".\"WORKER_STATS\"\n" +
"\tDROP CONSTRAINT \"SYS_C007550\" CASCADE;",
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);

Assert.assertEquals(1, visitor.getTables().size());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("JUNYU_ORCL.WORKER_STATS")));
}

public void test_13() throws Exception {
String sql = " /* QSMQ VALIDATION */ ALTER SUMMARY \"CHJMESPRO\".\"MV_PRODUCTION_OVERVIEW_HOUR\" COMPILE;";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

Assert.assertEquals("ALTER SUMMARY \"CHJMESPRO\".\"MV_PRODUCTION_OVERVIEW_HOUR\" COMPILE ;",
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
}
}

0 comments on commit ba4abe0

Please sign in to comment.