Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/33664-analytics-lookup-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/objectql': patch
'@objectstack/driver-sql': patch
'@objectstack/service-analytics': patch
---

fix(analytics): a dimension through a lookup is one `LEFT JOIN … GROUP BY` on SQL drivers, so it no longer answers 500 past the bind limit

Clause-②: no
Metadata-protocol: none

`POST /analytics/query` with a dimension through a lookup (`opportunity.stage` on a line-item cube) answered `500 DATABASE_ERROR` once the rows pointed at more distinct related records than the database binds parameters for: SQLite refused with `too many SQL variables`, PostgreSQL with a bind count overflow. The ObjectQL analytics path grouped the base rows by the foreign key, then read the related object with `id IN (every distinct key)` and re-bucketed the answer in the service.

On the SQL driver (every dialect, and the WASM SQLite driver) the same query is now one statement. The base aggregate `LEFT JOIN`s the related rows the caller may read and groups by the attribute in the database, with no id list and no bound-parameter ceiling. The related rows are the related object's own aggregate, planned as the caller, the read the old path ran without its id list. Its CRUD gate, read middlewares, row scope and field-level gate all apply, and the tenant wall bounds the joined table. The base read is still judged as grouped by the lookup field itself, so a caller who may not read that field is refused exactly as before. A line whose related record is missing, unreadable, in another tenant or not set still counts under `(restricted)`, as before. Every bucket and measure is the one the old path answered.

