Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -85,6 +85,7 @@
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.trees.expressions.literal.format.DateTimeChecker;
import org.apache.doris.nereids.types.BooleanType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.coercion.DateLikeType;
Expand All @@ -100,7 +101,6 @@
import com.google.common.collect.Lists;
import org.apache.commons.codec.digest.DigestUtils;

import java.time.DateTimeException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -468,17 +468,14 @@ public Expression visitCast(Cast cast, ExpressionRewriteContext context) {
if (child.isNullLiteral()) {
return new NullLiteral(dataType);
} else if (child instanceof StringLikeLiteral && dataType instanceof DateLikeType) {
String dateStr = ((StringLikeLiteral) child).getStringValue();
if (!DateTimeChecker.isValidDateTime(dateStr)) {
return cast;
}
try {
return ((DateLikeType) dataType).fromString(((StringLikeLiteral) child).getStringValue());
} catch (AnalysisException t) {
if (cast.isExplicitType()) {
return cast;
} else {
// If cast is from type coercion, we don't use NULL literal and will throw exception.
throw t;
}
} catch (DateTimeException e) {
return new NullLiteral(dataType);
return ((DateLikeType) dataType).fromString(dateStr);
} catch (Exception t) {
return cast;
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,16 @@ public static Result<DateLiteral, AnalysisException> parseDateLiteral(String s)

/** parseDateTime */
public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s) {
// fast parse '2022-01-01'
if (s.length() == 10 && s.charAt(4) == '-' && s.charAt(7) == '-') {
TemporalAccessor date = fastParseDate(s);
if (date != null) {
return Result.ok(date);
}
}

String originalString = s;
try {
// fast parse '2022-01-01'
if (s.length() == 10 && s.charAt(4) == '-' && s.charAt(7) == '-') {
TemporalAccessor date = fastParseDate(s);
if (date != null) {
return Result.ok(date);
}
}

TemporalAccessor dateTime;

// remove suffix/prefix ' '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.doris.catalog.Type;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.format.DateTimeChecker;
import org.apache.doris.nereids.types.coercion.DateLikeType;
import org.apache.doris.nereids.types.coercion.IntegralType;

Expand Down Expand Up @@ -87,7 +88,14 @@ public static DateTimeV2Type forType(DataType dataType) {
* maybe we need to check for validity?
*/
public static DateTimeV2Type forTypeFromString(String s) {
int scale = DateTimeLiteral.determineScale(s);
int scale = MAX_SCALE;
if (DateTimeChecker.isValidDateTime(s)) {
try {
scale = DateTimeLiteral.determineScale(s);
} catch (Exception e) {
// let be to process it
}
}
if (scale > MAX_SCALE) {
scale = MAX_SCALE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,7 @@ public static Optional<Expression> characterLiteralTypeCoercion(String value, Da
} else if (dataType.isDateTimeType() && DateTimeChecker.isValidDateTime(value)) {
ret = DateTimeLiteral.parseDateTimeLiteral(value, false).orElse(null);
} else if (dataType.isDateV2Type() && DateTimeChecker.isValidDateTime(value)) {
Result<DateLiteral, AnalysisException> parseResult
= DateV2Literal.parseDateLiteral(value);
Result<DateLiteral, AnalysisException> parseResult = DateV2Literal.parseDateLiteral(value);
if (parseResult.isOk()) {
ret = parseResult.get();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void testDate() {
new DateLiteral("20220101");

Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("-01-01"));
Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("01-01"));
}

@Test
Expand Down
10 changes: 10 additions & 0 deletions regression-test/data/nereids_syntax_p0/test_cast_datetime.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@
-- !1 --
1

-- !2 --
1 2000-01-01 2000-01-01 2000-01-01T00:00 2000-01-01T12:12:12
2 \N \N \N \N
3 \N \N \N \N

-- !3 --
1 \N \N \N \N
2 \N \N \N \N
3 \N \N \N \N

Loading
Loading