Skip to content

Commit

Permalink
[Enhancement] Support week/day_of_week function for trino parser (#26758
Browse files Browse the repository at this point in the history
)

Fixes 
#14825 

Signed-off-by: Youngwb <[email protected]>
  • Loading branch information
Youngwb authored Jul 10, 2023
1 parent 39814a7 commit b5d90b9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,11 @@ protected ParseNode visitIntervalLiteral(IntervalLiteral node, ParseTreeContext
@Override
protected ParseNode visitTimestampLiteral(TimestampLiteral node, ParseTreeContext context) {
try {
return new DateLiteral(node.getValue(), Type.DATETIME);
String value = node.getValue();
if (value.length() <= 10) {
value += " 00:00:00";
}
return new DateLiteral(value, Type.DATETIME);
} catch (AnalysisException e) {
throw new ParsingException(PARSER_ERROR_MSG.invalidDateFormat(node.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ private static void registerDateFunctionTransformer() {
ImmutableList.of(Expr.class, Expr.class));

// day_of_week -> dayofweek
registerFunctionTransformer("day_of_week", 1, "dayofweek",
registerFunctionTransformer("day_of_week", 1, "dayofweek_iso",
ImmutableList.of(Expr.class));

// dow -> dayofweek
registerFunctionTransformer("dow", 1, "dayofweek",
registerFunctionTransformer("dow", 1, "dayofweek_iso",
ImmutableList.of(Expr.class));

// day_of_month -> dayofmonth
Expand All @@ -162,6 +162,14 @@ private static void registerDateFunctionTransformer() {
// doy -> dayofyear
registerFunctionTransformer("doy", 1, "dayofyear",
ImmutableList.of(Expr.class));

// week_of_year -> week_iso
registerFunctionTransformer("week_of_year", 1, "week_iso",
ImmutableList.of(Expr.class));

// week -> week_iso
registerFunctionTransformer("week", 1, "week_iso",
ImmutableList.of(Expr.class));
}

private static void registerStringFunctionTransformer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ public void testDateFnTransform() throws Exception {
assertPlanContains(sql, "2014-12-21 12:34:56");

sql = "select day_of_week(timestamp '2022-03-06 01:02:03');";
assertPlanContains(sql, "dayofweek('2022-03-06 01:02:03')");
assertPlanContains(sql, "dayofweek_iso('2022-03-06 01:02:03')");

sql = "select dow(timestamp '2022-03-06 01:02:03');";
assertPlanContains(sql, "dayofweek('2022-03-06 01:02:03')");
assertPlanContains(sql, "dayofweek_iso('2022-03-06 01:02:03')");

sql = "select dow(date '2022-03-06');";
assertPlanContains(sql, "dayofweek('2022-03-06 00:00:00')");
assertPlanContains(sql, "dayofweek_iso('2022-03-06 00:00:00')");

sql = "select day_of_month(timestamp '2022-03-06 01:02:03');";
assertPlanContains(sql, "dayofmonth('2022-03-06 01:02:03')");
Expand All @@ -132,6 +132,18 @@ public void testDateFnTransform() throws Exception {

sql = "select doy(date '2022-03-06');";
assertPlanContains(sql, "dayofyear('2022-03-06 00:00:00')");

sql = "select week_of_year(timestamp '2023-01-01 00:00:00');";
assertPlanContains(sql, "week_iso('2023-01-01 00:00:00')");

sql = "select week(timestamp '2023-01-01');";
assertPlanContains(sql, "week_iso('2023-01-01 00:00:00')");

sql = "select week_of_year(date '2023-01-01');";
assertPlanContains(sql, "week_iso('2023-01-01 00:00:00')");

sql = "select week(date '2023-01-01');";
assertPlanContains(sql, "week_iso('2023-01-01 00:00:00')");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public void testDateExpression() throws Exception {

sql = "select date '2021-01-01'";
assertPlanContains(sql, "'2021-01-01'");

sql = "select timestamp '2023-07-01'";
assertPlanContains(sql, "'2023-07-01 00:00:00'");
}

@Test
Expand Down Expand Up @@ -1003,7 +1006,7 @@ public void testUnaryExpression() throws Exception {
" cast('2023-01-01' AS date)\n" +
" )\n" +
" );";
assertPlanContains(sql, "-1 * CAST(if(3: dayofweek = 7, 0, 3: dayofweek) AS BIGINT)");
assertPlanContains(sql, "-1 * CAST(if(3: dayofweek_iso = 7, 0, 3: dayofweek_iso) AS BIGINT)");
}

@Test
Expand Down

0 comments on commit b5d90b9

Please sign in to comment.