Skip to content

Commit

Permalink
[Feature] create new functions such as current_timestamp, current_date (
Browse files Browse the repository at this point in the history
#14319)

* add current_timestamp,current_time,current_date,localtime,localtimestamp funcation
like spark and presto

* remove key word CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP LOCALTIME LOCALTIMESTAMP

* add select time fun ut

* add one assertion to localtimestamp

* unify CURRENT_TIMESTAMP and specialDateTimeExpression together

(cherry picked from commit b4f413e)
  • Loading branch information
shshenhua authored and mergify[bot] committed Dec 22, 2022
1 parent 45e0620 commit ee62bd5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
14 changes: 12 additions & 2 deletions fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4508,12 +4508,22 @@ public ParseNode visitInformationFunctionExpression(StarRocksParser.InformationF
throw new ParsingException("Unknown special function " + context.name.getText());
}

@Override
public ParseNode visitSpecialDateTimeExpression(StarRocksParser.SpecialDateTimeExpressionContext context) {
if (context.name.getText().equalsIgnoreCase("current_timestamp")
|| context.name.getText().equalsIgnoreCase("current_time")
|| context.name.getText().equalsIgnoreCase("current_date")
|| context.name.getText().equalsIgnoreCase("localtime")
|| context.name.getText().equalsIgnoreCase("localtimestamp")) {
return new FunctionCallExpr(context.name.getText().toUpperCase(), Lists.newArrayList());
}
throw new ParsingException("Unknown special function " + context.name.getText());
}

@Override
public ParseNode visitSpecialFunctionExpression(StarRocksParser.SpecialFunctionExpressionContext context) {
if (context.CHAR() != null) {
return new FunctionCallExpr("char", visit(context.expression(), Expr.class));
} else if (context.CURRENT_TIMESTAMP() != null) {
return new FunctionCallExpr("current_timestamp", Lists.newArrayList());
} else if (context.DAY() != null) {
return new FunctionCallExpr("day", visit(context.expression(), Expr.class));
} else if (context.HOUR() != null) {
Expand Down
10 changes: 9 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/sql/parser/StarRocks.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,7 @@ functionCall
| GROUPING '(' (expression (',' expression)*)? ')' #groupingOperation
| GROUPING_ID '(' (expression (',' expression)*)? ')' #groupingOperation
| informationFunctionExpression #informationFunction
| specialDateTimeExpression #specialDateTime
| specialFunctionExpression #specialFunction
| aggregationFunction over? #aggregationFunctionCall
| windowFunction over #windowFunctionCall
Expand Down Expand Up @@ -1748,9 +1749,16 @@ informationFunctionExpression
| name = CURRENT_USER ('(' ')')?
;

specialDateTimeExpression
: name = CURRENT_DATE ('(' ')')?
| name = CURRENT_TIME ('(' ')')?
| name = CURRENT_TIMESTAMP ('(' ')')?
| name = LOCALTIME ('(' ')')?
| name = LOCALTIMESTAMP ('(' ')')?
;

specialFunctionExpression
: CHAR '(' expression ')'
| CURRENT_TIMESTAMP '(' ')'
| DAY '(' expression ')'
| HOUR '(' expression ')'
| IF '(' (expression (',' expression)*)? ')'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ CREATE: 'CREATE';
CROSS: 'CROSS';
CUBE: 'CUBE';
CURRENT: 'CURRENT';
CURRENT_DATE: 'CURRENT_DATE';
CURRENT_TIME: 'CURRENT_TIME';
CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP';
CURRENT_USER: 'CURRENT_USER';
DATA: 'DATA';
Expand Down Expand Up @@ -185,6 +187,8 @@ LIMIT: 'LIMIT';
LIST: 'LIST';
LOAD: 'LOAD';
LOCAL: 'LOCAL';
LOCALTIME: 'LOCALTIME';
LOCALTIMESTAMP: 'LOCALTIMESTAMP';
LOCATION: 'LOCATION';
LOGICAL: 'LOGICAL';
MANUAL: 'MANUAL';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,28 @@ public void testCurrentUserFunSupport() throws Exception {
sql = "select current_user";
starRocksAssert.query(sql).explainQuery();
}

@Test
public void testTimeFunSupport() throws Exception {
String sql = "select current_timestamp()";
starRocksAssert.query(sql).explainQuery();
sql = "select current_timestamp";
starRocksAssert.query(sql).explainQuery();
sql = "select current_time()";
starRocksAssert.query(sql).explainQuery();
sql = "select current_time";
starRocksAssert.query(sql).explainQuery();
sql = "select current_date()";
starRocksAssert.query(sql).explainQuery();
sql = "select current_date";
starRocksAssert.query(sql).explainQuery();
sql = "select localtime()";
starRocksAssert.query(sql).explainQuery();
sql = "select localtime";
starRocksAssert.query(sql).explainQuery();
sql = "select localtimestamp()";
starRocksAssert.query(sql).explainQuery();
sql = "select localtimestamp";
starRocksAssert.query(sql).explainQuery();
}
}

0 comments on commit ee62bd5

Please sign in to comment.