Skip to content

Add canThrow interface to functions - #100127

Open
m-selmi wants to merge 55 commits into
ClickHouse:masterfrom
m-selmi:avoid-array-join-lift-up-with-throwing-function
Open

m-selmi wants to merge 55 commits into
ClickHouse:masterfrom
m-selmi:avoid-array-join-lift-up-with-throwing-function

Conversation

@m-selmi

@m-selmi m-selmi commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds a canThrow interface to all functions. canThrow returns true if the function might throw during execution on the data. If a function throws independently of the data then that is seen as a formatting/structural issue with the query.

This can be useful for determining wether we can apply some optimizations or not (e.g. changing the order of some operations, like ARRAY JOINs and FILTERs)

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add canThrow interface to all functions that throw an exception during execution depending on its input data.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [074da97]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 3/6) FAIL
test_replicated_database/test.py::test_replicated_table_structure_alter FAIL cidb
Integration tests (amd_msan, 6/8) FAIL
test_storage_rabbitmq/test_system_stop.py::test_refresh_runs_once_while_start_keeps_consuming FAIL cidb
Stress test (amd_msan) FAIL
canThrow contract violations FAIL cidb
Stress test (arm_asan_ubsan) FAIL
canThrow contract violations FAIL cidb
Upgrade check (amd_release) FAIL
Changed settings are not reflected in the settings changes history (see changed_settings.txt) FAIL cidb
New settings are not reflected in settings changes history (see new_settings.txt) FAIL cidb
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

AI Review

Summary

This PR wires canThrow through IExecutableFunction and reclassifies a large set of functions so replicated-column execution can compact nested rows only when a function may still raise row-dependent exceptions. I did not find a remaining concrete classifier bug in the current head, but the change still lacks focused regression evidence for the new contract, so I would not approve it yet.

Tests
  • ⚠️ canThrow now directly controls whether replicated nested columns are compacted before execution (src/Functions/IFunction.h:137-152, src/Functions/IFunction.cpp:705-713), but the added coverage is only CI log-grep instrumentation plus unrelated test adjustments (tests/docker_scripts/stress_tests.lib, src/Processors/QueryPlan/Optimizations/tests/gtest_convert_any_join.cpp, tests/queries/0_stateless/02813_series_period_detect.sql). Please add small deterministic regressions that run at least one representative canThrow = true function and one representative canThrow = false function through replicated-column execution with skipped nested rows, so a false classification fails locally without relying on fuzz or stress jobs.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: add focused regression coverage for the replicated-column canThrow contract.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 19, 2026

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 fixes a correctness issue in the query-plan optimization that lifts ARRAY JOIN by ensuring that expressions which may throw (e.g. intDiv, parsing/casting functions) are not reordered in a way that triggers exceptions on rows that would have been eliminated by empty arrays. It does so by introducing a canThrow() capability on functions and using it during ARRAY JOIN lift-up to either keep throwing expressions in place or insert a notEmpty-based filter before executing lifted throwing expressions.

Changes:

  • Add canThrow(DataTypesWithConstInfo) to function interfaces (IFunction/IFunctionBase) and forward it through adaptors.
  • Extend ActionsDAG with throwing-function detection and use it to control splitActionsBeforeArrayJoin, optionally inserting a filter node in the lifted plan.
  • Add a stateless regression test covering multiple throwing functions around ARRAY JOIN, including aligned/unaligned and multi-column cases.

Reviewed changes

