All files / src/model lateral-keys-builder.ts

90% Statements 9/10
50% Branches 3/6
100% Functions 4/4
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                  131x                                                   2x 2x     2x 2x                   2x               2x 2x       2x                  
import {
  SqlAst,
  type ExprNode,
  type FunctionSource,
  type JoinNode,
  type SelectNode,
} from '../sql/ast.ts';
import type { ModelDefinition } from './model-definition.ts';
 
const ast = new SqlAst();
 
/** Parent keys of a top-N-per-parent read: child rows whose `attribute` equals one of `values`. */
export type LateralKeys = Readonly<{ attribute: string; values: readonly unknown[] }>;
 
/**
 * Wraps the per-parent query in `FROM unnest(keys) JOIN LATERAL (query) ON TRUE`, so that `limit`
 * and `order` of the query apply to each key separately.
 */
export class LateralKeysBuilder {
  /**
   * Builds the outer SELECT.
   *
   * @param definition - Definition of the child model.
   * @param inner - Per-parent SELECT (with `limit` and `order`) without the key condition.
   * @param keys - Parent keys and the child attribute they match.
   * @param alias - Alias of the child rows.
   * @returns SELECT of every child row, grouped by key in key order.
   * @throws Error when the key attribute is not a stored attribute.
   */
  build(
    definition: ModelDefinition,
    inner: SelectNode,
    keys: LateralKeys,
    alias: string,
  ): SelectNode {
    const attribute = definition.attributes.get(keys.attribute);
    Iif (attribute === undefined) {
      throw new Error(`Unknown attribute for a lateral include: ${keys.attribute}`);
    }
    const where = LateralKeysBuilder.#where(inner, alias, attribute.field);
    return {
      kind: 'select',
      columns: [{ kind: 'star', table: alias }],
      from: LateralKeysBuilder.#keys(keys, attribute.type.sqlType),
      joins: [LateralKeysBuilder.#join({ ...inner, where }, alias)],
      orderBy: [{ expr: ast.ident('keys', 'position') }, ...(inner.orderBy ?? [])],
    };
  }
 
  static #join(inner: SelectNode, alias: string): JoinNode {
    return {
      type: 'INNER',
      table: { subquery: inner, alias, lateral: true },
      on: ast.raw('TRUE'),
    };
  }
 
  static #where(inner: SelectNode, alias: string, field: string): ExprNode {
    const correlated = ast.binary('=', ast.ident(alias, field), ast.ident('keys', 'key'));
    return inner.where === undefined ? correlated : ast.and([correlated, inner.where]);
  }
 
  static #keys(keys: LateralKeys, sqlType: string): FunctionSource {
    return {
      function: ['unnest'],
      args: [{ kind: 'cast', expr: ast.param(keys.values), type: `${sqlType}[]` }],
      alias: 'keys',
      columns: ['key', 'position'],
      ordinality: 'position',
    };
  }
}