4444 * both its query and its dry run (`analytics-service.ts`,
4545 * `assertDelegatedDateBucketsTemporal`).
4646 *
47+ * [#29304] …and a PLAIN group key over a virtual field, which the engine's
48+ * storage-axis door (`assertGroupByTargetsAreStored`) now refuses ahead of its
49+ * date-bucket door, `INVALID_FIELD` / 400 at `groupBy.N`. Measured on
50+ * `origin/main` `447226b2` before it, a dataset or cube dimension over a
51+ * `formula` (`returnType` `date` or `text`): the dry run rendered
52+ * `GROUP BY calc` on both compositions and the preview served one `null`
53+ * group holding the total, while the native strategy answered
54+ * `DATABASE_ERROR` / 500 and the ObjectQL strategy the SQL driver's
55+ * missing-column backstop. {@link assertDatasetGroupTargetsStored} gives the
56+ * engine's refusal on the faces that hand it no keys (the preview, the dry
57+ * run, the delegated service), and `NativeSQLStrategy.canHandle` declines a
58+ * grouped dimension over a virtual field so the live query reaches the engine.
59+ *
4760 * Why a copy of the sentence: `@objectstack/objectql` is a dev dependency of
4861 * this package, never a runtime one, and its root does not export the door.
4962 * The message, code, status and keys are the engine's, and
@@ -61,7 +74,7 @@ type DateBucketRefusal = Error & {
6174 field ?: string ; fields ?: string [ ] ; object ?: string ; param ?: string ; path ?: string ;
6275} ;
6376
64- /** The refusal `assertDatasetDateBucketsTemporal` throws , with the engine's keys. */
77+ /** The refusal this module's two doors throw , with the engine's keys. */
6578function dateBucketRefusal ( message : string , object : string , field : string , path : string ) : DateBucketRefusal {
6679 const err = new Error ( message ) as DateBucketRefusal ;
6780 err . code = StandardErrorCode . enum . INVALID_FIELD ;
@@ -72,25 +85,27 @@ function dateBucketRefusal(message: string, object: string, field: string, path:
7285}
7386
7487/**
75- * Refuse the first bucketed key in `groupBy` (built for a query on `object`)
76- * whose field `declaredType` reads as a virtual field, or as declared and
77- * neither a date nor a datetime, with the engine's refusal for it.
88+ * [#29167, #29304] Refuse the first key in `groupBy` (built for a query on
89+ * `object`), plain or bucketed, whose field `declaredType` reads as a VIRTUAL
90+ * field (the spec's `isVirtualSearchField`), with the engine's refusal for it:
91+ * `aggregate-query-shape-door.ts` `assertGroupByTargetsAreStored`, the bucket
92+ * in `virtualDateBucketMessage`'s words, the plain key in
93+ * `virtualGroupMessage`'s. Called ahead of
94+ * {@link assertDatasetDateBucketsTemporal}, as the engine calls its two doors.
7895 */
79- export function assertDatasetDateBucketsTemporal (
96+ export function assertDatasetGroupTargetsStored (
8097 object : string ,
8198 groupBy : readonly AggregateGroupKey [ ] ,
8299 declaredType : ( field : string ) => string | undefined ,
83100) : void {
84101 for ( const [ i , key ] of groupBy . entries ( ) ) {
85- if ( typeof key === 'string' ) continue ;
86- const { field, dateGranularity } = key ;
102+ const field = typeof key === 'string' ? key : key . field ;
87103 const type = declaredType ( field ) ;
88- const path = `groupBy.${ i } .dateGranularity` ;
89- // [#29167] The engine's virtual-field arm (`aggregate-query-shape-door.ts`
90- // `virtualDateBucketMessage`), judged first as there.
91- if ( type !== undefined && isVirtualSearchField ( { type } as never ) ) {
104+ if ( type === undefined || ! isVirtualSearchField ( { type } as never ) ) continue ;
105+ if ( typeof key !== 'string' ) {
106+ const path = `groupBy.${ i } .dateGranularity` ;
92107 throw dateBucketRefusal (
93- `aggregate('${ object } '): ${ path } buckets '${ field } ' by '${ dateGranularity } ', but '${ field } ' `
108+ `aggregate('${ object } '): ${ path } buckets '${ field } ' by '${ key . dateGranularity } ', but '${ field } ' `
94109 + `is a ${ type } field — its value is computed on read and never stored, so no driver materialises a `
95110 + `column to bucket. The query was NOT run. Denormalise the value onto '${ object } ' (a stored date or `
96111 + 'datetime field, written when the source changes) and bucket that. A formula field is virtual: with '
@@ -99,6 +114,35 @@ export function assertDatasetDateBucketsTemporal(
99114 object , field , path ,
100115 ) ;
101116 }
117+ const path = `groupBy.${ i } ` ;
118+ throw dateBucketRefusal (
119+ `aggregate('${ object } '): ${ path } groups by '${ field } ', but '${ field } ' is a ${ type } field — its value `
120+ + 'is computed on read and never stored, so no driver materialises a column to group by. The query was NOT '
121+ + `run. Denormalise the value onto '${ object } ' (a stored field, written when the source changes) and group `
122+ + 'by that. A formula field is virtual: with no column behind it the group key reached the driver as a '
123+ + 'column that does not exist, and an engine that aggregates in memory put every row into one null group '
124+ + 'while the answer looked served.' ,
125+ object , field , path ,
126+ ) ;
127+ }
128+ }
129+
130+ /**
131+ * Refuse the first bucketed key in `groupBy` (built for a query on `object`)
132+ * whose field `declaredType` reads as declared and neither a date nor a
133+ * datetime, with the engine's refusal for it. A virtual field is judged ahead
134+ * of this door, by {@link assertDatasetGroupTargetsStored}.
135+ */
136+ export function assertDatasetDateBucketsTemporal (
137+ object : string ,
138+ groupBy : readonly AggregateGroupKey [ ] ,
139+ declaredType : ( field : string ) => string | undefined ,
140+ ) : void {
141+ for ( const [ i , key ] of groupBy . entries ( ) ) {
142+ if ( typeof key === 'string' ) continue ;
143+ const { field, dateGranularity } = key ;
144+ const type = declaredType ( field ) ;
145+ const path = `groupBy.${ i } .dateGranularity` ;
102146 if ( type === undefined || CALENDAR_DATE_TYPES . has ( type ) || INSTANT_TYPES . has ( type ) ) continue ;
103147 throw dateBucketRefusal (
104148 `aggregate('${ object } '): ${ path } buckets '${ field } ' by '${ dateGranularity } ', but '${ field } ' `
0 commit comments