Skip to content

ARRAY JOIN operations are not recognised in Clickhouse queries #48

@smoothml

Description

@smoothml

Consider the following example:

import os

import pytest
from sql_mock.clickhouse import column_mocks as col
from sql_mock.clickhouse.table_mocks import ClickHouseTableMock
from sql_mock.table_mocks import table_meta

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
GROUP BY
    city,
    browser
"""


@table_meta(query=QUERY)
class QueryMock(ClickHouseTableMock):
    impressions = col.Int(0)
    city = col.String("Istanbul")
    browser = col.String("Firefox")


@pytest.fixture(autouse=True)
def clickhouse_credentials() -> None:
    os.environ["SQL_MOCK_CLICKHOUSE_HOST"] = "127.0.0.1"
    os.environ["SQL_MOCK_CLICKHOUSE_PORT"] = "8123"
    os.environ["SQL_MOCK_CLICKHOUSE_USER"] = ""
    os.environ["SQL_MOCK_CLICKHOUSE_PASSWORD"] = ""


def test_query() -> None:
    expected = [
        {
            "impressions": 1,
            "city": "Istanbul",
            "browser": "Firefox",
        },
        {
            "impressions": 1,
            "city": "Berlin",
            "browser": "Chrome",
        },
        {
            "impressions": 1,
            "city": "Bobruisk",
            "browser": "Chrome",
        },
    ]
    result = QueryMock.from_mocks(input_data=[])
    result.assert_equal(expected)

When testing this, sql-mock will fail the test with the following error:

FAILED tests/test_example.py::test_query - sql_mock.exceptions.ValidationError: You need to provide the following input mocks to run your query: ['browsers', 'cities']

The arguments to the ARRAY JOIN operation should not need to be mocked as they are columns in the sub-query.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions