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
5 changes: 5 additions & 0 deletions .changeset/zero-rust-json-min-max-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'zero-rust': patch
---

`min_json` and `max_json` over a JSON path that holds both strings and numbers now order strings before numbers, as upstream Zero now does: `min_json` of `"x"` and `2` is `"x"`, not `2`, and `max_json` is `2`. This changes results: before, numbers came first. Synced views show the value zero-cache computes, so this only changes what a mutator's query computes on the client before the server has answered it.
5 changes: 5 additions & 0 deletions .changeset/zero-swift-json-min-max-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'zero-swift': patch
---

`min(json:)` and `max(json:)` over a JSON path that holds both strings and numbers now order strings before numbers, as upstream Zero now does: `min` of `"x"` and `2` is `"x"`, not `2`, and `max` is `2`. This changes results: before, numbers came first. Synced views show the value zero-cache computes, so this only changes what a mutator's query computes on the client before the server has answered it.
14 changes: 8 additions & 6 deletions packages/z2s/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ function aggregateExpr(spec: Spec, zqlTable: string, agg: Aggregate): SQLQuery {
* (skipped) rather than a cast error.
*
* `min`/`max` reduce numbers and strings separately and combine them with the
* client's cross-type order, numbers before strings (`compareAggregateValues`),
* instead of Postgres's jsonb order, which puts strings first. The result is
* jsonb so either type comes back as itself.
* client's cross-type order, strings before numbers (`compareAggregateValues`),
* which is also Postgres's jsonb order. The result is jsonb so either type
* comes back as itself.
*/
function jsonAggregateExpr(
spec: Spec,
Expand All @@ -301,16 +301,18 @@ function jsonAggregateExpr(
path,
);
const num = sql`(CASE WHEN ${jsonType} = 'number' THEN ${raw}::float8 END)`;
const str = sql`(CASE WHEN ${jsonType} = 'string' THEN ${raw} END)`;
// The client compares strings by their UTF-8 bytes, not the database's
// collation.
const str = sql`(CASE WHEN ${jsonType} = 'string' THEN ${raw} END) COLLATE "C"`;
switch (fn) {
case 'sum':
return sql`SUM(${num})::float8`;
case 'avg':
return sql`AVG(${num})::float8`;
case 'min':
return sql`COALESCE(to_jsonb(MIN(${num})), to_jsonb(MIN(${str})))`;
return sql`COALESCE(to_jsonb(MIN(${str})), to_jsonb(MIN(${num})))`;
case 'max':
return sql`COALESCE(to_jsonb(MAX(${str})), to_jsonb(MAX(${num})))`;
return sql`COALESCE(to_jsonb(MAX(${num})), to_jsonb(MAX(${str})))`;
case 'count':
throw new Error('count() takes no JSON path');
default:
Expand Down
42 changes: 21 additions & 21 deletions packages/zero-cache/src/services/view-syncer/view-syncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4362,27 +4362,27 @@ export class ViewSyncerService implements ViewSyncer, ActivityBasedService {
await attemptRetransform(backgroundConnCtx);
return;
} catch (e) {
if (isProtocolError(e)) {
if (isAuthErrorBody(e.errorBody)) {
lc.warn?.(
'Background retransform auth failed; failing connection and searching for replacement',
{
clientID: backgroundConnCtx.clientID,
message: e.message,
},
);
this.#failMaintenanceConnection(backgroundConnCtx, e);
} else if (isTransformFailedError(e)) {
lc.warn?.(
'Background retransform failed; deferring auth maintenance',
{
clientID: backgroundConnCtx.clientID,
message: e.message,
},
);
this.connContextManager.deferMaintenance('retransform');
return;
}
// Only an auth failure can be recovered by selecting another connection.
// In particular, a CVR version mismatch must escape the group lock.
if (isProtocolError(e) && isAuthErrorBody(e.errorBody)) {
lc.warn?.(
'Background retransform auth failed; failing connection and searching for replacement',
{
clientID: backgroundConnCtx.clientID,
message: e.message,
},
);
this.#failMaintenanceConnection(backgroundConnCtx, e);
} else if (isProtocolError(e) && isTransformFailedError(e)) {
lc.warn?.(
'Background retransform failed; deferring auth maintenance',
{
clientID: backgroundConnCtx.clientID,
message: e.message,
},
);
this.connContextManager.deferMaintenance('retransform');
return;
} else {
throw e;
}
Expand Down
18 changes: 9 additions & 9 deletions packages/zero-conformance/vectors/bughunt-ivm.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/zero-conformance/vectors/ivm-features.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/zero-protocol/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ export type Aggregate = {
/**
* The value an aggregate reduces for `row`: the `field` column itself, or, with
* a `path`, the JSON leaf at that path when it is one the aggregate takes: a
* number for `sum`/`avg`, a number or a string for `min`/`max` (numbers order
* before strings, see `compareAggregateValues`). Any other leaf, or a missing
* number for `sum`/`avg`, a number or a string for `min`/`max` (strings order
* before numbers, see `compareAggregateValues`). Any other leaf, or a missing
* one, is `null`, which aggregates skip. Shared by the IVM operator, the
* junction lift and the client's optimistic delta so the rule cannot drift.
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/zql/src/ivm/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,9 @@ export class Aggregate implements Operator {

/**
* Orders the values a min/max reduces. A JSON path's leaves can be numbers or
* strings, decided per value; numbers order before strings, as in JS and
* SQLite (z2s compiles the same rule rather than Postgres's jsonb ordering,
* which puts strings first). Otherwise it is {@link compareValues}.
* strings, decided per value; strings order before numbers, as in Postgres's
* jsonb ordering and in rindle (z2s compiles the same rule). Otherwise it is
* {@link compareValues}.
*/
function compareAggregateValues(
a: NormalizedValue,
Expand All @@ -734,7 +734,7 @@ function compareAggregateValues(
(typeof a === 'number' || typeof a === 'string') &&
(typeof b === 'number' || typeof b === 'string')
) {
return typeof a === 'number' ? -1 : 1;
return typeof a === 'string' ? -1 : 1;
}
return compareValues(a, b);
}
Expand Down
Loading
Loading