dybyte commented on code in PR #10360:
URL: https://github.com/apache/seatunnel/pull/10360#discussion_r2707015450
##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/DateTimeFunction.java:
##########
@@ -633,23 +635,39 @@ public static Temporal parsedatetime(List<Object> args) {
return null;
}
String format = (String) args.get(1);
- if (format.contains("yy") && format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDateTime.parse(str, df);
- }
- if (format.contains("yy")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDate.parse(str, df);
- }
- if (format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalTime.parse(str, df);
- }
- throw new TransformException(
- CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
- String.format(
- "Unknown pattern letter %s for function: %s",
- format, ZetaSQLFunction.PARSEDATETIME));
+
+ ZetaDateTimeFormat dateTimeFormat =
+ ZetaDateTimeFormat.fromPattern(format)
+ .orElseThrow(
+ () ->
+ new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ String.format(
+ "Unsupported datetime
format: '%s'.",
+ format)));
+
+ try {
+ DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(dateTimeFormat.getPattern());
+
+ switch (dateTimeFormat.getType()) {
+ case DATETIME:
+ return LocalDateTime.parse(str, formatter);
+ case DATE:
+ return LocalDate.parse(str, formatter);
+ case TIME:
+ return LocalTime.parse(str, formatter);
+ default:
+ throw new TransformException(
+ CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ "Unknown format type: " +
dateTimeFormat.getType());
+ }
+ } catch (DateTimeParseException e) {
+ throw new TransformException(
+ CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT,
+ String.format(
+ "Failed to parse '%s' with format '%s': %s",
+ str, format, e.getMessage()));
+ }
Review Comment:
We could use
https://github.com/apache/seatunnel/blob/d14cd21276354fae956e036bb9864cedc8ca34fc/seatunnel-common/src/main/java/org/apache/seatunnel/common/exception/CommonError.java#L300-L305
here
##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/DateTimeFunction.java:
##########
@@ -633,23 +635,39 @@ public static Temporal parsedatetime(List<Object> args) {
return null;
}
String format = (String) args.get(1);
- if (format.contains("yy") && format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDateTime.parse(str, df);
- }
- if (format.contains("yy")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDate.parse(str, df);
- }
- if (format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalTime.parse(str, df);
- }
- throw new TransformException(
- CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
- String.format(
- "Unknown pattern letter %s for function: %s",
- format, ZetaSQLFunction.PARSEDATETIME));
+
+ ZetaDateTimeFormat dateTimeFormat =
+ ZetaDateTimeFormat.fromPattern(format)
+ .orElseThrow(
+ () ->
+ new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ String.format(
+ "Unsupported datetime
format: '%s'.",
+ format)));
Review Comment:
ditto
##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/functions/DateTimeFunction.java:
##########
@@ -633,23 +635,39 @@ public static Temporal parsedatetime(List<Object> args) {
return null;
}
String format = (String) args.get(1);
- if (format.contains("yy") && format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDateTime.parse(str, df);
- }
- if (format.contains("yy")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalDate.parse(str, df);
- }
- if (format.contains("mm")) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
- return LocalTime.parse(str, df);
- }
- throw new TransformException(
- CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
- String.format(
- "Unknown pattern letter %s for function: %s",
- format, ZetaSQLFunction.PARSEDATETIME));
+
+ ZetaDateTimeFormat dateTimeFormat =
+ ZetaDateTimeFormat.fromPattern(format)
+ .orElseThrow(
+ () ->
+ new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ String.format(
+ "Unsupported datetime
format: '%s'.",
+ format)));
+
+ try {
+ DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(dateTimeFormat.getPattern());
+
+ switch (dateTimeFormat.getType()) {
+ case DATETIME:
+ return LocalDateTime.parse(str, formatter);
+ case DATE:
+ return LocalDate.parse(str, formatter);
+ case TIME:
+ return LocalTime.parse(str, formatter);
+ default:
+ throw new TransformException(
+ CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ "Unknown format type: " +
dateTimeFormat.getType());
Review Comment:
ditto
##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLType.java:
##########
@@ -430,21 +430,41 @@ private SeaTunnelDataType<?> getFunctionType(Function
function) {
case ZetaSQLFunction.PARSEDATETIME:
case ZetaSQLFunction.TO_DATE:
{
- String format =
function.getParameters().getExpressions().get(1).toString();
- if (format.contains("yy") && format.contains("mm")) {
- return LocalTimeType.LOCAL_DATE_TIME_TYPE;
+ Expression formatExpr =
function.getParameters().getExpressions().get(1);
+ String format;
+ if (formatExpr instanceof StringValue) {
+ format = ((StringValue)
formatExpr).getNotExcapedValue();
+ } else {
+ throw new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ String.format(
+ "Format parameter must be a string
literal for function: %s",
+ function.getName()));
}
- if (format.contains("yy")) {
- return LocalTimeType.LOCAL_DATE_TYPE;
- }
- if (format.contains("mm")) {
- return LocalTimeType.LOCAL_TIME_TYPE;
+
+ ZetaDateTimeFormat dateTimeFormat =
+ ZetaDateTimeFormat.fromPattern(format)
+ .orElseThrow(
+ () ->
+ new TransformException(
+
CommonErrorCodeDeprecated
+
.UNSUPPORTED_OPERATION,
+ String.format(
+
"Unsupported datetime format: '%s'.",
+ format)));
Review Comment:
We could use
https://github.com/apache/seatunnel/blob/d14cd21276354fae956e036bb9864cedc8ca34fc/seatunnel-common/src/main/java/org/apache/seatunnel/common/exception/CommonError.java#L213-L219
here
##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLType.java:
##########
@@ -430,21 +430,41 @@ private SeaTunnelDataType<?> getFunctionType(Function
function) {
case ZetaSQLFunction.PARSEDATETIME:
case ZetaSQLFunction.TO_DATE:
{
- String format =
function.getParameters().getExpressions().get(1).toString();
- if (format.contains("yy") && format.contains("mm")) {
- return LocalTimeType.LOCAL_DATE_TIME_TYPE;
+ Expression formatExpr =
function.getParameters().getExpressions().get(1);
+ String format;
+ if (formatExpr instanceof StringValue) {
+ format = ((StringValue)
formatExpr).getNotExcapedValue();
+ } else {
+ throw new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ String.format(
+ "Format parameter must be a string
literal for function: %s",
+ function.getName()));
}
- if (format.contains("yy")) {
- return LocalTimeType.LOCAL_DATE_TYPE;
- }
- if (format.contains("mm")) {
- return LocalTimeType.LOCAL_TIME_TYPE;
+
+ ZetaDateTimeFormat dateTimeFormat =
+ ZetaDateTimeFormat.fromPattern(format)
+ .orElseThrow(
+ () ->
+ new TransformException(
+
CommonErrorCodeDeprecated
+
.UNSUPPORTED_OPERATION,
+ String.format(
+
"Unsupported datetime format: '%s'.",
+ format)));
+
+ switch (dateTimeFormat.getType()) {
+ case DATETIME:
+ return LocalTimeType.LOCAL_DATE_TIME_TYPE;
+ case DATE:
+ return LocalTimeType.LOCAL_DATE_TYPE;
+ case TIME:
+ return LocalTimeType.LOCAL_TIME_TYPE;
+ default:
+ throw new TransformException(
+
CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
+ "Unknown format type: " +
dateTimeFormat.getType());
Review Comment:
ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]