Skip to content

Commit a7843c4

Browse files
Validate day numbers read into a DateTime column in Parquet, Arrow and Arrow IPC
The Parquet, Arrow and Arrow IPC readers decode a date32 column into an intermediate `Date32` column and cast it to the requested type with a context-less `castColumn`, which ignores `date_time_overflow_behavior`. With the extended `Date32` range, a valid day number like `9999-12-31` passed the `Date32` range check and then wrapped through `ToDateTimeImpl<Ignore>` to an unrelated timestamp in 2106 when read into a `DateTime` column. Validate day numbers against the [0, MAX_DATETIME_DAY_NUM] window that `ToDateTimeImpl` uses when the requested type is `DateTime`, alongside the existing `Date` target validation: 'saturate' clamps, anything else throws. Extends test `04761_parquet_arrow_date_overflow_behavior` with `DateTime` target cases. Addresses the AI review finding in #111534 (comment)
1 parent a4c57e9 commit a7843c4

7 files changed

Lines changed: 71 additions & 13 deletions

File tree

‎src/Processors/Formats/Impl/ArrowColumnToCHColumn.cpp‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <DataTypes/DataTypeInterval.h>
2626
#include <Common/DateLUTImpl.h>
2727
#include <Common/IntervalKind.h>
28+
#include <Functions/DateTimeTransforms.h>
2829
#include <Processors/Chunk.h>
2930
#include <Processors/Formats/Impl/ArrowBufferedStreams.h>
3031
#include <Processors/Formats/Impl/ArrowGeoTypes.h>
@@ -899,6 +900,7 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
899900
DataTypePtr internal_type;
900901
bool check_date32_range = false;
901902
bool check_date_range = false;
903+
bool check_datetime_range = false;
902904

903905
if (type_hint && isNumber(type_hint))
904906
{
@@ -913,6 +915,15 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
913915
internal_type = std::make_shared<DataTypeInt32>();
914916
check_date_range = true;
915917
}
918+
else if (type_hint && isDateTime(*type_hint))
919+
{
920+
/// The later cast of the intermediate Date32 column to DateTime is context-less, so
921+
/// `date_time_overflow_behavior` does not reach it and a day number whose midnight does not
922+
/// fit into DateTime wraps to an unrelated timestamp. Validate the range here against the
923+
/// same [0, MAX_DATETIME_DAY_NUM] window that `ToDateTimeImpl` uses.
924+
internal_type = std::make_shared<DataTypeDate32>();
925+
check_datetime_range = true;
926+
}
916927
else
917928
{
918929
internal_type = std::make_shared<DataTypeDate32>();
@@ -928,10 +939,13 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
928939
{
929940
const auto & chunk = checkedCast<arrow::Date32Array>(*(arrow_column->chunk(chunk_i)), column_name);
930941

931-
if (check_date32_range || check_date_range)
942+
if (check_date32_range || check_date_range || check_datetime_range)
932943
{
933-
const Int32 min_day = check_date_range ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM;
934-
const Int32 max_day = check_date_range ? DATE_LUT_MAX_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
944+
const Int32 min_day = check_date32_range ? DATE_LUT_MIN_EXTEND_DAY_NUM : 0;
945+
const Int32 max_day = check_date_range ? DATE_LUT_MAX_DAY_NUM
946+
: check_datetime_range ? static_cast<Int32>(MAX_DATETIME_DAY_NUM)
947+
: DATE_LUT_MAX_EXTEND_DAY_NUM;
948+
const char * target_type_name = check_date_range ? "Date" : check_datetime_range ? "DateTime" : "Date32";
935949
for (size_t value_i = 0, length = static_cast<size_t>(chunk.length()); value_i < length; ++value_i)
936950
{
937951
Int32 days_num = static_cast<Int32>(chunk.Value(value_i));
@@ -948,7 +962,7 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
948962
/// (As we want to make this backwards compatible, not break any workflows.)
949963
throw Exception{ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
950964
"Input value {} of a column \"{}\" is out of allowed {} range, which is [{}, {}]",
951-
days_num, column_name, check_date_range ? "Date" : "Date32", min_day, max_day};
965+
days_num, column_name, target_type_name, min_day, max_day};
952966
}
953967
}
954968

‎src/Processors/Formats/Impl/ArrowIPC/RecordBatchDecoder.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <Common/assert_cast.h>
2727
#include <Common/FloatUtils.h>
2828
#include <Common/DateLUTImpl.h>
29+
#include <Functions/DateTimeTransforms.h>
2930
#include <Core/UUID.h>
3031
#include <boost/algorithm/string/case_conv.hpp>
3132
#include <boost/algorithm/string/predicate.hpp>
@@ -431,9 +432,16 @@ ColumnPtr RecordBatchDecoder::decodeInner(const ArrowField & field, size_t rows,
431432
/// `Date`, enforce the narrower Date range [0, 65535] instead: `buildChunk` later casts the
432433
/// intermediate Date32 column to `Date` without checks, narrowing the day number to UInt16,
433434
/// so an unchecked extended Date32 value would wrap into an unrelated in-range `Date`.
435+
/// Similarly, when the requested header type is `DateTime`, enforce the
436+
/// [0, MAX_DATETIME_DAY_NUM] window that `ToDateTimeImpl` uses: the later context-less cast
437+
/// ignores `date_time_overflow_behavior` and wraps day numbers whose midnight does not fit.
434438
const bool date32_as_date = effective_hint && isDate(stripHint(effective_hint));
435-
const Int32 min_day = date32_as_date ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM;
436-
const Int32 max_day = date32_as_date ? DATE_LUT_MAX_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
439+
const bool date32_as_datetime = effective_hint && isDateTime(stripHint(effective_hint));
440+
const Int32 min_day = (date32_as_date || date32_as_datetime) ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM;
441+
const Int32 max_day = date32_as_date ? DATE_LUT_MAX_DAY_NUM
442+
: date32_as_datetime ? static_cast<Int32>(MAX_DATETIME_DAY_NUM)
443+
: DATE_LUT_MAX_EXTEND_DAY_NUM;
444+
const char * target_type_name = date32_as_date ? "Date" : date32_as_datetime ? "DateTime" : "Date32";
437445
checkBufferSize(values, requiredBytes(rows, sizeof(Int32)), "date32");
438446
auto & data = assert_cast<ColumnInt32 &>(*column).getData();
439447
data.resize(rows);
@@ -450,7 +458,7 @@ ColumnPtr RecordBatchDecoder::decodeInner(const ArrowField & field, size_t rows,
450458
throw Exception(
451459
ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
452460
"Arrow IPC date32 value {} is out of the allowed {} range [{}, {}]",
453-
days, date32_as_date ? "Date" : "Date32", min_day, max_day);
461+
days, target_type_name, min_day, max_day);
454462
}
455463
data[i] = days;
456464
}