Copilot reviewed 59 out of 59 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/queries/0_stateless/04041_array_join_with_throwing_function.sql New stateless test queries covering throwing functions with ARRAY JOIN lift-up scenarios.
tests/queries/0_stateless/04041_array_join_with_throwing_function.reference Expected outputs for the new stateless test.
src/Processors/QueryPlan/Optimizations/liftUpArrayJoin.cpp Insert notEmpty filter before lifted throwing expressions; avoid pushing throwing functions for aligned multi-column ARRAY JOIN.
src/Processors/QueryPlan/ArrayJoinStep.h Expose isUnaligned() for lift-up logic.
src/Interpreters/ExpressionActions.cpp Use ActionsDAG::Node::getArgumentTypesWithConstInfo() for short-circuit suitability checks.
src/Interpreters/ActionsDAG.h Add Node::getArgumentTypesWithConstInfo(), hasThrowingFunctions(), and extend splitActionsBeforeArrayJoin() signature.
src/Interpreters/ActionsDAG.cpp Implement argument const-info extraction, throwing-function detection, and throwing-aware array-join splitting.
src/Functions/widthBucket.cpp Mark function as throwing via canThrow().
src/Functions/vectorFunctions.cpp Propagate canThrow() through tuple/array wrapper function.
src/Functions/toFixedString.h Mark function as throwing via canThrow().
src/Functions/toDecimalString.cpp Mark function as throwing via canThrow().
src/Functions/throwIf.cpp Mark function as throwing via canThrow().
src/Functions/space.cpp Mark function as throwing via canThrow().
src/Functions/sleep.cpp Mark function as throwing via canThrow().
src/Functions/seriesDecomposeSTL.cpp Mark function as throwing via canThrow().
src/Functions/s2CellsIntersect.cpp Mark function as throwing via canThrow().
src/Functions/repeat.cpp Mark function as throwing via canThrow().
src/Functions/readWkt.cpp Mark functions as throwing via canThrow().
src/Functions/readWkb.cpp Mark functions as throwing via canThrow().
src/Functions/parseTimeDelta.cpp Mark function as throwing via canThrow().
src/Functions/parseReadableSize.cpp Make canThrow() depend on exception-mode behavior.
src/Functions/parseDateTime.cpp Mark function as throwing via canThrow().
src/Functions/padString.cpp Mark function as throwing via canThrow().
src/Functions/jsonMergePatch.cpp Mark function as throwing via canThrow().
src/Functions/isIPAddressInRange.cpp Mark function as throwing via canThrow().
src/Functions/h3kRing.cpp Mark function as throwing via canThrow().
src/Functions/geohashEncode.cpp Mark function as throwing via canThrow().
src/Functions/geoToH3.cpp Mark function as throwing via canThrow().
src/Functions/generateSerialID.cpp Mark function as throwing via canThrow().
src/Functions/convertCharset.cpp Mark function as throwing via canThrow().
src/Functions/conv.cpp Mark function as throwing via canThrow().
src/Functions/clamp.cpp Mark function as throwing via canThrow().
src/Functions/bech32.cpp Mark encode/decode functions as throwing via canThrow().
src/Functions/array/range.cpp Mark function as throwing via canThrow().
src/Functions/array/mapPopulateSeries.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayZip.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayWithConstant.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayShingles.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayShiftRotate.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayReduceInRanges.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayReduce.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayNormalizedGini.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayFold.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayEnumerateRanked.h Mark function as throwing via canThrow().
src/Functions/array/arrayDotProduct.cpp Mark function as throwing via canThrow().
src/Functions/array/arrayDistance.cpp Mark function as throwing via canThrow().
src/Functions/array/FunctionArrayMapped.h Mark function as throwing via canThrow().
src/Functions/ULIDStringToDateTime.cpp Mark function as throwing via canThrow().
src/Functions/TimeSeries/timeSeriesThrowDuplicateSeriesIf.cpp Mark function as throwing via canThrow().
src/Functions/IFunctionAdaptors.h Forward canThrow() from IFunction to IFunctionBase.
src/Functions/IFunction.h Introduce canThrow() API on function interfaces.
src/Functions/HMAC.cpp Mark function as throwing via canThrow().
src/Functions/FunctionsRound.h Implement canThrow() based on non-constant scale argument.
src/Functions/FunctionsConversion.h Add canThrow() to conversion/cast-related functions based on modes/argument types.
src/Functions/FunctionsCodingIP.cpp Make canThrow() depend on exception-mode behavior.
src/Functions/FunctionsAES.h Mark encrypt/decrypt as throwing via canThrow().
src/Functions/FunctionFile.cpp Mark function as throwing via canThrow().
src/Functions/FunctionBinaryArithmetic.h Add canThrow() for arithmetic operations (incl. intDiv/modulo/decimal division cases).
src/Functions/FunctionBaseXXConversion.h Add canThrow() derived from decode error-handling mode.

Comment thread src/Functions/FunctionsRound.h Resolved
Comment thread src/Functions/FunctionsRound.h Resolved
@m-selmi
m-selmi requested a review from Copilot March 23, 2026 20:27

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.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

@m-selmi

m-selmi commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor Author

This is the list of functions where canThrow differs from isSuitableForShortCircuitArgumentsExecution: https://pastila.clickhouse.com/?0077c8ab/7e782d5113d73415302ac6dd8b24e003.md#HIrxDWHqCCS8APYBRS7g5g==GCM. It was first AI generated then fixed a lot by hand.

@m-selmi
m-selmi marked this pull request as ready for review March 24, 2026 10:52
@m-selmi
m-selmi requested a review from KochetovNicolai March 24, 2026 10:52
Comment thread src/Functions/FunctionsStringSearch.h Resolved
Comment thread src/Functions/IFunction.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 91.90% -0.10%
Branches 78.40% 78.30% -0.10%

Changed lines: Changed C/C++ lines covered: 707/1434 (49.30%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Dear @Fgrtue, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@Fgrtue Fgrtue self-assigned this Aug 31, 2026
@abashkeev abashkeev added the comp-functions Built-in SQL function implementations + function infrastructure. label Sep 3, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand this PR. AFAICT every function can throw (memory, timeouts, etc), so it's "pointless"

@m-selmi

m-selmi commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I don't understand this PR. AFAICT every function can throw (memory, timeouts, etc), so it's "pointless"

Maybe canThrow is not the best name. The method is trying to answer the question of whether the function can throw for a specific row of data (col1 + col2 vs col1 / col2). Other reasons for throwing are not considered.

The idea was that this is useful to know if an optimization is safe or not. For example filter push down or array join lift up above an expression could make the function throw on a row that would have been filtered out: https://fiddle.clickhouse.com/4ee100ce-1c12-42e2-ba99-f6d2bd342e77.
Ofc if this leads to disabling an important optimization like filter pushdown for most functions then that's not great

@Fgrtue

Fgrtue commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

@Algunenano here is another example for the situation which @m-selmi describes:

#100827

@Algunenano

Copy link
Copy Markdown
Member

All these seem to come from a misunderstanding of SQL. SQL is a declarative language, the engine decides what and how to execute things. Then somebody goes and say functions are executed from left to right, from inner to outer, and everything breaks

@m-selmi

m-selmi commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

All these seem to come from a misunderstanding of SQL. SQL is a declarative language, the engine decides what and how to execute things. Then somebody goes and say functions are executed from left to right, from inner to outer, and everything breaks

Yes I agree with this. A point that is slightly problematic is that from a user perspective this is a behavior that can be toggled with a setting (that from first glance does not affect the result) and can break a query that used to work.

But as said, I would also be reluctant to do this if it would disable useful optimizations, and I also know that most systems accept this. So I would be fine with re-discussing or abandoning this. FYI there was a relevant discussion here.

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Dear @Fgrtue, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@Fgrtue Fgrtue self-assigned this Oct 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Dear @Fgrtue, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@Fgrtue Fgrtue self-assigned this Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-functions Built-in SQL function implementations + function infrastructure. pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants