Skip to content

Commit

Permalink
Fix generic_bq_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
violetbrina committed Oct 8, 2024
1 parent 300db0f commit 3445898
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions db/python/tables/bq/generic_bq_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,31 @@ def to_sql(
if not isinstance(column, str):
raise ValueError(f'Column {_column_name!r} must be a string')

# IN conditions
self._add_in_condition(self.in_, column, _column_name, conditionals, values)
self._add_condition(
'nin', column, _column_name, 'NOT IN UNNEST', conditionals, values
)

# Simple conditions
self._add_condition('eq', column, _column_name, '=', conditionals, values)
self._add_condition('gt', column, _column_name, '>', conditionals, values)
self._add_condition('gte', column, _column_name, '>=', conditionals, values)
self._add_condition('lt', column, _column_name, '<', conditionals, values)
self._add_condition('lte', column, _column_name, '<=', conditionals, values)
self._add_condition(
'nin', column, _column_name, 'NOT IN UNNEST', conditionals, values
)

# IN conditions
self._add_in_condition(self.in_, column, _column_name, conditionals, values)

return ' AND '.join(conditionals), values

def _add_condition(self, op, column, column_name, operator, conditionals, values):
if attr := getattr(self, op) is not None:
if (attr := getattr(self, op)) is not None:
k = self.generate_field_name(column_name + '_' + op)
conditionals.append(f'{column} {operator} {self._sql_cond_prep(k, attr)}')

# Add brackets for IN operator
b1, b2 = ('(', ')') if 'IN' in operator else (' ', '')

conditionals.append(
f'{column} {operator}{b1}{self._sql_cond_prep(k, attr)}{b2}'
)
values[k] = self._sql_value_prep(k, attr)

def _add_in_condition(self, attr, column, column_name, conditionals, values):
Expand Down

0 comments on commit 3445898

Please sign in to comment.