Repository navigation
Fix function result type mismatch with join_use_nulls - #95098
Closed
alexey-milovidov wants to merge 15 commits into
Closed
alexey-milovidov wants to merge 15 commits into
alexey-milovidov wants to merge 15 commits into
Conversation
When `join_use_nulls` is enabled, columns from the non-preserved side of a JOIN become `Nullable`. However, functions using these columns were resolved during query analysis before the nullability change, resulting in incorrect result types. For example, `concat(t2.name, '_suffix')` where `t2.name` becomes `Nullable(String)` due to a LEFT JOIN with `join_use_nulls=1`: - Analysis resolves `concat` with `String` argument, result type `String` - Planner makes `t2.name` nullable after JOIN processing - At execution, `defaultImplementationForNulls` returns `Nullable(String)` - Type mismatch: expected `String`, got `Nullable(String)` The fix detects when argument types in the ActionsDAG differ from the original function's argument types (specifically, when they became nullable), and wraps the result type in `Nullable` to match what the function execution will produce. This fixes the fuzzer error: "Unexpected return type from concat. Expected Nullable(String). Got String." Co-Authored-By: Claude Opus 4.5 <[email protected]>
Contributor
…_compatibility_join_using_top_level_identifier` This fixes the "Unexpected return type from concat. Expected Nullable(String). Got String." error that occurred with the following scenario: - `join_use_nulls = 1` - `analyzer_compatibility_join_using_top_level_identifier = 1` - Using an aliased expression (like `concat(t1.id, '_1') AS id`) in USING clause The issue was that `FunctionNode::wrap_with_nullable` was set during query analysis, causing `getResultType()` to return Nullable(String), but the actual DAG arguments were non-nullable. When the function executed with non-nullable arguments, it returned String, causing a type mismatch. The fix handles two cases in `ActionsDAG::addFunction`: 1. When arguments become nullable after JOIN, wrap the result in Nullable 2. When result type is nullable but no arguments are nullable, remove the Nullable wrapper Co-Authored-By: Claude Opus 4.5 <[email protected]>
The previous fix for function result type mismatch with `join_use_nulls` unconditionally modified the result type when arguments became nullable. However, functions like `CAST` have `useDefaultImplementationForNulls() = false` and handle nullable types themselves - modifying their result type caused a crash (NULL pointer dereference in `prepareRemoveNullable`). The fix now checks `useDefaultImplementationForNulls()` by trying to access the underlying `IFunction` via `FunctionToFunctionBaseAdaptor`. For complex functions (like CAST) that implement `IFunctionBase` directly without the adaptor pattern, we assume they handle nullable types correctly and don't modify their result type. Co-Authored-By: Claude Opus 4.5 <[email protected]>
…herently nullable results The previous fix for removing Nullable wrappers (Case 2) was too aggressive. It removed Nullable based only on top-level argument nullability, but functions like `arrayElement` return nullable based on nested types (array element type), not just top-level argument nullability. For example, `arrayElement(Array(Nullable(UInt8)), UInt64)` returns `Nullable(UInt8)` because the array elements are nullable, but: - `Array(Nullable(UInt8))` is not nullable at the top level - `UInt64` is not nullable - So Case 2 would incorrectly remove the Nullable wrapper The fix adds a check for `function_base->getResultType()->isNullable()`. If the function's actual result type is nullable, we don't remove the wrapper. Co-Authored-By: Claude Opus 4.5 <[email protected]>
alexey-milovidov
marked this pull request as draft
January 26, 2026 00:03
…ullability The previous fix checked `function_base->getResultType()->isNullable()`, but this doesn't work correctly because the function_base was built during query analysis with potentially nullable arguments, so its stored result type already reflects that (e.g., `Nullable(String)` for `concat` with nullable args). Instead, we now call `getReturnTypeImpl` on the underlying `IFunction` with non-nullable argument types to determine if the function would return nullable based on the actual argument types (not the analysis-time types). This correctly handles: - `arrayElement(Array(Nullable(T)), idx)` - returns `Nullable(T)` based on nested type, result type should remain nullable - `concat(String, String)` - returns `String`, result type should have nullable wrapper removed when actual args are non-nullable Co-Authored-By: Claude Opus 4.5 <[email protected]>
…lt-type' into fix-join-use-nulls-function-result-type
Some functions like `parseDateTimeBestEffortOrNull` only implement `getReturnTypeImpl(const ColumnsWithTypeAndName &)` and not the `getReturnTypeImpl(const DataTypes &)` overload. The previous code was calling the DataTypes overload which would hit the default implementation that throws "getReturnType is not implemented". Changed to create ColumnsWithTypeAndName from the arguments and call the correct overload. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Remove Case 2 (removing Nullable wrapper) as it required calling getReturnTypeImpl directly, which doesn't properly handle LowCardinality and other type wrappers. Functions like arrayElement were failing with "Second argument must be integer, got LowCardinality". The core fix (making result nullable when arguments become nullable due to join_use_nulls) is preserved. Removing Case 2 means we may keep Nullable wrappers that aren't strictly necessary, but this is safe behavior. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Instead of calling getReturnTypeImpl directly (which doesn't handle LowCardinality and other type wrappers properly), we now compare function_base->getResultType() with the expected result_type. If result_type is nullable but function_base->getResultType() is not, it means wrap_with_nullable was applied externally. In this case, if no current arguments are nullable, we remove the nullable wrapper. This approach correctly handles functions like arrayElement that may return nullable based on nested types - their function_base result type will already be nullable, so we preserve it. Co-Authored-By: Claude Opus 4.5 <[email protected]>
alexey-milovidov
requested review from
KochetovNicolai and
novikd
and removed request for
novikd
January 26, 2026 04:46
novikd
reviewed
Jan 26, 2026
novikd
left a comment
Member
There was a problem hiding this comment.
QueryAnalyzer is supposed to produce expressions in the query tree with correct types. It is done by src/Analyzer/Resolve/ReplaceColumnsVisitor.h. I suspect it does not work correctly with analyzer_compatibility_join_using_top_level_identifier enabled.
We should fix it in the analyzer.
cc @vdimir
When `join_use_nulls = 1` and `analyzer_compatibility_join_using_top_level_identifier = 1`, the analyzer would resolve a `FunctionNode` with Nullable argument types (due to join column type conversion), but when the `ActionsDAG` was built, the actual input columns had non-Nullable types. This caused a type mismatch error: "Unexpected return type from concat. Expected Nullable(String). Got String." The fix rebuilds the function in `ActionsDAG::addFunction` using the actual argument types from the children nodes, rather than using the pre-built function from the `FunctionNode` which may have been resolved with different argument types. Co-Authored-By: Claude Opus 4.5 <[email protected]>
…lt-type' into fix-join-use-nulls-function-result-type
| { | ||
| auto [arguments, all_const] = getFunctionArguments(children); | ||
|
|
||
| /// The FunctionNode may have been resolved with different argument types than the actual |
Member
Author
There was a problem hiding this comment.
Highly doubtful, again.
…tionFactory Some internal functions like `groupingOrdinary` are not registered in FunctionFactory. When trying to rebuild these functions, `tryGet` returns nullptr. In such cases, fall back to using the original function from the FunctionNode. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Member
Author
|
I don't like this approach, it looks like it can't solve the problem properly. |
This was referenced Feb 11, 2026
Member
vdimir
added a commit
that referenced
this pull request
Feb 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
join_use_nullsis enabled, columns from the non-preserved side of a JOIN becomeNullable. However, functions using these columns were resolved during query analysis before the nullability change, resulting in incorrect result types.For example,
concat(t2.name, '_suffix')wheret2.namebecomesNullable(String)due to a LEFT JOIN withjoin_use_nulls=1:concatwithStringargument, result typeStringt2.namenullable after JOIN processingdefaultImplementationForNullsreturnsNullable(String)String, gotNullable(String)The fix detects when argument types in the ActionsDAG differ from the original function's argument types (specifically, when they became nullable), and wraps the result type in
Nullableto match what the function execution will produce.This fixes the fuzzer error:
"Unexpected return type from concat. Expected Nullable(String). Got String."
Changelog category (leave one):
See https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=95078&sha=90a42d54dafe000546a3ebd79df554a9ce97e3f8&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan%29
#95078