Skip to content

Commit

Permalink
GaussDB support parseInterval.
Browse files Browse the repository at this point in the history
  • Loading branch information
woyumen4597 committed Nov 6, 2024
1 parent f060c27 commit b2a4046
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.dialect.gaussdb.ast.GaussDbPartitionValue;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser;
Expand Down Expand Up @@ -170,4 +169,29 @@ protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr
}
return aggregateExpr;
}

@Override
protected SQLExpr parseInterval() {
accept(Token.INTERVAL);
SQLIntervalExpr intervalExpr = new SQLIntervalExpr();
if (lexer.token() != Token.LITERAL_CHARS
&& lexer.token() != Token.LITERAL_INT
&& lexer.token() != Token.VARIANT
) {
return new SQLIdentifierExpr("INTERVAL");
}
SQLExpr value = expr();

if (value instanceof SQLCharExpr) {
String literal = ((SQLCharExpr) value).getText();
String[] split = literal.split(" ");
if (split.length == 2) {
return new SQLIntervalExpr(new SQLIntegerExpr(Integer.parseInt(split[0])), SQLIntervalUnit.of(split[1]));
} else {
intervalExpr.setValue(value);
return intervalExpr;
}
}
return intervalExpr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
Expand Down Expand Up @@ -338,4 +338,32 @@ protected void printCompression(SQLColumnDefinition x) {
x.getCompression().accept(this);
}
}

@Override
public boolean visit(SQLIntervalExpr x) {
print0(ucase ? "INTERVAL " : "interval ");
SQLExpr value = x.getValue();

boolean str = value instanceof SQLCharExpr;
if (!str) {
print('\'');
}
value.accept(this);

SQLIntervalUnit unit = x.getUnit();
if (unit != null) {
print(' ');
print0(ucase ? unit.name : unit.nameLCase);
if (value instanceof SQLIntegerExpr) {
SQLIntegerExpr integerExpr = (SQLIntegerExpr) value;
if (integerExpr.getNumber().intValue() > 1) {
print(ucase ? 'S' : 's');
}
}
}
if (!str) {
print('\'');
}
return false;
}
}
11 changes: 11 additions & 0 deletions core/src/test/resources/bvt/parser/gaussdb/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT interval '1 month'
--------------------
SELECT INTERVAL '1 MONTH'
------------------------------------------------------------------------------------------------------------------------
SELECT interval '3 days'
--------------------
SELECT INTERVAL '3 DAYS'
------------------------------------------------------------------------------------------------------------------------
SELECT INTERVAL '1 year 2 months 3 days 5 hours'
--------------------
SELECT INTERVAL '1 year 2 months 3 days 5 hours'

0 comments on commit b2a4046

Please sign in to comment.