Skip to content
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

[Fix](function) fix FE impl of some time functions #37746

Merged
merged 1 commit into from
Jul 18, 2024
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 @@ -51,7 +51,9 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
Expand Down Expand Up @@ -645,12 +647,22 @@ public static Expression timestamp(DateTimeV2Literal datetime) {
return datetime;
}

/**
* convert_tz
*/
@ExecFunction(name = "convert_tz", argTypes = {"DATETIMEV2", "VARCHAR", "VARCHAR"}, returnType = "DATETIMEV2")
public static Expression convertTz(DateTimeV2Literal datetime, StringLikeLiteral fromTz, StringLikeLiteral toTz) {
DateTimeFormatter zoneFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendZoneOrOffsetId()
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
ZoneId fromZone = ZoneId.from(zoneFormatter.parse(fromTz.getStringValue()));
ZoneId toZone = ZoneId.from(zoneFormatter.parse(toTz.getStringValue()));

LocalDateTime localDateTime = datetime.toJavaDateType();
ZonedDateTime fromDateTime = localDateTime.atZone(ZoneId.of(fromTz.getStringValue()));
ZonedDateTime toDateTime = fromDateTime.withZoneSameInstant(ZoneId.of(toTz.getStringValue()));
return DateTimeV2Literal.fromJavaDateType(toDateTime.toLocalDateTime(), datetime.getDataType().getScale());
ZonedDateTime resultDateTime = localDateTime.atZone(fromZone).withZoneSameInstant(toZone);
return DateTimeV2Literal.fromJavaDateType(resultDateTime.toLocalDateTime(), datetime.getDataType().getScale());
}

@ExecFunction(name = "weekday", argTypes = {"DATE"}, returnType = "TINYINT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static LocalDateTime getDateCeilOrFloor(DATE tag, LocalDateTime date, in
+ (dt.getHour() - start.getHour()) * 60 * 60
+ (dt.getMinute() - start.getMinute()) * 60
+ (dt.getSecond() - start.getSecond());
trivialPart = 0;
trivialPart = dt.getMicroSecond() - start.getMicroSecond();
break;
}
default: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class DateLiteral extends Literal {

// for cast datetime type to date type.
private static final LocalDateTime START_OF_A_DAY = LocalDateTime.of(0, 1, 1, 0, 0, 0);
private static final LocalDateTime END_OF_A_DAY = LocalDateTime.of(9999, 12, 31, 23, 59, 59);
private static final LocalDateTime END_OF_A_DAY = LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999999000);
private static final DateLiteral MIN_DATE = new DateLiteral(0, 1, 1);
private static final DateLiteral MAX_DATE = new DateLiteral(9999, 12, 31);
private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public DateTimeV2Literal roundFloor(int newScale) {
}

public static Expression fromJavaDateType(LocalDateTime dateTime) {
return fromJavaDateType(dateTime, 0);
return fromJavaDateType(dateTime, 6);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
import org.apache.doris.nereids.trees.expressions.functions.executable.DateTimeArithmetic;
import org.apache.doris.nereids.trees.expressions.functions.executable.DateTimeExtractAndTransform;
import org.apache.doris.nereids.trees.expressions.functions.executable.TimeRoundSeries;
import org.apache.doris.nereids.trees.expressions.functions.scalar.AppendTrailingCharIfAbsent;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ConvertTz;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DateFormat;
Expand Down Expand Up @@ -186,7 +187,11 @@ void testFoldString() {
ConvertTz c = new ConvertTz(DateTimeV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
StringLiteral.of("Asia/Shanghai"), StringLiteral.of("GMT"));
Expression rewritten = executor.rewrite(c, context);
Assertions.assertEquals(new DateTimeV2Literal("0000-12-31 16:55:18"), rewritten);
Assertions.assertTrue(new DateTimeV2Literal("0000-12-31 16:55:18.000000").compareTo((Literal) rewritten) == 0);
c = new ConvertTz(DateTimeV2Literal.fromJavaDateType(LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999999000)),
StringLiteral.of("Pacific/Galapagos"), StringLiteral.of("Pacific/Galapagos"));
rewritten = executor.rewrite(c, context);
Assertions.assertTrue(new DateTimeV2Literal("9999-12-31 23:59:59.999999").compareTo((Literal) rewritten) == 0);

DateFormat d = new DateFormat(DateTimeLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
StringLiteral.of("%y %m %d"));
Expand All @@ -212,7 +217,7 @@ void testFoldString() {
t = new DateTrunc(DateTimeV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
StringLiteral.of("week"));
rewritten = executor.rewrite(t, context);
Assertions.assertEquals(new DateTimeV2Literal("0001-01-01 00:00:00"), rewritten);
Assertions.assertTrue(((Literal) rewritten).compareTo(new DateTimeV2Literal("0001-01-01 00:00:00.000000")) == 0);
t = new DateTrunc(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
StringLiteral.of("week"));
rewritten = executor.rewrite(t, context);
Expand Down Expand Up @@ -600,6 +605,11 @@ void testDateTimeV2TypeDateTimeArithmeticFunctions() {
Assertions.assertEquals("'2023 18 2023 19'", DateTimeExtractAndTransform.dateFormat(
new DateTimeLiteral("2023-05-07 02:41:42"),
new VarcharLiteral("%x %v %X %V")).toSql());

Assertions.assertTrue(new DateTimeV2Literal("2021-01-01 12:12:14.000000").compareTo((Literal) TimeRoundSeries
.secondCeil(new DateTimeV2Literal("2021-01-01 12:12:12.123"), new IntegerLiteral(2))) == 0);
Assertions.assertTrue(new DateTimeV2Literal("2021-01-01 12:12:12.000000").compareTo((Literal) TimeRoundSeries
.secondFloor(new DateTimeV2Literal("2021-01-01 12:12:12.123"), new IntegerLiteral(2))) == 0);
}

@Test
Expand Down Expand Up @@ -632,9 +642,10 @@ void testDateTrunc() {
String[] tags = {"year", "month", "day", "hour", "minute", "second"};

String[] answer = {
"'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'", "'2001-12-01 00:00:00'", "'2001-12-01 00:00:00'",
"'2001-12-31 00:00:00'", "'2001-12-31 00:00:00'", "'2001-12-31 01:00:00'", "'2001-12-31 01:00:00'",
"'2001-12-31 01:01:00'", "'2001-12-31 01:01:00'", "'2001-12-31 01:01:01'", "'2001-12-31 01:01:01'",
"'2001-01-01 00:00:00'", "'2001-01-01 00:00:00.000000'", "'2001-12-01 00:00:00'",
"'2001-12-01 00:00:00.000000'", "'2001-12-31 00:00:00'", "'2001-12-31 00:00:00.000000'",
"'2001-12-31 01:00:00'", "'2001-12-31 01:00:00.000000'", "'2001-12-31 01:01:00'",
"'2001-12-31 01:01:00.000000'", "'2001-12-31 01:01:01'", "'2001-12-31 01:01:01.000000'",
"'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'",
"'2001-04-01 00:00:00'", "'2001-04-01 00:00:00'", "'2001-04-01 00:00:00'",
"'2001-07-01 00:00:00'", "'2001-07-01 00:00:00'", "'2001-07-01 00:00:00'",
Expand Down
34 changes: 29 additions & 5 deletions regression-test/data/correctness/test_timev2_fold.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select1 --
-- !select10 --
-07:00:00

-- !select2 --
-07:00:00
-- !select11 --
2020-02-02T04:00

-- !select12 --
2020-05-02T03:00

-- !select13 --
9999-12-31T23:59:59.999999

-- !select3 --
-- !select20 --
-07:00:00

-- !select4 --
-- !select21 --
2020-02-02T04:00

-- !select22 --
2020-05-02T03:00

-- !select23 --
9999-12-31T23:59:59.999999

-- !select20 --
-07:00:00

-- !select21 --
2020-02-02T04:00

-- !select22 --
2020-05-02T03:00

-- !select23 --
9999-12-31T23:59:59.999999

40 changes: 33 additions & 7 deletions regression-test/suites/correctness/test_timev2_fold.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,46 @@
// under the License.

suite("test_timev2_fold") {
// FE
sql """ set enable_fold_constant_by_be=false """
qt_select1 """
qt_select10 """
select timediff( convert_tz("2020-05-05 00:00:00", 'UTC', 'America/Los_Angeles'), "2020-05-05 00:00:00");
"""
sql """ set enable_nereids_planner=true,enable_fold_constant_by_be=false """
qt_select2 """
select timediff( convert_tz("2020-05-05 00:00:00", 'UTC', 'America/Los_Angeles'), "2020-05-05 00:00:00");
qt_select11 """
select convert_tz('2020-02-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select12 """
select convert_tz('2020-05-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select13 """
select CONVERT_TZ('9999-12-31 23:59:59.999999', 'Pacific/Galapagos', 'Pacific/GalapaGoS');
"""
// FE + BE
sql """ set enable_fold_constant_by_be=true """
qt_select3 """
qt_select20 """
select timediff( convert_tz("2020-05-05 00:00:00", 'UTC', 'America/Los_Angeles'), "2020-05-05 00:00:00");
"""
sql """ set enable_nereids_planner=true,enable_fold_constant_by_be=true """
qt_select4 """
qt_select21 """
select convert_tz('2020-02-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select22 """
select convert_tz('2020-05-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select23 """
select CONVERT_TZ('9999-12-31 23:59:59.999999', 'Pacific/Galapagos', 'Pacific/GalapaGoS');
"""
// BE
sql """ set debug_skip_fold_constant=true """
qt_select20 """
select timediff( convert_tz("2020-05-05 00:00:00", 'UTC', 'America/Los_Angeles'), "2020-05-05 00:00:00");
"""
qt_select21 """
select convert_tz('2020-02-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select22 """
select convert_tz('2020-05-01 12:00:00', 'America/Los_Angeles', '+08:00');
"""
qt_select23 """
select CONVERT_TZ('9999-12-31 23:59:59.999999', 'Pacific/Galapagos', 'Pacific/GalapaGoS');
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ suite("nereids_test_alias_function") {

test {
sql 'select cast(f1(\'2023-06-01\', 3) as string);'
result([['2023-05-29 00:00:00']])
result([['2023-05-29 00:00:00.000000']])
}
test {
sql 'select f2(f1(\'2023-05-20\', 2), 3)'
Expand Down
Loading