Repository navigation
Conversation
`convertFieldToTypeImpl` widens a `DateTime` constant to a `DateTime64` column type but had no branch for the other direction: the `DateTime` case accepted only `UInt64`, and `WhichDataType(DateTime).isUInt32()` is false so the numeric dispatch did not pick it up either. A `DecimalField<DateTime64>` aimed at `DataTypeDateTime` therefore fell through every branch to the `TYPE_MISMATCH` at the end of the function. `LogicalExpressionOptimizerPass::tryConvertToColumnType` round-trips a constant through the column's type to check that widening it lost nothing, so for a `DateTime64` column compared against a `DateTime` constant the reverse leg could only ever fail - and it failed by throwing. `tryConvertFieldToType` swallows the exception and the query is unaffected, but the count has already been recorded, because `system.errors` is incremented in the `Exception` constructor. A `SELECT` matching that shape drove `TYPE_MISMATCH` up on every execution, resettable only by restarting the server. Add the missing branch. Under `strict` a sub-second part makes the value unconvertible, which is exactly the lossiness the round trip is checking for, so the guard now answers without an exception and the folding applies to `DateTime64` columns instead of always bailing out. The round-trip guard arrived in 26.7 with 5e12963 (ClickHouse#99736), which is why the counter starts moving there. Closes: ClickHouse#117903
|
Workflow [PR], commit [8894e71] Summary: ❌
AI ReviewSummaryThis PR closes the original Findings
Tests
Final VerdictChanges requested: the recursive hint propagation is still incomplete for container elements whose semantic source type is hidden behind LLVM Coverage ReportMeasured on commit 8894e71.
Changed lines: Changed C/C++ lines covered: 56/61 (91.80%) · Uncovered code |
|
The new branch fixes the loud if (strict && DecimalUtils::getFractionalPart(from_value.getValue(), from_scale) != 0)
return {};The file's convention (see the Concrete consumer: Suggested fix: reject a non-zero fractional part unconditionally (drop the One adjacent hardening: the branch keys on the field type |
…urce type Two problems with the branch as first written, both raised in review. It gated the sub-second rejection on `strict`, which made it the only non-strict lossy conversion toward an integer-domain type in this file. Non-strict callers push the converted constant down as an exact comparison bound: `StorageMongoDB::visitWhereFunctionArguments` builds a BSON filter from it, and its own comment says the conversion exists because MongoDB cannot cast implicitly and the `WHERE` result would otherwise be wrong. Truncating a sub-second constant there turns `!=` or `<` at the boundary into rows that are never fetched, and the local re-filter can only drop rows, not restore them. Today that path fails loudly instead. Reject a non-zero fractional part unconditionally; the motivating caller passes `strict = true` and is unaffected. It also keyed on the `Field` tag alone. `Time64` is carried as `DecimalField<Decimal64>` too but counts seconds of day, so a `Time64` constant aimed at a `DateTime` column would have been reinterpreted as an epoch. Key both narrowings on the source type via `which_from_type`. Add the symmetric `Time64` -> `Time` branch while here. `tryConvertToColumnType` round-trips a `Time` constant through a `Time64` column exactly as it does for `DateTime`, and `enable_time_time64_type` defaults to true, so that leg was reachable as well - the earlier claim that nothing exercised it was wrong.
|
Thank you - both of these were real and I have fixed them in d416ab1. The The And the Worth noting for anyone reading later: the two failure modes you found are opposite in kind. The one this PR started with was noisy but harmless - a counter moving on a correct query. The one you caught would have been silent and wrong. Better to have found it here. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
`convertFieldToType` unwraps `to_type` but passes the source type exactly as the caller declared it, so a constant typed `Nullable(DateTime64)` arrives wrapped. Every test in `convertFieldToTypeImpl` reads that hint through `WhichDataType`, so all of them read false and the conversion falls through to the `TYPE_MISMATCH` this function was just changed to avoid - reappearing for a query that differs only in naming a wrapped type. `StorageMongoDB::visitWhereFunctionArguments` (`StorageMongoDB.cpp:392`) passes the literal's own result type, which is where this is reachable. Unwrap the pointer rather than only the `WhichDataType`. Several of the branches these tests guard go on to `static_cast<const DataTypeDateTime &>(*from_type_hint)` to read a time zone, so correcting the test alone would convert an inert branch into an invalid cast. A null source is rejected by `convertFieldToType` before this point, so the value carried under a `Nullable` hint is always a real one. This also subsumes the identical loop the enum-to-string branch was already running on its own, which is now removed. Tested in `gtest_convertFieldToType` rather than the stateless suite: the only caller that can produce a wrapped hint is the MongoDB predicate pushdown, which needs a server. Both cases fail against the unpatched function and pass with it; the surrounding `convertFieldToType` and `convertColumnToType` suites stay at 44/44.
The `from_type_hint` handling stopped at the top level: the `Array`, `Tuple` and `Map` branches of `convertFieldToTypeImpl` recursed with no hint, so each element was converted as a bare field with no semantic type. `StorageMongoDB` converts the constants of `$in` / `$nin` with the literal's type as the hint, and an `IN` list arrives as `Array(DateTime64(3))`; its elements then missed the `DateTime64` -> `DateTime` branch and threw `TYPE_MISMATCH`, which `visitWhereFunctionArguments` does not catch, so the query failed instead of being pushed down. `getElementTypeHint` yields the source type of one element - the nested type of an `Array` hint, or element `i` of a `Tuple` hint, since an `IN` list is a tuple literal converted to the set's array type - and the `Map` branch takes the hint's key and value types. The element hint is passed as written; the recursive call unwraps `Nullable` and `LowCardinality` itself. The gtest covers `Array`, `Tuple` and `Map` with a `DateTime64(3)` element hint into `DateTime`; the MongoDB integration test adds the `IN` / `NOT IN` shape against a `DateTime` column.
The test pinned the old number-instead-of-name value for an `Enum` element inside an `Array` as a documented limitation, waiting for the element type hint to be passed down the container recursion. That is what the previous commit does, so the cell now checks the name. Fast test report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117977&sha=28962baa5eda5099bea107aa02ea24ad0857e531&name_0=PR&name_1=Fast%20test PR: ClickHouse#117977
| /// tuple literal converted to the set's array type, so a `Tuple` source is paired with an `Array` | ||
| /// target as well. Null when the source type is not a container of a known shape, in which case | ||
| /// the element is converted with no hint, as before. | ||
| const IDataType * getElementTypeHint(const IDataType * container_hint, size_t i) |
There was a problem hiding this comment.
getElementTypeHint now preserves plain Array / Tuple / Map element declarations, but it still does not recover carrier-hidden element types. A Field loses the active Variant / Dynamic alternative, so an element from Array(Variant(DateTime64(3), String)) or Tuple(Variant(Enum8('7' = 3))) still reaches the recursive call with from_type_hint = Variant(...), not the active DateTime64 / Enum8 type.
That means the new which_from_type.isDateTime64() and enum-name branches are skipped again for semantically equivalent wrapped elements: values('x Array(DateTime)', [CAST(toDateTime64('2020-01-01 00:00:00', 3), 'Variant(DateTime64(3), String)')]) still falls through to TYPE_MISMATCH, and Array(Variant(Enum8('7' = 3))) -> Array(String) still serializes '3' instead of '7'. Please resolve the active alternative per element before recursing (analogous to resolveActiveAlternativeType for scalar Variant / Dynamic constants), or reject these carriers explicitly and cover that contract with a focused test.
|
Closing to rework and resubmit. |
Closes: #117903
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
TYPE_MISMATCHbeing counted insystem.errorson every execution of aSELECTcomparing aDateTime64column against aDateTimeconstant inside anAND.Documentation entry for user-facing changes
convertFieldToTypeImplwidens aDateTimeconstant to aDateTime64column type (convertFieldToType.cpp:501) but had no branch for the other direction. TheDateTimecase accepts onlyUInt64(:399), andWhichDataType(DateTime).isUInt32()is false so the numeric dispatch does not pick it up either, so aDecimalField<DateTime64>aimed atDataTypeDateTimefell through every branch to theTYPE_MISMATCHat:958.LogicalExpressionOptimizerPass::tryConvertToColumnTyperound-trips a constant through the column's type to check that widening it lost nothing:For a
DateTime64column compared against aDateTimeconstant the reverse leg could only ever fail, and it failed by throwing.tryConvertFieldToTypeswallows the exception, so results and granule pruning are unaffected - but the count is already recorded, becausesystem.errorsis incremented in theExceptionconstructor. A query of that shape droveTYPE_MISMATCHup on every execution, resettable only by restarting the server:Adding the branch also lets the folding apply to
DateTime64columns, where it previously bailed out every time.On the reported reproduction conditions. The issue attributes this to MergeTree key analysis and to the
ORDER BYplacing theDateTime64column after a prefix column. Neither holds:KeyCondition::extractAtomFromTreeshort-circuits this type pair before any conversion (KeyCondition.cpp:4080), and that path converts to the supertype so its message would readExpected: DateTime64(9). The pass that throws runs on the query tree and cannot see the sorting key - what is actually required is anANDplus the type mismatch.Why 26.7. The round-trip guard came in with 5e12963 ("Fix optimize_redundant_comparisons folding on truncated DateTime64 constants", #99736), which is in
26.7and not in26.6. It was added to fix genuinely wrong folding on lossy constants; attemptingDateTime64 -> DateTimewas incidental.Why not suppress the counter.
Exception::SuppressErrorCodesScopeexists for handled exceptions and #111845 is rolling it out broadly, but it does not touch either file involved here, and using it would hide a conversion gap worth closing on its own merits.Time64 -> Timelooks like the same gap; I have left it out of this change since nothing exercises it yet.Workflow [PR]
Sync PR [sync-upstream/pr/117977]