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

Add parenthesis for custom where and having #1337

Merged
merged 1 commit into from
Oct 20, 2016
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
8 changes: 5 additions & 3 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
from caravel import app, db, db_engine_specs, get_session, utils, sm
from caravel.source_registry import SourceRegistry
from caravel.viz import viz_types
from caravel.utils import flasher, MetricPermException, DimSelector
from caravel.utils import (
flasher, MetricPermException, DimSelector, wrap_clause_in_parens
)

config = app.config

Expand Down Expand Up @@ -1005,9 +1007,9 @@ def visit_column(element, compiler, **kw):
cond = ~cond
where_clause_and.append(cond)
if extras and 'where' in extras:
where_clause_and += [text(extras['where'])]
where_clause_and += [wrap_clause_in_parens(extras['where'])]
if extras and 'having' in extras:
having_clause_and += [text(extras['having'])]
having_clause_and += [wrap_clause_in_parens(extras['having'])]
if granularity:
qry = qry.where(and_(*(time_filter + where_clause_and)))
else:
Expand Down
7 changes: 7 additions & 0 deletions caravel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,10 @@ def __exit__(self, type, value, traceback):
except ValueError as e:
logging.warning("timeout can't be used in the current context")
logging.exception(e)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 says 2 blank lines before a module scoped function


def wrap_clause_in_parens(sql):
"""Wrap where/having clause with parenthesis if necessary"""
if sql.strip():
sql = '({})'.format(sql)
return sa.text(sql)