‎src/Processors/Formats/Impl/Parquet/Decoding.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <base/arithmeticOverflow.h>
44
#include <Columns/ColumnString.h>
55
#include <Common/FloatUtils.h>
6+
#include <Functions/DateTimeTransforms.h>
67

78
#include <arrow/util/bit_stream_utils_internal.h>
89
#include <arrow/util/byte_stream_split_internal.h>
@@ -1423,8 +1424,10 @@ void IntConverter::convertColumn(std::span<const char> data, size_t num_values,
14231424

14241425
if (date_overflow_behavior != FormatSettings::DateTimeOverflowBehavior::Ignore)
14251426
{
1426-
const Int32 min_day = date_target_is_date ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM;
1427-
const Int32 max_day = date_target_is_date ? DATE_LUT_MAX_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
1427+
const Int32 min_day = (date_target_is_date || date_target_is_datetime) ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM;
1428+
const Int32 max_day = date_target_is_date ? DATE_LUT_MAX_DAY_NUM
1429+
: date_target_is_datetime ? static_cast<Int32>(MAX_DATETIME_DAY_NUM)
1430+
: DATE_LUT_MAX_EXTEND_DAY_NUM;
14281431
auto & values = assert_cast<ColumnInt32 &>(col).getData();
14291432
for (size_t i = values.size() - num_values; i < values.size(); ++i)
14301433
{
@@ -1436,7 +1439,7 @@ void IntConverter::convertColumn(std::span<const char> data, size_t num_values,
14361439
else
14371440
throw Exception{ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
14381441
"Input value {} is out of allowed {} range, which is [{}, {}]",
1439-
days_num, date_target_is_date ? "Date" : "Date32", min_day, max_day};
1442+
days_num, date_target_is_date ? "Date" : date_target_is_datetime ? "DateTime" : "Date32", min_day, max_day};
14401443
}
14411444
}
14421445
}
@@ -1496,8 +1499,10 @@ void IntConverter::convertField(std::span<const char> data, bool /*is_max*/, Fie
14961499
else if (field_signed)
14971500
{
14981501
if (date_overflow_behavior != FormatSettings::DateTimeOverflowBehavior::Ignore &&
1499-
(Int64(val) > (date_target_is_date ? DATE_LUT_MAX_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM)
1500-
|| Int64(val) < (date_target_is_date ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM)))
1502+
(Int64(val) > (date_target_is_date ? DATE_LUT_MAX_DAY_NUM
1503+
: date_target_is_datetime ? Int64(MAX_DATETIME_DAY_NUM)
1504+
: DATE_LUT_MAX_EXTEND_DAY_NUM)
1505+
|| Int64(val) < ((date_target_is_date || date_target_is_datetime) ? 0 : DATE_LUT_MIN_EXTEND_DAY_NUM)))
15011506
return;
15021507

15031508
out = Field(Int64(val));

‎src/Processors/Formats/Impl/Parquet/Decoding.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ struct IntConverter : public FixedSizeConverter
198198
/// cast of the decoded Int32 column to Date narrows to UInt16 without checks, so an unchecked
199199
/// extended Date32 value would wrap into an unrelated in-range Date.
200200
bool date_target_is_date = false;
201+
/// Same idea for a DateTime output type: the final context-less cast ignores
202+
/// `date_time_overflow_behavior` and wraps day numbers whose midnight does not fit into
203+
/// DateTime, so range-check against the [0, MAX_DATETIME_DAY_NUM] window of ToDateTimeImpl.
204+
bool date_target_is_datetime = false;
201205

202206
bool isTrivial() const override
203207
{

‎src/Processors/Formats/Impl/Parquet/SchemaConverter.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,10 @@ void SchemaConverter::processPrimitiveColumn(
12081208
/// When the requested type is Date, enforce the narrower Date range [0, 65535]:
12091209
/// `formOutputColumn` later casts the decoded Date32 column to Date without checks,
12101210
/// narrowing the day number to UInt16, so an unchecked extended Date32 value would
1211-
/// wrap into an unrelated in-range Date.
1211+
/// wrap into an unrelated in-range Date. Similarly for a DateTime target, whose
1212+
/// context-less cast wraps day numbers whose midnight does not fit into DateTime.
12121213
converter->date_target_is_date = type_hint && WhichDataType(type_hint->getTypeId()).isDate();
1214+
converter->date_target_is_datetime = type_hint && WhichDataType(type_hint->getTypeId()).isDateTime();
12131215
}
12141216

12151217
out_decoder.allow_stats = dispatch_int_stats_converter(/*allow_datetime_and_ipv4=*/ false, *converter);

‎tests/queries/0_stateless/04761_parquet_arrow_date_overflow_behavior.reference‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@ Parquet
44
1
55
2149-06-06
66
1970-01-01
7+
1
8+
2106-02-06 00:00:00
9+
1970-01-01 00:00:00
10+
1970-01-01 00:00:00
11+
2106-02-06 00:00:00
712
Arrow
813
1970-01-01
914
2149-06-06
1015
1
1116
2149-06-06
1217
1970-01-01
18+
1
19+
2106-02-06 00:00:00
20+
1970-01-01 00:00:00
21+
1970-01-01 00:00:00
22+
2106-02-06 00:00:00
1323
ArrowStream
1424
1970-01-01
1525
2149-06-06
1626
1
1727
2149-06-06
1828
1970-01-01
29+
1
30+
2106-02-06 00:00:00
31+
1970-01-01 00:00:00
32+
1970-01-01 00:00:00
33+
2106-02-06 00:00:00

‎tests/queries/0_stateless/04761_parquet_arrow_date_overflow_behavior.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,15 @@ do
2323
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date Date')" 2>&1 | grep -c "VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE"
2424
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date Date') settings date_time_overflow_behavior='saturate'"
2525

26+
# A day number whose midnight does not fit into DateTime must not wrap through the
27+
# context-less Date32 -> DateTime cast: 9999-12-31 is day 2932896, which without validation
28+
# would wrap to an unrelated timestamp in 2106, and 0000-01-01 would wrap to one in 2033.
29+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date DateTime(\'UTC\')')" 2>&1 | grep -c "VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE"
30+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date DateTime(\'UTC\')') settings date_time_overflow_behavior='saturate'"
31+
32+
# The DateTime-representable day range [1970-01-01, 2106-02-06] round-trips with a DateTime type hint.
33+
${CLICKHOUSE_LOCAL} -q "insert into function file('$FILE', $format, 'date Date32') settings engine_file_truncate_on_insert = 1 values ('1970-01-01'), ('2106-02-06')"
34+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date DateTime(\'UTC\')')"
35+
2636
rm -f "$FILE"
2737
done

0 commit comments

Comments
 (0)