Repository navigation
Commit 59a53a6
fix(service-analytics): the draft preview resolves a { $field } comparand as the live faces do (#37299)
Fixes #36677
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:在 Studio
预览模式里给数据集或仪表盘写"字段与字段比较"的筛选时,预览的数字现在和发布后一致;发布后会被拒绝的写法,预览也同样报错。
- 为什么做:分析预览此前不认识字段引用,作者对着错误的数字调筛选,发布后数字才变(拆自"预览成员筛选"那张卡)。
- 出错会怎样:只影响草稿预览,不改任何已存数据或已发布查询;有问题可直接撤回本改动。
- 要你做什么:无
## What changes
The analytics draft preview (`queryDataset` with `previewDrafts`)
compared the row value against the `{ $field }` reference OBJECT, as
text, so every field-to-field filter answered a wrong count with a 200.
It now gives the live faces' answer:
- **Scalar-to-scalar references are resolved.** Under the six scalar
operators (`$eq` `$ne` `$gt` `$gte` `$lt` `$lte`) the preview compares
the row's two columns, with the shared cross-field corpus's truth table
(both empty is equal, `$ne` is the exact complement, an ordering needs
both sides). New module `preview-field-reference.ts`; the arm sits in
`matchesWhere`.
- **What the live faces refuse is refused**, `INVALID_FILTER` / 400,
judged once before any row is read: a multi-value / JSON / file /
formula column on either side and a cross-class pair (through
`@objectstack/spec`'s `crossFieldComparisonVerdict`, the one
classification driver-sql and the write check read), an undeclared
column, a dotted path, and a reference in an `$in` / `$nin` list or
under a text-pattern operator (through this package's own `where`-door
sentences in `comparand-shape.ts`).
- **The ruling's second choice was not taken**: the live faces'
resolution rules are reusable (the spec verdict, the corpus truth table,
this package's refusal sentences), so references are resolved, not
refused wholesale.
- **One shape is refused where a live face serves it**: a `{ $field,
addDays }` offset reference. The live faces shift a date/datetime
referent in SQL; the only in-memory copy of that arithmetic is private
to `@objectstack/formula`, which this package does not depend on, and a
second copy here would be a second rule set. The preview refuses it by
name, in its not-evaluated words. On the numeric pair the live faces
refuse it too.
## Tests
`src/__tests__/preview-field-reference-36677.test.ts`, on the production
composition (ObjectQL + SqlDriver on SQLite, six rows, one per
truth-table cell):
- the scalar rows `$eq` / `$ne` / `$gt` (and `$gte` / `$lt` / `$lte`, a
numeric pair, `$not`, `$or`, a self-reference): native, ObjectQL and
preview counts all equal the native count (2 / 4 / 1 for the card's
three);
- the multi-value rows `$eq` / `$ne` on `tags` and on a `select`
declared `multiple`, and the other refused shapes: `code`
`INVALID_FILTER` and `status` 400 on all three faces;
- the shared corpus from `@objectstack/driver-sql`: every
`CROSS_FIELD_CASES` row through the preview's filters with the corpus's
expected ids, every `CROSS_FIELD_REFUSALS` row refused except the two
tenant-column rows (see Acceptance notes), every offset case refused by
name, and the offset fixture's bare-reference controls served with their
rows.
<details><summary>Measured on origin/main e845656 before the change
(native / objectql / preview)</summary>
```
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} 400 / 400 / 200 count 0
label $ne {$field:tags} 400 / 400 / 200 count 6
label $eq {$field:picks} 400 / 400 / 200 count 0
label $eq {$field:amount} 400 / 400 / 200 count 0
label $in [{$field:alias}] 400 / 400 / 200 count 0
```
</details>
<details><summary>Ablation (one-shot, not kept)</summary>
The new arm in `matchesWhere` replaced by `undefined` with
`scripts/ablation-replace.mjs` (anchor hit 1, injected text present 1):
the 12 scalar pins went red (`Tests 12 failed | 104 skipped`), the
card's scalar `$eq` pin among them. Restore proven by the script: blob
after restore `821f350cb98d` equals the HEAD blob, `git diff HEAD`
empty. The subject is imported from `src/`, so no rebuild leg applies.
</details>
<details><summary>Package suite and typecheck</summary>
- `pnpm --filter @objectstack/service-analytics exec vitest run
--maxWorkers=2` on 9370100: `Test Files 227 passed (227)`, `Tests 5897
passed | 345 skipped (6242)`, exit 0.
- `tsc --noEmit` (the package's `typecheck`): red on 9370100 with one
TS2352 in the new test file; fixed in 8a7f8ff, re-run exit 0. The new
pin file re-run on 8a7f8ff: `Tests 116 passed (116)`.
</details>
## Acceptance notes
- **The tenant-isolation column is not judged on the preview.**
driver-sql refuses a reference to it on either side, reading which
column it is off the object schema (`resolveTenantFieldName`). The
preview is handed a field's declared shape only (`sourceFieldMeta`), so
`{ label: { $eq: { $field: 'organization_id' } } }` is still compared on
the preview (measured: native 400, preview count 2). Closing it needs
the tenant column handed to the service, a change to the exported
`AnalyticsServiceConfig` shape, which is outside this card's surface;
raised in the report as an open question rather than guessed by name.
- The refusal wording on the preview is the preview's own sentence
(`code` and `status` match the live faces; the pins assert those, not
the prose).
## Verification
Read first:
`packages/services/service-analytics/src/preview-field-reference.ts`
(the refusal walk and the truth table), the `matchesWhere` hunk in
`preview-evaluator.ts`, then the integration block of the test file.
Two-minute check: `pnpm --filter @objectstack/service-analytics exec
vitest run --maxWorkers=2
src/__tests__/preview-field-reference-36677.test.ts` prints `Tests 116
passed (116)`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_011y7As92T28dA9wc7wtu3iE)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 9e6533b commit 59a53a6
4 files changed
Lines changed: 546 additions & 2 deletions
File tree
- .changeset
- packages/services/service-analytics/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
65 | 74 | | |
66 | 75 | | |
67 | 76 | | |
| |||
404 | 413 | | |
405 | 414 | | |
406 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
407 | 424 | | |
408 | 425 | | |
409 | 426 | | |
| |||
781 | 798 | | |
782 | 799 | | |
783 | 800 | | |
784 | | - | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
785 | 804 | | |
786 | 805 | | |
787 | 806 | | |
| |||
903 | 922 | | |
904 | 923 | | |
905 | 924 | | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
906 | 929 | | |
907 | 930 | | |
908 | 931 | | |
| |||
915 | 938 | | |
916 | 939 | | |
917 | 940 | | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
918 | 948 | | |
919 | 949 | | |
920 | 950 | | |
| |||
0 commit comments