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
10 changes: 10 additions & 0 deletions .changeset/preview-field-reference-comparand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/service-analytics': patch
---

Clause-②: no
Metadata-protocol: none

The analytics draft preview (`queryDataset` with `previewDrafts`, the face Studio's preview mode reads) now resolves a `{ $field }` comparand the way the published dataset does. A comparison between two scalar columns of the row (`{ label: { $eq: { $field: 'alias' } } }`, and `$ne` / `$gt` / `$gte` / `$lt` / `$lte`) compares the two columns, with the same answer for empty values as the live faces (both empty is equal). A reference the live faces refuse is refused with their `INVALID_FILTER` / 400: a multi-value, JSON, file or formula column on either side, two columns of different comparison classes, an undeclared column, a dotted path, and a reference in an `$in` / `$nin` list or under a text-pattern operator. Before, the preview compared the row value with the reference object as text, so every field-to-field filter showed a wrong count with no error, and the count changed at publish.

One shape is refused on the preview although the published dataset serves it: a reference with an `addDays` offset (`{ $field: 'due_on', addDays: 3 }`). The preview refuses it by name rather than evaluate the day shift differently; publish the draft to chart it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#36677] The draft preview answers a `{ $field }` comparand as the live
* faces of the dataset door answer it: a scalar-to-scalar reference compares
* the row's two columns, and a reference the live faces refuse is refused with
* their `INVALID_FILTER` / 400.
*
* Measured on `origin/main` (`e845656d`) before the change, through
* `queryDataset` on the production composition below (counts over the six
* rows of {@link ROWS}):
*
* | filter | native | ObjectQL | preview |
* |---|---|---|---|
* | `label $eq {$field:'alias'}` | 2 | 2 | 0 |
* | `label $ne {$field:'alias'}` | 4 | 4 | 6 |
* | `label $gt {$field:'alias'}` | 1 | 1 | 4 |
* | `label $eq {$field:'tags'}` (and `picks`, a `select` declared `multiple`) | 400 | 400 | 0 |
* | `label $ne {$field:'tags'}` (and `picks`) | 400 | 400 | 6 |
* | `label $eq {$field:'amount'}` (cross-class) | 400 | 400 | 0 |
* | `label $in [{$field:'alias'}]` | 400 | 400 | 0 |
*
* The preview compared the row value with the reference OBJECT, as text.
*
* What these pins hold:
* - the scalar rows, under all six operators, `$not` and `$or`: the preview's
* count EQUALS the native strategy's, and both equal the declared count;
* - the multi-value rows, and every other reference the live faces refuse:
* the preview refuses with the same `code` and `status`;
* - the shared cross-field corpus (`@objectstack/driver-sql`'s
* `CROSS_FIELD_CASES`, which holds `@objectstack/formula`'s evaluator and the
* SQL compile to the same rows) row for row at the preview's filters, and
* its refusal table refused, but for the two tenant-column rows the preview
* does not judge (`preview-field-reference.ts`);
* - the one shape the preview refuses where a live face serves it: an
* `addDays` offset reference, refused by name over the corpus's offset
* cases, while the same corpus's bare-reference controls are served with its
* rows.
*/

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { ObjectQL } from '@objectstack/objectql';
import {
CROSS_FIELD_CASES,
CROSS_FIELD_OBJECT_FIELDS,
CROSS_FIELD_OFFSET_CASES,
CROSS_FIELD_OFFSET_OBJECT_FIELDS,
CROSS_FIELD_OFFSET_ROWS,
CROSS_FIELD_REFUSALS,
CROSS_FIELD_ROWS,
SqlDriver,
} from '@objectstack/driver-sql';
import type { ValueShapeFieldDef } from '@objectstack/spec/data';
import { DatasetSchema, type Dataset } from '@objectstack/spec/ui';
import type { AnalyticsQuery } from '@objectstack/spec/contracts';
import type { AnalyticsService } from '../analytics-service.js';
import { AnalyticsServicePlugin } from '../plugin.js';
import { lowerPreviewRowFilters } from '../preview-evaluator.js';
import { PREVIEW_FIELD_REFERENCE_OPERATORS } from '../preview-field-reference.js';
import { CROSS_FIELD_COMPARISON_OPERATORS } from '../comparand-shape.js';
import { installDefaultOrganization } from './default-organization.testkit.js';

const OBJECT = 'os36677_item';

/** One row per cell of the comparison's truth table, on a text pair and a numeric pair. */
const ROWS = [
{ id: 'r1', label: 'b', alias: 'a', amount: 10, budget: 5, tags: ['b'], picks: ['b'] }, // target > referent
{ id: 'r2', label: 'a', alias: 'b', amount: 3, budget: 5, tags: ['a'], picks: ['a'] }, // target < referent
{ id: 'r3', label: 'm', alias: 'm', amount: 7, budget: 7, tags: ['m'], picks: ['m'] }, // equal
{ id: 'r4', label: null, alias: 'm', amount: null, budget: 5, tags: null, picks: null }, // target empty
{ id: 'r5', label: 'w', alias: null, amount: 10, budget: null, tags: ['w'], picks: ['w'] }, // referent empty
{ id: 'r6', label: null, alias: null, amount: null, budget: null, tags: null, picks: null }, // both empty
];

const DATASET = DatasetSchema.parse({
name: 'os36677_items',
label: 'Items',
object: OBJECT,
dimensions: [{ name: 'label', field: 'label', type: 'string' }],
measures: [{ name: 'cnt', aggregate: 'count' }],
}) as Dataset;

/** A `'protocol'` service answering one pending seed draft of the same rows, which routes `previewDrafts` onto the draft preview. */
const pendingSeedProtocol = {
getMetaItems: async () => [{ name: 'os36677_seed', object: OBJECT }],
getMetaItem: async () => ({ item: { records: ROWS } }),
};

const quiet = { debug() {}, info() {}, warn() {}, error() {}, trace() {}, fatal() {}, child() { return quiet; } };

type Face = 'native' | 'objectql' | 'preview';
const FACES: readonly Face[] = ['native', 'objectql', 'preview'];

/** The scalar rows: the filter and the count the native strategy answers (measured, see the header). */
const SCALAR: Array<[Record<string, unknown>, number]> = [
[{ label: { $eq: { $field: 'alias' } } }, 2],
[{ label: { $ne: { $field: 'alias' } } }, 4],
[{ label: { $gt: { $field: 'alias' } } }, 1],
[{ label: { $gte: { $field: 'alias' } } }, 2],
[{ label: { $lt: { $field: 'alias' } } }, 1],
[{ label: { $lte: { $field: 'alias' } } }, 2],
[{ amount: { $eq: { $field: 'budget' } } }, 2],
[{ amount: { $ne: { $field: 'budget' } } }, 4],
[{ amount: { $gt: { $field: 'budget' } } }, 1],
[{ $not: { label: { $eq: { $field: 'alias' } } } }, 4],
[{ $or: [{ label: { $gt: { $field: 'alias' } } }, { label: 'w' }] }, 2],
[{ label: { $eq: { $field: 'label' } } }, 6],
];

/** References every live face refuses `INVALID_FILTER` / 400. */
const REFUSED: Record<string, unknown>[] = [
{ label: { $eq: { $field: 'tags' } } },
{ label: { $ne: { $field: 'tags' } } },
{ label: { $eq: { $field: 'picks' } } },
{ label: { $ne: { $field: 'picks' } } },
{ label: { $gt: { $field: 'tags' } } },
{ tags: { $eq: { $field: 'label' } } },
{ label: { $eq: { $field: 'amount' } } },
{ label: { $eq: { $field: 'nope' } } },
{ label: { $eq: { $field: 'account.name' } } },
{ label: { $in: [{ $field: 'alias' }] } },
{ label: { $contains: { $field: 'alias' } } },
{ amount: { $eq: { $field: 'budget', addDays: 1 } } },
];

describe('[#36677] the draft preview resolves a { $field } comparand as the live faces do', () => {
let engine: ObjectQL;
const services: Partial<Record<'native' | 'objectql', AnalyticsService>> = {};

const query = (face: Face, runtimeFilter: Record<string, unknown>) =>
services[face === 'preview' ? 'objectql' : face]!.queryDataset(
DATASET,
{ measures: ['cnt'], runtimeFilter } as never,
undefined,
face === 'preview' ? { previewDrafts: true } : undefined,
) as Promise<{ rows: Array<Record<string, unknown>> }>;

const countOf = async (face: Face, filter: Record<string, unknown>) => Number((await query(face, filter)).rows[0]?.cnt ?? 0);

const refusalOf = async (face: Face, filter: Record<string, unknown>) => {
try {
await query(face, filter);
} catch (e) {
const { code, status } = e as { code?: unknown; status?: unknown };
return { code, status };
}
return 'served';
};

beforeAll(async () => {
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as never);
engine = new ObjectQL({ logger: quiet } as never);
engine.registerDriver(driver as never, true);
await engine.init();
engine.registry.registerObject({
name: OBJECT,
label: 'Item',
fields: {
label: { name: 'label', type: 'text' },
alias: { name: 'alias', type: 'text' },
amount: { name: 'amount', type: 'number' },
budget: { name: 'budget', type: 'number' },
tags: { name: 'tags', type: 'tags' },
picks: { name: 'picks', type: 'select', multiple: true, options: ['a', 'b', 'm', 'w'].map((value) => ({ label: value, value })) },
},
} as never);
await engine.syncSchemas();
await installDefaultOrganization(engine as never);
for (const row of ROWS) await engine.insert(OBJECT, { ...row } as never);

for (const [face, caps] of [
['native', undefined],
['objectql', () => ({ nativeSql: false, objectqlAggregate: true, inMemory: false })],
] as const) {
const registered: Record<string, unknown> = {};
const provided: Record<string, unknown> = { data: engine, protocol: pendingSeedProtocol };
await new AnalyticsServicePlugin({ ...(caps ? { queryCapabilities: caps } : {}) } as never).init({
getService: (name: string) => provided[name] ?? registered[name],
registerService: (name: string, svc: unknown) => { registered[name] = svc; },
replaceService: (name: string, svc: unknown) => { registered[name] = svc; },
hook: () => {},
logger: quiet,
} as never);
services[face] = registered.analytics as AnalyticsService;
}
});

afterAll(async () => {
try { await engine?.destroy(); } catch { /* noop */ }
});

it('CONTROL: the fixture is served unfiltered on every face', async () => {
for (const face of FACES) expect(await countOf(face, {}), face).toBe(ROWS.length);
});

for (const [filter, count] of SCALAR) {
it(`${JSON.stringify(filter)} → ${count}, the preview equal to the native strategy`, async () => {
const [native, objectql, preview] = await Promise.all(FACES.map((face) => countOf(face, filter)));
expect({ native, objectql, preview }).toEqual({ native: count, objectql: count, preview: count });
});
}

for (const filter of REFUSED) {
it(`${JSON.stringify(filter)} is refused INVALID_FILTER / 400 on every face`, async () => {
const answers = await Promise.all(FACES.map((face) => refusalOf(face, filter)));
const refused = { code: 'INVALID_FILTER', status: 400 };
expect(answers).toEqual([refused, refused, refused]);
});
}
});

