Repository navigation
Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN - #102156
Michicosun merged 10 commits into
Conversation
|
Workflow [PR], commit [3fe6a7d] Summary: ❌
AI ReviewSummaryThis PR rewrites the Findings❌ Blockers
Final VerdictStatus: |
Remarks on CI FailuresAll CI failures are pre-existing timeouts on ARM ASAN/UBSAN builds (sanitizer overhead causes 300s/10min query timeouts) and a known BuzzHouse DDL worker assertion. None involve nullable conversion or files touched by this PR. Our test (03575_modify_column_null_to_default_cross_type) passed across all configurations. |
|
@il9ue, the issues is that the test |
|
Valid point, thanks! I will update the test to set use_variant_as_common_type = 0, which reproduces the underlying issue in convertRequiredExpressions() even on master. This would also demonstrate why the fix matters for older branches (24.8, 25.8) where Variant support isn't the default.
|
PR ClickHouse#84770 generates _CAST(ifNull(col, default), TargetType). When source and target base types differ (e.g. Nullable(UInt8) -> String), ifNull throws NO_COMMON_TYPE because getLeastSupertype({UInt8, String}) fails. Fix: swap to ifNull(_CAST(col, Nullable(TargetType)), _CAST(default, TargetType)). This works independently of use_variant_as_common_type and handles both same-type and cross-type nullable conversions correctly. Signed-off-by: Daniel Q <[email protected]>
d11480a to
4f7386c
Compare
|
Dear @alesapin, 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. |
…OLUMN Address review on ClickHouse#102156. The previous fix built the intermediate cast target by string-concatenating "Nullable(" + type->getName() + ")". For a LowCardinality(T) target this produced Nullable(LowCardinality(T)), which is invalid (LowCardinality cannot be nested inside Nullable), so _CAST threw before ifNull could replace NULLs. Use makeNullableOrLowCardinalityNullable(required_column.type)->getName() instead, which yields LowCardinality(Nullable(T)) for LowCardinality targets and Nullable(T) otherwise, and is a no-op for already-nullable targets. The constructed expression is now ifNull(_CAST(col, 'Nullable(T)'), _CAST(default, 'T')), documented inline in place of the prose comment. Extend the stateless test with a Nullable(UInt8) -> LowCardinality(String) case, and set use_variant_as_common_type = 0 so it reproduces the NO_COMMON_TYPE failure on master (otherwise Variant resolution via ClickHouse#96790 masks the bug and bugfix-validation passes without the fix).
|
Daniel Q. Kim seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
…s-type-modify-upstream
| auto cast_value = makeASTFunction("_CAST", | ||
| makeASTFunction("assumeNotNull", make_intrusive<ASTIdentifier>(required_column.name)), | ||
| make_intrusive<ASTLiteral>(required_column.type->getName())); | ||
| convert_func = makeASTFunction("if", std::move(is_null), std::move(cast_default), std::move(cast_value)); |
There was a problem hiding this comment.
This still makes valid Nullable(X) -> non-Nullable-able target conversions depend on the caller's short_circuit_function_evaluation setting. performRequiredConversions builds ExpressionActions from the query/read context, and when the setting is disable the third argument is evaluated before if, so a NULL row in the new test shape executes _CAST(assumeNotNull(x), 'Array(UInt8)') on the nested placeholder ('') and throws before the default branch can be selected.
That means SET short_circuit_function_evaluation = 'disable'; ALTER TABLE ... MODIFY COLUMN x Array(UInt8) DEFAULT [] still fails, and reads of old parts with a pending MODIFY COLUMN can fail the same way until the mutation materializes. Please make this internal conversion independent of the user setting, for example by forcing lazy execution for this generated expression or by using an eager-safe expression, and add a regression with short_circuit_function_evaluation = 'disable'.
8b216a7 to
14278f5
Compare
14278f5 to
76fab57
Compare
…column type Follow-up to ClickHouse#84770 / the LowCardinality revision. convertRequiredExpressions builds the null-filling conversion and now pins the result to exactly required_column.type on both branches: - Nullable-able targets (String, LowCardinality, numerics): _CAST(ifNull(_CAST(col, 'LowCardinality(Nullable(T))'), _CAST(default, 'T')), 'T') The inner ifNull already resolves to the non-nullable target because the default arg is non-nullable; the outer _CAST pins the type explicitly so the generated column does not depend on ifNull's supertype resolution, and keeps this branch symmetric with the Array branch below. - Targets that cannot be nested in Nullable (Array/Tuple/Map/...): _CAST(ifNull(col, _CAST(default, 'X')), 'T') (X = source base type) NULL rows are replaced by the default re-encoded in the source type before any cast to T, so the placeholder is never parsed; no branch to short-circuit, so independent of short_circuit_function_evaluation. Test 03575 covers scalar String, LowCardinality(String), and Array(UInt8) targets, each with a NULL source row taking the DEFAULT, materialized with OPTIMIZE TABLE ... FINAL to exercise the part-writer path, plus an Array case under short_circuit_function_evaluation = 'disable'. Reproduces on upstream/master with NO_COMMON_TYPE when use_variant_as_common_type = 0. Signed-off-by: Daniel Q <[email protected]>
76fab57 to
7d10fe4
Compare
…ple/Map) Follow-up to ClickHouse#84770 / the LowCardinality revision. `convertRequiredExpressions` builds the null-filling conversion and pins the result to exactly `required_column.type` on both branches: - Nullable-able targets (`String`, `LowCardinality`, numerics): `_CAST(ifNull(_CAST(col, 'LowCardinality(Nullable(T))'), _CAST(default, 'T')), 'T')` The inner `ifNull` already resolves to the non-nullable target because its default arg is non-nullable; the outer `_CAST` pins the type explicitly so the generated column does not depend on `ifNull`'s supertype resolution, and keeps this branch symmetric with the else branch below. - Targets that cannot be nested in `Nullable` (`Array`/`Tuple`/`Map`/...): `if(isNull(col), _CAST(default, 'T'), _CAST(assumeNotNull(col), 'T'))` The default now stays on the TARGET side. The previous construction re-encoded the default in the source base type, `_CAST(ifNull(col, _CAST(default, 'X')), 'T')` (X = source base type), which threw `TYPE_MISMATCH` whenever the target default is not castable back to the source type. For example, a `Map` target with a `map(...)` default over a `Nullable(Tuple(...))` source built `_CAST(map('a', 1), 'Tuple(...)')`, and `CAST AS Tuple` rejects `Map` input. Keeping the default in the target type avoids the source round-trip entirely. The value branch `_CAST(assumeNotNull(col), 'T')` would throw on NULL rows if it parsed the `assumeNotNull` placeholder, but `performRequiredConversions` runs the expression through `ExpressionActions` that lazily evaluate short-circuit functions regardless of `short_circuit_function_evaluation`, so the branch is only evaluated for non-NULL rows. Measured: a plain top-level `if` over the same placeholder throws under `short_circuit_function_evaluation = 'disable'`, while this conversion path does not. Test 03575 covers scalar `String`, `LowCardinality(String)`, `Array(UInt8)`, `Tuple`, and `Map` targets, each with a NULL source row taking the `DEFAULT`, materialized with `OPTIMIZE TABLE ... FINAL` to exercise the part-writer path, plus an `Array` case under `short_circuit_function_evaluation = 'disable'`. The scalar cases reproduce on upstream/master with `NO_COMMON_TYPE` when `use_variant_as_common_type = 0`; the new `Map`-over-`Tuple` regression (Case 9) reproduces with `TYPE_MISMATCH` on the previous source-round-trip else branch. Signed-off-by: Daniel Q <[email protected]>
regression checkThe arm_tsan stress hung-check failure is unrelated to this PR. |
| /// if(isNull(col), _CAST(default, 'T'), _CAST(assumeNotNull(col), 'T')) | ||
| /// Default stays target-side (no source round-trip). Value branch runs only on | ||
| /// non-NULL rows: this conversion path evaluates short-circuit funcs lazily. | ||
| auto is_null = makeASTFunction("isNull", make_intrusive<ASTIdentifier>(required_column.name)); | ||
| auto cast_default = makeASTFunction("_CAST", default_value, | ||
| make_intrusive<ASTLiteral>(required_column.type->getName())); | ||
| auto cast_value = makeASTFunction("_CAST", | ||
| makeASTFunction("assumeNotNull", make_intrusive<ASTIdentifier>(required_column.name)), | ||
| make_intrusive<ASTLiteral>(required_column.type->getName())); | ||
| convert_func = makeASTFunction("if", std::move(is_null), std::move(cast_default), std::move(cast_value)); |
There was a problem hiding this comment.
I suppose we can leave only this branch, as it is generic?
There was a problem hiding this comment.
Double-checked at the local with the testcase 2, because that one represents the case above. Removing the if-branch and running Nullable(UInt8) -> LowCardinality(String) DEFAULT 'def',
./build/programs/clickhouse local --query "
SET use_variant_as_common_type = 0;
CREATE TABLE lc (x Nullable(UInt8), y String) ENGINE=MergeTree ORDER BY tuple();
INSERT INTO lc VALUES (NULL,'a'),(7,'b');
ALTER TABLE lc MODIFY COLUMN x LowCardinality(String) DEFAULT 'def';
OPTIMIZE TABLE lc FINAL;
SELECT toTypeName(x), x, y FROM lc ORDER BY y;
"
and had a exception as below,
Code: 341. DB::Exception: Exception happened during execution of mutation 'mutation_2.txt' with part 'all_1_1_0' reason: 'Code: 49. DB::Exception: Bad cast from type DB::ColumnString to DB::ColumnLowCardinality. (LOGICAL_ERROR) (version 26.7.1.1)'. This error maybe retryable or not. In case of unretryable error, mutation can be killed with KILL MUTATION query
As far as I understood, the generic if(...) builds a plain ColumnString and just re-labels it LowCardinality(String), and it never builds the dictionary column. So at part-write the mutation fails casting ColumnString into ColumnLowCardinality.
The if-branch's _CAST(col, 'LowCardinality(Nullable(T))') is what actually builds the dictionary, so the branches aren't redundant for LowCardinality.
@Michicosun Please correct me if I'm mistaken!
There was a problem hiding this comment.
I think this will work
/// _CAST(if(isNull(col), _CAST(default, 'T'), _CAST(assumeNotNull(col), 'T')), 'T')
auto is_null = makeASTFunction("isNull", make_intrusive<ASTIdentifier>(required_column.name));
auto cast_default = makeASTFunction("_CAST", default_value, make_intrusive<ASTLiteral>(required_column.type->getName()));
auto cast_value = makeASTFunction("_CAST", makeASTFunction("assumeNotNull", make_intrusive<ASTIdentifier>(required_column.name)), make_intrusive<ASTLiteral>(required_column.type->getName()));
auto filled = makeASTFunction("if", std::move(is_null), std::move(cast_default), std::move(cast_value));
auto convert_func = makeASTFunction("_CAST", std::move(filled), make_intrusive<ASTLiteral>(required_column.type->getName()));
conversion_expr_list->children.emplace_back(setAlias(convert_func, required_column.name));
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
cceaa01
…OLUMN Address review on ClickHouse#102156. The previous fix built the intermediate cast target by string-concatenating "Nullable(" + type->getName() + ")". For a LowCardinality(T) target this produced Nullable(LowCardinality(T)), which is invalid (LowCardinality cannot be nested inside Nullable), so _CAST threw before ifNull could replace NULLs. Use makeNullableOrLowCardinalityNullable(required_column.type)->getName() instead, which yields LowCardinality(Nullable(T)) for LowCardinality targets and Nullable(T) otherwise, and is a no-op for already-nullable targets. The constructed expression is now ifNull(_CAST(col, 'Nullable(T)'), _CAST(default, 'T')), documented inline in place of the prose comment. Extend the stateless test with a Nullable(UInt8) -> LowCardinality(String) case, and set use_variant_as_common_type = 0 so it reproduces the NO_COMMON_TYPE failure on master (otherwise Variant resolution via ClickHouse#96790 masks the bug and bugfix-validation passes without the fix).
…pe-modify-upstream Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
24.8.14 Backport of ClickHouse#102156: Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Antalya 25.8 Backport of ClickHouse#102156 - Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Stable 25.8 Backport of ClickHouse#102156 - Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Stable 26.3 Backport of ClickHouse#102156 - Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Antalya 26.6 Backport of ClickHouse#102156 - Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Antalya 26.3 Backport of ClickHouse#102156 - Fix Nullable(X)->Y cross-type ALTER MODIFY COLUMN
Changelog category
Changelog entry
Fixed
ALTER MODIFY COLUMNfailing when converting aNullablecolumn to a different type with aDEFAULT(e.g.Nullable(UInt8)→String,LowCardinality(String), orArray(UInt8)).Details
Follow-up to #84770.
convertRequiredExpressionsnow branches on whether the target type can be nested inNullable:ifNull(_CAST(col, 'Nullable(T)'), _CAST(default, 'T'))— NULLs pass through the cast unparsed, so this path is exception-safe regardless of settings.Nullable(T)exists, so it branches per row:if(isNull(col), _CAST(default, 'T'), _CAST(assumeNotNull(col), 'T')).Behavior note: the second form relies on short-circuit evaluation. On a NULL row
assumeNotNull(col)exposes the placeholder (e.g.'') and_CASTof it can throw; withshort_circuit_function_evaluationenabled (the default) the value branch is skipped for NULL rows. The test pins this and tagsno-random-settings.The masking by
use_variant_as_common_type(#96790) is why the regression test sets it to0to reproduce on master.