Skip to content

Commit

Permalink
fix isouter join clause #586
Browse files Browse the repository at this point in the history
  • Loading branch information
toluaina committed Dec 4, 2024
1 parent 5dc674a commit e6bc23a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
29 changes: 23 additions & 6 deletions pgsync/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ def _children(self, node: Node) -> None:
self.from_obj = child.parent.model

if child._filters:
self.isouter = False

self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
for column in _filter._orig:
Expand Down Expand Up @@ -569,7 +574,11 @@ def _through(self, node: Node) -> None: # noqa: C901
from_obj = node.model

if child._filters:
self.isouter = False

self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
Expand Down Expand Up @@ -690,7 +699,10 @@ def _through(self, node: Node) -> None: # noqa: C901
)

if node._filters:
self.isouter = False
self.isouter = not any(
column in node.table_columns
for column in node.relationship.foreign_key.parent
)

op = sa.and_
if node.table == node.parent.table:
Expand Down Expand Up @@ -746,7 +758,13 @@ def _non_through(self, node: Node) -> None: # noqa: C901
from_obj = node.model

if child._filters:
self.isouter = False

# NB:sometimes the FK is in the parent not the child
# if none of child.relationship.foreign_key.parent is in child.table_columns then isouter = True
self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
Expand Down Expand Up @@ -776,11 +794,10 @@ def _non_through(self, node: Node) -> None: # noqa: C901
== column.value
)

isouter = not (len(child._filters) > 0)
from_obj = from_obj.join(
child._subquery,
onclause=sa.and_(*onclause),
isouter=isouter,
isouter=self.isouter,
)

foreign_keys: dict = self.get_foreign_keys(node.parent, node)
Expand Down
8 changes: 4 additions & 4 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#
async-timeout==5.0.1
# via redis
boto3==1.35.67
boto3==1.35.75
# via -r requirements/base.in
botocore==1.35.67
botocore==1.35.75
# via
# boto3
# s3transfer
Expand Down Expand Up @@ -47,7 +47,7 @@ marshmallow==3.23.1
# via environs
opensearch-dsl==2.1.0
# via -r requirements/base.in
opensearch-py==2.7.1
opensearch-py==2.8.0
# via opensearch-dsl
packaging==24.2
# via marshmallow
Expand All @@ -73,7 +73,7 @@ requests-aws4auth==1.3.1
# via -r requirements/base.in
s3transfer==0.10.4
# via boto3
six==1.16.0
six==1.17.0
# via
# opensearch-dsl
# python-dateutil
Expand Down
44 changes: 22 additions & 22 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ async-timeout==5.0.1
# via redis
black==24.10.0
# via -r requirements/dev.in
boto3==1.35.67
# via -r requirements/base.in
botocore==1.35.67
boto3==1.35.75
# via -r /Users/tolu/pgsync/requirements/base.in
botocore==1.35.75
# via
# boto3
# s3transfer
Expand All @@ -25,9 +25,9 @@ charset-normalizer==3.4.0
# via requests
click==8.1.7
# via
# -r requirements/base.in
# -r /Users/tolu/pgsync/requirements/base.in
# black
coverage[toml]==7.6.7
coverage[toml]==7.6.8
# via
# -r requirements/dev.in
# pytest-cov
Expand All @@ -39,17 +39,17 @@ elastic-transport==8.15.1
# elasticsearch-dsl
elasticsearch==8.16.0
# via
# -r requirements/base.in
# -r /Users/tolu/pgsync/requirements/base.in
# elasticsearch-dsl
elasticsearch-dsl==8.16.0
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
environs==11.2.1
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
events==0.5
# via opensearch-py
exceptiongroup==1.2.2
# via pytest
faker==33.0.0
faker==33.1.0
# via -r requirements/dev.in
filelock==3.16.1
# via virtualenv
Expand All @@ -59,7 +59,7 @@ freezegun==1.5.1
# via -r requirements/dev.in
greenlet==3.1.1
# via sqlalchemy
identify==2.6.2
identify==2.6.3
# via pre-commit
idna==3.10
# via requests
Expand All @@ -82,8 +82,8 @@ mypy-extensions==1.0.0
nodeenv==1.9.1
# via pre-commit
opensearch-dsl==2.1.0
# via -r requirements/base.in
opensearch-py==2.7.1
# via -r /Users/tolu/pgsync/requirements/base.in
opensearch-py==2.8.0
# via opensearch-dsl
packaging==24.2
# via
Expand All @@ -101,12 +101,12 @@ pluggy==1.5.0
pre-commit==4.0.1
# via -r requirements/dev.in
psycopg2-binary==2.9.10
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
pycodestyle==2.12.1
# via flake8
pyflakes==3.2.0
# via flake8
pytest==8.3.3
pytest==8.3.4
# via
# -r requirements/dev.in
# pytest-cov
Expand All @@ -125,29 +125,29 @@ python-dateutil==2.9.0.post0
# opensearch-py
python-dotenv==1.0.1
# via
# -r requirements/base.in
# -r /Users/tolu/pgsync/requirements/base.in
# environs
pyyaml==6.0.2
# via pre-commit
redis==5.2.0
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
requests==2.32.3
# via
# opensearch-py
# requests-aws4auth
requests-aws4auth==1.3.1
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
s3transfer==0.10.4
# via boto3
six==1.16.0
six==1.17.0
# via
# opensearch-dsl
# python-dateutil
sqlalchemy==2.0.36
# via -r requirements/base.in
# via -r /Users/tolu/pgsync/requirements/base.in
sqlparse==0.5.2
# via -r requirements/base.in
tomli==2.1.0
# via -r /Users/tolu/pgsync/requirements/base.in
tomli==2.2.1
# via
# black
# coverage
Expand All @@ -164,5 +164,5 @@ urllib3==1.26.20
# elastic-transport
# opensearch-py
# requests
virtualenv==20.27.1
virtualenv==20.28.0
# via pre-commit

0 comments on commit e6bc23a

Please sign in to comment.