The request is an engine-internal read option, so nothing a client sends changes and the public `groupBy` still refuses a dotted path. The in-memory driver, MongoDB, OData and Turso keep the old path for this dimension, unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Classification =
/** The read, in words. */
read: string;
/** The operation the target is judged as. */
operation: 'get' | 'list';
operation: 'get' | 'list' | 'aggregate';
/** The function that asks the decision, and the file it lives in. */
decision: { file: string; fn: string };
/** Where the read takes its answer from that function: a call site that must exist. */
Expand Down Expand Up @@ -195,6 +195,17 @@ const CALLERS: Record<string, Classification> = {
wiring: { file: 'packages/plugins/plugin-audit/src/audit-writers.ts', call: 'servesSummaryTitleTarget(objectName' },
pin: 'packages/plugins/plugin-audit/src/audit-lookup-summary.test.ts',
},
// The engine's relation group join: an analytics cross-object dimension as one `LEFT JOIN … GROUP BY`.
'packages/objectql/src/relation-group-join.ts': {
kind: 'decided',
read:
"the engine's relation group join (an analytics dimension through a lookup grouped by the related record's " +
'attribute in one statement; `referenceTargetOf` checks that the relation points at the object the request names)',
operation: 'aggregate',
decision: { file: 'packages/services/service-analytics/src/api-exposure-door.ts', fn: 'assertObjectsExposed' },
wiring: { file: 'packages/services/service-analytics/src/analytics-service.ts', call: 'assertObjectsExposed(objects, provider' },
pin: 'packages/services/service-analytics/src/__tests__/cross-object-relation-group-join.test.ts',
},
'packages/core/src/utils/relation-condition-sites.ts': {
kind: 'not-a-served-read',
reason:
Expand Down Expand Up @@ -489,6 +500,7 @@ describe('[#22661] every read that reaches a second object asks that object its
(c): c is Extract<Classification, { kind: 'decided' }> => c.kind === 'decided',
);
expect(decided.map((d) => d.decision.fn).sort()).toEqual([
'assertObjectsExposed',
'refuseUnservedRelationTarget',
'servesExpansionTarget',
'servesLabelTarget',
Expand Down
116 changes: 114 additions & 2 deletions packages/drivers/driver-sql/src/sql-driver-aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/

import { coreSqlDialectOf } from './sql-driver-oracle-dialect.js';
import { emptyGroupValueFor } from '@objectstack/spec/data';
import { emptyGroupValueFor, isComposedReadableIds } from '@objectstack/spec/data';
import type { Knex } from 'knex';
import type { DriverOptions } from '@objectstack/spec/data';
import type { ComposedReadableIds, DriverOptions } from '@objectstack/spec/data';
import type { DriverQuery } from '@objectstack/spec/contracts';
import { AGGREGATE_ACCUMULATION, AGGREGATE_ANSWER_KIND, aggregandOperandSql } from '@objectstack/core';
import type { AggregandColumnClass } from '@objectstack/core';
Expand Down Expand Up @@ -79,6 +79,10 @@ export interface AggregationHost {
export class AggregationFacet {
/** The body of `SqlDriver#aggregate`; the method's docblock there is its contract. */
static async aggregate(this: Pick<AggregationHost, 'aggregandColumnClassOf' | 'aggregateBackendFault' | 'aliasIdentifierSql' | 'applyFilters' | 'applyTenantScope' | 'buildDateBucketExpr' | 'coercionKey' | 'config' | 'dateGranularityCapabilities' | 'dialectName' | 'foldEmptyAggregateAnswers' | 'getBuilder' | 'isMssql' | 'isOracle' | 'knex' | 'mapAggregateFunc' | 'presentReadColumns' | 'primeMssqlGuidColumns' | 'primeOracleRawUuidColumns' | 'readPresentationKind' | 'remoteColumn'>, object: string, query: DriverQuery, options?: DriverOptions): Promise<Record<string, unknown>[]> {
// [relation group join] The related rows each relation joins, handed by
// the engine (`objectql` `relation-group-join.ts`); read once, judged
// before anything compiles.
const relationJoins = relationGroupJoinsOf(query);
// [#33698] The per-measure `filter`s that carry a condition (`{}` is the
// vacuous filter, same convention as `where` / `having`): each compiles
// with the where compiler below, so each is primed and read like a `where`.
Expand All @@ -95,6 +99,13 @@ export class AggregationFacet {
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
// the remote catalog before a comparand or a written value is bound.
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
// …and the same two primes for each joined related object, whose scope
// compiles against its own table.
for (const join of relationJoins ?? []) {
if (join.parent === null) continue;
if (this.isMssql) await this.primeMssqlGuidColumns(join.parent.object, join.parent.scope);
if (this.isOracle) await this.primeOracleRawUuidColumns(join.parent.object);
}
const builder = this.getBuilder(object, options);
// The column a FIELD names in this statement: its `external.columnMap`
// remote column on a federated object, the field itself otherwise. Every
Expand Down Expand Up @@ -240,6 +251,61 @@ export class AggregationFacet {
}
}

// [relation group join] Each relation the engine hands joins the related
// rows the caller may read, and groups by their attributes:
//
// LEFT JOIN (SELECT id AS __rgN_id, attr AS __rgN_aJ … FROM related
// WHERE <tenant wall> AND <composed read scope>) AS __rgN
// ON fk = __rgN.__rgN_id
// … GROUP BY __rgN.__rgN_aJ, CASE WHEN __rgN.__rgN_id IS NULL THEN 0 ELSE 1 END
//
// The derived table is compiled as a composed `ReadableIds` subquery is
// (`readableIdsSubquery`): the related object's tenant wall from THIS
// read's options (ADR-0139 D3 — the engine proved them the related read's
// own) and its composed scope through this same where compiler, so it
// binds only the scope's and the wall's own parameters, never one per
// related record. Its columns are renamed so no column of the
// aggregated table is ambiguous beside them; the join's left column is
// the FK, unqualified, for the same reason. `__rgN_id` is unique in the
// derived table (it is the key), so the join multiplies no row and every
// measure aggregates the rows it would have without it. The resolved
// column is `1` where a readable related record joined, `0` where none
// did (an empty relation, or a record the caller cannot read) — the
// attribute is NULL there, and the resolved column tells it from a real
// NULL. A `null` parent is a related object nothing is readable of.
(relationJoins ?? []).forEach((join, i) => {
const at = `__rg${i}`;
const idColumn = `${at}_id`;
if (join.parent === null) {
for (const a of join.attrs) builder.select(this.knex.raw(`NULL as ${this.aliasIdentifierSql(a.alias)}`));
builder.select(this.knex.raw(`0 as ${this.aliasIdentifierSql(join.resolvedAlias)}`));
presentedOutput.set(join.resolvedAlias, 'number');
return;
}
const related = join.parent.object;
const sub = this.getBuilder(related).select(
this.knex.raw(`?? as ${this.aliasIdentifierSql(idColumn)}`, [this.remoteColumn(related, 'id', 'id')]),
);
join.attrs.forEach((a, j) => {
sub.select(this.knex.raw(`?? as ${this.aliasIdentifierSql(`${at}_a${j}`)}`, [this.remoteColumn(related, a.attr, a.attr)]));
});
this.applyTenantScope(sub, related, options);
if (join.parent.scope !== undefined) this.applyFilters(sub, join.parent.scope, options);
const relatedTable = this.coercionKey(sub);
builder.leftJoin(sub.as(at), columnOf(join.field), `${at}.${idColumn}`);
join.attrs.forEach((a, j) => {
const ref = `${at}.${at}_a${j}`;
builder.groupBy(ref);
builder.select(this.knex.raw(`?? as ${this.aliasIdentifierSql(a.alias)}`, [ref]));
const kind = this.readPresentationKind(relatedTable, a.attr);
if (kind) presentedOutput.set(a.alias, kind);
});
const resolved = 'CASE WHEN ?? IS NULL THEN 0 ELSE 1 END';
builder.groupByRaw(resolved, [`${at}.${idColumn}`]);
builder.select(this.knex.raw(`${resolved} as ${this.aliasIdentifierSql(join.resolvedAlias)}`, [`${at}.${idColumn}`]));
presentedOutput.set(join.resolvedAlias, 'number');
});

// [#6321] Was `query.aggregations || query.aggregate` / `agg.function ||
// agg.func`. Neither `aggregate` nor `func` is declared anywhere in the
// Query Protocol — `QueryASTSchema` declares `aggregations` and
Expand Down Expand Up @@ -457,6 +523,52 @@ export class AggregationFacet {
}
}

/**
* [relation group join] The AST key the engine hands its relation joins on
* (`objectql` `relation-group-join.ts`, the same `Symbol.for` key — this
* package does not import the engine). A symbol, so no wire request and no
* JSON query can carry one; set by the engine after every middleware ran.
*/
export const RELATION_GROUP_JOINS: unique symbol = Symbol.for('objectstack.driver.aggregate.relationGroupJoins');

/** One relation join, as the engine hands it. */
export interface RelationGroupJoin {
/** The single-valued relation (FK) column of the aggregated object. */
readonly field: string;
/** The related rows the caller may read, composed by the engine; `null` = none. */
readonly parent: ComposedReadableIds | null;
readonly attrs: ReadonlyArray<{ readonly attr: string; readonly alias: string }>;
/** The output column: `1` where a readable related record joined, `0` where none did. */
readonly resolvedAlias: string;
}

/**
* The joins on `query`, or `undefined` when it carries none. A parent that is
* not a leaf the engine composed is refused before anything compiles, as an
* uncomposed `ReadableIds` leaf is: its scope is the only thing that bounds
* the joined rows, and ⛔ a scope the engine did not compose is never read as
* one.
*/
function relationGroupJoinsOf(query: DriverQuery): readonly RelationGroupJoin[] | undefined {
const joins = (query as unknown as Record<symbol, unknown>)[RELATION_GROUP_JOINS];
if (joins === undefined) return undefined;
const ok = Array.isArray(joins) && joins.every((j: RelationGroupJoin) =>
j !== null && typeof j === 'object'
&& typeof j.field === 'string'
&& (j.parent === null || isComposedReadableIds(j.parent))
&& Array.isArray(j.attrs) && j.attrs.every((a) => typeof a?.attr === 'string' && typeof a?.alias === 'string')
&& typeof j.resolvedAlias === 'string');
if (!ok) {
const err = new Error(
'aggregate: a relation group join the engine did not compose was refused; the query was NOT run.',
) as Error & { code?: string; status?: number };
err.code = 'INTERNAL_ERROR';
err.status = 500;
throw err;
}
return joins as readonly RelationGroupJoin[];
}

/**
* The aggregate operand on SQL Server — the two `@objectstack/core` operand
* policies (`aggregandOperandSql`), restated for the one dialect that policy
Expand Down
Loading
Loading