Skip to content

Commit

Permalink
Fix ob partition by column issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingo-xp authored and wenshao committed Nov 27, 2024
1 parent efb0d6d commit 82adc39
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class SQLPartitionByList extends SQLPartitionBy {
protected PartitionByListType type;
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
Expand All @@ -29,6 +30,14 @@ protected void accept0(SQLASTVisitor visitor) {
visitor.endVisit(this);
}

public PartitionByListType getType() {
return type;
}

public void setType(PartitionByListType type) {
this.type = type;
}

public SQLPartitionByList clone() {
SQLPartitionByList x = new SQLPartitionByList();

Expand All @@ -46,4 +55,8 @@ public SQLPartitionByList clone() {
public void cloneTo(SQLPartitionByList x) {
super.cloneTo(x);
}
public enum PartitionByListType {
LIST_EXPRESSION,
LIST_COLUMNS,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected SQLCreateTableStatement newCreateStatement() {
public SQLPartitionBy parsePartitionBy() {
lexer.nextToken();
accept(Token.BY);
SQLPartitionBy sqlPartitionBy = new SQLPartitionByList();
SQLPartitionByList sqlPartitionBy = new SQLPartitionByList();
sqlPartitionBy.setType(SQLPartitionByList.PartitionByListType.LIST_EXPRESSION);
boolean hasParen = false;
if (lexer.nextIf(Token.LPAREN)) {
hasParen = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ public SQLPartitionBy parsePartitionBy() {
partitionClause = clause;

partitionClauseRest(clause);
} else if (lexer.identifierEquals("HASH") || lexer.identifierEquals("UNI_HASH")) {
} else if (lexer.identifierEquals(FnvHash.Constants.HASH) || lexer.identifierEquals("UNI_HASH")) {
SQLPartitionByHash clause = new SQLPartitionByHash();

if (lexer.identifierEquals("UNI_HASH")) {
Expand All @@ -1096,28 +1096,30 @@ public SQLPartitionBy parsePartitionBy() {

partitionClauseRest(clause);

} else if (lexer.identifierEquals("RANGE")) {
} else if (lexer.identifierEquals(FnvHash.Constants.RANGE)) {
SQLPartitionByRange clause = partitionByRange();
partitionClause = clause;

partitionClauseRest(clause);

} else if (lexer.identifierEquals("VALUE")) {
} else if (lexer.identifierEquals(FnvHash.Constants.VALUE)) {
SQLPartitionByValue clause = partitionByValue();
partitionClause = clause;

partitionClauseRest(clause);

} else if (lexer.identifierEquals("LIST")) {
} else if (lexer.identifierEquals(FnvHash.Constants.LIST)) {
lexer.nextToken();
SQLPartitionByList clause = new SQLPartitionByList();

if (lexer.token() == Token.LPAREN) {
clause.setType(SQLPartitionByList.PartitionByListType.LIST_EXPRESSION);
lexer.nextToken();
clause.addColumn(this.exprParser.expr());
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
acceptIdentifier(FnvHash.Constants.COLUMNS);
clause.setType(SQLPartitionByList.PartitionByListType.LIST_COLUMNS);
accept(Token.LPAREN);
for (; ; ) {
clause.addColumn(this.exprParser.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public SQLPartitionBy parsePartitionBy() {
SQLPartitionByList list = new SQLPartitionByList();

if (lexer.token() == Token.LPAREN) {
list.setType(SQLPartitionByList.PartitionByListType.LIST_EXPRESSION);
lexer.nextToken();
list.addColumn(this.exprParser.expr());
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
list.setType(SQLPartitionByList.PartitionByListType.LIST_COLUMNS);
accept(Token.LPAREN);
for (; ; ) {
list.addColumn(this.exprParser.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ public SQLPartitionBy parsePartitionBy() {
SQLPartitionByList list = new SQLPartitionByList();

if (lexer.token() == Token.LPAREN) {
list.setType(SQLPartitionByList.PartitionByListType.LIST_EXPRESSION);
lexer.nextToken();
list.addColumn(this.exprParser.expr());
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
list.setType(SQLPartitionByList.PartitionByListType.LIST_COLUMNS);
accept(Token.LPAREN);
for (; ; ) {
list.addColumn(this.exprParser.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public SQLPartitionBy parsePartitionBy() {
hasLparen = true;
} else if (lexer.nextIfIdentifier(FnvHash.Constants.LIST)) {
partitionClause = new SQLPartitionByList();
((SQLPartitionByList) partitionClause).setType(SQLPartitionByList.PartitionByListType.LIST_EXPRESSION);
accept(Token.LPAREN);
hasLparen = true;
} else if (lexer.nextIf(Token.LPAREN)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8011,12 +8011,12 @@ public boolean visit(SQLPartitionByRange x) {
@Override
public boolean visit(SQLPartitionByList x) {
print0(ucase ? "LIST " : "list ");
if (x.getColumns().size() == 1) {
print('(');
x.getColumns().get(0).accept(this);
if (SQLPartitionByList.PartitionByListType.LIST_COLUMNS.equals(x.getType())) {
print0(ucase ? "COLUMNS (" : "columns (");
printAndAccept(x.getColumns(), ", ");
print0(")");
} else {
print0(ucase ? "COLUMNS (" : "columns (");
print('(');
printAndAccept(x.getColumns(), ", ");
print0(")");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.sql.oceanbase;

import com.alibaba.druid.DbType;
import com.alibaba.druid.bvt.sql.SQLResourceTest;
import org.junit.Test;

public class OceanBaseResourceTest extends SQLResourceTest {
public OceanBaseResourceTest() {
super(DbType.oceanbase);
}

@Test
public void oceanbase_parse() throws Exception {
fileTest(0, 999, i -> "bvt/parser/oceanbase/" + i + ".txt");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static org.junit.Assert.assertEquals;

public class TrinoResourceTest extends SQLResourceTest{
public class TrinoResourceTest extends SQLResourceTest {

public TrinoResourceTest() {
super(DbType.trino);
Expand Down Expand Up @@ -159,7 +159,7 @@ public void test_92() throws Exception {

@Test
public void trino_parse() throws Exception {
fileTest(1, 999, i -> "bvt/parser/trino/" + i + ".txt");
fileTest(0, 999, i -> "bvt/parser/trino/" + i + ".txt");
}

public void exec_test(String resource) throws Exception {
Expand Down
38 changes: 38 additions & 0 deletions core/src/test/resources/bvt/parser/oceanbase/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE TABLE t2_m_lch(col1 INT,col2 INT)
PARTITION BY LIST COLUMNS(col1)
SUBPARTITION BY HASH(col2) SUBPARTITIONS 5
(PARTITION p0 VALUES IN(100),
PARTITION p1 VALUES IN(200),
PARTITION p2 VALUES IN(300)
);
--------------------
CREATE TABLE t2_m_lch (
col1 INT,
col2 INT
)
PARTITION BY LIST COLUMNS (col1)
SUBPARTITION BY HASH (col2) SUBPARTITIONS 5 (
PARTITION p0 VALUES IN (100),
PARTITION p1 VALUES IN (200),
PARTITION p2 VALUES IN (300)
);
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE t2_m_lh (col1 INT NOT NULL,col2 varchar(50),col3 INT NOT NULL)
PARTITION BY LIST (col1)
SUBPARTITION BY HASH(col3) SUBPARTITIONS 3
(PARTITION p0 VALUES IN(100),
PARTITION p1 VALUES IN(200),
PARTITION p2 VALUES IN(300)
);
--------------------
CREATE TABLE t2_m_lh (
col1 INT NOT NULL,
col2 varchar(50),
col3 INT NOT NULL
)
PARTITION BY LIST (col1)
SUBPARTITION BY HASH (col3) SUBPARTITIONS 3 (
PARTITION p0 VALUES IN (100),
PARTITION p1 VALUES IN (200),
PARTITION p2 VALUES IN (300)
);
8 changes: 7 additions & 1 deletion core/src/test/resources/bvt/parser/trino/0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ CREATE TABLE kudu.dwd_market.dynamic_qrcode_hquid_relate_unionid (
del_flag integer WITH(nullable = true),
uname varchar WITH(nullable = true)
)
WITH (number_of_replicas = 3, partition_by_hash_buckets = 2, partition_by_hash_columns = ARRAY['unionid'], partition_by_range_columns = ARRAY['unionid', 'hq_uid'], range_partitions = '[{lower:null,upper:null}]')
WITH (
number_of_replicas = 3,
partition_by_hash_buckets = 2,
partition_by_hash_columns = ARRAY['unionid'],
partition_by_range_columns = ARRAY['unionid', 'hq_uid'],
range_partitions = '[{lower:null,upper:null}]'
)
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS orders (
orderkey bigint,
Expand Down

0 comments on commit 82adc39

Please sign in to comment.