sql parser improved pg support, fix #4128

This commit is contained in:
高铁
2021-01-12 22:32:04 +08:00
parent 0d5f54baaa
commit 3197ae8db9
11 changed files with 78 additions and 0 deletions
@@ -53,6 +53,8 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
protected int buckets;
protected int shards;
private boolean ifExists = false;
public SQLAlterTableStatement(){
}
@@ -85,6 +87,14 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
this.offline = offline;
}
public boolean isIfExists() {
return ifExists;
}
public void setIfExists(boolean ifExists) {
this.ifExists = ifExists;
}
public boolean isRemovePatiting() {
return removePatiting;
}
@@ -557,6 +557,10 @@ public class PGSQLStatementParser extends SQLStatementParser {
values.add(new SQLCharExpr(lexer.stringVal()));
} else if (lexer.token() == Token.LITERAL_INT) {
values.add(new SQLIdentifierExpr(lexer.numberString()));
} else if (lexer.identifierEquals(FnvHash.Constants.JSON_SET)
|| lexer.identifierEquals(FnvHash.Constants.JSONB_SET)) {
SQLExpr json_set = this.exprParser.expr();
values.add(json_set);
} else {
values.add(new SQLIdentifierExpr(lexer.stringVal()));
}
@@ -1763,6 +1763,13 @@ public class SQLStatementParser extends SQLParser {
lexer.nextToken();
SQLAlterTableStatement stmt = new SQLAlterTableStatement(getDbType());
if (lexer.token == Token.IF) {
lexer.nextToken();
accept(Token.EXISTS);
stmt.setIfExists(true);
}
stmt.setName(this.exprParser.name());
for (;;) {
@@ -5405,6 +5405,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
@Override
public boolean visit(SQLAlterTableStatement x) {
print0(ucase ? "ALTER TABLE " : "alter table ");
if (x.isIfExists()) {
print0(ucase ? "IF EXISTS " : "if exists ");
}
printTableSourceExpr(x.getName());
this.indentCount++;
for (int i = 0; i < x.getItems().size(); ++i) {
@@ -1117,5 +1117,7 @@ public final class FnvHash {
long RESTART = fnv1a_64_lower("RESTART");
long ASOF = fnv1a_64_lower("ASOF");
long JSON_SET = fnv1a_64_lower("JSON_SET");
long JSONB_SET = fnv1a_64_lower("JSONB_SET");
}
}
@@ -52,6 +52,18 @@ public class PostgresqlResourceTest extends PGTest {
exec_test("bvt/parser/postgresql-3.txt");
}
public void test_4() throws Exception {
exec_test("bvt/parser/postgresql-4.txt");
}
public void test_5() throws Exception {
exec_test("bvt/parser/postgresql-5.txt");
}
public void test_6() throws Exception {
exec_test("bvt/parser/postgresql-6.txt");
}
public void exec_test(String resource) throws Exception {
System.out.println(resource);
InputStream is = null;
+4
View File
@@ -0,0 +1,4 @@
ALTER TABLE tb_a ADD COLUMNS ( a bigint );
---------------------------
ALTER TABLE tb_a
ADD COLUMNS (a bigint);
+4
View File
@@ -0,0 +1,4 @@
INSERT INTO tb_a VALUES ( 1 );
---------------------------
INSERT INTO TABLE tb_a
VALUES (1);
@@ -0,0 +1,20 @@
SET data = jsonb_set(
data,
ARRAY[(
SELECT idx - 1 AS idx
FROM cgm_data c1,
jsonb_array_elements(data)
WHERE
info ->> 'index' = ?
and c1.id = ?
)]::TEXT[],
?::jsonb
)
WHERE t1.id = ?;
---------------------------
SET data TO jsonb_set(data, ARRAY[(
SELECT idx - 1 AS idx
FROM cgm_data c1, jsonb_array_elements(data)
WHERE info ->> 'index' = ?
AND c1.id = ?
)]::TEXT[], ?::jsonb), t1, id, id
@@ -0,0 +1,5 @@
select * from com_demo order by sex asc nulls first
---------------------------
SELECT *
FROM com_demo
ORDER BY sex ASC NULLS FIRST
@@ -0,0 +1,5 @@
alter table if exists tcweatherdata
add column predictiontime timestamp
---------------------------
ALTER TABLE IF EXISTS tcweatherdata
ADD COLUMN predictiontime timestamp