Skip to content

Non-const second argument support for IN - #77906

Merged
yariks5s merged 24 commits into
ClickHouse:masterfrom
yariks5s:non-constant-in-v2
Dec 1, 2025
Merged

yariks5s merged 24 commits into
ClickHouse:masterfrom
yariks5s:non-constant-in-v2

Conversation

@yariks5s

@yariks5s yariks5s commented Mar 19, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

  • Documentation is written (mandatory for new features)

Details

Follow-up #65398

@yariks5s
yariks5s marked this pull request as ready for review March 19, 2025 12:23
@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [750fd18]

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 19, 2025
@novikd novikd self-assigned this Mar 19, 2025
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2025

Copy link
Copy Markdown
Contributor

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 KochetovNicolai self-assigned this May 8, 2025
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
@yariks5s yariks5s added the do not test disable testing on pull request label May 19, 2025
@yariks5s yariks5s removed the do not test disable testing on pull request label May 20, 2025
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved

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

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.

@yariks5s
yariks5s requested a review from KochetovNicolai June 16, 2025 09:57
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6617096]

Summary: ❌

job_name test_name status info comment
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb, issue
BuzzHouse (amd_tsan) failure
Logical error: 'Distributed task iterator is not initialized' FAIL cidb

@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

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.

@KochetovNicolai KochetovNicolai self-assigned this Nov 21, 2025
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Outdated
Comment on lines +1073 to +1093
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);
}

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.

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. │ ᴺᵁᴸᴸ                   │
   └────────────────────────┘

Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Outdated
}

/// Checks if node is a NULL constant
bool QueryAnalyzer::isNullConstant(const QueryTreeNodePtr & node)

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.

Perhaps we should also move this code to resolveFunction?
QueryAnalyzer.cpp is big already, and these functions look independent.

@yariks5s
yariks5s added this pull request to the merge queue Dec 1, 2025
Merged via the queue into ClickHouse:master with commit ca37ff2 Dec 1, 2025
@yariks5s
yariks5s deleted the non-constant-in-v2 branch December 1, 2025 14:07
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 1, 2025
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @yariks5s @KochetovNicolai — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants