Add parens for custom where and having (#1337)

This commit is contained in:
Ryan Ye 2016-10-20 23:22:21 +08:00 committed by Maxime Beauchemin
parent b2f7081c6f
commit 458651fa3e
2 changed files with 12 additions and 3 deletions

View File

@ -55,7 +55,9 @@ import caravel
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
@ -1005,9 +1007,9 @@ class SqlaTable(Model, Queryable, AuditMixinNullable, ImportMixin):
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:

View File

@ -491,3 +491,10 @@ class timeout(object):
except ValueError as e:
logging.warning("timeout can't be used in the current context")
logging.exception(e)
def wrap_clause_in_parens(sql):
"""Wrap where/having clause with parenthesis if necessary"""
if sql.strip():
sql = '({})'.format(sql)
return sa.text(sql)