Skip to content

Clean up thread safety in FunctionConvert - #90644

Merged
alexey-milovidov merged 13 commits into
masterfrom
unmut
Jan 18, 2026
Merged

alexey-milovidov merged 13 commits into
masterfrom
unmut

Conversation

@al13n321

@al13n321 al13n321 commented Nov 22, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Details

A few functions do this terrible-looking hack with assigning mutable fields 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

  • Merged into: 26.1.1.632

@clickhouse-gh

clickhouse-gh Bot commented Nov 22, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bfd6bbb]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, 4/4) failure
test_peak_memory_usage/test.py::test_clickhouse_client_max_peak_memory_usage_distributed FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa) FAIL cidb, issue
BuzzHouse (amd_msan) failure
Logical error: Not-ready Set is passed as the second argument for function 'A (STID: None) FAIL cidb
Integration tests (amd_asan, targeted) error

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 22, 2025
@al13n321
al13n321 force-pushed the unmut branch 2 times, most recently from 1be3aff to 97b6e1d Compare November 22, 2025 09:23
@al13n321 al13n321 added the no-fast-tests Drops preliminary CI jobs: Style, clang-tidy, Fast Test label Nov 23, 2025
@serxa serxa self-assigned this Nov 24, 2025
@al13n321

al13n321 commented Dec 2, 2025

Copy link
Copy Markdown
Contributor Author

FunctionsConversion.h is unhinged.

@al13n321 al13n321 removed the no-fast-tests Drops preliminary CI jobs: Style, clang-tidy, Fast Test label Dec 3, 2025
@al13n321
al13n321 marked this pull request as ready for review January 6, 2026 22:04
@al13n321 al13n321 mentioned this pull request Jan 7, 2026
@serxa
serxa requested a review from Copilot January 13, 2026 11:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FunctionConvert by passing nullability information as a function parameter instead
  • Implemented custom nullable handling in executeImpl instead 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

Comment on lines +2905 to +2906
else if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64> || std::is_same_v<ToDataType, DataTypeDateTime>)
return {1, 2};

Copilot AI Jan 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot generated this review using guidance from repository custom instructions.

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);

Copilot AI Jan 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
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

Copilot AI Jan 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
if (null_map)
{
const NullMap & null_map_data = assert_cast<const ColumnUInt8 &>(*null_map).getData();
is_null = null_map_data[0];

Copilot AI Jan 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot generated this review using guidance from repository custom instructions.
@alexey-milovidov

Copy link
Copy Markdown
Member

Thanks! Yes, we shouldn't share any mutable data between getReturnType and execute. It's better if we do the calculations twice.

@alexey-milovidov
alexey-milovidov merged commit cd26fee into master Jan 18, 2026
124 of 128 checks passed
@alexey-milovidov
alexey-milovidov deleted the unmut branch January 18, 2026 12:28
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants