Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a table is not expressed as alias the referencing isn't correct later #43

Closed
fgarzadeleon opened this issue Feb 2, 2024 · 5 comments · Fixed by #44
Closed

When a table is not expressed as alias the referencing isn't correct later #43

fgarzadeleon opened this issue Feb 2, 2024 · 5 comments · Fixed by #44

Comments

@fgarzadeleon
Copy link
Collaborator

fgarzadeleon commented Feb 2, 2024

Using a simple query:

QUERY_SIMPLE = """
SELECT col1
FROM bee as b 
JOIN a ON a.col1 = b.col1
"""

With models:

@table_meta(table_ref="a")
class aMock(ClickHouseTableMock):
    col1 = col.String(default="1")

@table_meta(table_ref="bee")
class bMock(ClickHouseTableMock):
    col1 = col.String(default="1")

@table_meta(query=QUERY_SIMPLE)
class BugTableMock(ClickHouseTableMock):
    col1 = col.String(default="1")
and test:
    def test_working():
    input_table_mock_1 = aMock()
    input_table_mock_2 = bMock()

    res = BugTableMock.from_mocks(input_data=[
        input_table_mock_1, input_table_mock_2])

    expected = [{'col1': '1'}]
    res.assert_equal(expected)
Yields error ` Cannot get JOIN keys from JOIN ON section: 'a.col1 = col1', found keys: [Left keys: [] Right keys [] Condition columns: 'equals(a.col1, col1)', '']. (INVALID_JOIN_ON_EXPRESSION) (version 23.8.4.69 (official build))`

Internal sql_mock cte is:
   WITH sql_mock__a AS (
 SELECT
   CAST('1' AS String) AS col1
 FROM (
   SELECT
     1
 )
 WHERE
   FALSE
), sql_mock__bee AS (
 SELECT
   CAST('1' AS String) AS col1
 FROM (
   SELECT
     1
 )
 WHERE
   FALSE
), result AS (
 SELECT
   col1
 FROM sql_mock__bee AS b /* bee */
 JOIN sql_mock__a /* a */
   ON a.col1 = b.col1
)
SELECT
 CAST(col1 AS String) AS col1
FROM result

Because table a does not have a AS a this fails. Because the table has now been replaced with slq_mock__a but it is still being referenced as a in a.col1.

@fgarzadeleon fgarzadeleon changed the title When a table is not referenced with an AS it isn't replaced correctly When a table is not expressed with an AS the referencing isn't correct later Feb 2, 2024
@thecasper2
Copy link
Collaborator

As far as I can tell this helper function is only designed to replace the table reference and not prefixed references to columns in that table, which wouldn't be necessary when using an alias anyway.

I can't readily think of a solution to this one, ideas welcome!

@fgarzadeleon fgarzadeleon changed the title When a table is not expressed with an AS the referencing isn't correct later When a table is not expressed as alias the referencing isn't correct later Feb 2, 2024
@fgarzadeleon
Copy link
Collaborator Author

Easiest would be to add documentation to always alias your tables but a fix that considers this in the package would be best.

@Somtom
Copy link
Collaborator

Somtom commented Feb 5, 2024

Thanks for filing that bug @fgarzadeleon!
@thecasper2 I can have a look the upcoming days. I think the library we use (sqlglot) should be able to help us out there.

@Somtom
Copy link
Collaborator

Somtom commented Feb 5, 2024

@thecasper2 I got some response from Tobiko Data (the maintainers of sqlglot). Seems that replace_table is not a good tool for this type of job. They mentioned that the scope module could be interesting (there is also some explanation here)

@Somtom
Copy link
Collaborator

Somtom commented Feb 8, 2024

@thecasper2 @fgarzadeleon I might have found a solution: #44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants