Skip to content

Analyzer: Forbid JOIN ON arrayJoin - #56807

Closed
vdimir wants to merge 3 commits into
masterfrom
vdimir/join_on_array_join
Closed

vdimir wants to merge 3 commits into
masterfrom
vdimir/join_on_array_join

Conversation

@vdimir

@vdimir vdimir commented Nov 15, 2023

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

...

Comment thread src/Planner/PlannerJoins.cpp Outdated
return table_side;
}

void buildJoinClause(ActionsDAGPtr join_expression_dag,

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.

join_expression_dag seemed to be unused, removed the argument

@robot-ch-test-poll robot-ch-test-poll added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 15, 2023
@robot-ch-test-poll

robot-ch-test-poll commented Nov 15, 2023 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 49fce41 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS⏳ pending
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here❌ failure
Successful checks
Check nameDescriptionStatus
Docs checkBuilds and tests the documentation✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success

@novikd novikd self-assigned this Nov 15, 2023
@@ -0,0 +1 @@
SELECT * FROM ( SELECT 1 as a ) as t1 JOIN ( SELECT 1 as a) as t2 ON arrayJoin([t1.a]) = t2.a; -- { serverError INVALID_JOIN_ON_EXPRESSION }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why we need to disable this ? Seems that this can be useful if you have array key in some table.

@vdimir vdimir Nov 16, 2023 •

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.

Sometimes it's difficult to wrap head around how it works.

SELECT *
FROM ( SELECT 1 as a, 'a' as attr ) as t1
JOIN ( SELECT * FROM Values('a UInt32, attr String', (1, 'A'), (2, 'B')) ) as t2
ON arrayJoin([t1.a, t1.a + 1]) = t2.a
;


-- ┌─a─┬─attr─┬─t2.a─┬─t2.attr─┐
-- │ 1 │ a    │    1 │ A       │
-- │ 1 │ a    │    1 │ A       │
-- │ 1 │ a    │    2 │ B       │
-- │ 1 │ a    │    2 │ B       │
-- └───┴──────┴──────┴─────────┘

This result I can explain like we copy rows from t1 and result is the same if it would be joined

a, attr, a+1
1, 'a', 1
1, 'a', 2

by column a+1

But in the following query

SELECT *
FROM ( SELECT 1 as a, 'a' as attr ) as t1
JOIN ( SELECT * FROM Values('a UInt32, attr String', (1, 'A'), (2, 'B')) ) as t2
ON arrayJoin([t1.a, t1.a * 10]) = t2.a
FORMAT PrettyCompactMonoBlock
;


-- ┌─a─┬─attr─┬─t2.a─┬─t2.attr─┐
-- │ 1 │ a    │    1 │ A       │
-- │ 1 │ a    │    1 │ A       │
-- └───┴──────┴──────┴─────────┘

it's unclear why row joined 2 times.

Also it doesn't work for non-constant column so far

SELECT *
FROM ( SELECT materialize(1) as a ) as t1
JOIN ( SELECT 1 as a ) as t2
ON arrayJoin([t1.a]) = t2.a

-- DB::Exception: Cannot find column a_0 in ActionsDAG result. (UNKNOWN_IDENTIFIER)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that arrayJoin need to be applied to left or right table before actual JOIN happens.
In your first query, this need to work something like this:

SELECT * FROM (SELECT arrayJoin([1, 1 + 1]) AS a, 'a' as attr) AS t1
INNER JOIN (SELECT * FROM Values('a UInt32, attr String', (1, 'A'), (2, 'B')) ) as t2
ON t1.a = t2.a;

@vdimir
vdimir marked this pull request as draft July 16, 2024 10:15
@ClickHouse ClickHouse deleted a comment from clickhouse-gh Bot Nov 19, 2024
@ClickHouse ClickHouse deleted a comment from clickhouse-gh Bot Nov 19, 2024
@vdimir vdimir closed this Nov 19, 2024
@ClickHouse ClickHouse deleted a comment from clickhouse-gh Bot Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants