Skip to content

Commit

Permalink
[feature](Nereids): date literal suppose Zone (#33534)
Browse files Browse the repository at this point in the history
support
```
'2022-05-01 01:02:55+02:30
'2022-05-01 01:02:55Asia/Shanghai
```
  • Loading branch information
jackwener authored Apr 15, 2024
1 parent 9fbe6f5 commit 96eb7ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,6 @@ static String normalize(String s) {

sb.append(s.substring(i));

// Zone Part
// while(i < s.length()) {
//
// }

// add missing :00 in Zone part
// int len = sb.length();
// int signIdx = sb.indexOf("+", 10); // from index:10, skip date part (it contains '-')
// signIdx = signIdx == -1 ? sb.indexOf("-", 10) : signIdx;
// if (signIdx != -1 && len - signIdx == 3) {
// sb.append(":00");
// }

return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
public class DateTimeFormatterUtils {
public static final DateTimeFormatter ZONE_FORMATTER = new DateTimeFormatterBuilder()
.optionalStart()
// .appendZoneText(TextStyle.FULL)
.appendZoneOrOffsetId()
.optionalEnd()
.toFormatter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,32 +427,6 @@ public Expression visit(Expression expr, Map<? extends Expression, ? extends Exp
}
}

private static class ExpressionReplacerContext {
private final Map<? extends Expression, ? extends Expression> replaceMap;
// if the key of replaceMap is named expr and withAlias is true, we should
// add alias after replaced
private final boolean withAliasIfKeyNamed;

private ExpressionReplacerContext(Map<? extends Expression, ? extends Expression> replaceMap,
boolean withAliasIfKeyNamed) {
this.replaceMap = replaceMap;
this.withAliasIfKeyNamed = withAliasIfKeyNamed;
}

public static ExpressionReplacerContext of(Map<? extends Expression, ? extends Expression> replaceMap,
boolean withAliasIfKeyNamed) {
return new ExpressionReplacerContext(replaceMap, withAliasIfKeyNamed);
}

public Map<? extends Expression, ? extends Expression> getReplaceMap() {
return replaceMap;
}

public boolean isWithAliasIfKeyNamed() {
return withAliasIfKeyNamed;
}
}

/**
* merge arguments into an expression array
*
Expand Down Expand Up @@ -833,13 +807,6 @@ public static Set<Slot> getInputSlotSet(Collection<? extends Expression> exprs)
return set;
}

public static boolean checkTypeSkipCast(Expression expression, Class<? extends Expression> cls) {
while (expression instanceof Cast) {
expression = ((Cast) expression).child();
}
return cls.isInstance(expression);
}

public static Expression getExpressionCoveredByCast(Expression expression) {
while (expression instanceof Cast) {
expression = ((Cast) expression).child();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,7 @@ void testZone() {
new DateTimeV2Literal("2022-08-01 01:01:01Z");
new DateTimeV2Literal("2022-08-01 01:01:01Europe/Berlin");
new DateTimeV2Literal("2022-08-01 01:01:01Europe/London");
}

@Test
@Disabled("Test results can change over time")
void testZoneOrOffsetRight() {
java.util.function.BiConsumer<DateTimeV2Literal, Long> assertHour = (dateTimeV2Literal, expectHour) -> {
Assertions.assertEquals(dateTimeV2Literal.hour, expectHour);
};
DateTimeV2Literal dateTimeV2Literal;
dateTimeV2Literal = new DateTimeV2Literal("2022-08-01 00:00:00Europe/London"); // +01:00
assertHour.accept(dateTimeV2Literal, 7L);
dateTimeV2Literal = new DateTimeV2Literal("2022-08-01 00:00:00America/New_York"); // -04:00
assertHour.accept(dateTimeV2Literal, 12L);
dateTimeV2Literal = new DateTimeV2Literal("2022-08-01 00:00:00Asia/Shanghai");
assertHour.accept(dateTimeV2Literal, 0L);
dateTimeV2Literal = new DateTimeV2Literal("2022-08-01 00:00:00+01:00");
assertHour.accept(dateTimeV2Literal, 7L);
dateTimeV2Literal = new DateTimeV2Literal("2022-08-01 00:00:00-01:00");
assertHour.accept(dateTimeV2Literal, 9L);
new DateTimeV2Literal("2022-08-01 00:00:00Asia/Shanghai");
}

@Test
Expand Down Expand Up @@ -238,6 +220,27 @@ void testDateTimeZone() {
new DateTimeV2Literal("0001-01-01");
}

@Test
void testDateTimeZone1() {
Consumer<DateTimeV2Literal> assertFunc = (datetime) -> {
Assertions.assertEquals(2022, datetime.year);
Assertions.assertEquals(1, datetime.month);
Assertions.assertEquals(2, datetime.day);
Assertions.assertEquals(12, datetime.hour);
Assertions.assertEquals(0, datetime.minute);
Assertions.assertEquals(0, datetime.second);
};
DateTimeV2Literal literal;
literal = new DateTimeV2Literal("2022-01-02 12:00:00UTC+08:00");
assertFunc.accept(literal);
literal = new DateTimeV2Literal("2022-01-02 04:00:00UTC");
assertFunc.accept(literal);
literal = new DateTimeV2Literal("2022-01-01 20:00:00UTC-08:00");
assertFunc.accept(literal);
literal = new DateTimeV2Literal("2022-01-02 04:00:00Z");
assertFunc.accept(literal);
}

@Test
void testIrregularDateTime() {

Expand Down

0 comments on commit 96eb7ec

Please sign in to comment.