Skip to content

Commit 8b224f7

Browse files
committed
Do not require mocks for ARRAY JOIN clause arguments
The `ARRAY JOIN` clause in Clickhouse is currently recognised as a table that requires a mock by the library. This is not the case as this clause is used for expanding arrays in the source table into separate rows. This commit adds an additional clause to the `get_source_tables` helper function to ignore joins of kind `ARRAY`. Closes #48
1 parent 6b9ec4a commit 8b224f7

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
* Add default target path for dbt
1919
* Improve replacement of tables (also taking into account missing alias)
20+
* Do not require mocks for ARRAY JOIN clause arguments
2021

2122
## [0.6.0]
2223

src/sql_mock/helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def get_source_tables(query, dialect) -> List[str]:
144144
# then `source` will be a Table instance.
145145
for alias, (node, source) in scope.selected_sources.items()
146146
if isinstance(source, sqlglot.exp.Table)
147+
# When using ARRAY joins sqlglot percieves the inputs as tables even though they are infact not.
148+
# This fixes it but does not allow for multiple types of joins to be mixed with the ARRAY JOIN,
149+
# For now we consider it a reasonable solution.
150+
and not (node.parent.key == "join" and any(join.kind == "ARRAY" for join in node.parent_select.args["joins"]))
147151
}
148152

149153
return list(tables)

tests/sql_mock/test_helpers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,27 @@ def test_query_with_comment(self):
321321

322322
expected = ["table_1"]
323323
assert res == expected
324+
325+
def test_query_with_array_joins(self):
326+
"""...then the array join fields should not be treated as tables to be mocked"""
327+
328+
query = """
329+
SELECT
330+
sum(1) AS impressions,
331+
city,
332+
browser
333+
FROM
334+
(
335+
SELECT
336+
['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
337+
['Firefox', 'Chrome', 'Chrome'] AS browsers
338+
)
339+
ARRAY JOIN
340+
cities AS city,
341+
browsers AS browser
342+
"""
343+
344+
res = get_source_tables(query, dialect="clickhouse")
345+
346+
expected = []
347+
assert res == expected

0 commit comments

Comments
 (0)