Skip to content

Commit e4f0c71

Browse files
wt0530githubgxll
authored andcommitted
[fix][common] Fix date type lower limit returning null
1 parent 45e49cd commit e4f0c71

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

common/src/main/java/io/dingodb/expr/common/timezone/converter/SafeTypeConverter.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ public java.sql.Date timestampToDate(DingoDateTime timestamp, ZoneId contextZone
5454
if (timestamp instanceof DingoDateTime.DingoLocalDateTime) {
5555
DingoDateTime.DingoLocalDateTime localDt = (DingoDateTime.DingoLocalDateTime) timestamp;
5656
LocalDate localDate = localDt.getValue().toLocalDate();
57+
if (localDate.getYear() < 0) {
58+
return null;
59+
}
5760
return java.sql.Date.valueOf(localDate);
5861
} else if (timestamp instanceof DingoDateTime.DingoTimestampTZ) {
5962
DingoDateTime.DingoTimestampTZ tzValue = (DingoDateTime.DingoTimestampTZ) timestamp;
6063
LocalDate localDate = tzValue.getUtcValue()
6164
.atZone(tzValue.getOriginalZone())
6265
.toLocalDate();
66+
if (localDate.getYear() < 0) {
67+
return null;
68+
}
6369
return java.sql.Date.valueOf(localDate);
6470
} else {
6571
throw new IllegalArgumentException("Unsupported timestamp type for date conversion: "
@@ -118,12 +124,18 @@ public java.sql.Timestamp timestampTZToTimestamp(DingoDateTime timestamp, ZoneId
118124
if (timestamp instanceof DingoDateTime.DingoLocalDateTime) {
119125
DingoDateTime.DingoLocalDateTime localDt = (DingoDateTime.DingoLocalDateTime) timestamp;
120126
LocalDateTime localDateTime = localDt.getValue();
127+
if (localDateTime.getYear() < 0) {
128+
return null;
129+
}
121130
return java.sql.Timestamp.valueOf(localDateTime);
122131
} else if (timestamp instanceof DingoDateTime.DingoTimestampTZ) {
123132
DingoDateTime.DingoTimestampTZ tzValue = (DingoDateTime.DingoTimestampTZ) timestamp;
124133
LocalDateTime localDateTime = tzValue.getUtcValue()
125134
.atZone(tzValue.getOriginalZone())
126135
.toLocalDateTime();
136+
if (localDateTime.getYear() < 0) {
137+
return null;
138+
}
127139
return java.sql.Timestamp.valueOf(localDateTime);
128140
} else {
129141
throw new IllegalArgumentException("Unsupported timestamp type for time conversion: "
@@ -150,6 +162,9 @@ public java.sql.Timestamp dateToTimestamp(DingoDateTime date, ZoneId contextZone
150162
if (date instanceof DingoDateTime.DingoLocalDate) {
151163
DingoDateTime.DingoLocalDate localDate = (DingoDateTime.DingoLocalDate) date;
152164
LocalDateTime localDateTime = localDate.getValue().atStartOfDay();
165+
if (localDateTime.getYear() < 0) {
166+
return null;
167+
}
153168
return java.sql.Timestamp.valueOf(localDateTime);
154169
} else {
155170
throw new IllegalArgumentException("Unsupported date type for timestamp conversion: "
@@ -223,7 +238,11 @@ public Object convert(DingoDateTime source, DateTimeType sourceType, DateTimeTyp
223238
if (targetType == DateTimeType.TIMESTAMP || targetType == DateTimeType.TIMESTAMP_TZ) {
224239
return dateToTimestamp(source, contextZone);
225240
} else if (targetType == DateTimeType.DATE) {
226-
return Date.valueOf((LocalDate) source.getValue());
241+
LocalDate localDate = (LocalDate) source.getValue();
242+
if (localDate.getYear() < 0) {
243+
return null;
244+
}
245+
return Date.valueOf(localDate);
227246
}
228247
} else if (sourceType == DateTimeType.TIME) {
229248
if (targetType == DateTimeType.TIMESTAMP || targetType == DateTimeType.TIMESTAMP_TZ) {

0 commit comments

Comments
 (0)