Repository navigation
Non-const second argument support for IN - #77906
Conversation
|
Dear @novikd, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
KochetovNicolai
left a comment
There was a problem hiding this comment.
This is correct
SELECT
NULL AS x,
[if(number > 1, NULL, number)] AS arr,
tuple(arr[1]) AS t,
x IN (t),
x IN (arr)
FROM numbers(3)
SETTINGS transform_null_in = 0
Query id: a77a6773-908f-43cb-8085-79d59eaad82d
┌─x────┬─arr────┬─t──────┬─in───┬─in───┐
1. │ ᴺᵁᴸᴸ │ [0] │ (0) │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
2. │ ᴺᵁᴸᴸ │ [1] │ (1) │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
3. │ ᴺᵁᴸᴸ │ [NULL] │ (NULL) │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
└──────┴────────┴────────┴──────┴──────┘
but this is not when x is Null
:) select arrayJoin([0, 1, Null]) as x, [number > 1 ? Null : number] as arr, tuple(arrayElement(arr, 1)) as t, x in t, x in arr from numbers(3) settings transform_null_in=0
SELECT
arrayJoin([0, 1, NULL]) AS x,
[if(number > 1, NULL, number)] AS arr,
tuple(arr[1]) AS t,
x IN (t),
x IN (arr)
FROM numbers(3)
SETTINGS transform_null_in = 0
Query id: f806d470-b5bc-4a0b-9ebf-7e67161394bb
┌────x─┬─arr────┬─t──────┬─in(x, t)─┬─in(x, arr)─┐
1. │ 0 │ [0] │ (0) │ 1 │ 1 │
2. │ 1 │ [0] │ (0) │ 0 │ 0 │
3. │ ᴺᵁᴸᴸ │ [0] │ (0) │ 0 │ 0 │
4. │ 0 │ [1] │ (1) │ 0 │ 0 │
5. │ 1 │ [1] │ (1) │ 1 │ 1 │
6. │ ᴺᵁᴸᴸ │ [1] │ (1) │ 0 │ 0 │
7. │ 0 │ [NULL] │ (NULL) │ 0 │ 0 │
8. │ 1 │ [NULL] │ (NULL) │ 0 │ 0 │
9. │ ᴺᵁᴸᴸ │ [NULL] │ (NULL) │ 1 │ 1 │
└──────┴────────┴────────┴──────────┴────────────┘
This should not throw
:) select 0 as x, [number > 1 ? Null : number] as arr, tuple(arrayElement(arr, 1)) as t, x in t, x in arr from numbers(3) settings transform_null_in=
SELECT
0 AS x,
[if(number > 1, NULL, number)] AS arr,
tuple(arr[1]) AS t,
x IN (t),
x IN (arr)
FROM numbers(3)
SETTINGS transform_null_in = 0
Query id: e955e6a3-f2d6-42bc-afb6-97653fd70582
Elapsed: 0.004 sec.
Received exception from server (version 25.6.1):
Code: 349. DB::Exception: Received from localhost:9000. DB::Exception: Cannot convert NULL value to non-Nullable type: while executing 'FUNCTION CAST(arrayElement(array(if(greater(__table1.number, 1_UInt8), _CAST(NULL_Nullable(Nothing), 'Nullable(Nothing)'_String), __table1.number)), 1_UInt8) :: 7, 'UInt8'_String :: 4) -> CAST(arrayElement(array(if(greater(__table1.number, 1_UInt8), _CAST(NULL_Nullable(Nothing), 'Nullable(Nothing)'_String), __table1.number)), 1_UInt8), 'UInt8'_String) UInt8 : 0'. (CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN)
(because e.g. select 0 in [Null] and select 0 in (Null) does not.
|
Workflow [PR], commit [6617096] Summary: ❌
|
|
Dear @KochetovNicolai, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
| if (left_is_null) | ||
| { | ||
| // left operand is NULL - infer type from tuple elements | ||
| DataTypes element_types; | ||
| for (const auto & arg : tuple_args) | ||
| if (!arg->getResultType()->onlyNull()) | ||
| element_types.push_back(arg->getResultType()); | ||
|
|
||
| if (element_types.empty()) | ||
| { | ||
| common_type = std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>()); | ||
| } | ||
| else | ||
| { | ||
| common_type = tryGetLeastSupertype(element_types); | ||
| if (!common_type) | ||
| common_type = std::make_shared<DataTypeString>(); | ||
|
|
||
| if (!common_type->isNullable()) | ||
| common_type = makeNullable(common_type); | ||
| } |
There was a problem hiding this comment.
Not sure we need to handle a supertype here.
If I am correct, this is the case Null in (a, b, c) with transform_null_in = 0. If so, the result should always be Null.
SELECT NULL IN (1, NULL, 2)
SETTINGS transform_null_in = 0
┌─in(NULL, (1, NULL, 2))─┐
1. │ ᴺᵁᴸᴸ │
└────────────────────────┘
| } | ||
|
|
||
| /// Checks if node is a NULL constant | ||
| bool QueryAnalyzer::isNullConstant(const QueryTreeNodePtr & node) |
There was a problem hiding this comment.
Perhaps we should also move this code to resolveFunction?
QueryAnalyzer.cpp is big already, and these functions look independent.
|
Hi @yariks5s @KochetovNicolai — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Allows the use of non-constant second arguments for
IN. It also supports a tuple as a second argument.Documentation entry for user-facing changes
Details
Follow-up #65398