Skip to content

Commit

Permalink
[fix](nereids)prevent null pointer exception if datetime value overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 committed Aug 16, 2024
1 parent 7a09202 commit f54c15b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,10 @@ private static boolean cannotAdjust(DateTimeLiteral l, ComparisonPredicate cp) {
private static Expression migrateToDateV2(DateTimeLiteral l, AdjustType type) {
DateV2Literal d = new DateV2Literal(l.getYear(), l.getMonth(), l.getDay());
if (type == AdjustType.UPPER && (l.getHour() != 0 || l.getMinute() != 0 || l.getSecond() != 0)) {
d = ((DateV2Literal) d.plusDays(1));
return d.plusDays(1);
} else {
return d;
}
return d;
}

private static Expression migrateToDate(DateV2Literal l) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,27 @@ private static LocalDateTime getDateCeilOrFloor(DATE tag, LocalDateTime date, in
if (getCeil) {
step = step + (deltaInsidePeriod == 0 ? 0 : period);
}
switch (tag) {
case YEAR:
return ((DateTimeLiteral) start.plusYears(step)).toJavaDateType();
case MONTH:
return ((DateTimeLiteral) start.plusMonths(step)).toJavaDateType();
case DAY:
return ((DateTimeLiteral) start.plusDays(step)).toJavaDateType();
case HOUR:
return ((DateTimeLiteral) start.plusHours(step)).toJavaDateType();
case MINUTE:
return ((DateTimeLiteral) start.plusMinutes(step)).toJavaDateType();
case SECOND:
return ((DateTimeLiteral) start.plusSeconds(step)).toJavaDateType();
default:
break;
try {
switch (tag) {
case YEAR:
return ((DateTimeLiteral) start.plusYears(step)).toJavaDateType();
case MONTH:
return ((DateTimeLiteral) start.plusMonths(step)).toJavaDateType();
case DAY:
return ((DateTimeLiteral) start.plusDays(step)).toJavaDateType();
case HOUR:
return ((DateTimeLiteral) start.plusHours(step)).toJavaDateType();
case MINUTE:
return ((DateTimeLiteral) start.plusMinutes(step)).toJavaDateType();
case SECOND:
return ((DateTimeLiteral) start.plusSeconds(step)).toJavaDateType();
default:
break;
}
} catch (Exception ex) {
// do nothing
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected boolean checkDate() {
}

protected static boolean isDateOutOfRange(LocalDateTime dateTime) {
return dateTime.isBefore(START_OF_A_DAY) || dateTime.isAfter(END_OF_A_DAY);
return dateTime.isBefore(START_OF_A_DAY) || dateTime.isAfter(END_OF_A_DAY) || dateTime == null;
}

private boolean checkDatetime(TemporalAccessor dateTime) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

suite("test_datetime_overflow") {
sql 'set enable_nereids_planner=true'
sql 'set enable_fallback_to_original_planner=false'
sql """drop table if exists datetime_overflow_t"""
sql """CREATE TABLE datetime_overflow_t (
`id` bigint NULL,
`c` datetime NULL,
`d` date NULL,
INDEX idx_c (`c`) USING INVERTED
) ENGINE=OLAP
DUPLICATE KEY(`id`)
DISTRIBUTED BY RANDOM BUCKETS AUTO
PROPERTIES (
"replication_num" = "1"
);"""

sql """select * from datetime_overflow_t where d between "9999-12-31 00:00:01" and "9999-12-31 10:00:01";"""
sql """select * from datetime_overflow_t where d > "9999-12-31 00:00:01";"""
}

0 comments on commit f54c15b

Please sign in to comment.