Skip to content

Commit

Permalink
Improve gaussdb ddl parser (#6207)
Browse files Browse the repository at this point in the history
* Improve gaussdb ddl parser

* fix temp table
  • Loading branch information
woyumen4597 authored Oct 30, 2024
1 parent a74a796 commit 1613a76
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 483 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,8 @@ public enum Feature {
Dimension(1 << 8),
Set(1 << 9),
MultiSet(1 << 10),
Volatile(1 << 11);
Volatile(1 << 11),
Unlogged(1 << 12);
public final int mask;

Feature(int mask) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.alibaba.druid.sql.dialect.gaussdb.ast.stmt;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.gaussdb.ast.GaussDbDistributeBy;
import com.alibaba.druid.sql.dialect.gaussdb.ast.GaussDbObject;
import com.alibaba.druid.sql.dialect.gaussdb.visitor.GaussDbASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class GaussDbCreateTableStatement extends SQLCreateTableStatement implements GaussDbObject {
protected GaussDbDistributeBy distributeBy;

public GaussDbCreateTableStatement() {
super(DbType.gaussdb);
}

public void setDistributeBy(GaussDbDistributeBy distributeBy) {
if (distributeBy != null) {
distributeBy.setParent(this);
}
this.distributeBy = distributeBy;
}

public GaussDbDistributeBy getDistributeBy() {
return distributeBy;
}

@Override
public void accept0(SQLASTVisitor v) {
if (v instanceof GaussDbASTVisitor) {
accept0((GaussDbASTVisitor) v);
return;
}
if (v.visit(this)) {
acceptChild(v);
}
v.endVisit(this);
}

@Override
public void accept0(GaussDbASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.distributeBy);
acceptChild((SQLASTVisitor) visitor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.gaussdb.ast.GaussDbCreateTableStatement;
import com.alibaba.druid.sql.dialect.gaussdb.ast.GaussDbDistributeBy;
import com.alibaba.druid.sql.dialect.gaussdb.ast.stmt.GaussDbCreateTableStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGCreateTableParser;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.util.FnvHash;
Expand Down Expand Up @@ -54,58 +53,6 @@ protected void createTableBodyItem(SQLCreateTableStatement createTable) {

protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
GaussDbCreateTableStatement gdStmt = (GaussDbCreateTableStatement) stmt;
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
stmt.setComment(expr);
}
if (lexer.identifierEquals(FnvHash.Constants.AUTO_INCREMENT)) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
gdStmt.setAutoIncrement(expr);
}
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
}
if (lexer.identifierEquals(FnvHash.Constants.CHARSET)) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
gdStmt.setCharset(expr);
} else if (lexer.identifierEquals(FnvHash.Constants.CHARACTER)) {
lexer.nextToken();
accept(Token.SET);
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
gdStmt.setCharset(expr);
}
if (lexer.identifierEquals(FnvHash.Constants.COLLATE)) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
gdStmt.setCollate(expr);
}
if (lexer.identifierEquals(FnvHash.Constants.ENGINE)) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
gdStmt.setEngine(
this.exprParser.expr()
);
}
if (lexer.token() == Token.WITH) {
lexer.nextToken();
accept(Token.LPAREN);
Expand All @@ -116,89 +63,28 @@ protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
if (distributeByClause != null) {
gdStmt.setDistributeBy(distributeByClause);
}
SQLPartitionBy partitionClause = parsePartitionBy();
if (partitionClause != null) {
gdStmt.setPartitionBy(partitionClause);
if (lexer.nextIf(Token.COMMENT)) {
lexer.nextIf(Token.EQ);
SQLExpr comment = this.exprParser.expr();
gdStmt.setComment(comment);
}
}

public SQLPartitionBy parsePartitionBy() {
if (lexer.nextIf(Token.PARTITION)) {
accept(Token.BY);
if (lexer.nextIfIdentifier(FnvHash.Constants.HASH)) {
SQLPartitionBy hashPartition = new SQLPartitionByHash();
if (lexer.nextIf(Token.LPAREN)) {
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("expect identifier. " + lexer.info());
}
for (; ; ) {
hashPartition.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
return hashPartition;
}
} else if (lexer.nextIfIdentifier(FnvHash.Constants.RANGE)) {
return partitionByRange();
} else if (lexer.nextIfIdentifier(FnvHash.Constants.LIST)) {
return partitionByList();
}
}
return null;
}

protected SQLPartitionByRange partitionByRange() {
SQLPartitionByRange rangePartition = new SQLPartitionByRange();
accept(Token.LPAREN);
for (; ; ) {
rangePartition.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
accept(Token.LPAREN);
for (; ; ) {
rangePartition.addPartition(this.getExprParser().parsePartition());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
return rangePartition;
protected void createTableBefore(SQLCreateTableStatement createTable) {
parseTableType(createTable);
parseTableType(createTable);
}

private SQLPartitionByList partitionByList() {
SQLPartitionByList listPartition = new SQLPartitionByList();
accept(Token.LPAREN);
for (; ; ) {
listPartition.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
private void parseTableType(SQLCreateTableStatement createTable) {
if (lexer.nextIfIdentifier("UNLOGGED")) {
createTable.config(SQLCreateTableStatement.Feature.Unlogged);
} else if (lexer.nextIfIdentifier(FnvHash.Constants.GLOBAL)) {
createTable.config(SQLCreateTableStatement.Feature.Global);
} else if (lexer.nextIfIdentifier(FnvHash.Constants.TEMPORARY) || lexer.nextIfIdentifier("TEMP")) {
createTable.config(SQLCreateTableStatement.Feature.Temporary);
} else if (lexer.nextIf(Token.LOCAL)) {
createTable.config(SQLCreateTableStatement.Feature.Local);
}
accept(Token.RPAREN);
accept(Token.LPAREN);
for (; ; ) {
listPartition.addPartition(this.getExprParser().parsePartition());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
return listPartition;
}

public GaussDbDistributeBy parseDistributeBy() {
Expand Down
Loading

0 comments on commit 1613a76

Please sign in to comment.