// ─────────────────────────────────────────────────────────────────────────────

/** The preview's row filters for `where`, with the corpus fixture's declarations. */
function previewFiltersOver(fields: Record<string, Record<string, unknown>>) {
const declaredType = (field: string) => (typeof fields[field]?.type === 'string' ? (fields[field]!.type as string) : undefined);
const declaredShape = (field: string): ValueShapeFieldDef | undefined => {
const type = declaredType(field);
return type === undefined ? undefined : { type, multiple: fields[field]!.multiple === true };
};
const CUBE = { name: 'c', sql: 'x', measures: {}, dimensions: {} } as never;
return (where: unknown) =>
lowerPreviewRowFilters({ cube: 'c', measures: [], where } as unknown as AnalyticsQuery, CUBE, declaredType, undefined, declaredShape);
}

/** Does any `{ $field }` in `node` carry an `addDays` offset? */
function carriesOffset(node: unknown): boolean {
if (Array.isArray(node)) return node.some(carriesOffset);
if (node === null || typeof node !== 'object') return false;
const record = node as Record<string, unknown>;
if (typeof record.$field === 'string' && record.addDays !== undefined) return true;
return Object.values(record).some(carriesOffset);
}

describe('[#36677] the preview filters, measured against the shared cross-field corpus', () => {
const filtersOf = previewFiltersOver(CROSS_FIELD_OBJECT_FIELDS);

it('evaluates exactly the six operators the live faces compile a reference under', () => {
expect([...PREVIEW_FIELD_REFERENCE_OPERATORS.keys()].sort()).toEqual([...CROSS_FIELD_COMPARISON_OPERATORS].sort());
});

it.each(CROSS_FIELD_CASES.map((c) => [c.name, c] as const))('%s', (_name, c) => {
const { matches } = filtersOf(c.filter);
const ids = CROSS_FIELD_ROWS.filter((row) => matches({ ...row })).map((row) => row.id);
expect(ids).toEqual(c.expected);
});

/** The two rows the preview cannot judge: it is handed no tenant-column fact (see `preview-field-reference.ts`). */
const TENANT_ROWS = new Set([
'the tenant-isolation column is refused as the REFERENT',
'the tenant-isolation column is refused as the TARGET',
]);

it('the tenant rows are present in the corpus, so the exclusion below names real rows', () => {
expect(CROSS_FIELD_REFUSALS.filter((c) => TENANT_ROWS.has(c.name))).toHaveLength(TENANT_ROWS.size);
});

it.each(CROSS_FIELD_REFUSALS.filter((c) => !TENANT_ROWS.has(c.name)).map((c) => [c.name, c] as const))('%s', (_name, c) => {
expect(() => filtersOf(c.filter)).toThrow(expect.objectContaining({ code: 'INVALID_FILTER', status: 400 }));
});

const offsetFiltersOf = previewFiltersOver(CROSS_FIELD_OFFSET_OBJECT_FIELDS);
const OFFSET = CROSS_FIELD_OFFSET_CASES.filter((c) => carriesOffset(c.filter));
const BARE = CROSS_FIELD_OFFSET_CASES.filter((c) => !carriesOffset(c.filter));

it('the offset corpus carries both kinds of row, so neither table below is empty', () => {
expect({ offset: OFFSET.length > 0, bare: BARE.length > 0 }).toEqual({ offset: true, bare: true });
});

it.each(OFFSET.map((c) => [c.name, c] as const))('an offset reference is refused by name: %s', (_name, c) => {
expect(() => offsetFiltersOf(c.filter)).toThrow(
expect.objectContaining({ code: 'INVALID_FILTER', status: 400, message: expect.stringContaining('addDays offset') }),
);
});

it.each(BARE.map((c) => [c.name, c] as const))('a bare reference on the offset fixture is served: %s', (_name, c) => {
const { matches } = offsetFiltersOf(c.filter);
const ids = CROSS_FIELD_OFFSET_ROWS.filter((row) => matches({ ...row })).map((row) => row.id);
expect(ids).toEqual(c.expected);
});
});
34 changes: 32 additions & 2 deletions packages/services/service-analytics/src/preview-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
//
// Scope (deliberately the dataset-query subset, not a general engine):
// • Mongo-style `where` filters ($eq implicit, $ne/$gt/$gte/$lt/$lte/
// $between/$in/$nin/$contains/$notContains/$null, $and/$or/$not)
// $between/$in/$nin/$contains/$notContains/$null, $and/$or/$not), and
// a `{ $field }` comparand under the six scalar operators (#36677)
// • timeDimensions date-range filtering + granularity bucketing
// (day/week/month/quarter/year)
// • group-by dimensions; count / count_distinct / sum / avg / min / max
Expand Down Expand Up @@ -62,6 +63,14 @@ import type { DurationMeasureSpec } from './strategies/types.js';
import { invalidFilterError, normalizeWhereComparands } from './strategies/filter-normalizer.js';
import { comparisonColumnClass, refusePreviewJsonColumnComparisons } from './json-column-comparison-refusal.js';
import { isJsonStoredShape } from './contains-membership-sql.js';
// [#36677] A `{ $field }` comparand: resolved against the row's own column
// under the six scalar operators, and refused where the live faces refuse it.
import { isFieldReference } from './comparand-shape.js';
import {
assertPreviewFieldReferencesEvaluable,
fieldReferencePredicate,
refusePreviewFieldReferenceClasses,
} from './preview-field-reference.js';
import type { AnalyticsQuery, AnalyticsResult } from '@objectstack/spec/contracts';
import { emptyGroupValueFor, lowerFilterCondition, type Cube, type FilterLoweringOptions, type ValueShapeFieldDef } from '@objectstack/spec/data';

