CUBEfor referencing members of the same cube.FILTER_PARAMSandFILTER_GROUPfor optimizing generated SQL queries.SQL_UTILSfor time zone conversion.COMPILE_CONTEXTfor creation of dynamic data models.
CUBE
You can use the CUBE context variable to reference columns or members of
the current cube so you don’t have to repeat the its name over and over.
It helps reference members while keeping the data
model code DRY and easy to maintain.
FILTER_PARAMS
FILTER_PARAMS context variable allows you to use filter
values from the Cube query during SQL generation.
This is useful for hinting your database optimizer to use a specific index
or filter out partitions or shards in your cloud data warehouse so you won’t
be billed for scanning those. It can also be useful for constructing links.
FILTER_PARAMS has to be a top-level expression in WHERE and it has the
following syntax:
filter() function accepts sql_expression, which could be either
a string or a function returning a string.
Example with string
See the example below for the case when a string is passed tofilter():
['2018-01-01', '2018-12-31'] date range passed for the
order_facts.date dimension as in following query:
Example with function
You can also pass a function as afilter() argument. This way, you can
add BigQuery shard filtering, which will reduce your billing cost.
When a function is passed to
filter(), its arguments are passed as
strings from the data source driver and it’s your responsibility to handle
type conversions in this case.FILTER_GROUP
If you use FILTER_PARAMS in your query more than once, you must wrap them
with FILTER_GROUP.
FILTER_GROUP has to be a top-level expression in WHERE and it has the
following syntax:
Example
To understand the value ofFILTER_GROUP, consider the following data model
where two FILTER_PARAMS are combined in SQL using the OR operator:
AND semantics, Cube has
correctly used the AND operator in the “outer” WHERE. At the same time,
the hardcoded OR operator has propagated to the “inner” WHERE, leading to
a logically incorrect query.
Now, if the cube is defined the following way…
FILTER_GROUP, the following correct SQL will be generated:
SQL_UTILS
convertTz
In case you need to convert your timestamp to user request timezone in cube or
member SQL you can use SQL_UTILS.convertTz() method. Note that Cube will
automatically convert timezones for timeDimensions fields in
queries.
In case the same database field needs to be queried in dimensions and
timeDimensions, create dedicated dimensions in the cube definition for the
respective use:
COMPILE_CONTEXT
A global COMPILE_CONTEXT contains securityContext and any other variables provided by
extendContext.
Use Jinja {{ }} syntax to access COMPILE_CONTEXT variable.
SECURITY_CONTEXT
SECURITY_CONTEXT global variable holds a security context that is passed to Cube via API.
Please read the Security Context page for more information on how
to provide security context to Cube.
requiredFilter can be used: