Skip to content

Commit

Permalink
Add support for jinja templates in WHERE/HAVING clauses (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch authored Nov 1, 2016
1 parent 61509bb commit 2fd2526
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion caravel/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class BaseContext(object):
def __init__(self, database, query):
self.database = database
self.query = query
self.schema = query.schema
self.schema = None
if query and query.schema:
self.schema = query.schema
elif database:
self.schema = database.schema


class PrestoContext(BaseContext):
Expand Down
7 changes: 5 additions & 2 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
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.jinja_context import process_template
from caravel.utils import (
flasher, MetricPermException, DimSelector, wrap_clause_in_parens
)
Expand Down Expand Up @@ -1086,9 +1087,11 @@ def visit_column(element, compiler, **kw):
cond = ~cond
where_clause_and.append(cond)
if extras and 'where' in extras:
where_clause_and += [wrap_clause_in_parens(extras['where'])]
where = wrap_clause_in_parens(process_template(extras['where'], self.database))
where_clause_and += [where]
if extras and 'having' in extras:
having_clause_and += [wrap_clause_in_parens(extras['having'])]
having = wrap_clause_in_parens(process_template(extras['having'], self.database))
having_clause_and += [having]
if granularity:
qry = qry.where(and_(*(time_filter + where_clause_and)))
else:
Expand Down

0 comments on commit 2fd2526

Please sign in to comment.