Skip to content

Commit

Permalink
Do not require mocks for ARRAY JOIN clause arguments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smoothml committed Mar 27, 2024
1 parent 85f4761 commit d3c12f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[tool.poetry]
name = "sql-mock"
version = "0.6.0"
version = "0.6.1"
description = "Simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios. It provides a consistent and convenient way to test the execution of your query without the need to process a massive amount of data."
repository = "https://github.com/DeepLcom/sql-mock"
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions src/sql_mock/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def get_source_tables(query, dialect) -> List[str]:
# then `source` will be a Table instance.
for alias, (node, source) in scope.selected_sources.items()
if isinstance(source, sqlglot.exp.Table)
# When using ARRAY joins sqlglot percieves the inputs as tables even though they are infact not.
# This fixes it but does not allow for multiple types of joins to be mixed with the ARRAY JOIN,
# For now we consider it a reasonable solution.
and not (node.parent.key == "join" and any(join.kind == "ARRAY" for join in node.parent_select.args["joins"]))
}

return list(tables)
Expand Down
24 changes: 24 additions & 0 deletions tests/sql_mock/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,27 @@ def test_query_with_comment(self):

expected = ["table_1"]
assert res == expected

def test_query_with_array_joins(self):
"""...then the array join fields should not be treated as tables to be mocked"""

query = """
SELECT
sum(1) AS impressions,
city,
browser
FROM
(
SELECT
['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
['Firefox', 'Chrome', 'Chrome'] AS browsers
)
ARRAY JOIN
cities AS city,
browsers AS browser
"""

res = get_source_tables(query, dialect="clickhouse")

expected = []
assert res == expected

0 comments on commit d3c12f2

Please sign in to comment.