Expand Down Expand Up @@ -404,6 +413,14 @@ export function matchesWhere(
} else if (cond !== null && typeof cond === 'object' && !Array.isArray(cond)) {
const shape = shapeOf?.(key);
for (const [op, expected] of Object.entries(cond as Row)) {
// [#36677] A `{ $field }` comparand compares the row's two columns.
// It used to reach {@link matchOp} as a literal and be compared as the
// text `[object Object]`. See `preview-field-reference.ts`.
const byReference = fieldReferencePredicate(key, op, expected);
if (byReference) {
if (!byReference(row[key], row[(expected as { $field: string }).$field], compare)) return false;
continue;
}
if (!matchOp(row[key], op, expected, key, shape)) return false;
}
} else if (!(row[key] === cond || String(row[key]) === String(cond))) {
Expand Down Expand Up @@ -781,7 +798,9 @@ function previewStorageComparands(node: Row | undefined, kindOf: PreviewTemporal
} else {
out[key] = Object.fromEntries(Object.entries(cond as Row).map(([op, expected]) => [
op,
STORAGE_FORM_OPERATORS.has(op) && expected != null ? storageForm(expected, kind) : expected,
// [#36677] A `{ $field }` comparand names a column, not a value: the
// referent is read from the row, which is in its storage form already.
STORAGE_FORM_OPERATORS.has(op) && expected != null && !isFieldReference(expected) ? storageForm(expected, kind) : expected,
]));
}
}
Expand Down Expand Up @@ -903,6 +922,10 @@ export function lowerPreviewRowFilters(
// text `'null'` or `'undefined'` — onto every driver's answer.
const lowered = lowerFilterCondition(normalizeWhereComparands(query.where), lowering);
assertPreviewCanEvaluate(lowered);
// [#36677] …and every `{ $field }` comparand where this face evaluates one
// (the six scalar operators, a same-object column, no offset), refused in
// the live faces' positions otherwise. Row-independent, so before any row.
assertPreviewFieldReferencesEvaluable(lowered);
// [#36168] …then a scalar comparison on a field declared multi-valued or
// JSON-stored, refused as the live faces refuse it: a drafted row holds the
// list, which never equals a scalar, so `$in` matched no row and `$nin`
Expand All @@ -915,6 +938,13 @@ export function lowerPreviewRowFilters(
const classOf = (column: string) => comparisonColumnClass(declaredShape?.(column), storedJsonClass?.(column));
refusePreviewJsonColumnComparisons(lowered, classOf, (member) => whereColumns?.[member] ?? member);
}
// [#36677] …then each `{ $field }` comparison's two columns, as the live
// faces judge them: both declared, one comparison class
// (`crossFieldComparisonVerdict`). A host that names no declaration is not
// judged, and its references are compared type-blind.
if (declaredShape) {
refusePreviewFieldReferenceClasses(lowered, (member) => declaredShape(whereColumns?.[member] ?? member), declaredShape);
}
// [#21505] …then each value comparison in the temporal STORAGE form, the
// comparand here and the row value at the match (see {@link previewTemporalKind}).
const whereKindOf = previewTemporalKind(whereType);
Expand Down
Loading
Loading