Skip to content

Commit

Permalink
Fix sr ColumnDefinition null/not null constraints order.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingo-xp committed Dec 6, 2024
1 parent 1ce01d7 commit c634068
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.DistributedByType;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint;
import com.alibaba.druid.sql.ast.statement.SQLNullConstraint;
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKeyImpl;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksAggregateKey;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksDuplicateKey;
Expand All @@ -16,6 +21,7 @@
import com.alibaba.druid.sql.dialect.starrocks.ast.statement.StarRocksCreateTableStatement;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.JdbcUtils;

import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -218,7 +224,42 @@ protected void print(List<? extends SQLExpr> exprList) {
}

public boolean visit(SQLColumnDefinition x) {
super.visit((SQLColumnDefinition) x);
x.getName().accept(this);
final SQLDataType dataType = x.getDataType();

if (dataType != null) {
if (JdbcUtils.isPgsqlDbType(dbType) && x.getParent() instanceof SQLAlterTableAlterColumn) {
print0(ucase ? " TYPE " : " type ");
} else {
print(' ');
}
dataType.accept(this);
}

if (x.getAggType() != null) {
visitAggType(x);
}

for (SQLColumnConstraint item : x.getConstraints()) {
if (item instanceof SQLNullConstraint || item instanceof SQLNotNullConstraint) {
print(' ');
item.accept(this);
}
}

if (x.getDefaultExpr() != null) {
visitColumnDefault(x);
}

if (x.isAutoIncrement()) {
printAutoIncrement();
}

if (x.getComment() != null) {
print0(ucase ? " COMMENT " : " comment ");
x.getComment().accept(this);
}

if (x.getAsExpr() != null) {
print(' ');
print0(ucase ? "AS " : "as ");
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/resources/bvt/parser/starrocks/1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ CREATE TABLE `ads_ad_tt_ad_product_shopping_ads_type_global_report_d` (
`product_name` varchar(1024) NULL COMMENT '商品名称',
`account_name` varchar(255) NULL COMMENT '媒体账号名称',
`adv_name` varchar(1024) NULL COMMENT '广告主名称',
`spend_amt_1d` DECIMAL(38, 2) DEFAULT "0.0" NULL COMMENT '最近1天消耗金额',
`imps_cnt_1d` bigint(20) DEFAULT "0" NULL COMMENT '最近1天曝光量',
`clk_cnt_1d` bigint(20) DEFAULT "0" NULL COMMENT '最近1天点击量',
`cvr_cnt_1d` bigint(20) DEFAULT "0" NULL COMMENT '最近1天转化量',
`cart_cnt_1d` bigint(20) DEFAULT "0" NULL COMMENT '最近1天加购量',
`pur_cnt_1d` bigint(20) DEFAULT "0" NULL COMMENT '最近1天购买量',
`revenue_amt_1d` DECIMAL(38, 2) DEFAULT "0.0" NULL COMMENT '最近1天收入金额',
`spend_amt_1d` DECIMAL(38, 2) NULL DEFAULT "0.0" COMMENT '最近1天消耗金额',
`imps_cnt_1d` bigint(20) NULL DEFAULT "0" COMMENT '最近1天曝光量',
`clk_cnt_1d` bigint(20) NULL DEFAULT "0" COMMENT '最近1天点击量',
`cvr_cnt_1d` bigint(20) NULL DEFAULT "0" COMMENT '最近1天转化量',
`cart_cnt_1d` bigint(20) NULL DEFAULT "0" COMMENT '最近1天加购量',
`pur_cnt_1d` bigint(20) NULL DEFAULT "0" COMMENT '最近1天购买量',
`revenue_amt_1d` DECIMAL(38, 2) NULL DEFAULT "0.0" COMMENT '最近1天收入金额',
`collect_timestamp` datetime NULL COMMENT '采集时间戳',
INDEX product_name_idx(`product_name`) USING BITMAP COMMENT 'Bitmap index on column product_name',
INDEX adv_name_idx(`adv_name`) USING BITMAP COMMENT 'Bitmap index on column adv_name',
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/bvt/parser/starrocks/2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ CREATE TABLE example_db.table_hash (
k1 TINYINT,
k2 DECIMAL(10, 2) DEFAULT "10.5",
k3 CHAR(10) COMMENT 'string column',
k4 INT DEFAULT "1" NOT NULL COMMENT 'int column'
k4 INT NOT NULL DEFAULT "1" COMMENT 'int column'
)
DISTRIBUTED BY HASH (k1) BUCKETS 32

0 comments on commit c634068

Please sign in to comment.