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

fix: fix select.join #284

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clickhouse_sqlalchemy/sql/selectable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from sqlalchemy.sql.base import _generative
from sqlalchemy.sql.selectable import (
Select as StandardSelect,
Join
)

from ..ext.clauses import (
Expand Down Expand Up @@ -77,8 +76,8 @@ def join(self, right, onclause=None, isouter=False, full=False, type=None,
'strictness': strictness,
'distribution': distribution
}
return Join(self, right, onclause=onclause, isouter=isouter,
full=flags)
return super().join(right, onclause=onclause, isouter=isouter,
full=flags)


select = Select
345 changes: 181 additions & 164 deletions tests/sql/test_selectable.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,191 +340,208 @@ def make_statement(type=None,
)
return select(table_1.c.x).select_from(join)

self.assertEqual(
self.compile(make_statement(
type='INNER'
)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)
def make_statement_select_join(type=None,
strictness=None,
distribution=None,
full=False,
isouter=False):
join = select(table_1).join(
table_2,
table_2.c.y == table_1.c.x,
isouter=isouter,
full=full,
type=type,
strictness=strictness,
distribution=distribution
)
return join

for make_statement in (make_statement, make_statement_select_join):
self.assertEqual(
self.compile(make_statement(
type='INNER'
)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='INNER',
strictness='all'
)),
'SELECT table_1.x FROM table_1 '
'ALL INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='INNER',
strictness='all'
)),
'SELECT table_1.x FROM table_1 '
'ALL INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='INNER',
strictness='any'
)),
'SELECT table_1.x FROM table_1 '
'ANY INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='INNER',
strictness='any'
)),
'SELECT table_1.x FROM table_1 '
'ANY INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global',
strictness='any'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ANY INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global',
strictness='any'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ANY INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global',
strictness='all'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='INNER',
distribution='global',
strictness='all'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='LEFT OUTER',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='LEFT OUTER',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='RIGHT OUTER',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL RIGHT OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='RIGHT OUTER',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL RIGHT OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
type='CROSS',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL CROSS JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='CROSS',
distribution='global',
strictness='all')),
'SELECT table_1.x FROM table_1 '
'GLOBAL ALL CROSS JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(type='FULL OUTER')),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(type='FULL OUTER')),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(isouter=False,
full=False)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(isouter=False,
full=False)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(isouter=True,
full=False)),
'SELECT table_1.x FROM table_1 '
'LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(isouter=True,
full=False)),
'SELECT table_1.x FROM table_1 '
'LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(isouter=True,
full=True)),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(isouter=True,
full=True)),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(isouter=False,
full=True)),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(isouter=False,
full=True)),
'SELECT table_1.x FROM table_1 '
'FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='INNER'
)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='INNER'
)),
'SELECT table_1.x FROM table_1 '
'INNER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT OUTER'
)),
'SELECT table_1.x FROM table_1 '
'LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT OUTER'
)),
'SELECT table_1.x FROM table_1 '
'LEFT OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ALL'
)),
'SELECT table_1.x FROM table_1 '
'ALL LEFT JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ALL'
)),
'SELECT table_1.x FROM table_1 '
'ALL LEFT JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ANY'
)),
'SELECT table_1.x FROM table_1 '
'ANY LEFT JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ANY'
)),
'SELECT table_1.x FROM table_1 '
'ANY LEFT JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ANY', distribution='GLOBAL'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ANY LEFT JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=False, full=False,
type='LEFT', strictness='ANY', distribution='GLOBAL'
)),
'SELECT table_1.x FROM table_1 '
'GLOBAL ANY LEFT JOIN table_2 ON table_2.y = table_1.x'
)

self.assertRaises(
CompileError,
lambda: self.compile(make_statement(
isouter=True, full=False,
type='INNER', strictness='ANY', distribution='GLOBAL'
)),
)
self.assertRaises(
CompileError,
lambda: self.compile(make_statement(
isouter=True, full=False,
type='INNER', strictness='ANY', distribution='GLOBAL'
)),
)

self.assertEqual(
self.compile(make_statement(
type='FULL OUTER', strictness='ANY', distribution='GLOBAL'
)),
'SELECT table_1.x FROM table_1 GLOBAL '
'ANY FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
type='FULL OUTER', strictness='ANY', distribution='GLOBAL'
)),
'SELECT table_1.x FROM table_1 GLOBAL '
'ANY FULL OUTER JOIN table_2 ON table_2.y = table_1.x'
)

self.assertEqual(
self.compile(make_statement(
isouter=True, full=False,
type='CROSS',
)),
'SELECT table_1.x FROM table_1 '
'CROSS JOIN table_2 ON table_2.y = table_1.x'
)
self.assertEqual(
self.compile(make_statement(
isouter=True, full=False,
type='CROSS',
)),
'SELECT table_1.x FROM table_1 '
'CROSS JOIN table_2 ON table_2.y = table_1.x'
)

def test_join_same_column_name(self):
table_1 = self._make_table(
Expand Down
Loading