kura_query_compiler (kura v2.31.3)
View SourcePublic entry point for compiling kura_query ASTs into SQL.
Resolves the dialect per repo: the configured dialect module lives in
the repo's config map (set automatically when the repo declares a
{backend, ...}). Different repos in the same app may use different
dialects.
This module is internal. Use kura_repo_worker for executing queries.
Summary
Types
What holds for every query of one shape, so that a cached shape is not walked again: how its parameters are shown to telemetry, the table telemetry names as its source, and what it reads that needs a tenant.
Types
-type facts() :: #{shown := plain | redacted | hidden, source := binary() | undefined, tenant_scoped := [atom() | binary()]}.
What holds for every query of one shape, so that a cached shape is not walked again: how its parameters are shown to telemetry, the table telemetry names as its source, and what it reads that needs a tenant.
Functions
-spec delete_all(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> {iodata(), [term()]}.
Return the dialect module configured for RepoMod.
Resolution order:
dialectkey in the repo's config map (multi-repo or v2.4 flat env).- Global
application:get_env(kura, dialect)for legacy single-dialect setups.
-spec to_sql(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> {iodata(), [term()]}.
Compile a query record into {SQL, Params} for RepoMod's dialect.
Every query compiled here, and by the other query entry points in this
module, is first confined to the ambient tenant with
kura_tenant:scope_query/2, so no caller of this module can compile a query that
skips it.
Each of those entry points first runs both guards over the whole query:
the distance checks that kura_query:order_by/2,
kura_query:select_expr/2, kura_query:where/2 and
kura_query:having/2 apply, and the encrypted filter guard with
kura_query:check_encrypted/1, so a #kura_query{} record built or
updated without kura_query:where/2 cannot bind an encrypted field's
plaintext either.
-spec to_sql_cached(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> {iodata(), [term()]}.
Cached to_sql/2. The cache key includes RepoMod so two repos with
different dialects don't share an entry.
The key is the query's shape, with bound values replaced by markers
(see kura_query_shape), so every call that differs only in its values
shares one entry. The entry holds the SQL and a parameter template; the
values are bound into the template on each call. A shape the dialect
cannot compile, or whose template fails
kura_query_shape:valid_template/2, is remembered as uncacheable and
compiled directly from then on, so a genuine compile error still raises
from the real query.
The key holds the whole shape term, never a hash of it. erlang:phash2/1
returns only 27 bits, so a long-lived cache accumulates collisions, and a
collision here does not degrade a lookup - it returns another shape's SQL
and parameter template, so the caller silently reads rows from a
different table.
The key also carries the ambient tenant. An attribute tenant is added to the
query by to_sql/2 itself, and a nested CTE or combination query keeps
prefix = undefined and resolves a prefix tenant from kura_tenant at emit
time, so the compiled SQL is a function of ambient state the query term does
not describe. Without the tenant in the key, one tenant's cached entry is
served to the next - a cross-tenant read.
The dialect is deliberately not in the key: it is resolved from boot-time
repo config. Swapping a repo's dialect at runtime requires an explicit
kura_query_cache:flush/0.
The encrypted filter guard (kura_query:check_encrypted/1) runs on a miss,
before anything is stored, and on every call that compiles directly. A hit
skips it: the guard reads the sources, joins, selects, CTEs, combinations and
the structure of every condition and subquery, all of which the shape keeps,
and never a bound value, which is all the shape replaces. A hit therefore
means a query of the same shape already passed. The capability check
(kura_capabilities:check_query/2) runs at the same points for the same
reason. A query the shape split
cannot take apart, such as one with an improper condition list, goes through
to_sql/2, so it is checked and never cached.
An entry also holds what else is the same for every query of its shape
(facts/0): what it reads that needs a tenant, how telemetry may show its
parameters, and the table telemetry names. A hit judges the tenant
requirement by the first and hands the rest to the caller, so nothing walks
the query again.
-spec to_sql_from(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, pos_integer()) -> {iodata(), [term()], pos_integer()}.
-spec update_all(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}, map()) -> {iodata(), [term()]}.