Skip to content

Commit

Permalink
Merge pull request #284 from Net-Mist/fix_join
Browse files Browse the repository at this point in the history
fix: fix select.join
  • Loading branch information
xzkostyan authored Mar 14, 2024
2 parents 2875475 + a71f687 commit 7c05244
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 167 deletions.
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

0 comments on commit 7c05244

Please sign in to comment.