Skip to content

[Enhancement] Convert sql function parse_datetime(...) to str_to_jodatime(...) with accepting 'T' and 'Z' in format for Trino transformer when sql_dialect='trino' #56565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public static Expr transform(String functionName, Expr... args) {
throw new SemanticException("isnotnull function must have 1 argument");
}
return new IsNullPredicate(args[0], true);
} else if (functionName.equalsIgnoreCase("parse_datetime") && args.length == 2
&& args[1] instanceof StringLiteral) {
// parse_datetime -> str_to_jodatime
String formatString = ((StringLiteral) args[1]).getStringValue();
// "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" -> "yyyy-MM-ddTHH:mm:ss.SSS"
formatString = formatString.replace("'T'", "T").replace("'Z'", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should handle any character within the quotes, rather than just handling the character 'T'.
eg. select parse_datetime('2025-01-01ABC00:00:00.000Z', 'yyyy-MM-dd''ABC''HH:mm:ss.SSS''Z''');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove all ' characters for the format parameter, plus removing all Z characters because str_to_jodatime doesn't support it (when it's OK in Trino).
current example with Z in the format parameter:

mysql> set sql_dialect=trino;
mysql> select parse_datetime('2023-08-02T14:37:02Z', 'yyyy-MM-dd''T''HH:mm:ssZ');
ERROR 1064 (HY000): Invalid format 'yyyy-MM-ddTHH:mm:ssZ' for '2023-08-02T14:37:02Z'
mysql> select parse_datetime('2023-08-02T14:37:02Z', 'yyyy-MM-dd''T''HH:mm:ss''Z''');
+----------------------------------------------------------------+
| str_to_jodatime('2023-08-02T14:37:02Z', 'yyyy-MM-ddTHH:mm:ss') |
+----------------------------------------------------------------+
| 2023-08-02 14:37:02                                            |
+----------------------------------------------------------------+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code updated and add one more test for function parse_datetime()

return new FunctionCallExpr("str_to_jodatime", java.util.List.of(args[0], new StringLiteral(formatString)));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,6 @@ private static void registerDateFunctionTransformer() {
registerFunctionTransformer("to_char", 2, "jodatime_format",
List.of(Expr.class, Expr.class));

// parse_datetime -> str_to_jodatime
registerFunctionTransformer("parse_datetime", 2, "str_to_jodatime",
List.of(Expr.class, Expr.class));

// to_date -> to_tera_date
registerFunctionTransformer("to_date", 2, "to_tera_date",
List.of(Expr.class, Expr.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void testDateFnTransform() throws Exception {

sql = "select parse_datetime('2023-05','yyyy-MM')";
assertPlanContains(sql, "str_to_jodatime('2023-05', 'yyyy-MM')");

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

Expand Down
Loading