[fix](cast) fix wrong result of casting empty string to array date (#22281)

This commit is contained in:
TengJianPing
2023-07-30 21:15:03 +08:00
committed by GitHub
parent 63a9a886f5
commit 79289e32dc
4 changed files with 31 additions and 13 deletions

View File

@ -67,12 +67,6 @@ void DataTypeDateV2::to_string(const IColumn& column, size_t row_num, BufferWrit
UInt32 int_val = assert_cast<const ColumnUInt32&>(*ptr).get_element(row_num);
DateV2Value<DateV2ValueType> val = binary_cast<UInt32, DateV2Value<DateV2ValueType>>(int_val);
// if this is an invalid date, write nothing(instead of 0000-00-00) to output string, or else
// it will cause problem for null DataTypeDateV2 value in cast function,
// e.g. cast(cast(null_date as char) as date)
if (!val.is_valid_date()) {
return;
}
char buf[64];
char* pos = val.to_string(buf);
@ -150,12 +144,6 @@ void DataTypeDateTimeV2::to_string(const IColumn& column, size_t row_num,
UInt64 int_val = assert_cast<const ColumnUInt64&>(*ptr).get_element(row_num);
DateV2Value<DateTimeV2ValueType> val =
binary_cast<UInt64, DateV2Value<DateTimeV2ValueType>>(int_val);
// if this is an invalid date, write nothing(instead of 0000-00-00) to output string, or else
// it will cause problem for null DataTypeDateV2 value in cast function,
// e.g. cast(cast(null_date as char) as date)
if (!val.is_valid_date()) {
return;
}
char buf[64];
char* pos = val.to_string(buf, _scale);

View File

@ -2406,6 +2406,12 @@ bool DateV2Value<T>::from_date_format_str(const char* format, int format_len, co
template <typename T>
int32_t DateV2Value<T>::to_buffer(char* buffer, int scale) const {
// if this is an invalid date, write nothing(instead of 0000-00-00) to output string, or else
// it will cause problem for null DataTypeDateV2 value in cast function,
// e.g. cast(cast(null_date as char) as date)
if (!is_valid_date()) {
return 0;
}
char* start = buffer;
uint32_t temp;
// Year

View File

@ -30,5 +30,20 @@
[1.340, 2.010, 0.000, 0.000, 0.000]
-- !sql --
[2022-09-01, 0000-00-00, 0000-00-00]
[2022-09-01, , ]
-- !sql --
["2022-09-01", "", ""]
-- !sql --
[2022-09-01, NULL, NULL]
-- !sql --
[2022-09-01, , ]
-- !sql --
["2022-09-01", "", ""]
-- !sql --
[2022-09-01, NULL, NULL]

View File

@ -41,5 +41,14 @@ suite("test_cast_string_to_array") {
qt_sql """ select cast ("[1,2,3,,,]" as array<int>) """
qt_sql """ select cast ("[a,b,c,,,]" as array<string>) """
qt_sql """ select cast ("[1.34,2.01,,,]" as array<decimal(10, 3)>) """
sql """ ADMIN SET FRONTEND CONFIG ("enable_date_conversion" = "false"); """
qt_sql """ select cast ("[2022-09-01,,]" as array<date>) """
qt_sql """ select cast ("[2022-09-01,,]" as array<string>) """
qt_sql """ select cast(cast ("[2022-09-01,,]" as array<string>) as array<date>) """
sql """ ADMIN SET FRONTEND CONFIG ("enable_date_conversion" = "true"); """
qt_sql """ select cast ("[2022-09-01,,]" as array<date>) """
qt_sql """ select cast ("[2022-09-01,,]" as array<string>) """
qt_sql """ select cast(cast ("[2022-09-01,,]" as array<string>) as array<date>) """
}