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 @@ -456,6 +456,10 @@
import org.apache.doris.nereids.types.AggStateType;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.DateTimeType;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DateType;
import org.apache.doris.nereids.types.DateV2Type;
import org.apache.doris.nereids.types.MapType;
import org.apache.doris.nereids.types.StructField;
import org.apache.doris.nereids.types.StructType;
Expand Down Expand Up @@ -2313,19 +2317,37 @@ public String visitUnitIdentifier(UnitIdentifierContext ctx) {
}

@Override
public Literal visitTypeConstructor(TypeConstructorContext ctx) {
public Expression visitTypeConstructor(TypeConstructorContext ctx) {
String value = ctx.STRING_LITERAL().getText();
value = value.substring(1, value.length() - 1);
String type = ctx.type.getText().toUpperCase();
switch (type) {
case "DATE":
return Config.enable_date_conversion ? new DateV2Literal(value) : new DateLiteral(value);
try {
return Config.enable_date_conversion ? new DateV2Literal(value) : new DateLiteral(value);
} catch (Exception e) {
return new Cast(new StringLiteral(value),
Config.enable_date_conversion ? DateV2Type.INSTANCE : DateType.INSTANCE);
}
case "TIMESTAMP":
return Config.enable_date_conversion ? new DateTimeV2Literal(value) : new DateTimeLiteral(value);
try {
return Config.enable_date_conversion ? new DateTimeV2Literal(value) : new DateTimeLiteral(value);
} catch (Exception e) {
return new Cast(new StringLiteral(value),
Config.enable_date_conversion ? DateTimeV2Type.MAX : DateTimeType.INSTANCE);
}
case "DATEV2":
return new DateV2Literal(value);
try {
return new DateV2Literal(value);
} catch (Exception e) {
return new Cast(new StringLiteral(value), DateV2Type.INSTANCE);
}
case "DATEV1":
return new DateLiteral(value);
try {
return new DateLiteral(value);
} catch (Exception e) {
return new Cast(new StringLiteral(value), DateType.INSTANCE);
}
default:
throw new ParseException("Unsupported data type : " + type, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ suite("test_cast_datetime") {
qt_3 "select a, '' = mydate, '' = mydatev2, '' = mydatetime, '' = mydatetimev2 from casttbl"

def wrong_date_strs = [
"date '2020-01'",
"datev1 '2020-01'",
"datev2 '2020-01'",
"timestamp '2020-01'",
"'' > date '2019-06-01'",
"'' > date_sub('2019-06-01', -10)",
"'' > cast('2019-06-01 00:00:00' as datetime)",
Expand Down Expand Up @@ -572,5 +576,15 @@ suite("test_cast_datetime") {
sql "select date_add('2023-11-05 01:30:00 America/New_York', INTERVAL 1 DAY)"
result([[LocalDateTime.parse('2023-11-06T01:30:00')]])
}

test {
sql "select date '2025年1月20日'"
result([[Date.valueOf('2025-01-20')]])
}

test {
sql "select timestamp '2025年1月20日10时20分5秒'"
result([[LocalDateTime.parse('2025-01-20T10:20:05')]])
}
}
}