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

feat: query support array join #187

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/infi/clickhouse_orm/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ def __init__(self, model_cls, database):
self._limit_by_fields = None
self._distinct = False
self._final = False
self._array_join = None
self._left_array_join = False

def __iter__(self):
"""
Expand Down Expand Up @@ -380,6 +382,12 @@ def as_sql(self):
params = (distinct, self.select_fields_as_sql(), table_name, final)
sql = u'SELECT %s%s\nFROM %s%s' % params

if self._array_join:
sql += "\n"
if self._left_array_join:
sql += "LEFT "
sql += "ARRAY JOIN %s" % string_or_func(self._array_join)

if self._prewhere_q and not self._prewhere_q.is_empty:
sql += '\nPREWHERE ' + self.conditions_as_sql(prewhere=True)

Expand Down Expand Up @@ -534,6 +542,15 @@ def distinct(self):
qs._distinct = True
return qs

def array_join(self, field, left=False):
"""
Adds a ARRAY JOIN clause to the query
"""
qs = copy(self)
qs._array_join = field
qs._left_array_join = left
return qs

def final(self):
"""
Adds a FINAL modifier to table, meaning data will be collapsed to final version.
Expand Down Expand Up @@ -628,6 +645,8 @@ def __init__(self, base_qs, grouping_fields, calculated_fields):
self._prewhere_q = base_qs._prewhere_q
self._limits = base_qs._limits
self._distinct = base_qs._distinct
self._array_join = base_qs._array_join
self._left_array_join = base_qs._left_array_join

def group_by(self, *args):
"""
Expand Down