Skip to content

Commit

Permalink
enhance holo-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
woyumen4597 committed Aug 6, 2024
1 parent be9bbdb commit 6be4b90
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.alibaba.druid.sql.dialect.hologres.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLParserFeature;
Expand All @@ -21,8 +18,4 @@ public HologresExprParser(Lexer lexer) {
dbType = DbType.hologres;
}

@Override
protected void parseAssignItemOnComma(SQLExpr sqlExpr, SQLAssignItem item, SQLObject parent) {
item.setValue(sqlExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,34 @@ public PGCreateTableParser(SQLExprParser exprParser) {
}

protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
// For partition of/by for PG
for (int i = 0; i < 2; i++) {
if (lexer.token() == Token.PARTITION) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (Token.OF.equals(lexer.token())) {
lexer.reset(mark);
SQLPartitionOf partitionOf = parsePartitionOf();
stmt.setPartitionOf(partitionOf);
} else if (Token.BY.equals(lexer.token())) {
lexer.reset(mark);
SQLPartitionBy partitionClause = parsePartitionBy();
stmt.setPartitionBy(partitionClause);
}
}
}

if (lexer.nextIf(Token.WITH)) {
accept(Token.LPAREN);
parseAssignItems(stmt.getTableOptions(), stmt, false);
accept(Token.RPAREN);
}
super.parseCreateTableRest(stmt);

if (lexer.nextIf(Token.TABLESPACE)) {
stmt.setTablespace(
this.exprParser.name()
);
}
}

public SQLPartitionBy parsePartitionBy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.*;
import com.alibaba.druid.sql.parser.Lexer;
Expand Down Expand Up @@ -223,24 +221,6 @@ protected void parseDataTypeDouble(StringBuilder typeName) {
lexer.nextToken();
}

@Override
protected void parseAssignItemOnComma(SQLExpr sqlExpr, SQLAssignItem item, SQLObject parent) {
if (lexer.token() == Token.COMMA) {
SQLListExpr listExpr = new SQLListExpr();
listExpr.addItem(sqlExpr);
sqlExpr.setParent(listExpr);
do {
lexer.nextToken();
SQLExpr listItem = this.expr();
listItem.setParent(listExpr);
listExpr.addItem(listItem);
} while (lexer.token() == Token.COMMA);
item.setValue(listExpr);
} else {
item.setValue(sqlExpr);
}
}

@Override
protected SQLExpr parseSelectItemRest(String ident, long hash_lower) {
SQLExpr expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2880,4 +2880,15 @@ protected void printTableOptionsPrefix(SQLCreateTableStatement x) {
protected boolean legacyCube() {
return true;
}

protected void printTableOption(SQLExpr name, SQLExpr value, int index) {
if (index != 0) {
print(",");
println();
}
String key = name.toString();
print0(key);
print0(" = ");
value.accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ protected void createTableAfter(SQLCreateTableStatement stmt) {
}

protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
if (lexer.nextIf(Token.TABLESPACE)) {
stmt.setTablespace(
this.exprParser.name()
);
}

// For partition of for PG
if (lexer.token() == Token.PARTITION) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
Expand All @@ -196,7 +191,7 @@ protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
stmt.setPartitionBy(partitionClause);
}
}

// For partition by
if (lexer.token() == Token.PARTITION) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
Expand All @@ -210,6 +205,12 @@ protected void parseCreateTableRest(SQLCreateTableStatement stmt) {
stmt.setPartitionBy(partitionClause);
}
}

if (lexer.nextIf(Token.TABLESPACE)) {
stmt.setTablespace(
this.exprParser.name()
);
}
}

public SQLPartitionBy parsePartitionBy() {
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/resources/bvt/parser/hologres/0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ CREATE TABLE public.tb0 (
PRIMARY KEY (id)
)
WITH (
'orientation' = 'column',
'clustering_key' = 'class',
'bitmap_columns' = 'name',
'event_time_column' = 'in_time'
orientation = 'column',
clustering_key = 'class',
bitmap_columns = 'name',
event_time_column = 'in_time'
);
------------------------------------------------------------------------------------------------------------------------
ALTER SCHEMA oldschema RENAME TO newschema;
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/resources/bvt/parser/hologres/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE replicated_orders (
order_id BIGINT NOT NULL,
customer_id INTEGER,
order_date DATE,
product_id INTEGER,
quantity INTEGER,
price DECIMAL(10, 2),
PRIMARY KEY (order_date, customer_id, order_id)
)
PARTITION BY LIST(order_date)
WITH (orientation='column',
distribution_key='order_date',
clustering_key='order_date'
);
--------------------
CREATE TABLE replicated_orders (
order_id BIGINT NOT NULL,
customer_id INTEGER,
order_date DATE,
product_id INTEGER,
quantity INTEGER,
price DECIMAL(10, 2),
PRIMARY KEY (order_date, customer_id, order_id)
)
PARTITION BY LIST (order_date)
WITH (
orientation = 'column',
distribution_key = 'order_date',
clustering_key = 'order_date'
);
14 changes: 14 additions & 0 deletions core/src/test/resources/bvt/parser/postgresql/1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ CREATE TABLE IF NOT EXISTS test_dim_segment (
)
PARTITION BY LIST (ds);
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE distributors (
did integer,
name varchar(40)
)
WITH (fillfactor=70);
--------------------
CREATE TABLE distributors (
did integer,
name varchar(40)
)
WITH (
fillfactor = 70
);
------------------------------------------------------------------------------------------------------------------------
MERGE INTO CustomerAccount CA
USING (Select CustomerId, TransactionValue From RecentTransactions) AS T
ON CA.CustomerId = T.CustomerId
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/bvt/parser/postgresql/17.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ CREATE TABLE "public"."city" (
"name" varchar(32) NOT NULL
)
WITH (
'OIDS' = false
OIDS = false
);
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE KTV.KTV_FUNCTION_ROLE_20151211 (
Expand Down

0 comments on commit 6be4b90

Please sign in to comment.