Repository navigation
Clean up thread safety in FunctionConvert - #90644
Conversation
|
Workflow [PR], commit [bfd6bbb] Summary: ❌
|
1be3aff to
97b6e1d
Compare
|
|
There was a problem hiding this comment.
Pull request overview
This PR removes mutable state from FunctionConvert to improve thread safety and clean up the implementation. The function previously used mutable fields (checked_return_type, to_nullable) set during getReturnTypeImpl calls, which made instances non-reusable and potentially not thread-safe.
Changes:
- Removed mutable state from
FunctionConvertby passing nullability information as a function parameter instead - Implemented custom nullable handling in
executeImplinstead of relying on default implementation - Optimized null map handling to share column references when possible, avoiding unnecessary copies
- Added documentation about nullable behavior and edge cases in the codebase
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/clickhouse-test | Added randomization for short_circuit_function_evaluation_for_nulls_threshold setting |
| src/Functions/IFunction.h | Added documentation clarifying nullable default implementation behavior |
| src/Functions/IFunction.cpp | Optimized null map handling to share column references instead of copying when only one nullable argument exists |
| src/Functions/FunctionsConversion.h | Removed mutable state fields and reimplemented nullable handling directly in executeImpl |
| src/Functions/FunctionHelpers.h | Updated documentation for wrapInNullable to clarify nullptr behavior |
| src/Functions/FunctionHelpers.cpp | Enhanced wrapInNullable to handle nullptr null_map and share column references |
| else if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64> || std::is_same_v<ToDataType, DataTypeDateTime>) | ||
| return {1, 2}; |
There was a problem hiding this comment.
When ToDataType is DataTypeDateTime, returning {1, 2} assumes a third argument exists, but DateTime conversions typically only have 1-2 arguments. This could cause index out of bounds when accessing argument 2. Verify that DateTime conversions always have at least 3 arguments, or adjust the logic to return {1} for DateTime and {1, 2} only for DateTime64.
|
|
||
| ColumnsWithTypeAndName temporary_columns = createBlockWithNestedColumns(arguments); | ||
| auto temporary_result_type = removeNullable(result_type); | ||
| ColumnPtr res = executeInternal(temporary_columns, temporary_result_type, input_rows_count, /*to_nullable=*/ true); |
There was a problem hiding this comment.
The parameter name to_nullable in the inline comment is misleading. Based on the code context, this parameter indicates whether the final result type is nullable, not whether the operation should convert to nullable. Consider renaming to result_is_nullable or output_is_nullable for clarity.
| ColumnPtr res = executeInternal(temporary_columns, temporary_result_type, input_rows_count, /*to_nullable=*/ true); | |
| ColumnPtr res = executeInternal(temporary_columns, temporary_result_type, input_rows_count, /*result_is_nullable=*/ true); |
| bool canBeExecutedOnDefaultArguments() const override { return false; } | ||
|
|
||
| ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override | ||
| ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & weird_result_type, size_t input_rows_count) const override |
There was a problem hiding this comment.
The parameter name weird_result_type is unprofessional and unclear for production code. While the comment below explains the complexity, the parameter should have a neutral name like result_type or provided_result_type.
| if (null_map) | ||
| { | ||
| const NullMap & null_map_data = assert_cast<const ColumnUInt8 &>(*null_map).getData(); | ||
| is_null = null_map_data[0]; |
There was a problem hiding this comment.
Accessing null_map_data[0] without checking if the null_map column is empty could cause an out-of-bounds access. While the calling code likely ensures non-empty columns, defensive programming suggests checking !null_map_data.empty() before accessing the first element.
|
Thanks! Yes, we shouldn't share any mutable data between getReturnType and execute. It's better if we do the calculations twice. |
Co-Authored-By: Claude Opus 4.5 <[email protected]>
Changelog category (leave one):
Details
A few functions do this terrible-looking hack with assigning
mutablefields in getReturnTypeImpl. It makes IFunctionOverloadResolver usable only for one overload per instance, and sometimes makes getReturnTypeImpl not thread safe. I'm trying to figure out whether to make the functions not do that or to document these weird requirements in IFunctionOverloadResolver (and check if they're even met currently).Version info
26.1.1.632