Skip to content

Fix function result type mismatch with join_use_nulls - #95098

Closed
alexey-milovidov wants to merge 15 commits into
masterfrom
fix-join-use-nulls-function-result-type
Closed

alexey-milovidov wants to merge 15 commits into
masterfrom
fix-join-use-nulls-function-result-type

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jan 25, 2026 •

Copy link
Copy Markdown
Member

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."

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

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]>
@clickhouse-gh

clickhouse-gh Bot commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6a6be47]

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jan 25, 2026
alexey-milovidov and others added 5 commits January 25, 2026 20:40
…_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
alexey-milovidov marked this pull request as draft January 26, 2026 00:03
alexey-milovidov and others added 6 commits January 26, 2026 02:26
…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
alexey-milovidov requested review from KochetovNicolai and novikd and removed request for novikd January 26, 2026 04:46

@novikd novikd 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.

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

@novikd novikd self-assigned this Jan 26, 2026
alexey-milovidov and others added 2 commits January 26, 2026 19:47
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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]>
@alexey-milovidov

alexey-milovidov commented Jan 26, 2026 •

Copy link
Copy Markdown
Member Author

I don't like this approach, it looks like it can't solve the problem properly.

@vdimir

vdimir commented Feb 18, 2026 •

Copy link
Copy Markdown
Member

Seems issue got fixed with #95157

I'll add test from this PR in #97297

vdimir added a commit that referenced this pull request Feb 18, 2026
@vdimir
vdimir deleted the fix-join-use-nulls-function-result-type branch February 18, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants