Skip to content

Commit 207b5d8

Browse files
Validate day numbers read into a Date column in Parquet, Arrow and Arrow IPC
These readers validated only the widened `Date32` range and later cast the intermediate `Date32` column to `Date` with a context-less `castColumn`, which narrows the day number to `UInt16`. Extended values like `0000-01-01` and `9999-12-31` therefore wrapped into unrelated in-range `Date`s instead of honoring `date_time_overflow_behavior`. Mirror the ORC/Avro fixes: when the requested type is `Date`, range-check against `[0, DATE_LUT_MAX_DAY_NUM]` before the cast — 'saturate' clamps, anything else throws. Covers the native Parquet v3 reader (`SchemaConverter`/`IntConverter`), the Arrow IPC `RecordBatchDecoder`, and `readColumnWithDate32Data` in `ArrowColumnToCHColumn` (used by ArrowFlight and DeltaLake). Addresses the AI review blockers on #111534 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent fd8d3f0 commit 207b5d8

7 files changed

Lines changed: 93 additions & 17 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,26 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
897897
const DataTypePtr & type_hint, FormatSettings::DateTimeOverflowBehavior date_time_overflow_behavior)
898898
{
899899
DataTypePtr internal_type;
900+
bool check_date32_range = false;
900901
bool check_date_range = false;
901902

902903
if (type_hint && isNumber(type_hint))
903904
{
904905
/// If requested type is a number, read as raw number without checking if it's a valid date.
905906
internal_type = std::make_shared<DataTypeInt32>();
906907
}
908+
else if (type_hint && isDate(*type_hint))
909+
{
910+
/// Arrow date32 can represent days outside the Date range [0, 65535]. The later cast of the
911+
/// intermediate column to Date narrows the day number to UInt16 without checks, so validate
912+
/// the range here, honoring `date_time_overflow_behavior` the same way as the Date32 branch.
913+
internal_type = std::make_shared<DataTypeInt32>();
914+
check_date_range = true;
915+
}
907916
else
908917
{
909918
internal_type = std::make_shared<DataTypeDate32>();
910-
check_date_range = true;
919+
check_date32_range = true;
911920
}
912921

913922
auto internal_column = internal_type->createColumn();
@@ -919,25 +928,27 @@ static ColumnWithTypeAndName readColumnWithDate32Data(const std::shared_ptr<arro
919928
{
920929
const auto & chunk = checkedCast<arrow::Date32Array>(*(arrow_column->chunk(chunk_i)), column_name);
921930

922-
if (check_date_range)
931+
if (check_date32_range || check_date_range)
923932
{
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;
924935
for (size_t value_i = 0, length = static_cast<size_t>(chunk.length()); value_i < length; ++value_i)
925936
{
926937
Int32 days_num = static_cast<Int32>(chunk.Value(value_i));
927-
if (days_num > DATE_LUT_MAX_EXTEND_DAY_NUM || days_num < DATE_LUT_MIN_EXTEND_DAY_NUM)
938+
if (days_num > max_day || days_num < min_day)
928939
{
929940
switch (date_time_overflow_behavior)
930941
{
931942
case FormatSettings::DateTimeOverflowBehavior::Saturate:
932-
days_num = (days_num < DATE_LUT_MIN_EXTEND_DAY_NUM) ? DATE_LUT_MIN_EXTEND_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
943+
days_num = (days_num < min_day) ? min_day : max_day;
933944
break;
934945
default:
935946
/// Prior to introducing `date_time_overflow_behavior`, this function threw an error in case value was out of range.
936947
/// In order to leave this behavior as default, we also throw when `date_time_overflow_mode == ignore`, as it is the setting's default value
937948
/// (As we want to make this backwards compatible, not break any workflows.)
938949
throw Exception{ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
939-
"Input value {} of a column \"{}\" is out of allowed Date32 range, which is [{}, {}]",
940-
days_num, column_name, DATE_LUT_MIN_EXTEND_DAY_NUM, DATE_LUT_MAX_EXTEND_DAY_NUM};
950+
"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};
941952
}
942953
}
943954

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,13 @@ ColumnPtr RecordBatchDecoder::decodeInner(const ArrowField & field, size_t rows,
427427
/// (`readColumnWithDate32Data`): a day number outside ClickHouse's allowed Date32 range is
428428
/// saturated or rejected according to `date_time_overflow_behavior` (its default `Ignore`,
429429
/// like `Throw`, rejects — preserving the pre-`date_time_overflow_behavior` behavior)
430-
/// instead of leaving an invalid Date32 in the result.
430+
/// instead of leaving an invalid Date32 in the result. When the requested header type is
431+
/// `Date`, enforce the narrower Date range [0, 65535] instead: `buildChunk` later casts the
432+
/// intermediate Date32 column to `Date` without checks, narrowing the day number to UInt16,
433+
/// so an unchecked extended Date32 value would wrap into an unrelated in-range `Date`.
434+
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;
431437
checkBufferSize(values, requiredBytes(rows, sizeof(Int32)), "date32");
432438
auto & data = assert_cast<ColumnInt32 &>(*column).getData();
433439
data.resize(rows);
@@ -436,15 +442,15 @@ ColumnPtr RecordBatchDecoder::decodeInner(const ArrowField & field, size_t rows,
436442
for (size_t i = 0; i < rows; ++i)
437443
{
438444
Int32 days = src[i];
439-
if (days > DATE_LUT_MAX_EXTEND_DAY_NUM || days < DATE_LUT_MIN_EXTEND_DAY_NUM)
445+
if (days > max_day || days < min_day)
440446
{
441447
if (saturate)
442-
days = days < DATE_LUT_MIN_EXTEND_DAY_NUM ? DATE_LUT_MIN_EXTEND_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
448+
days = days < min_day ? min_day : max_day;
443449
else
444450
throw Exception(
445451
ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
446-
"Arrow IPC date32 value {} is out of the allowed Date32 range [{}, {}]",
447-
days, DATE_LUT_MIN_EXTEND_DAY_NUM, DATE_LUT_MAX_EXTEND_DAY_NUM);
452+
"Arrow IPC date32 value {} is out of the allowed {} range [{}, {}]",
453+
days, date32_as_date ? "Date" : "Date32", min_day, max_day);
448454
}
449455
data[i] = days;
450456
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,18 +1423,20 @@ void IntConverter::convertColumn(std::span<const char> data, size_t num_values,
14231423

14241424
if (date_overflow_behavior != FormatSettings::DateTimeOverflowBehavior::Ignore)
14251425
{
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;
14261428
auto & values = assert_cast<ColumnInt32 &>(col).getData();
14271429
for (size_t i = values.size() - num_values; i < values.size(); ++i)
14281430
{
14291431
Int32 & days_num = values[i];
1430-
if (days_num > DATE_LUT_MAX_EXTEND_DAY_NUM || days_num < DATE_LUT_MIN_EXTEND_DAY_NUM)
1432+
if (days_num > max_day || days_num < min_day)
14311433
{
14321434
if (date_overflow_behavior == FormatSettings::DateTimeOverflowBehavior::Saturate)
1433-
days_num = (days_num < DATE_LUT_MIN_EXTEND_DAY_NUM) ? DATE_LUT_MIN_EXTEND_DAY_NUM : DATE_LUT_MAX_EXTEND_DAY_NUM;
1435+
days_num = (days_num < min_day) ? min_day : max_day;
14341436
else
14351437
throw Exception{ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE,
1436-
"Input value {} is out of allowed Date32 range, which is [{}, {}]",
1437-
days_num, DATE_LUT_MIN_EXTEND_DAY_NUM, DATE_LUT_MAX_EXTEND_DAY_NUM};
1438+
"Input value {} is out of allowed {} range, which is [{}, {}]",
1439+
days_num, date_target_is_date ? "Date" : "Date32", min_day, max_day};
14381440
}
14391441
}
14401442
}
@@ -1494,7 +1496,8 @@ void IntConverter::convertField(std::span<const char> data, bool /*is_max*/, Fie
14941496
else if (field_signed)
14951497
{
14961498
if (date_overflow_behavior != FormatSettings::DateTimeOverflowBehavior::Ignore &&
1497-
(Int64(val) > DATE_LUT_MAX_EXTEND_DAY_NUM || Int64(val) < DATE_LUT_MIN_EXTEND_DAY_NUM))
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)))
14981501
return;
14991502

15001503
out = Field(Int64(val));

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,13 @@ struct IntConverter : public FixedSizeConverter
191191
bool field_ipv4 = false; // IPv4
192192
bool field_timestamp_from_millis = false; // convert DateTime64(3) to DateTime
193193
bool field_signed = true; // Int64, otherwise UInt64
194-
/// If not Ignore, it's a Date32 column and we should range-check it.
194+
/// If not Ignore, it's a date column and we should range-check it.
195195
FormatSettings::DateTimeOverflowBehavior date_overflow_behavior = FormatSettings::DateTimeOverflowBehavior::Ignore;
196+
/// Only used when date_overflow_behavior is not Ignore: the requested output type is Date rather
197+
/// than Date32, so range-check against the narrower [0, DATE_LUT_MAX_DAY_NUM] window. The final
198+
/// cast of the decoded Int32 column to Date narrows to UInt16 without checks, so an unchecked
199+
/// extended Date32 value would wrap into an unrelated in-range Date.
200+
bool date_target_is_date = false;
196201

197202
bool isTrivial() const override
198203
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,12 @@ void SchemaConverter::processPrimitiveColumn(
12041204
/// (As we want to make this backwards compatible, not break any workflows.)
12051205
if (converter->date_overflow_behavior == FormatSettings::DateTimeOverflowBehavior::Ignore)
12061206
converter->date_overflow_behavior = FormatSettings::DateTimeOverflowBehavior::Throw;
1207+
1208+
/// When the requested type is Date, enforce the narrower Date range [0, 65535]:
1209+
/// `formOutputColumn` later casts the decoded Date32 column to Date without checks,
1210+
/// narrowing the day number to UInt16, so an unchecked extended Date32 value would
1211+
/// wrap into an unrelated in-range Date.
1212+
converter->date_target_is_date = type_hint && WhichDataType(type_hint->getTypeId()).isDate();
12071213
}
12081214

12091215
out_decoder.allow_stats = dispatch_int_stats_converter(/*allow_datetime_and_ipv4=*/ false, *converter);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Parquet
2+
1970-01-01
3+
2149-06-06
4+
1
5+
2149-06-06
6+
1970-01-01
7+
Arrow
8+
1970-01-01
9+
2149-06-06
10+
1
11+
2149-06-06
12+
1970-01-01
13+
ArrowStream
14+
1970-01-01
15+
2149-06-06
16+
1
17+
2149-06-06
18+
1970-01-01
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
# Tags: no-fasttest
3+
4+
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
5+
# shellcheck source=../shell_config.sh
6+
. "$CUR_DIR"/../shell_config.sh
7+
8+
for format in Parquet Arrow ArrowStream
9+
do
10+
echo "$format"
11+
FILE=${CLICKHOUSE_TMP}/04761_date.$format
12+
13+
# The whole Date range [1970-01-01, 2149-06-06] round-trips through the date types
14+
# of these formats when read with a Date type hint.
15+
${CLICKHOUSE_LOCAL} -q "insert into function file('$FILE', $format, 'date Date') settings engine_file_truncate_on_insert = 1 values ('1970-01-01'), ('2149-06-06')"
16+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date Date')"
17+
18+
# A date beyond the Date range must not wrap in the UInt16 representation:
19+
# an error by default, the boundary value with 'saturate'.
20+
# 9999-12-31 is day 2932896, which without validation would wrap to day 49312 = 2105-01-05,
21+
# and 0000-01-01 is day -719528, which would wrap to day 1368 = 1973-09-30.
22+
${CLICKHOUSE_LOCAL} -q "insert into function file('$FILE', $format, 'date Date32') settings engine_file_truncate_on_insert = 1 values ('9999-12-31'), ('0000-01-01')"
23+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date Date')" 2>&1 | grep -c "VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE"
24+
${CLICKHOUSE_LOCAL} -q "select * from file('$FILE', $format, 'date Date') settings date_time_overflow_behavior='saturate'"
25+
26+
rm -f "$FILE"
27+
done

0 commit comments

Comments
 (0)