kura_query_shape (kura v2.31.3)

View Source

Separates a query's shape from the values bound into it, so the query cache can key on the shape (kura#163).

split/1 replaces every bound value in a #kura_query{} with a marker {'$kura_param', N} and returns the values in a tuple. Compiling the shape yields SQL plus a parameter template in which each marker stands where its value would be. bind/2 puts the values back.

Bound positions, in the query and in every CTE, combination and subquery it contains:

  • the right-hand side of a comparison, including like, ilike, matches and the text matches (?KURA_IS_TEXT_MATCH_OP), and of {Field, Value};
  • the elements of an in/not_in list, and of each row tuple in a row-value in whose tuples all match the column count;
  • the elements of a deterministic lookup (?KURA_IS_LOOKUP_OP) whose elements are all laid out as deterministic frames (version 2, mode 1, a full header);
  • both ends of a between;
  • the parameters of a condition fragment, in wheres or havings;
  • the parameters of a select-expression fragment;
  • the vector of a kura_query:distance/3 node, in a select expression, in order_by or in a distance threshold, and the threshold itself;
  • limit and offset.

nil and undefined stay in the shape wherever they appear, so a dialect that compiles them differently from other values still gets them. A {subquery, #kura_query{}} in a bound position is shaped like any other subquery rather than replaced, because the encrypted filter guard reads it. Unary conditions (?KURA_IS_UNARY_OP) and anything not recognised also stay in the shape, so an unrecognised condition costs a cache entry per value set rather than correctness.

Invariant: the encrypted filter guard's verdict (kura_query:check_encrypted/1) on a query equals its verdict on the query's shape. Every bound position above is one the guard never reads, except the elements of a lookup, where the guard reads only whether each is laid out as a deterministic frame, and a lookup's elements are replaced only when all of them are, so that fact is in the shape. So kura_query_compiler:to_sql_cached/2 checks on a miss only. A new bound position, or a guard rule that reads a value, breaks that.

A dialect is free to emit any SQL for a shape, but it must pass a bound value through to the parameter list untouched. valid_template/2 checks the half of that a template can show: every marker appears exactly once, at the top level. A template that fails is never cached.

This module is internal. The marker format is not part of kura's API.

Summary

Functions

Substitute Values for the markers in a template produced by a shape.

Split Query into its shape and the tuple of values bound into it.

True when Params, compiled from a shape with Arity bound values, holds each marker once as a top-level element and nowhere else.

Functions

bind(Template, Values)

-spec bind([term()], tuple()) -> [term()].

Substitute Values for the markers in a template produced by a shape.

split(Query)

-spec split(#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()}) ->
               {#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()},
                tuple()}.

Split Query into its shape and the tuple of values bound into it.

valid_template(Params, Arity)

-spec valid_template([term()], non_neg_integer()) -> boolean().

True when Params, compiled from a shape with Arity bound values, holds each marker once as a top-level element and nowhere else.