diff --git a/.changeset/zero-rust-json-min-max-order.md b/.changeset/zero-rust-json-min-max-order.md new file mode 100644 index 0000000000..7e0d6d3d32 --- /dev/null +++ b/.changeset/zero-rust-json-min-max-order.md @@ -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. diff --git a/.changeset/zero-swift-json-min-max-order.md b/.changeset/zero-swift-json-min-max-order.md new file mode 100644 index 0000000000..3b04400efb --- /dev/null +++ b/.changeset/zero-swift-json-min-max-order.md @@ -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. diff --git a/packages/z2s/src/compiler.ts b/packages/z2s/src/compiler.ts index aac751cf00..7fed3cd7e8 100644 --- a/packages/z2s/src/compiler.ts +++ b/packages/z2s/src/compiler.ts @@ -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, @@ -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: diff --git a/packages/zero-cache/src/services/view-syncer/view-syncer.ts b/packages/zero-cache/src/services/view-syncer/view-syncer.ts index 18d50c488d..187f465f4a 100644 --- a/packages/zero-cache/src/services/view-syncer/view-syncer.ts +++ b/packages/zero-cache/src/services/view-syncer/view-syncer.ts @@ -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; } diff --git a/packages/zero-conformance/vectors/bughunt-ivm.json b/packages/zero-conformance/vectors/bughunt-ivm.json index c812ebfa71..9bc47500cd 100644 --- a/packages/zero-conformance/vectors/bughunt-ivm.json +++ b/packages/zero-conformance/vectors/bughunt-ivm.json @@ -19,9 +19,9 @@ {"name":"zero min","dataset":2,"program":{"table":"issue","ops":[{"op":"min","field":"createdAt"}]},"ast":{"table":"issue","aggregate":{"fn":"min","field":"createdAt"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"createdAt"}},"result":{"initial":0,"steps":[{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1}]}}, {"name":"json empty key sum","dataset":3,"program":{"table":"user","ops":[{"op":"sum","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"sum","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"sum","field":"metadata","path":[""]}},"result":{"initial":9007199254740992,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":9007199254740998,"_0_sum":9007199254740998,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":9007199254740992,"_0_sum":9007199254740992,"_0_count":2}}],"data":9007199254740998},{"changes":[],"data":9007199254740998},{"changes":[],"data":9007199254740998},{"changes":[],"data":9007199254740998},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":6,"_0_sum":6,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":9007199254740998,"_0_sum":9007199254740998,"_0_count":2}}],"data":6},{"changes":[],"data":6},{"changes":[],"data":6},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":-1,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":6,"_0_sum":6,"_0_count":1}}],"data":null}]}}, {"name":"json empty key avg","dataset":3,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":[""]}},"result":{"initial":4503599627370496,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":4503599627370499,"_0_sum":9007199254740998,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":4503599627370496,"_0_sum":9007199254740992,"_0_count":2}}],"data":4503599627370499},{"changes":[],"data":4503599627370499},{"changes":[],"data":4503599627370499},{"changes":[],"data":4503599627370499},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":6,"_0_sum":6,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":4503599627370499,"_0_sum":9007199254740998,"_0_count":2}}],"data":6},{"changes":[],"data":6},{"changes":[],"data":6},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":-1,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":6,"_0_sum":6,"_0_count":1}}],"data":null}]}}, -{"name":"json empty key min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":[""]}},"result":{"initial":1,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":7},"oldRow":{"_0_key":0,"_0_value":1}}],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":7}}],"data":null}]}}, -{"name":"json empty key max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":[""]}},"result":{"initial":"","steps":[{"changes":[],"data":""},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"b"},"oldRow":{"_0_key":0,"_0_value":""}}],"data":"b"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"zz"},"oldRow":{"_0_key":0,"_0_value":"b"}}],"data":"zz"},{"changes":[],"data":"zz"},{"changes":[],"data":"zz"},{"changes":[],"data":"zz"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":7},"oldRow":{"_0_key":0,"_0_value":"zz"}}],"data":7},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":7}}],"data":null}]}}, -{"name":"json empty key groupBy max","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"max","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":[""]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"u1","max":1},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":""},{"name":"u9","max":9007199254740992}],"steps":[{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":7},"oldRow":{"name":"u1","_0_key":"su1","_0_value":1}}],"data":[{"name":"u1","max":7},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":""},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"remove","row":{"name":"u8","_0_key":"su8","_0_value":""}}],"data":[{"name":"u1","max":7},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":"zz"},"oldRow":{"name":"u1","_0_key":"su1","_0_value":7}}],"data":[{"name":"u1","max":"zz"},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"edit","row":{"name":"u2","_0_key":"su2","_0_value":null},"oldRow":{"name":"u2","_0_key":"su2","_0_value":"b"}}],"data":[{"name":"u1","max":"zz"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"remove","row":{"name":"u9","_0_key":"su9","_0_value":9007199254740992}}],"data":[{"name":"u1","max":"zz"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":"zz"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":7},"oldRow":{"name":"u1","_0_key":"su1","_0_value":"zz"}}],"data":[{"name":"u1","max":7},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"remove","row":{"name":"u1","_0_key":"su1","_0_value":7}}],"data":[{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]}]}}, +{"name":"json empty key min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":[""]}},"result":{"initial":"b","steps":[{"changes":[],"data":"b"},{"changes":[],"data":"b"},{"changes":[],"data":"b"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"zz"},"oldRow":{"_0_key":0,"_0_value":"b"}}],"data":"zz"},{"changes":[],"data":"zz"},{"changes":[],"data":"zz"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":7},"oldRow":{"_0_key":0,"_0_value":"zz"}}],"data":7},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":7}}],"data":null}]}}, +{"name":"json empty key max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":[""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":[""]}},"result":{"initial":9007199254740992,"steps":[{"changes":[],"data":9007199254740992},{"changes":[],"data":9007199254740992},{"changes":[],"data":9007199254740992},{"changes":[],"data":9007199254740992},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":7},"oldRow":{"_0_key":0,"_0_value":9007199254740992}}],"data":7},{"changes":[],"data":7},{"changes":[],"data":7},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":7}}],"data":null}]}}, +{"name":"json empty key groupBy max","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"max","field":"metadata","path":[""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":[""]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"u1","max":1},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":""},{"name":"u9","max":9007199254740992}],"steps":[{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":7},"oldRow":{"name":"u1","_0_key":"su1","_0_value":1}}],"data":[{"name":"u1","max":7},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":""},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"remove","row":{"name":"u8","_0_key":"su8","_0_value":""}}],"data":[{"name":"u1","max":7},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[],"data":[{"name":"u1","max":7},{"name":"u2","max":"b"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"edit","row":{"name":"u2","_0_key":"su2","_0_value":null},"oldRow":{"name":"u2","_0_key":"su2","_0_value":"b"}}],"data":[{"name":"u1","max":7},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":9007199254740992}]},{"changes":[{"type":"remove","row":{"name":"u9","_0_key":"su9","_0_value":9007199254740992}}],"data":[{"name":"u1","max":7},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":7},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":7},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"remove","row":{"name":"u1","_0_key":"su1","_0_value":7}}],"data":[{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]}]}}, {"name":"json empty key where = 1","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",[""],"=",1]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":[""]},"right":{"type":"literal","value":1}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":1,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, {"name":"json empty key where > 0","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",[""],">",0]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":">","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":[""]},"right":{"type":"literal","value":0}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":2,"steps":[{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":2}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0}]}}, {"name":"json empty key where < \"z\"","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",[""],"<","z"]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"<","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":[""]},"right":{"type":"literal","value":"z"}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":1,"steps":[{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, @@ -30,8 +30,8 @@ {"name":"json empty key where != \"a\"","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",[""],"!=","a"]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"!=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":[""]},"right":{"type":"literal","value":"a"}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":4,"steps":[{"changes":[],"data":4},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":4}}],"data":3},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":4},"oldRow":{"_0_key":0,"_0_value":3}}],"data":4},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":4}}],"data":3},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":2},"oldRow":{"_0_key":0,"_0_value":3}}],"data":2},{"changes":[],"data":2},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":2}}],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0}]}}, {"name":"json key 0 sum","dataset":3,"program":{"table":"user","ops":[{"op":"sum","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"sum","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"sum","field":"metadata","path":["0"]}},"result":{"initial":2,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0,"_0_sum":0,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":2,"_0_sum":2,"_0_count":2}}],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":0,"_0_sum":0,"_0_count":1}}],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":2}}],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":1}}],"data":null},{"changes":[],"data":null}]}}, {"name":"json key 0 avg","dataset":3,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":["0"]}},"result":{"initial":1,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0,"_0_sum":0,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":1,"_0_sum":2,"_0_count":2}}],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0.5,"_0_sum":1,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":0,"_0_sum":0,"_0_count":1}}],"data":0.5},{"changes":[],"data":0.5},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":0.5,"_0_sum":1,"_0_count":2}}],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":1,"_0_sum":1,"_0_count":1}}],"data":null},{"changes":[],"data":null}]}}, -{"name":"json key 0 min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["0"]}},"result":{"initial":0,"steps":[{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":1}}],"data":null},{"changes":[],"data":null}]}}, -{"name":"json key 0 max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["0"]}},"result":{"initial":"a","steps":[{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":"a"}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":1}}],"data":null},{"changes":[],"data":null}]}}, +{"name":"json key 0 min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["0"]}},"result":{"initial":"a","steps":[{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":"a"}}],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":1}}],"data":null},{"changes":[],"data":null}]}}, +{"name":"json key 0 max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["0"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["0"]}},"result":{"initial":2,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":2}}],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":1}}],"data":null},{"changes":[],"data":null}]}}, {"name":"json key 0 groupBy max","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"max","field":"metadata","path":["0"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["0"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"u1","max":2},{"name":"u2","max":"a"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":null},{"name":"u9","max":0}],"steps":[{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":null},"oldRow":{"name":"u1","_0_key":"su1","_0_value":2}}],"data":[{"name":"u1","max":null},{"name":"u2","max":"a"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":null},{"name":"u9","max":0}]},{"changes":[{"type":"remove","row":{"name":"u8","_0_key":"su8","_0_value":null}}],"data":[{"name":"u1","max":null},{"name":"u2","max":"a"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":0}]},{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":1},"oldRow":{"name":"u1","_0_key":"su1","_0_value":null}}],"data":[{"name":"u1","max":1},{"name":"u2","max":"a"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":0}]},{"changes":[{"type":"edit","row":{"name":"u2","_0_key":"su2","_0_value":null},"oldRow":{"name":"u2","_0_key":"su2","_0_value":"a"}}],"data":[{"name":"u1","max":1},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":0}]},{"changes":[{"type":"remove","row":{"name":"u9","_0_key":"su9","_0_value":0}}],"data":[{"name":"u1","max":1},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":1},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":null},"oldRow":{"name":"u1","_0_key":"su1","_0_value":1}}],"data":[{"name":"u1","max":null},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"remove","row":{"name":"u1","_0_key":"su1","_0_value":null}}],"data":[{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]}]}}, {"name":"json key 0 where = 1","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["0"],"=",1]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["0"]},"right":{"type":"literal","value":1}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":0,"steps":[{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0}]}}, {"name":"json key 0 where > 0","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["0"],">",0]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":">","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["0"]},"right":{"type":"literal","value":0}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":1,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0}]}}, @@ -63,8 +63,8 @@ {"name":"json dotted key where != \"a\"","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["a.b"],"!=","a"]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"!=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["a.b"]},"right":{"type":"literal","value":"a"}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":2,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":2}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, {"name":"json quoted key sum","dataset":3,"program":{"table":"user","ops":[{"op":"sum","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"sum","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"sum","field":"metadata","path":["\"q\""]}},"result":{"initial":4,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":4,"_0_sum":4,"_0_count":1}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, {"name":"json quoted key avg","dataset":3,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":["\"q\""]}},"result":{"initial":4,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":4,"_0_sum":4,"_0_count":1}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, -{"name":"json quoted key min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["\"q\""]}},"result":{"initial":4,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"z"},"oldRow":{"_0_key":0,"_0_value":4}}],"data":"z"},{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"z"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, -{"name":"json quoted key max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["\"q\""]}},"result":{"initial":"z","steps":[{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"z"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, +{"name":"json quoted key min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["\"q\""]}},"result":{"initial":"z","steps":[{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"z"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, +{"name":"json quoted key max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["\"q\""]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["\"q\""]}},"result":{"initial":4,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"z"},"oldRow":{"_0_key":0,"_0_value":4}}],"data":"z"},{"changes":[],"data":"z"},{"changes":[],"data":"z"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"z"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, {"name":"json quoted key groupBy max","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"max","field":"metadata","path":["\"q\""]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["\"q\""]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"u1","max":4},{"name":"u2","max":"z"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":null},{"name":"u9","max":null}],"steps":[{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":null},"oldRow":{"name":"u1","_0_key":"su1","_0_value":4}}],"data":[{"name":"u1","max":null},{"name":"u2","max":"z"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":null},{"name":"u9","max":null}]},{"changes":[{"type":"remove","row":{"name":"u8","_0_key":"su8","_0_value":null}}],"data":[{"name":"u1","max":null},{"name":"u2","max":"z"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":null}]},{"changes":[],"data":[{"name":"u1","max":null},{"name":"u2","max":"z"},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":null}]},{"changes":[{"type":"edit","row":{"name":"u2","_0_key":"su2","_0_value":null},"oldRow":{"name":"u2","_0_key":"su2","_0_value":"z"}}],"data":[{"name":"u1","max":null},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":null}]},{"changes":[{"type":"remove","row":{"name":"u9","_0_key":"su9","_0_value":null}}],"data":[{"name":"u1","max":null},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":null},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":null},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"remove","row":{"name":"u1","_0_key":"su1","_0_value":null}}],"data":[{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]}]}}, {"name":"json quoted key where = 1","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["\"q\""],"=",1]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["\"q\""]},"right":{"type":"literal","value":1}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":0,"steps":[{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, {"name":"json quoted key where > 0","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["\"q\""],">",0]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":">","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["\"q\""]},"right":{"type":"literal","value":0}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":1,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, @@ -96,8 +96,8 @@ {"name":"json decomposed key where != \"a\"","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["é"],"!=","a"]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"!=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["é"]},"right":{"type":"literal","value":"a"}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":1,"steps":[{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, {"name":"json mixed leaf sum","dataset":3,"program":{"table":"user","ops":[{"op":"sum","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"sum","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"sum","field":"metadata","path":["p"]}},"result":{"initial":1e+21,"steps":[{"changes":[],"data":1e+21},{"changes":[],"data":1e+21},{"changes":[],"data":1e+21},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1e+21,"_0_sum":1e+21,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":1e+21,"_0_sum":1e+21,"_0_count":2}}],"data":1e+21},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":1e+21,"_0_sum":1e+21,"_0_count":1}}],"data":null},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3,"_0_sum":3,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0}}],"data":3},{"changes":[],"data":3},{"changes":[],"data":3}]}}, {"name":"json mixed leaf avg","dataset":3,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":["p"]}},"result":{"initial":500000000000000000000,"steps":[{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1e+21,"_0_sum":1e+21,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":500000000000000000000,"_0_sum":1e+21,"_0_count":2}}],"data":1e+21},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0},"oldRow":{"_0_key":0,"_0_value":1e+21,"_0_sum":1e+21,"_0_count":1}}],"data":null},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3,"_0_sum":3,"_0_count":1},"oldRow":{"_0_key":0,"_0_value":null,"_0_sum":0,"_0_count":0}}],"data":3},{"changes":[],"data":3},{"changes":[],"data":3}]}}, -{"name":"json mixed leaf min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["p"]}},"result":{"initial":2,"steps":[{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1e+21},"oldRow":{"_0_key":0,"_0_value":2}}],"data":1e+21},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"a"},"oldRow":{"_0_key":0,"_0_value":1e+21}}],"data":"a"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":"a"}}],"data":3},{"changes":[],"data":3},{"changes":[],"data":3}]}}, -{"name":"json mixed leaf max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["p"]}},"result":{"initial":"😀","steps":[{"changes":[],"data":"😀"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"y"},"oldRow":{"_0_key":0,"_0_value":"😀"}}],"data":"y"},{"changes":[],"data":"y"},{"changes":[],"data":"y"},{"changes":[],"data":"y"},{"changes":[],"data":"y"},{"changes":[],"data":"y"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":"y"}}],"data":3}]}}, +{"name":"json mixed leaf min","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["p"]}},"result":{"initial":"x","steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"y"},"oldRow":{"_0_key":0,"_0_value":"x"}}],"data":"y"},{"changes":[],"data":"y"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"a"},"oldRow":{"_0_key":0,"_0_value":"y"}}],"data":"a"},{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[],"data":"a"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"y"},"oldRow":{"_0_key":0,"_0_value":"a"}}],"data":"y"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":"y"}}],"data":3}]}}, +{"name":"json mixed leaf max","dataset":3,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["p"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["p"]}},"result":{"initial":1e+21,"steps":[{"changes":[],"data":1e+21},{"changes":[],"data":1e+21},{"changes":[],"data":1e+21},{"changes":[],"data":1e+21},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"y"},"oldRow":{"_0_key":0,"_0_value":1e+21}}],"data":"y"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":3},"oldRow":{"_0_key":0,"_0_value":"y"}}],"data":3},{"changes":[],"data":3},{"changes":[],"data":3}]}}, {"name":"json mixed leaf groupBy max","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"max","field":"metadata","path":["p"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["p"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"u1","max":"x"},{"name":"u2","max":2},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":"😀"},{"name":"u9","max":1e+21}],"steps":[{"changes":[{"type":"edit","row":{"name":"u1","_0_key":"su1","_0_value":"y"},"oldRow":{"name":"u1","_0_key":"su1","_0_value":"x"}}],"data":[{"name":"u1","max":"y"},{"name":"u2","max":2},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u8","max":"😀"},{"name":"u9","max":1e+21}]},{"changes":[{"type":"remove","row":{"name":"u8","_0_key":"su8","_0_value":"😀"}}],"data":[{"name":"u1","max":"y"},{"name":"u2","max":2},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":1e+21}]},{"changes":[],"data":[{"name":"u1","max":"y"},{"name":"u2","max":2},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":1e+21}]},{"changes":[{"type":"edit","row":{"name":"u2","_0_key":"su2","_0_value":null},"oldRow":{"name":"u2","_0_key":"su2","_0_value":2}}],"data":[{"name":"u1","max":"y"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null},{"name":"u9","max":1e+21}]},{"changes":[{"type":"remove","row":{"name":"u9","_0_key":"su9","_0_value":1e+21}}],"data":[{"name":"u1","max":"y"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":null},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"edit","row":{"name":"u4","_0_key":"su4","_0_value":3},"oldRow":{"name":"u4","_0_key":"su4","_0_value":null}}],"data":[{"name":"u1","max":"y"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":3},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[],"data":[{"name":"u1","max":"y"},{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":3},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]},{"changes":[{"type":"remove","row":{"name":"u1","_0_key":"su1","_0_value":"y"}}],"data":[{"name":"u2","max":null},{"name":"u3","max":null},{"name":"u4","max":3},{"name":"u5","max":null},{"name":"u6","max":null},{"name":"u7","max":null}]}]}}, {"name":"json mixed leaf where = 1","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["p"],"=",1]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":"=","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["p"]},"right":{"type":"literal","value":1}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":0,"steps":[{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0},{"changes":[],"data":0}]}}, {"name":"json mixed leaf where > 0","dataset":3,"program":{"table":"user","ops":[{"op":"whereExpr","expr":{"cmpJson":["metadata",["p"],">",0]}},{"op":"count"}]},"ast":{"table":"user","where":{"type":"simple","op":">","left":{"type":"json","value":{"type":"column","name":"metadata"},"path":["p"]},"right":{"type":"literal","value":0}},"aggregate":{"fn":"count"}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"count"}},"result":{"initial":2,"steps":[{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[],"data":2},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":2}}],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0},"oldRow":{"_0_key":0,"_0_value":1}}],"data":0},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1},"oldRow":{"_0_key":0,"_0_value":0}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1}]}}, diff --git a/packages/zero-conformance/vectors/ivm-features.json b/packages/zero-conformance/vectors/ivm-features.json index 19272065b7..8e8a6aee07 100644 --- a/packages/zero-conformance/vectors/ivm-features.json +++ b/packages/zero-conformance/vectors/ivm-features.json @@ -178,8 +178,8 @@ {"name":"avg json","dataset":2,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":["points"]}},"result":{"initial":666666666666666600000,"steps":[{"changes":[],"data":666666666666666600000},{"changes":[],"data":666666666666666600000},{"changes":[],"data":666666666666666600000},{"changes":[],"data":666666666666666600000},{"changes":[],"data":666666666666666600000},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":500000000000000000000,"_0_sum":2e+21,"_0_count":4},"oldRow":{"_0_key":0,"_0_value":666666666666666600000,"_0_sum":2e+21,"_0_count":3}}],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000},{"changes":[],"data":500000000000000000000}]}}, {"name":"avg json","dataset":3,"program":{"table":"user","ops":[{"op":"avg","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"avg","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"avg","field":"metadata","path":["points"]}},"result":{"initial":1.6666666666666667,"steps":[{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1.875,"_0_sum":7.5,"_0_count":4},"oldRow":{"_0_key":0,"_0_value":1.6666666666666667,"_0_sum":5,"_0_count":3}}],"data":1.875},{"changes":[],"data":1.875},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1.5,"_0_sum":4.5,"_0_count":3},"oldRow":{"_0_key":0,"_0_value":1.875,"_0_sum":7.5,"_0_count":4}}],"data":1.5},{"changes":[],"data":1.5},{"changes":[],"data":1.5},{"changes":[],"data":1.5},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":1,"_0_sum":2,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":1.5,"_0_sum":4.5,"_0_count":3}}],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[],"data":1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0.3333333333333333,"_0_sum":1,"_0_count":3},"oldRow":{"_0_key":0,"_0_value":1,"_0_sum":2,"_0_count":2}}],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[],"data":0.3333333333333333},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":-1,"_0_sum":-2,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":0.3333333333333333,"_0_sum":1,"_0_count":3}}],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":0.75,"_0_sum":1.5,"_0_count":2},"oldRow":{"_0_key":0,"_0_value":-1,"_0_sum":-2,"_0_count":2}}],"data":0.75},{"changes":[],"data":0.75},{"changes":[],"data":0.75},{"changes":[],"data":0.75},{"changes":[],"data":0.75},{"changes":[],"data":0.75}]}}, {"name":"min json","dataset":0,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":-1,"steps":[{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":-1}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, -{"name":"min json","dataset":1,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":2.5,"steps":[{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5}]}}, -{"name":"min json","dataset":2,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":2.5,"steps":[{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5}]}}, +{"name":"min json","dataset":1,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":"3","steps":[{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":2.5},"oldRow":{"_0_key":0,"_0_value":"3"}}],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5}]}}, +{"name":"min json","dataset":2,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":"3","steps":[{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[],"data":"3"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":2.5},"oldRow":{"_0_key":0,"_0_value":"3"}}],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5},{"changes":[],"data":2.5}]}}, {"name":"min json","dataset":3,"program":{"table":"user","ops":[{"op":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"min","field":"metadata","path":["points"]}},"result":{"initial":-1,"steps":[{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1},{"changes":[],"data":-1}]}}, {"name":"max json","dataset":0,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["registrar"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["registrar"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["registrar"]}},"result":{"initial":"github","steps":[{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"github"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null}]}}, {"name":"max json","dataset":1,"program":{"table":"user","ops":[{"op":"max","field":"metadata","path":["registrar"]}]},"ast":{"table":"user","aggregate":{"fn":"max","field":"metadata","path":["registrar"]}},"format":{"singular":true,"relationships":{},"aggregate":{"fn":"max","field":"metadata","path":["registrar"]}},"result":{"initial":"google","steps":[{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[],"data":"google"},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":null},"oldRow":{"_0_key":0,"_0_value":"google"}}],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[],"data":null},{"changes":[{"type":"edit","row":{"_0_key":0,"_0_value":"github"},"oldRow":{"_0_key":0,"_0_value":null}}],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"},{"changes":[],"data":"github"}]}}, @@ -247,7 +247,7 @@ {"name":"groupBy sum json","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"sum","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"sum","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"A","sum":3},{"name":"a_c","sum":3},{"name":"abc","sum":-1}],"steps":[{"changes":[{"type":"add","row":{"name":"b","_0_key":"sb","_0_value":2.5,"_0_sum":2.5,"_0_count":1,"_0_rows":1}}],"data":[{"name":"A","sum":3},{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[],"data":[{"name":"A","sum":3},{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[{"type":"remove","row":{"name":"A","_0_key":"sA","_0_value":3,"_0_sum":3,"_0_count":1,"_0_rows":1}}],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"b","sum":2.5}]},{"changes":[{"type":"remove","row":{"name":"b","_0_key":"sb","_0_value":2.5,"_0_sum":2.5,"_0_count":1,"_0_rows":1}}],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1}]},{"changes":[{"type":"add","row":{"name":"é","_0_key":"sé","_0_value":-1,"_0_sum":-1,"_0_count":1,"_0_rows":1}}],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"é","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"é","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"é","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"é","sum":-1}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"abc","sum":-1},{"name":"é","sum":-1}]},{"changes":[{"type":"remove","row":{"name":"abc","_0_key":"sabc","_0_value":-1,"_0_sum":-1,"_0_count":1,"_0_rows":1}},{"type":"edit","row":{"name":"é","_0_key":"sé","_0_value":-2,"_0_sum":-2,"_0_count":2,"_0_rows":2},"oldRow":{"name":"é","_0_key":"sé","_0_value":-1,"_0_sum":-1,"_0_count":1,"_0_rows":1}}],"data":[{"name":"a_c","sum":3},{"name":"é","sum":-2}]},{"changes":[],"data":[{"name":"a_c","sum":3},{"name":"é","sum":-2}]},{"changes":[{"type":"remove","row":{"name":"a_c","_0_key":"sa_c","_0_value":3,"_0_sum":3,"_0_count":1,"_0_rows":1}}],"data":[{"name":"é","sum":-2}]},{"changes":[],"data":[{"name":"é","sum":-2}]},{"changes":[],"data":[{"name":"é","sum":-2}]},{"changes":[],"data":[{"name":"é","sum":-2}]},{"changes":[],"data":[{"name":"é","sum":-2}]},{"changes":[{"type":"edit","row":{"name":"é","_0_key":"sé","_0_value":1.5,"_0_sum":1.5,"_0_count":2,"_0_rows":2},"oldRow":{"name":"é","_0_key":"sé","_0_value":-2,"_0_sum":-2,"_0_count":2,"_0_rows":2}}],"data":[{"name":"é","sum":1.5}]},{"changes":[],"data":[{"name":"é","sum":1.5}]},{"changes":[],"data":[{"name":"é","sum":1.5}]},{"changes":[],"data":[{"name":"é","sum":1.5}]},{"changes":[],"data":[{"name":"é","sum":1.5}]},{"changes":[],"data":[{"name":"é","sum":1.5}]}]}}, {"name":"groupBy min json","dataset":0,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"a_c","min":0},{"name":"é","min":-1},{"name":"中文","min":null}],"steps":[{"changes":[],"data":[{"name":"a_c","min":0},{"name":"é","min":-1},{"name":"中文","min":null}]},{"changes":[{"type":"remove","row":{"name":"中文","_0_key":"s中文","_0_value":null}}],"data":[{"name":"a_c","min":0},{"name":"é","min":-1}]},{"changes":[{"type":"remove","row":{"name":"a_c","_0_key":"sa_c","_0_value":0}}],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[{"type":"remove","row":{"name":"é","_0_key":"sé","_0_value":-1}},{"type":"add","row":{"name":"Bug","_0_key":"sBug","_0_value":-1}}],"data":[{"name":"Bug","min":-1}]},{"changes":[{"type":"remove","row":{"name":"Bug","_0_key":"sBug","_0_value":-1}}],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]},{"changes":[],"data":[]}]}}, {"name":"groupBy min json","dataset":1,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"z%","min":"3"},{"name":"中文","min":null},{"name":"","min":2.5},{"name":"😀","min":"3"}],"steps":[{"changes":[{"type":"remove","row":{"name":"中文","_0_key":"s中文","_0_value":null}}],"data":[{"name":"z%","min":"3"},{"name":"","min":2.5},{"name":"😀","min":"3"}]},{"changes":[],"data":[{"name":"z%","min":"3"},{"name":"","min":2.5},{"name":"😀","min":"3"}]},{"changes":[{"type":"remove","row":{"name":"z%","_0_key":"sz%","_0_value":"3"}}],"data":[{"name":"","min":2.5},{"name":"😀","min":"3"}]},{"changes":[{"type":"remove","row":{"name":"","_0_key":"s","_0_value":2.5}},{"type":"add","row":{"name":"ab","_0_key":"sab","_0_value":2.5}}],"data":[{"name":"ab","min":2.5},{"name":"😀","min":"3"}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"😀","min":"3"}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"😀","min":"3"}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"😀","min":"3"}]},{"changes":[{"type":"edit","row":{"name":"😀","_0_key":"\"😀\"","_0_value":2.5},"oldRow":{"name":"😀","_0_key":"\"😀\"","_0_value":"3"}}],"data":[{"name":"ab","min":2.5},{"name":"😀","min":2.5}]},{"changes":[{"type":"remove","row":{"name":"😀","_0_key":"\"😀\"","_0_value":2.5}},{"type":"add","row":{"name":"bug","_0_key":"sbug","_0_value":2.5}}],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5}]},{"changes":[{"type":"add","row":{"name":"é","_0_key":"sé","_0_value":null}}],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[{"type":"add","row":{"name":"Bug","_0_key":"sBug","_0_value":null}}],"data":[{"name":"Bug","min":null},{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"Bug","min":null},{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"Bug","min":null},{"name":"ab","min":2.5},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[{"type":"remove","row":{"name":"ab","_0_key":"sab","_0_value":2.5}}],"data":[{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[{"type":"add","row":{"name":"","_0_key":"s","_0_value":3}}],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]},{"changes":[],"data":[{"name":"","min":3},{"name":"Bug","min":null},{"name":"bug","min":2.5},{"name":"é","min":null}]}]}}, -{"name":"groupBy min json","dataset":2,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}],"steps":[{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[{"type":"add","row":{"name":"z%","_0_key":"sz%","_0_value":2.5}},{"type":"edit","row":{"name":"Bug","_0_key":"sBug","_0_value":3},"oldRow":{"name":"Bug","_0_key":"sBug","_0_value":2.5}}],"data":[{"name":"","min":1e+21},{"name":"Bug","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[{"type":"remove","row":{"name":"Bug","_0_key":"sBug","_0_value":3}},{"type":"add","row":{"name":"a_c","_0_key":"sa_c","_0_value":3}}],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]}]}}, +{"name":"groupBy min json","dataset":2,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}],"steps":[{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":"3"},{"name":"ab","min":1e+21}]},{"changes":[{"type":"edit","row":{"name":"Bug","_0_key":"sBug","_0_value":2.5},"oldRow":{"name":"Bug","_0_key":"sBug","_0_value":"3"}}],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":2.5},{"name":"ab","min":1e+21}]},{"changes":[{"type":"add","row":{"name":"z%","_0_key":"sz%","_0_value":2.5}},{"type":"edit","row":{"name":"Bug","_0_key":"sBug","_0_value":3},"oldRow":{"name":"Bug","_0_key":"sBug","_0_value":2.5}}],"data":[{"name":"","min":1e+21},{"name":"Bug","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"Bug","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[{"type":"remove","row":{"name":"Bug","_0_key":"sBug","_0_value":3}},{"type":"add","row":{"name":"a_c","_0_key":"sa_c","_0_value":3}}],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]},{"changes":[],"data":[{"name":"","min":1e+21},{"name":"a_c","min":3},{"name":"ab","min":1e+21},{"name":"z%","min":2.5}]}]}}, {"name":"groupBy min json","dataset":3,"program":{"table":"user","ops":[{"op":"groupBy","columns":["name"],"fn":"min","field":"metadata","path":["points"]}]},"ast":{"table":"user","aggregate":{"fn":"min","field":"metadata","path":["points"]},"groupBy":["name"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"name":"A","min":3},{"name":"a_c","min":3},{"name":"abc","min":-1}],"steps":[{"changes":[{"type":"add","row":{"name":"b","_0_key":"sb","_0_value":2.5}}],"data":[{"name":"A","min":3},{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[],"data":[{"name":"A","min":3},{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[{"type":"remove","row":{"name":"A","_0_key":"sA","_0_value":3}}],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"b","min":2.5}]},{"changes":[{"type":"remove","row":{"name":"b","_0_key":"sb","_0_value":2.5}}],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1}]},{"changes":[{"type":"add","row":{"name":"é","_0_key":"sé","_0_value":-1}}],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"abc","min":-1},{"name":"é","min":-1}]},{"changes":[{"type":"remove","row":{"name":"abc","_0_key":"sabc","_0_value":-1}}],"data":[{"name":"a_c","min":3},{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"a_c","min":3},{"name":"é","min":-1}]},{"changes":[{"type":"remove","row":{"name":"a_c","_0_key":"sa_c","_0_value":3}}],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]},{"changes":[],"data":[{"name":"é","min":-1}]}]}}, {"name":"groupBy ordered","dataset":0,"program":{"table":"issue","ops":[{"op":"orderBy","field":"closed","dir":"desc"},{"op":"groupBy","columns":["ownerId","closed"],"fn":"count"}]},"ast":{"table":"issue","orderBy":[["closed","desc"]],"aggregate":{"fn":"count"},"groupBy":["ownerId","closed"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}],"steps":[{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":1}]},{"changes":[{"type":"edit","row":{"closed":false,"ownerId":"u3","_0_key":"[false,\"u3\"]","_0_value":2},"oldRow":{"closed":false,"ownerId":"u3","_0_key":"[false,\"u3\"]","_0_value":1}}],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2}]},{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":2},{"ownerId":"u2","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u1","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2}]}]}}, {"name":"groupBy ordered","dataset":1,"program":{"table":"issue","ops":[{"op":"orderBy","field":"closed","dir":"desc"},{"op":"groupBy","columns":["ownerId","closed"],"fn":"count"}]},"ast":{"table":"issue","orderBy":[["closed","desc"]],"aggregate":{"fn":"count"},"groupBy":["ownerId","closed"]},"format":{"singular":false,"relationships":{}},"result":{"initial":[{"ownerId":"u1","closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}],"steps":[{"changes":[],"data":[{"ownerId":"u1","closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[{"type":"add","row":{"closed":true,"ownerId":null,"_0_key":"[true,null]","_0_value":1}}],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u1","closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u1","closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u1","closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[{"type":"remove","row":{"closed":true,"ownerId":"u1","_0_key":"[true,\"u1\"]","_0_value":1}},{"type":"add","row":{"closed":true,"ownerId":"u4","_0_key":"[true,\"u4\"]","_0_value":1}}],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u2","closed":false,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[{"type":"remove","row":{"closed":false,"ownerId":"u2","_0_key":"[false,\"u2\"]","_0_value":1}}],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":2},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[{"type":"edit","row":{"closed":true,"ownerId":"u3","_0_key":"[true,\"u3\"]","_0_value":1},"oldRow":{"closed":true,"ownerId":"u3","_0_key":"[true,\"u3\"]","_0_value":2}}],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]},{"changes":[],"data":[{"ownerId":null,"closed":true,"count":1},{"ownerId":"u3","closed":true,"count":1},{"ownerId":"u4","closed":true,"count":1},{"ownerId":"u3","closed":false,"count":2},{"ownerId":"u4","closed":false,"count":1}]}]}}, diff --git a/packages/zero-protocol/src/ast.ts b/packages/zero-protocol/src/ast.ts index 5a9360d31e..d689d6593d 100644 --- a/packages/zero-protocol/src/ast.ts +++ b/packages/zero-protocol/src/ast.ts @@ -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. */ diff --git a/packages/zql/src/ivm/aggregate.ts b/packages/zql/src/ivm/aggregate.ts index 55cb6b9994..5c42ec71ce 100644 --- a/packages/zql/src/ivm/aggregate.ts +++ b/packages/zql/src/ivm/aggregate.ts @@ -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, @@ -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); } diff --git a/packages/zql/src/ivm/cap.ts b/packages/zql/src/ivm/cap.ts index 2e79128823..d6f0d0a729 100644 --- a/packages/zql/src/ivm/cap.ts +++ b/packages/zql/src/ivm/cap.ts @@ -17,7 +17,7 @@ import { type Storage, } from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import {type Stream} from './stream.ts'; +import {andThen, doneResult, type Stream} from './stream.ts'; import { constraintMatchesPartitionKey, makePartitionKeyComparator, @@ -87,7 +87,7 @@ export class Cap implements Operator { return this.#input.getSchema(); } - *fetch(req: FetchRequest): Stream { + fetch(req: FetchRequest): Stream { assert(!req.start, 'Cap does not support start'); assert(!req.reverse, 'Cap does not support reverse'); @@ -105,35 +105,22 @@ export class Cap implements Operator { const capStateKey = getCapStateKey(this.#partitionKey, req.constraint); const capState = this.#storage.get(capStateKey); if (!capState) { - yield* this.#initialFetch(req, capStateKey); - return; + return this.#initialFetch(req, capStateKey); } if (capState.size === 0) { - return; + return emptyArray; } if (req.countOnly) { - for (let i = 0; i < capState.size; i++) { - yield placeholderNode; - } - return; + return placeholderNodes(capState.size); } // PK-based point lookups: fetch each tracked row by its PK directly, // rather than scanning the partition and filtering. - for (const pk of capState.pks) { - const pkConstraint = deserializePKToConstraint(pk, this.#primaryKey); - // Preserve req.constraint (the partition key) so upstream partitioned - // operators (e.g. Take) retain their partition scope and do not throw. - const constraint = req.constraint - ? {...req.constraint, ...pkConstraint} - : pkConstraint; - for (const inputNode of this.#input.fetch({constraint})) { - if (inputNode === 'yield') { - yield inputNode; - continue; - } - yield inputNode; - } - } + return new CapNodes( + this.#input, + capState.pks, + this.#primaryKey, + req.constraint, + ); } *#initialFetch( @@ -188,10 +175,9 @@ export class Cap implements Operator { } } - *push(change: Change): Stream<'yield'> { + push(change: Change): Stream<'yield'> { if (change[ChangeIndex.TYPE] === ChangeType.EDIT) { - yield* this.#pushEditChange(change); - return; + return this.#pushEditChange(change); } const capStateKey = getCapStateKey( @@ -200,7 +186,7 @@ export class Cap implements Operator { ); const capState = this.#storage.get(capStateKey); if (!capState) { - return; + return emptyArray; } const pk = serializePK(change[ChangeIndex.NODE].row, this.#primaryKey); @@ -209,16 +195,15 @@ export class Cap implements Operator { if (capState.size < this.#limit) { const pks = [...capState.pks, pk]; this.#storage.set(capStateKey, {size: capState.size + 1, pks}); - yield* this.#output.push(change, this); - return; + return this.#output.push(change, this); } // Full — drop - return; + return emptyArray; } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) { const pkIndex = capState.pks.indexOf(pk); if (pkIndex === -1) { // Not in our set — drop - return; + return emptyArray; } // Remove from set const pks = [...capState.pks]; @@ -236,39 +221,98 @@ export class Cap implements Operator { ) as Constraint) : undefined; - let replacement: Node | undefined; - for (const node of this.#input.fetch({constraint})) { - if (node === 'yield') { - yield node; - continue; - } - const nodePK = serializePK(node.row, this.#primaryKey); - if (!pkSet.has(nodePK)) { - replacement = node; - break; - } + const it = this.#input.fetch({constraint})[Symbol.iterator](); + const replacement = this.#readReplacement(it, pkSet); + if (replacement === 'yield') { + return this.#refillAfterYield( + change, + capStateKey, + pks, + newSize, + pkSet, + it, + ); } - if (replacement) { - // Store state WITHOUT replacement during remove forward, - // matching Take's pattern of hiding in-flight changes from re-fetches. - this.#storage.set(capStateKey, {size: newSize, pks}); - yield* this.#output.push(change, this); + it.return?.(); + } + return this.#pushRemove(change, capStateKey, pks, newSize, replacement); + } + const pkSet = new Set(capState.pks); + return pkSet.has(pk) ? this.#output.push(change, this) : emptyArray; + } + + /** + * Reads `it` up to the first row that is not tracked, which takes the place + * of a removed one. Returns 'yield' if the fetch yields before that, and + * `undefined` if the fetch ends. + */ + #readReplacement( + it: Iterator, + pkSet: Set, + ): Node | 'yield' | undefined { + for (;;) { + const r = it.next(); + if (r.done) { + return undefined; + } + const node = r.value; + if ( + node === 'yield' || + !pkSet.has(serializePK(node.row, this.#primaryKey)) + ) { + return node; + } + } + } + + /** The rest of a remove whose fetch for a replacement has yielded. */ + *#refillAfterYield( + change: Change, + capStateKey: string, + pks: string[], + newSize: number, + pkSet: Set, + it: Iterator, + ): Stream<'yield'> { + let replacement: Node | 'yield' | undefined = 'yield'; + try { + while (replacement === 'yield') { + yield 'yield'; + replacement = this.#readReplacement(it, pkSet); + } + } finally { + // Not at its end: a replacement was found, or the consumer stopped + // reading at a 'yield'. + if (replacement !== undefined) { + it.return?.(); + } + } + yield* this.#pushRemove(change, capStateKey, pks, newSize, replacement); + } + + /** Forwards a remove and, if there is one, the add of its replacement. */ + #pushRemove( + change: Change, + capStateKey: string, + pks: string[], + newSize: number, + replacement: Node | undefined, + ): Stream<'yield'> { + if (replacement) { + // Store state WITHOUT replacement during remove forward, + // matching Take's pattern of hiding in-flight changes from re-fetches. + this.#storage.set(capStateKey, {size: newSize, pks}); + return andThen(this.#output.push(change, this), () => { // Now add replacement to set and forward the add. const replacementPK = serializePK(replacement.row, this.#primaryKey); pks.push(replacementPK); this.#storage.set(capStateKey, {size: newSize + 1, pks}); - yield* this.#output.push(makeAddChange(replacement), this); - } else { - this.#storage.set(capStateKey, {size: newSize, pks}); - yield* this.#output.push(change, this); - } - } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) { - const pkSet = new Set(capState.pks); - if (pkSet.has(pk)) { - yield* this.#output.push(change, this); - } + return this.#output.push(makeAddChange(replacement), this); + }); } + this.#storage.set(capStateKey, {size: newSize, pks}); + return this.#output.push(change, this); } #pushEditChange(change: EditChange): Stream<'yield'> { @@ -312,11 +356,84 @@ export class Cap implements Operator { this.#storage.destroy(); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; + } +} + +/** + * The tracked rows of a Cap, each fetched by its primary key. An iterator + * rather than a generator, see `mapNodes` in stream.ts. + */ +class CapNodes implements IterableIterator { + readonly #input: Input; + readonly #pks: readonly string[]; + readonly #primaryKey: PrimaryKey; + readonly #constraint: Constraint | undefined; + #next = 0; + #it: Iterator | undefined = undefined; + + constructor( + input: Input, + pks: readonly string[], + primaryKey: PrimaryKey, + constraint: Constraint | undefined, + ) { + this.#input = input; + this.#pks = pks; + this.#primaryKey = primaryKey; + this.#constraint = constraint; + } + + [Symbol.iterator](): this { + return this; + } + + next(): IteratorResult { + try { + for (;;) { + if (this.#it !== undefined) { + const r = this.#it.next(); + if (!r.done) { + return r; + } + this.#it = undefined; + } + if (this.#next >= this.#pks.length) { + return doneResult; + } + const pkConstraint = deserializePKToConstraint( + this.#pks[this.#next++], + this.#primaryKey, + ); + // Preserve req.constraint (the partition key) so upstream partitioned + // operators (e.g. Take) retain their partition scope and do not throw. + const constraint = this.#constraint + ? {...this.#constraint, ...pkConstraint} + : pkConstraint; + this.#it = this.#input.fetch({constraint})[Symbol.iterator](); + } + } catch (e) { + // A fetch that threw is over, as a generator's is: it does not go on + // to the next row. + this.#it = undefined; + this.#next = this.#pks.length; + throw e; } } + + return(): IteratorResult { + const it = this.#it; + this.#it = undefined; + this.#next = this.#pks.length; + it?.return?.(); + return doneResult; + } +} + +/** `size` placeholder nodes, for a countOnly fetch. */ +function placeholderNodes(size: number): Node[] { + return Array.from({length: size}).fill(placeholderNode); } // Yielded for each tracked row of a countOnly fetch. diff --git a/packages/zql/src/ivm/exists.ts b/packages/zql/src/ivm/exists.ts index 363899318e..1a1d7792a9 100644 --- a/packages/zql/src/ivm/exists.ts +++ b/packages/zql/src/ivm/exists.ts @@ -1,5 +1,7 @@ import {areEqual} from '../../../shared/src/arrays.ts'; import {assert, unreachable} from '../../../shared/src/asserts.ts'; +import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {CompoundKey} from '../../../zero-protocol/src/ast.ts'; import {ChangeIndex} from './change-index.ts'; import {ChangeType} from './change-type.ts'; @@ -18,7 +20,7 @@ import { } from './filter-operators.ts'; import type {InputBase} from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import {type Stream} from './stream.ts'; +import {andFinally, type Stream} from './stream.ts'; /** * The Exists operator filters data based on whether or not a relationship is @@ -84,13 +86,18 @@ export class Exists implements FilterOperator { this.#output.endFilter(); } - *filter(node: Node): IterableIterator<'yield', boolean> { + filter(node: Node): IterableIterator<'yield', boolean> { let exists: boolean | undefined; if (!this.#noSizeReuse && !this.#inPush) { const key = this.#getCacheKey(node, this.#parentJoinKey); exists = this.#cache.get(key); if (exists === undefined) { - exists = yield* this.#fetchExists(node); + const size = this.#fetchSizeNow(node); + if (typeof size !== 'number') { + // The relationship yielded: the rest is read lazily. + return this.#filterRest(node, key, size); + } + exists = size > 0; this.#cache.set(key, exists); } else if (this.#cacheHitCountsForTesting) { this.#cacheHitCountsForTesting.set( @@ -99,10 +106,29 @@ export class Exists implements FilterOperator { ); } } + if (exists === undefined) { + const size = this.#fetchSizeNow(node); + if (typeof size !== 'number') { + return this.#filterRest(node, undefined, size); + } + exists = size > 0; + } + return (this.#not ? !exists : exists) + ? this.#output.filter(node) + : emptyIteratorReturnFalse; + } - const result = - (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node)); - return result; + /** filter() from the point where reading the node's relationship yielded. */ + *#filterRest( + node: Node, + cacheKey: string | undefined, + read: PartialRead, + ): IterableIterator<'yield', boolean> { + const exists = (yield* this.#fetchSizeRest(node, read)) > 0; + if (cacheKey !== undefined) { + this.#cache.set(cacheKey, exists); + } + return (this.#not ? !exists : exists) && (yield* this.#output.filter(node)); } destroy(): void { @@ -113,51 +139,60 @@ export class Exists implements FilterOperator { return this.#input.getSchema(); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } - *push(change: Change): Stream<'yield'> { + push(change: Change): Stream<'yield'> { assert(!this.#inPush, 'Unexpected re-entrancy'); this.#inPush = true; + let s: Stream<'yield'>; try { - switch (change[ChangeIndex.TYPE]) { - // add, remove and edit cannot change the size of the - // this.#relationshipName relationship, so simply #pushWithFilter - case ChangeType.ADD: - case ChangeType.EDIT: - case ChangeType.REMOVE: { - yield* this.#pushWithFilter(change); - return; + s = this.#pushInPush(change); + } catch (e) { + this.#inPush = false; + throw e; + } + return andFinally(s, this.#endPush); + } + + readonly #endPush = (): void => { + this.#inPush = false; + }; + + #pushInPush(change: Change): Stream<'yield'> { + switch (change[ChangeIndex.TYPE]) { + // add, remove and edit cannot change the size of the + // this.#relationshipName relationship, so simply #pushWithFilter + case ChangeType.ADD: + case ChangeType.EDIT: + case ChangeType.REMOVE: + return this.#pushWithFilter(change); + case ChangeType.CHILD: + // Only add and remove child changes for the + // this.#relationshipName relationship, can change the size + // of the this.#relationshipName relationship, for other + // child changes simply #pushWithFilter + if ( + change[ChangeIndex.CHILD_DATA].relationshipName !== + this.#relationshipName || + change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] === + ChangeType.EDIT || + change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] === + ChangeType.CHILD + ) { + return this.#pushWithFilter(change); } - case ChangeType.CHILD: - // Only add and remove child changes for the - // this.#relationshipName relationship, can change the size - // of the this.#relationshipName relationship, for other - // child changes simply #pushWithFilter - if ( - change[ChangeIndex.CHILD_DATA].relationshipName !== - this.#relationshipName || - change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] === - ChangeType.EDIT || - change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] === - ChangeType.CHILD - ) { - yield* this.#pushWithFilter(change); - return; - } - switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) { - case ChangeType.ADD: { - const size = yield* this.#fetchSize(change[ChangeIndex.NODE]); + switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) { + case ChangeType.ADD: + return this.#withSize(change[ChangeIndex.NODE], size => { if (size === 1) { if (this.#not) { // Since the add child change currently being processed is not // pushed to output, the added child needs to be excluded from // the remove being pushed to output (since the child has // never been added to the output). - yield* this.#output.push( + return this.#output.push( makeRemoveChange({ row: change[ChangeIndex.NODE].row, relationships: { @@ -167,73 +202,48 @@ export class Exists implements FilterOperator { }), this, ); - } else { - yield* this.#output.push( - makeAddChange(change[ChangeIndex.NODE]), - this, - ); } - } else { - yield* this.#pushWithFilter(change, size > 0); + return this.#output.push( + makeAddChange(change[ChangeIndex.NODE]), + this, + ); } - return; - } - case ChangeType.REMOVE: { - const size = yield* this.#fetchSize(change[ChangeIndex.NODE]); + return this.#pushWithFilter(change, size > 0); + }); + case ChangeType.REMOVE: + return this.#withSize(change[ChangeIndex.NODE], size => { if (size === 0) { if (this.#not) { - yield* this.#output.push( + return this.#output.push( makeAddChange(change[ChangeIndex.NODE]), this, ); - } else { - // Since the remove child change currently being processed is - // not pushed to output, the removed child needs to be added to - // the remove being pushed to output. - yield* this.#output.push( - makeRemoveChange({ - row: change[ChangeIndex.NODE].row, - relationships: { - ...change[ChangeIndex.NODE].relationships, - [this.#relationshipName]: () => [ - change[ChangeIndex.CHILD_DATA].change[ - ChangeIndex.NODE - ], - ], - }, - }), - this, - ); } - } else { - yield* this.#pushWithFilter(change, size > 0); + // Since the remove child change currently being processed is + // not pushed to output, the removed child needs to be added to + // the remove being pushed to output. + return this.#output.push( + makeRemoveChange({ + row: change[ChangeIndex.NODE].row, + relationships: { + ...change[ChangeIndex.NODE].relationships, + [this.#relationshipName]: () => [ + change[ChangeIndex.CHILD_DATA].change[ChangeIndex.NODE], + ], + }, + }), + this, + ); } - return; - } - } - return; - default: - unreachable(change); - } - } finally { - this.#inPush = false; + return this.#pushWithFilter(change, size > 0); + }); + } + return emptyArray; + default: + unreachable(change); } } - /** - * Returns whether or not the node's this.#relationshipName - * relationship passes the exist/not exists filter condition. - * If the optional `size` is passed it is used. - * Otherwise, if there is a stored size for the row it is used. - * Otherwise the size is computed by streaming the node's - * relationship with this.#relationshipName (this computed size is also - * stored). - */ - *#filter(node: Node, exists?: boolean): IterableIterator<'yield', boolean> { - exists = exists ?? (yield* this.#fetchExists(node)); - return this.#not ? !exists : exists; - } - #getCacheKey(node: Node, def: CompoundKey): string { if (def.length === 1) { return valueToKey(node.row[def[0]]); @@ -248,20 +258,48 @@ export class Exists implements FilterOperator { /** * Pushes a change if this.#filter is true for its row. */ - *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> { - if (yield* this.#filter(change[ChangeIndex.NODE], exists)) { - yield* this.#output.push(change, this); + #pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> { + if (exists === undefined) { + return this.#withSize(change[ChangeIndex.NODE], size => + this.#pushIfPasses(change, size > 0), + ); } + return this.#pushIfPasses(change, exists); + } + + #pushIfPasses(change: Change, exists: boolean): Stream<'yield'> { + return (this.#not ? !exists : exists) + ? this.#output.push(change, this) + : emptyArray; + } + + /** + * Calls `rest` with the size of the node's relationship: right away, or + * once the stream returned has been read if reading the relationship yields. + */ + #withSize( + node: Node, + rest: (size: number) => Stream<'yield'>, + ): Stream<'yield'> { + const size = this.#fetchSizeNow(node); + return typeof size === 'number' + ? rest(size) + : this.#withSizeRest(node, size, rest); } - *#fetchExists(node: Node): IterableIterator<'yield', boolean> { - // While it seems like this should be able to fetch just 1 node - // to check for exists, we can't because Take does not support - // early return during initial fetch. - return (yield* this.#fetchSize(node)) > 0; + *#withSizeRest( + node: Node, + read: PartialRead, + rest: (size: number) => Stream<'yield'>, + ): Stream<'yield'> { + yield* rest(yield* this.#fetchSizeRest(node, read)); } - *#fetchSize(node: Node): IterableIterator<'yield', number> { + /** + * The size of the node's this.#relationshipName relationship, or, if + * reading it yields, what was read up to there, for #fetchSizeRest. + */ + #fetchSizeNow(node: Node): number | PartialRead { const relationship = node.relationships[this.#relationshipName]; assert( relationship, @@ -271,28 +309,81 @@ export class Exists implements FilterOperator { if (this.#inPush) { // Only the size is needed, which Cap can answer from its state. let size = 0; - for (const n of relationship(true)) { - if (n === 'yield') { - yield 'yield'; - } else { - size++; + const it = relationship(true)[Symbol.iterator](); + for (;;) { + const r = it.next(); + if (r.done) { + return size; } + if (r.value === 'yield') { + return {it, size, children: undefined}; + } + size++; } - return size; } // During a fetch, keep the children read to count them and make the // node's relationship return them, so that the consumer of the node does // not fetch them again. The exists subquery is limited, so this holds at // most a few nodes. `node.relationships` is created per node by the Join. const children: Node[] = []; - for (const n of relationship()) { - if (n === 'yield') { - yield 'yield'; - } else { - children.push(n); + const it = relationship()[Symbol.iterator](); + for (;;) { + const r = it.next(); + if (r.done) { + break; + } + if (r.value === 'yield') { + return {it, size: 0, children}; } + children.push(r.value); } node.relationships[this.#relationshipName] = () => children; return children.length; } + + /** #fetchSizeNow from the 'yield' it stopped at. */ + *#fetchSizeRest( + node: Node, + {it, size, children}: PartialRead, + ): IterableIterator<'yield', number> { + let ended = false; + try { + yield 'yield'; + for (;;) { + const r = it.next(); + if (r.done) { + ended = true; + break; + } + if (r.value === 'yield') { + yield 'yield'; + } else if (children) { + children.push(r.value); + } else { + size++; + } + } + } finally { + // The consumer stopped reading at a 'yield'. + if (!ended) { + it.return?.(); + } + } + if (children) { + node.relationships[this.#relationshipName] = () => children; + return children.length; + } + return size; + } } + +const emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false); + +/** A relationship read that stopped at a 'yield'. */ +type PartialRead = { + readonly it: Iterator; + /** Nodes counted so far, when only the size is wanted. */ + readonly size: number; + /** Nodes read so far, when they are kept. */ + readonly children: Node[] | undefined; +}; diff --git a/packages/zql/src/ivm/fan-in.ts b/packages/zql/src/ivm/fan-in.ts index 31b5b8e891..312ebc4f76 100644 --- a/packages/zql/src/ivm/fan-in.ts +++ b/packages/zql/src/ivm/fan-in.ts @@ -100,12 +100,10 @@ export class FanIn implements FilterOperator { return emptyArray; } - *fanOutDoneReconciling(): Stream<'yield'> { + fanOutDoneReconciling(): Stream<'yield'> { if (this.#inputs.length === 0) { - return; - } - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); + return emptyArray; } + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } } diff --git a/packages/zql/src/ivm/fan-out.ts b/packages/zql/src/ivm/fan-out.ts index ba18b62b95..03837b3c9c 100644 --- a/packages/zql/src/ivm/fan-out.ts +++ b/packages/zql/src/ivm/fan-out.ts @@ -1,4 +1,6 @@ +import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts'; import {must} from '../../../shared/src/must.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import {ChangeIndex} from './change-index.ts'; import type {Change} from './change.ts'; import type {Node} from './data.ts'; @@ -9,7 +11,7 @@ import type { FilterOutput, } from './filter-operators.ts'; import type {InputBase} from './operator.ts'; -import type {Stream} from './stream.ts'; +import {andThen, eachStream, type Stream} from './stream.ts'; /** * Forks a stream into multiple streams. @@ -62,35 +64,89 @@ export class FanOut implements FilterOperator { } } - *filter(node: Node): IterableIterator<'yield', boolean> { - let result = false; - for (const output of this.#outputs) { - result = (yield* output.filter(node)) || result; - if (result) { - return true; + filter(node: Node): IterableIterator<'yield', boolean> { + const outputs = this.#outputs; + for (let i = 0; i < outputs.length; i++) { + const it = outputs[i].filter(node); + const r = it.next(); + if (!r.done) { + // A branch yielded: the rest is read lazily. + return this.#filterRest(node, i, it); + } + if (r.value) { + return emptyIteratorReturnTrue; } } - return result; - } - - *push(change: Change) { - for (const out of this.#outputs) { - yield* out.push(change, this); - } - yield* must( - this.#fanIn, - 'fan-out must have a corresponding fan-in set!', - ).fanOutDonePushingToAllBranches(change[ChangeIndex.TYPE]); + return emptyIteratorReturnFalse; } - *reconcile(_pusher: InputBase): Stream<'yield'> { - for (const output of this.#outputs) { - if (output.reconcile) { - yield* output.reconcile(this); + /** filter() from the branch `i`, whose answer `it` has just yielded. */ + *#filterRest( + node: Node, + i: number, + it: Iterator<'yield', boolean>, + ): IterableIterator<'yield', boolean> { + let answered = false; + try { + yield 'yield'; + for (;;) { + const r = it.next(); + if (r.done) { + answered = true; + if (r.value) { + return true; + } + break; + } + yield r.value; + } + } finally { + // The consumer stopped reading while the branch was yielding. + if (!answered) { + it.return?.(); } } - if (this.#fanIn) { - yield* this.#fanIn.fanOutDoneReconciling(); + const outputs = this.#outputs; + for (i++; i < outputs.length; i++) { + if (yield* outputs[i].filter(node)) { + return true; + } } + return false; + } + + push(change: Change): Stream<'yield'> { + const outputs = this.#outputs; + let i = 0; + return andThen( + eachStream(() => + i === outputs.length ? undefined : outputs[i++].push(change, this), + ), + () => + must( + this.#fanIn, + 'fan-out must have a corresponding fan-in set!', + ).fanOutDonePushingToAllBranches(change[ChangeIndex.TYPE]), + ); + } + + reconcile(_pusher: InputBase): Stream<'yield'> { + const outputs = this.#outputs; + let i = 0; + return andThen( + eachStream(() => { + while (i < outputs.length) { + const output = outputs[i++]; + if (output.reconcile) { + return output.reconcile(this); + } + } + return undefined; + }), + () => (this.#fanIn ? this.#fanIn.fanOutDoneReconciling() : emptyArray), + ); } } + +const emptyIteratorReturnTrue = makeEmptyIteratorWithReturn(true); +const emptyIteratorReturnFalse = makeEmptyIteratorWithReturn(false); diff --git a/packages/zql/src/ivm/filter-operators.ts b/packages/zql/src/ivm/filter-operators.ts index 359fe77767..3600e9053b 100644 --- a/packages/zql/src/ivm/filter-operators.ts +++ b/packages/zql/src/ivm/filter-operators.ts @@ -1,11 +1,12 @@ import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {BuilderDelegate} from '../builder/builder.ts'; import type {NoSubqueryCondition} from '../builder/filter.ts'; import type {Change} from './change.ts'; import {type Node} from './data.ts'; import type {FetchRequest, Input, InputBase, Output} from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import {type Stream} from './stream.ts'; +import {doneResult, type Stream} from './stream.ts'; /** * The `where` clause of a ZQL query is implemented using a sub-graph of @@ -98,35 +99,136 @@ export class FilterStart implements FilterInput, Output { return this.#output.push(change, this); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } - *fetch(req: FetchRequest): Stream { + fetch(req: FetchRequest): Stream { const mergedFilter = mergeFilters(req.filter, this.#condition); const childReq = mergedFilter === req.filter ? req : {...req, filter: mergedFilter}; - this.#output.beginFilter(); + return new FilteredNodes(this.#input, childReq, this.#output); + } +} + +/** + * The nodes of FilterStart's input that pass the filters. An iterator rather + * than a generator, see `mapNodes` in stream.ts. + * + * Nothing happens until the first `next()`, so that `beginFilter` and + * `endFilter` bracket the iteration, and `endFilter` runs however the + * iteration ends: at the end of the input, on an error or when the consumer + * stops early. + */ +class FilteredNodes implements IterableIterator { + readonly #input: Input; + readonly #req: FetchRequest; + readonly #output: FilterOutput; + #it: Iterator | undefined = undefined; + #begun = false; + #ended = false; + // A filter() answer that yielded, and the result holding its node. + #pending: Iterator<'yield', boolean> | undefined = undefined; + #pendingResult: IteratorResult | undefined = undefined; + + constructor(input: Input, req: FetchRequest, output: FilterOutput) { + this.#input = input; + this.#req = req; + this.#output = output; + } + + [Symbol.iterator](): this { + return this; + } + + next(): IteratorResult { + if (this.#ended) { + return doneResult; + } + let it = this.#it; try { - for (const node of this.#input.fetch(childReq)) { - if (node === 'yield') { - yield node; - continue; + if (it === undefined) { + // A filter that failed to begin is not ended. + this.#output.beginFilter(); + this.#begun = true; + it = this.#it = this.#input.fetch(this.#req)[Symbol.iterator](); + } + for (;;) { + let r: IteratorResult; + let answer: IteratorResult<'yield', boolean>; + if (this.#pending !== undefined) { + // oxlint-disable-next-line typescript/no-non-null-assertion + r = this.#pendingResult!; + answer = this.#pending.next(); + } else { + r = it.next(); + if (r.done) { + this.#end(); + return r; + } + if (r.value === 'yield') { + return r; + } + this.#pending = this.#output.filter(r.value); + this.#pendingResult = r; + answer = this.#pending.next(); } - if (yield* this.#output.filter(node)) { - yield node; + if (!answer.done) { + return yieldResult; } + this.#pending = undefined; + this.#pendingResult = undefined; + if (answer.value) { + return r; + } + } + } catch (e) { + // As a `for...of` does when its body throws. + try { + it?.return?.(); + } finally { + this.#end(); + } + throw e; + } + } + + return(): IteratorResult { + if (!this.#ended) { + // A filter that has yielded is closed first, then the input, as a + // generator suspended in `yield* filter(node)` inside its `for...of` + // would close them. + const pending = this.#pending; + this.#pending = undefined; + this.#pendingResult = undefined; + try { + pending?.return?.(); + } finally { + try { + this.#it?.return?.(); + } finally { + this.#end(); + } + } + } + return doneResult; + } + + #end() { + if (!this.#ended) { + this.#ended = true; + if (this.#begun) { + this.#output.endFilter(); } - } finally { - // finally is important if an exception is thrown or - // if the stream is not fully consumed. - this.#output.endFilter(); } } } +const yieldResult: IteratorResult = Object.freeze({ + value: 'yield', + done: false, +}); + function mergeFilters( reqFilter: NoSubqueryCondition | undefined, ownCondition: NoSubqueryCondition | undefined, @@ -179,10 +281,8 @@ export class FilterEnd implements Input, FilterOutput { return this.#output.push(change, this); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } } diff --git a/packages/zql/src/ivm/filter-push.ts b/packages/zql/src/ivm/filter-push.ts index 7de20e079f..cfc006ac4f 100644 --- a/packages/zql/src/ivm/filter-push.ts +++ b/packages/zql/src/ivm/filter-push.ts @@ -1,4 +1,5 @@ import {unreachable} from '../../../shared/src/asserts.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {Row} from '../../../zero-protocol/src/data.ts'; import {ChangeIndex} from './change-index.ts'; import {ChangeType} from './change-type.ts'; @@ -7,31 +8,24 @@ import {maybeSplitAndPushEditChange} from './maybe-split-and-push-edit-change.ts import type {InputBase, Output} from './operator.ts'; import type {Stream} from './stream.ts'; -export function* filterPush( +export function filterPush( change: Change, output: Output, pusher: InputBase, predicate?: (row: Row) => boolean, ): Stream<'yield'> { if (!predicate) { - yield* output.push(change, pusher); - return; + return output.push(change, pusher); } switch (change[ChangeIndex.TYPE]) { case ChangeType.ADD: case ChangeType.REMOVE: - if (predicate(change[ChangeIndex.NODE].row)) { - yield* output.push(change, pusher); - } - break; case ChangeType.CHILD: - if (predicate(change[ChangeIndex.NODE].row)) { - yield* output.push(change, pusher); - } - break; + return predicate(change[ChangeIndex.NODE].row) + ? output.push(change, pusher) + : emptyArray; case ChangeType.EDIT: - yield* maybeSplitAndPushEditChange(change, predicate, output, pusher); - break; + return maybeSplitAndPushEditChange(change, predicate, output, pusher); default: unreachable(change); } diff --git a/packages/zql/src/ivm/filter.ts b/packages/zql/src/ivm/filter.ts index e879b02c42..68db537adf 100644 --- a/packages/zql/src/ivm/filter.ts +++ b/packages/zql/src/ivm/filter.ts @@ -1,4 +1,5 @@ import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {Row} from '../../../zero-protocol/src/data.ts'; import type {Change} from './change.ts'; import {type Node} from './data.ts'; @@ -61,10 +62,8 @@ export class Filter implements FilterOperator { return filterPush(change, this.#output, this, this.#predicate); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } } diff --git a/packages/zql/src/ivm/join-index-tap.ts b/packages/zql/src/ivm/join-index-tap.ts index 21cf98afc6..636d522f46 100644 --- a/packages/zql/src/ivm/join-index-tap.ts +++ b/packages/zql/src/ivm/join-index-tap.ts @@ -1,4 +1,5 @@ import {unreachable} from '../../../shared/src/asserts.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import {ChangeIndex} from './change-index.ts'; import {ChangeType} from './change-type.ts'; import type {Change} from './change.ts'; @@ -13,7 +14,7 @@ import { type Output, } from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import type {Stream} from './stream.ts'; +import {type Stream, tapNodes} from './stream.ts'; /** * Keeps the parent indexes of a pipeline's EXISTS joins equal to the rows @@ -55,17 +56,16 @@ export class JoinIndexTap implements Operator { this.#input.destroy(); } - *fetch(req: FetchRequest): Stream { - for (const node of this.#input.fetch(req)) { - if (node !== 'yield') { - for (const index of this.#indexes) { - index.add(node.row); - } - } - yield node; - } + fetch(req: FetchRequest): Stream { + return tapNodes(this.#input.fetch(req), this.#indexNode); } + readonly #indexNode = (node: Node): void => { + for (const index of this.#indexes) { + index.add(node.row); + } + }; + push(change: Change): Stream<'yield'> { switch (change[ChangeIndex.TYPE]) { case ChangeType.ADD: @@ -92,9 +92,7 @@ export class JoinIndexTap implements Operator { return this.#output.push(change, this); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } } diff --git a/packages/zql/src/ivm/join.ts b/packages/zql/src/ivm/join.ts index 2137ae18a7..86d6104c3c 100644 --- a/packages/zql/src/ivm/join.ts +++ b/packages/zql/src/ivm/join.ts @@ -1,4 +1,5 @@ import {assert, unreachable} from '../../../shared/src/asserts.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {CompoundKey, System} from '../../../zero-protocol/src/ast.ts'; import type {Row, Value} from '../../../zero-protocol/src/data.ts'; import {ChangeIndex} from './change-index.ts'; @@ -28,7 +29,13 @@ import { type Output, } from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import {type Stream} from './stream.ts'; +import { + andFinally, + eachStream, + mapNodes, + type Stream, + yieldOnce, +} from './stream.ts'; import { isInParentFetch, readParentFetchBounds, @@ -170,10 +177,8 @@ export class Join implements Input { }); } - *#reconcile(): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + #reconcile(): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } destroy(): void { @@ -193,22 +198,20 @@ export class Join implements Input { return this.#schema; } - *fetch(req: FetchRequest): Stream { - for (const parentNode of this.#parent.fetch(req)) { - if (parentNode === 'yield') { - yield parentNode; - continue; - } - this.#indexParentRow(parentNode.row); - yield this.#processParentNode(parentNode.row, parentNode.relationships); - } + fetch(req: FetchRequest): Stream { + return mapNodes(this.#parent.fetch(req), this.#fetchParentNode); } - *#pushParent(change: Change): Stream<'yield'> { + readonly #fetchParentNode = (parentNode: Node): Node => { + this.#indexParentRow(parentNode.row); + return this.#processParentNode(parentNode.row, parentNode.relationships); + }; + + #pushParent(change: Change): Stream<'yield'> { switch (change[ChangeIndex.TYPE]) { case ChangeType.ADD: this.#indexParentRow(change[ChangeIndex.NODE].row); - yield* this.#output.push( + return this.#output.push( makeAddChange( this.#processParentNode( change[ChangeIndex.NODE].row, @@ -217,10 +220,9 @@ export class Join implements Input { ), this, ); - break; case ChangeType.REMOVE: this.#unindexParentRow(change[ChangeIndex.NODE].row); - yield* this.#output.push( + return this.#output.push( makeRemoveChange( this.#processParentNode( change[ChangeIndex.NODE].row, @@ -229,9 +231,8 @@ export class Join implements Input { ), this, ); - break; case ChangeType.CHILD: - yield* this.#output.push( + return this.#output.push( makeChildChange( this.#processParentNode( change[ChangeIndex.NODE].row, @@ -241,7 +242,6 @@ export class Join implements Input { ), this, ); - break; case ChangeType.EDIT: { // Assert the edit could not change the relationship. assert( @@ -254,7 +254,7 @@ export class Join implements Input { ); this.#unindexParentRow(change[ChangeIndex.OLD_NODE].row); this.#indexParentRow(change[ChangeIndex.NODE].row); - yield* this.#output.push( + return this.#output.push( makeEditChange( this.#processParentNode( change[ChangeIndex.NODE].row, @@ -267,22 +267,18 @@ export class Join implements Input { ), this, ); - break; } default: unreachable(change); } } - *#pushChild(change: Change): Stream<'yield'> { + #pushChild(change: Change): Stream<'yield'> { switch (change[ChangeIndex.TYPE]) { case ChangeType.ADD: case ChangeType.REMOVE: - yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change); - break; case ChangeType.CHILD: - yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change); - break; + return this.#pushChildChange(change[ChangeIndex.NODE].row, change); case ChangeType.EDIT: { const childRow = change[ChangeIndex.NODE].row; const oldChildRow = change[ChangeIndex.OLD_NODE].row; @@ -291,8 +287,7 @@ export class Join implements Input { rowEqualsForCompoundKey(oldChildRow, childRow, this.#childKey), 'Child edit must not change relationship.', ); - yield* this.#pushChildChange(childRow, change); - break; + return this.#pushChildChange(childRow, change); } default: @@ -300,78 +295,109 @@ export class Join implements Input { } } - *#pushChildChange(childRow: Row, change: Change): Stream<'yield'> { + #pushChildChange(childRow: Row, change: Change): Stream<'yield'> { this.#inprogressChildChange = change; this.#inprogressChildChangePosition = undefined; this.#inprogressReachedParents = this.#parent.getSchema().sort === undefined ? new Set() : undefined; + let s: Stream<'yield'>; try { - const constraint = buildJoinConstraint( - childRow, - this.#childKey, - this.#parentKey, + s = this.#pushChildChangeToParents(childRow, change); + } catch (e) { + this.#endChildChange(); + throw e; + } + return andFinally(s, this.#endChildChange); + } + + readonly #endChildChange = (): void => { + this.#inprogressChildChange = undefined; + this.#inprogressChildChangePosition = undefined; + this.#inprogressReachedParents = undefined; + this.#inprogressParentFetchBounds = undefined; + }; + + #pushChildChangeToParents(childRow: Row, change: Change): Stream<'yield'> { + const constraint = buildJoinConstraint( + childRow, + this.#childKey, + this.#parentKey, + ); + if (!constraint) { + return emptyArray; + } + const partitions = this.#parentIndex.lookup(childRow, this.#childKey); + let fetchConstraints: Record[]; + if (partitions) { + fetchConstraints = partitions.map(partitionConstraint => + partitionConstraint + ? {...constraint, ...partitionConstraint} + : constraint, ); - if (constraint) { - const partitions = this.#parentIndex.lookup(childRow, this.#childKey); - let fetchConstraints: Record[]; - if (partitions) { - fetchConstraints = partitions.map(partitionConstraint => - partitionConstraint - ? {...constraint, ...partitionConstraint} - : constraint, - ); - } else if (change[ChangeIndex.TYPE] === this.#qualifyingChildChange) { - fetchConstraints = [constraint]; - } else { - return; - } + } else if (change[ChangeIndex.TYPE] === this.#qualifyingChildChange) { + fetchConstraints = [constraint]; + } else { + return emptyArray; + } - if (this.#boundProvider) { - this.#inprogressParentFetchBounds = readParentFetchBounds( - this.#boundProvider, - fetchConstraints, - ); - } + if (this.#boundProvider) { + this.#inprogressParentFetchBounds = readParentFetchBounds( + this.#boundProvider, + fetchConstraints, + ); + } - let parentNodeStream: Stream; - if (fetchConstraints.length === 1) { - parentNodeStream = this.#parent.fetch({ - constraint: fetchConstraints[0], - }); - } else { - const streams = fetchConstraints.map(c => - this.#parent.fetch({constraint: c}), - ); - const compare = (a: Node, b: Node) => - this.#schema.compareRows(a.row, b.row); - parentNodeStream = mergeSortedStreams(streams, compare); - } + let parentNodeStream: Stream; + if (fetchConstraints.length === 1) { + parentNodeStream = this.#parent.fetch({ + constraint: fetchConstraints[0], + }); + } else { + const streams = fetchConstraints.map(c => + this.#parent.fetch({constraint: c}), + ); + const compare = (a: Node, b: Node) => + this.#schema.compareRows(a.row, b.row); + parentNodeStream = mergeSortedStreams(streams, compare); + } - for (const parentNode of parentNodeStream) { - if (parentNode === 'yield') { - yield parentNode; - continue; - } - this.#inprogressChildChangePosition = parentNode.row; - this.#inprogressReachedParents?.add( - canonicalKey(parentNode.row, this.#schema.primaryKey), - ); - const childChange = makeChildChange( - this.#processParentNode(parentNode.row, parentNode.relationships), - { - relationshipName: this.#relationshipName, - change, - }, - ); - yield* this.#output.push(childChange, this); - } + return this.#pushChildChangeFrom( + parentNodeStream[Symbol.iterator](), + change, + ); + } + + /** + * Pushes the child change to each parent in `it`. Closes `it` if it stops + * before the end: on an error, or when the stream returned is not read to + * its end. + */ + #pushChildChangeFrom( + it: Iterator, + change: Change, + ): Stream<'yield'> { + return eachStream(() => { + const r = it.next(); + if (r.done) { + return undefined; } - } finally { - this.#inprogressChildChange = undefined; - this.#inprogressChildChangePosition = undefined; - this.#inprogressReachedParents = undefined; - this.#inprogressParentFetchBounds = undefined; - } + const parentNode = r.value; + if (parentNode === 'yield') { + return yieldOnce; + } + this.#inprogressChildChangePosition = parentNode.row; + this.#inprogressReachedParents?.add( + canonicalKey(parentNode.row, this.#schema.primaryKey), + ); + const childChange = makeChildChange( + this.#processParentNode(parentNode.row, parentNode.relationships), + { + relationshipName: this.#relationshipName, + change, + }, + ); + return this.#output.push(childChange, this); + }, it); } /** diff --git a/packages/zql/src/ivm/memory-source.ts b/packages/zql/src/ivm/memory-source.ts index 5f450449aa..1af86a568b 100644 --- a/packages/zql/src/ivm/memory-source.ts +++ b/packages/zql/src/ivm/memory-source.ts @@ -8,6 +8,7 @@ import { sortByNaturalRuns, } from '../../../shared/src/natural-runs.ts'; import {assignProperty} from '../../../shared/src/objects.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type { Condition, Ordering, @@ -59,7 +60,14 @@ import type { SourceInput, } from './source.ts'; import {makeSourceChangeAdd, makeSourceChangeRemove} from './source.ts'; -import {consume, type Stream} from './stream.ts'; +import { + andFinally, + andThen, + consume, + doneResult, + eachStream, + type Stream, +} from './stream.ts'; export type Overlay = { epoch: number; @@ -384,7 +392,7 @@ export class MemorySource implements Source { return [...this.#indexes.keys()]; } - *#fetch(req: FetchRequest, conn: Connection): Stream { + #fetch(req: FetchRequest, conn: Connection): Stream { // multiConstraints is handled by driving sub-fetches off the first // entry's values and post-filtering matches against any remaining // entries. TableSource implements multi-IN natively via SQL `AND` of @@ -395,8 +403,7 @@ export class MemorySource implements Source { req.multiConstraints && req.multiConstraints.some(mc => mc.length > 0) ) { - yield* this.#fetchMulti(req, conn); - return; + return this.#fetchMulti(req, conn); } const requestedSort = must(conn.sort); const {compareRows} = conn; @@ -511,17 +518,11 @@ export class MemorySource implements Source { const overlayActive = this.#overlay && conn.lastPushedEpoch >= this.#overlay.epoch; if (!overlayActive && !req.start && !pkConstraint) { - const {constraint} = req; - for (const row of rowsIterable) { - if (constraint && !constraintMatchesRow(constraint, row)) { - break; - } - if (mergedFilterPredicate && !mergedFilterPredicate(row)) { - continue; - } - yield {row, relationships: {}}; - } - return; + return new SourceNodes( + rowsIterable, + req.constraint, + mergedFilterPredicate, + ); } const withOverlay = generateWithOverlay( @@ -564,7 +565,7 @@ export class MemorySource implements Source { req.constraint, ); - yield* mergedFilterPredicate + return mergedFilterPredicate ? generateWithFilter(withConstraint, mergedFilterPredicate) : withConstraint; } @@ -661,12 +662,20 @@ export class MemorySource implements Source { index.data = BTreeSet.fromSorted(comparator, sorted); } - *push(change: SourceChange): Stream<'yield'> { - for (const result of this.genPush(change)) { - if (result === 'yield') { - yield result; - } - } + push(change: SourceChange): Stream<'yield'> { + const primaryIndex = this.#getPrimaryIndex(); + const {data} = primaryIndex; + const exists = (row: Row) => data.has(row); + const setOverlay = (o: Overlay | undefined) => (this.#overlay = o); + const writeChange = (c: SourceChange) => this.#writeChange(c); + return pushAndWriteWithSplitEdit( + this.#connections, + change, + exists, + setOverlay, + writeChange, + () => ++this.#pushEpoch, + ); } genPush(change: SourceChange) { @@ -741,6 +750,70 @@ function mergePredicates( return row => connPredicate(row) && reqPredicate(row); } +/** + * The fast path of `#fetch`: walks rows from the scan start, stops once the + * constraint no longer matches, drops the rows the filter rejects and wraps + * the rest as Nodes. An iterator rather than a generator, see `mapNodes` in + * stream.ts. + */ +class SourceNodes implements IterableIterator { + readonly #rows: Iterator; + readonly #constraint: Constraint | undefined; + readonly #predicate: ((row: Row) => boolean) | undefined; + #done = false; + + constructor( + rows: Iterator, + constraint: Constraint | undefined, + predicate: ((row: Row) => boolean) | undefined, + ) { + this.#rows = rows; + this.#constraint = constraint; + this.#predicate = predicate; + } + + [Symbol.iterator](): this { + return this; + } + + next(): IteratorResult { + if (this.#done) { + return doneResult; + } + const constraint = this.#constraint; + const predicate = this.#predicate; + for (;;) { + let row: Row; + let matches: boolean; + try { + const r = this.#rows.next(); + if (r.done) { + this.#done = true; + return doneResult; + } + row = r.value; + if (constraint && !constraintMatchesRow(constraint, row)) { + this.#done = true; + return doneResult; + } + matches = !predicate || predicate(row); + } catch (e) { + // A fetch that threw is over, as a generator's is. + this.#done = true; + throw e; + } + if (matches) { + return {value: {row, relationships: {}}, done: false}; + } + } + } + + return(): IteratorResult { + this.#done = true; + return doneResult; + } +} + function* generateWithConstraint( it: Stream, constraint: Constraint | undefined, @@ -769,26 +842,7 @@ export function* genPushAndWriteWithSplitEdit( writeChange: (c: SourceChange) => void, getNextEpoch: () => number, ) { - let shouldSplitEdit = false; - if (change[SourceChangeIndex.TYPE] === ChangeType.EDIT) { - for (const {splitEditKeys} of connections) { - if (splitEditKeys) { - for (const key of splitEditKeys) { - if ( - !valuesEqual( - change[SourceChangeIndex.ROW][key], - change[SourceChangeIndex.OLD_ROW][key], - ) - ) { - shouldSplitEdit = true; - break; - } - } - } - } - } - - if (change[SourceChangeIndex.TYPE] === ChangeType.EDIT && shouldSplitEdit) { + if (mustSplitEdit(connections, change)) { yield* genPushAndWrite( connections, makeSourceChangeRemove(change[SourceChangeIndex.OLD_ROW]), @@ -838,53 +892,19 @@ function* genPush( setOverlay: (o: Overlay | undefined) => void, pushEpoch: number, ) { - if (exists) { - switch (change[SourceChangeIndex.TYPE]) { - case ChangeType.ADD: - assert( - !exists(change[SourceChangeIndex.ROW]), - () => `Row already exists ${stringify(change)}`, - ); - break; - case ChangeType.REMOVE: - assert( - exists(change[SourceChangeIndex.ROW]), - () => `Row not found ${stringify(change)}`, - ); - break; - case ChangeType.EDIT: - assert( - exists(change[SourceChangeIndex.OLD_ROW]), - () => `Row not found ${stringify(change)}`, - ); - break; - default: - unreachable(change); - } - } - + assertCanPush(change, exists); setOverlay({epoch: pushEpoch, change}); try { for (const conn of connections) { const {output, filters, input} = conn; if (output) { conn.lastPushedEpoch = pushEpoch; - const outputChange: Change = - change[SourceChangeIndex.TYPE] === ChangeType.EDIT - ? makeEditChange( - {row: change[SourceChangeIndex.ROW], relationships: {}}, - {row: change[SourceChangeIndex.OLD_ROW], relationships: {}}, - ) - : change[SourceChangeIndex.TYPE] === ChangeType.ADD - ? makeAddChange({ - row: change[SourceChangeIndex.ROW], - relationships: {}, - }) - : makeRemoveChange({ - row: change[SourceChangeIndex.ROW], - relationships: {}, - }); - yield* filterPush(outputChange, output, input, filters?.predicate); + yield* filterPush( + toOutputChange(change), + output, + input, + filters?.predicate, + ); yield undefined; } } @@ -901,6 +921,194 @@ function* genPush( } } +/** + * Whether `change` is an edit of a column that one of the connections splits + * edits on, and so is pushed as a remove and an add. + */ +function mustSplitEdit( + connections: readonly Connection[], + change: SourceChange, +): change is SourceChangeEdit { + if (change[SourceChangeIndex.TYPE] !== ChangeType.EDIT) { + return false; + } + for (const {splitEditKeys} of connections) { + if (splitEditKeys) { + for (const key of splitEditKeys) { + if ( + !valuesEqual( + change[SourceChangeIndex.ROW][key], + change[SourceChangeIndex.OLD_ROW][key], + ) + ) { + return true; + } + } + } + } + return false; +} + +function assertCanPush( + change: SourceChange, + exists: ((row: Row) => boolean) | undefined, +): void { + if (!exists) { + return; + } + switch (change[SourceChangeIndex.TYPE]) { + case ChangeType.ADD: + assert( + !exists(change[SourceChangeIndex.ROW]), + () => `Row already exists ${stringify(change)}`, + ); + break; + case ChangeType.REMOVE: + assert( + exists(change[SourceChangeIndex.ROW]), + () => `Row not found ${stringify(change)}`, + ); + break; + case ChangeType.EDIT: + assert( + exists(change[SourceChangeIndex.OLD_ROW]), + () => `Row not found ${stringify(change)}`, + ); + break; + default: + unreachable(change); + } +} + +/** The change a connection's output gets for a change to the source. */ +function toOutputChange(change: SourceChange): Change { + return change[SourceChangeIndex.TYPE] === ChangeType.EDIT + ? makeEditChange( + {row: change[SourceChangeIndex.ROW], relationships: {}}, + {row: change[SourceChangeIndex.OLD_ROW], relationships: {}}, + ) + : change[SourceChangeIndex.TYPE] === ChangeType.ADD + ? makeAddChange({ + row: change[SourceChangeIndex.ROW], + relationships: {}, + }) + : makeRemoveChange({ + row: change[SourceChangeIndex.ROW], + relationships: {}, + }); +} + +// `genPushAndWriteWithSplitEdit` and the two generators it uses, for +// `MemorySource.push`: run without a generator for as long as nothing yields +// (see `andThen` in stream.ts), and without the `undefined` the generators +// yield after each connection, which `push` drops. `genPush` keeps the +// generators for callers that step through a push connection by connection. +function pushAndWriteWithSplitEdit( + connections: readonly Connection[], + change: SourceChange, + exists: ((row: Row) => boolean) | undefined, + setOverlay: (o: Overlay | undefined) => Overlay | undefined, + writeChange: (c: SourceChange) => void, + getNextEpoch: () => number, +): Stream<'yield'> { + if (mustSplitEdit(connections, change)) { + return andThen( + pushAndWrite( + connections, + makeSourceChangeRemove(change[SourceChangeIndex.OLD_ROW]), + exists, + setOverlay, + writeChange, + getNextEpoch(), + ), + () => + pushAndWrite( + connections, + makeSourceChangeAdd(change[SourceChangeIndex.ROW]), + exists, + setOverlay, + writeChange, + getNextEpoch(), + ), + ); + } + return pushAndWrite( + connections, + change, + exists, + setOverlay, + writeChange, + getNextEpoch(), + ); +} + +function pushAndWrite( + connections: readonly Connection[], + change: SourceChangeAdd | SourceChangeRemove | SourceChangeEdit, + exists: ((row: Row) => boolean) | undefined, + setOverlay: (o: Overlay | undefined) => Overlay | undefined, + writeChange: (c: SourceChange) => void, + pushEpoch: number, +): Stream<'yield'> { + assertCanPush(change, exists); + setOverlay({epoch: pushEpoch, change}); + let s: Stream<'yield'>; + try { + s = pushToConnections(connections, change, pushEpoch); + } catch (e) { + setOverlay(undefined); + throw e; + } + if (s === emptyArray) { + setOverlay(undefined); + writeChange(change); + return emptyArray; + } + return andThen( + andFinally(s, () => setOverlay(undefined)), + () => { + writeChange(change); + return emptyArray; + }, + ); +} + +/** Pushes `change` to each connection, then reconciles each. */ +function pushToConnections( + connections: readonly Connection[], + change: SourceChange, + pushEpoch: number, +): Stream<'yield'> { + let i = 0; + let reconciling = false; + return eachStream(() => { + if (!reconciling) { + while (i < connections.length) { + const conn = connections[i++]; + const {output, filters, input} = conn; + if (output) { + conn.lastPushedEpoch = pushEpoch; + return filterPush( + toOutputChange(change), + output, + input, + filters?.predicate, + ); + } + } + reconciling = true; + i = 0; + } + while (i < connections.length) { + const {output, input} = connections[i++]; + if (output?.reconcile) { + return output.reconcile(input); + } + } + return undefined; + }); +} + export function* generateWithStart( nodes: Iterable, start: Start | undefined, diff --git a/packages/zql/src/ivm/push-accumulated.ts b/packages/zql/src/ivm/push-accumulated.ts index 16217915b7..4994b6c8dc 100644 --- a/packages/zql/src/ivm/push-accumulated.ts +++ b/packages/zql/src/ivm/push-accumulated.ts @@ -13,7 +13,7 @@ import { import type {Node} from './data.ts'; import type {InputBase, Output} from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import type {Stream} from './stream.ts'; +import {andThen, eachStream, type Stream} from './stream.ts'; /** * # pushAccumulatedChanges @@ -84,7 +84,7 @@ import type {Stream} from './stream.ts'; * If an edit enters and is converted to only add or only remove, it exits as that change. * If an edit enters and exits as edits only, it exits as a single edit. */ -export function* pushAccumulatedChanges( +export function pushAccumulatedChanges( accumulatedPushes: Change[], output: Output, pusher: InputBase, @@ -98,7 +98,7 @@ export function* pushAccumulatedChanges( if (accumulatedPushes.length === 0) { // It is possible for no forks to pass along the push. // E.g., if no filters match in any fork. - return; + return emptyArray; } // A union nested in a branch keeps the row but reports its witness changes @@ -152,24 +152,37 @@ export function* pushAccumulatedChanges( candidatesToPush.size > 0, 'Fan-in: witness changes without a change for their row', ); - for (const change of witnessesLeft) { - yield* output.push(change, pusher); + const collapsed = () => + pushCollapsed( + candidatesToPush, + output, + pusher, + fanOutChangeType, + mergeRelationships, + addEmptyRelationships, + isBranchRel, + ); + if (witnessesLeft.length === 0 && witnessesEntered.length === 0) { + return collapsed(); } - yield* pushCollapsed( - candidatesToPush, - output, - pusher, - fanOutChangeType, - mergeRelationships, - addEmptyRelationships, - isBranchRel, + return andThen(pushAll(witnessesLeft, output, pusher), () => + andThen(collapsed(), () => pushAll(witnessesEntered, output, pusher)), + ); +} + +/** Pushes `changes`, one after the other. */ +function pushAll( + changes: readonly Change[], + output: Output, + pusher: InputBase, +): Stream<'yield'> { + let i = 0; + return eachStream(() => + i === changes.length ? undefined : output.push(changes[i++], pusher), ); - for (const change of witnessesEntered) { - yield* output.push(change, pusher); - } } -function* pushCollapsed( +function pushCollapsed( candidatesToPush: Map, output: Output, pusher: InputBase, @@ -195,21 +208,19 @@ function* pushCollapsed( types.length === 1 && types[0] === ChangeType.REMOVE, 'Fan-in:remove expected all removes', ); - yield* output.push( + return output.push( addEmptyRelationships(must(candidatesToPush.get(ChangeType.REMOVE))), pusher, ); - return; case ChangeType.ADD: assert( types.length === 1 && types[0] === ChangeType.ADD, 'Fan-in:add expected all adds', ); - yield* output.push( + return output.push( addEmptyRelationships(must(candidatesToPush.get(ChangeType.ADD))), pusher, ); - return; case ChangeType.EDIT: { assert( types.every( @@ -229,41 +240,47 @@ function* pushCollapsed( // them; branches it entered bring theirs. An edit syncs one row, so // those must travel as child changes around it. if (isBranchRel && (editChange || (addChange && removeChange))) { - if (removeChange) { - yield* pushWitnessDeltas( - removeChange[ChangeIndex.NODE], - ChangeType.REMOVE, - isBranchRel, - output, - pusher, - ); - } const strip = (n: Node): Node => ({ row: n.row, relationships: Object.fromEntries( Object.entries(n.relationships).filter(([k]) => !isBranchRel(k)), ), }); - yield* output.push( - addEmptyRelationships( - editChange ?? - makeEditChange( - strip(must(addChange)[ChangeIndex.NODE]), - strip(must(removeChange)[ChangeIndex.NODE]), + const edit = editChange; + return andThen( + removeChange + ? pushWitnessDeltas( + removeChange[ChangeIndex.NODE], + ChangeType.REMOVE, + isBranchRel, + output, + pusher, + ) + : emptyArray, + () => + andThen( + output.push( + addEmptyRelationships( + edit ?? + makeEditChange( + strip(must(addChange)[ChangeIndex.NODE]), + strip(must(removeChange)[ChangeIndex.NODE]), + ), + ), + pusher, ), - ), - pusher, + () => + addChange + ? pushWitnessDeltas( + addChange[ChangeIndex.NODE], + ChangeType.ADD, + isBranchRel, + output, + pusher, + ) + : emptyArray, + ), ); - if (addChange) { - yield* pushWitnessDeltas( - addChange[ChangeIndex.NODE], - ChangeType.ADD, - isBranchRel, - output, - pusher, - ); - } - return; } // If an `edit` is present, it supersedes `add` and `remove` @@ -275,8 +292,7 @@ function* pushCollapsed( if (removeChange) { editChange = mergeRelationships(editChange, removeChange); } - yield* output.push(addEmptyRelationships(editChange), pusher); - return; + return output.push(addEmptyRelationships(editChange), pusher); } // If `edit` didn't make it through but both `add` and `remove` did, @@ -297,7 +313,7 @@ function* pushCollapsed( // The left filter converts the edit into a remove. // The right filter converts the edit into an add. if (addChange && removeChange) { - yield* output.push( + return output.push( addEmptyRelationships( makeEditChange( addChange[ChangeIndex.NODE], @@ -306,14 +322,12 @@ function* pushCollapsed( ), pusher, ); - return; } - yield* output.push( + return output.push( addEmptyRelationships(must(addChange ?? removeChange)), pusher, ); - return; } case ChangeType.CHILD: { assert( @@ -335,26 +349,32 @@ function* pushCollapsed( if (childChange) { const left = candidatesToPush.get(ChangeType.REMOVE); const entered = candidatesToPush.get(ChangeType.ADD); - if (isBranchRel && left) { - yield* pushWitnessDeltas( - left[ChangeIndex.NODE], - ChangeType.REMOVE, - isBranchRel, - output, - pusher, - ); - } - yield* output.push(childChange, pusher); - if (isBranchRel && entered) { - yield* pushWitnessDeltas( - entered[ChangeIndex.NODE], - ChangeType.ADD, - isBranchRel, - output, - pusher, - ); + if (!isBranchRel) { + return output.push(childChange, pusher); } - return; + return andThen( + left + ? pushWitnessDeltas( + left[ChangeIndex.NODE], + ChangeType.REMOVE, + isBranchRel, + output, + pusher, + ) + : emptyArray, + () => + andThen(output.push(childChange, pusher), () => + entered + ? pushWitnessDeltas( + entered[ChangeIndex.NODE], + ChangeType.ADD, + isBranchRel, + output, + pusher, + ) + : emptyArray, + ), + ); } const addChange = candidatesToPush.get(ChangeType.ADD); @@ -365,15 +385,15 @@ function* pushCollapsed( 'Fan-in:child expected either add or remove, not both', ); - yield* output.push( + return output.push( addEmptyRelationships(must(addChange ?? removeChange)), pusher, ); - return; } default: fanOutChangeType satisfies never; } + return emptyArray; } /** diff --git a/packages/zql/src/ivm/skip.ts b/packages/zql/src/ivm/skip.ts index 8174ed0d9e..d812949bb6 100644 --- a/packages/zql/src/ivm/skip.ts +++ b/packages/zql/src/ivm/skip.ts @@ -1,4 +1,5 @@ import {assert} from '../../../shared/src/asserts.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {Row} from '../../../zero-protocol/src/data.ts'; import {ChangeIndex} from './change-index.ts'; import {ChangeType} from './change-type.ts'; @@ -86,29 +87,25 @@ export class Skip implements Operator { return cmp < 0 || (cmp === 0 && !this.#bound.exclusive); } - *push(change: Change): Stream<'yield'> { - const shouldBePresent = (row: Row) => this.#shouldBePresent(row); + push(change: Change): Stream<'yield'> { if (change[ChangeIndex.TYPE] === ChangeType.EDIT) { - yield* maybeSplitAndPushEditChange( + return maybeSplitAndPushEditChange( change, - shouldBePresent, + row => this.#shouldBePresent(row), this.#output, this, ); - return; } change satisfies AddChange | RemoveChange | ChildChange; - if (shouldBePresent(change[ChangeIndex.NODE].row)) { - yield* this.#output.push(change, this); - } + return this.#shouldBePresent(change[ChangeIndex.NODE].row) + ? this.#output.push(change, this) + : emptyArray; } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } #getStart(req: FetchRequest): Start | undefined | 'empty' { diff --git a/packages/zql/src/ivm/stream.ts b/packages/zql/src/ivm/stream.ts index 087f62e0f0..63c639ff8b 100644 --- a/packages/zql/src/ivm/stream.ts +++ b/packages/zql/src/ivm/stream.ts @@ -1,3 +1,6 @@ +import {emptyArray} from '../../../shared/src/sentinels.ts'; +import type {Node} from './data.ts'; + /** * streams are lazy forward-only iterables. * Once a stream reaches the end it can't be restarted. @@ -24,3 +27,349 @@ export function consume(stream: Stream): void { // Required to prevent some minifiers (e.g. Terser) from removing this empty loop for (const _ of stream); } +/** + * `stream`, then the stream `rest` returns. + * + * A push through a chain of generators costs a generator per operator, though + * a push rarely yields. So an operator does its push right away and returns + * `emptyArray` when all of it is done. It returns a stream only when a push it + * makes downstream, or a fetch it reads, yields. That stream holds the yields + * and, behind them, the rest of the operator's work. + * + * So nothing may follow a downstream push directly. What follows it goes in + * `rest`: + * + * return andThen(output.push(a, this), () => output.push(b, this)); + * + * `rest` runs right away when the first push is done, and otherwise once the + * stream of the first push has been read. Only `emptyArray` says that a push + * is done: any other stream is read first, an empty one too, which is correct + * and only slower. + * + * The stream a push returns must be read: by the time it is returned, the + * operator may have done part of its work and hold state that only the rest + * of the stream clears. + * + * This is for a fixed number of steps. A loop goes through `eachStream`. + */ +export function andThen( + stream: Stream<'yield'>, + rest: () => Stream<'yield'>, +): Stream<'yield'> { + return stream === emptyArray ? rest() : chain(stream, rest); +} + +function* chain( + stream: Stream<'yield'>, + rest: () => Stream<'yield'>, +): Stream<'yield'> { + yield* stream; + const s = rest(); + if (s !== emptyArray) { + yield* s; + } +} + +/** + * Runs `cleanup` once `stream` is done, however it ends: `try { yield* stream + * } finally { cleanup() }` without the generator when there is nothing to + * wait for. The caller runs `cleanup` itself if getting the stream throws. + */ +export function andFinally( + stream: Stream<'yield'>, + cleanup: () => void, +): Stream<'yield'> { + if (stream === emptyArray) { + cleanup(); + return emptyArray; + } + return finallyAfter(stream, cleanup); +} + +function* finallyAfter( + stream: Stream<'yield'>, + cleanup: () => void, +): Stream<'yield'> { + try { + yield* stream; + } finally { + cleanup(); + } +} + +/** + * The streams `next` returns, one after the other, until it returns + * `undefined`: a loop whose body makes one push per round, without a generator + * for as long as every push is done. + * + * Written with `andThen`, such a loop would nest one generator per round that + * is not done, and a push to several thousand parents through an output that + * returns a generator would overflow the stack. Here, the first stream that is + * not done hands the rest of the loop to a single generator. + * + * `open` is an iterator that `next` reads. It is closed if the loop stops + * before `next` returns `undefined`, because `next` or a stream threw or the + * consumer stopped reading, as a `for...of` over that iterator would have + * closed it. + */ +export function eachStream( + next: () => Stream<'yield'> | undefined, + open?: Iterator, +): Stream<'yield'> { + for (;;) { + let s: Stream<'yield'> | undefined; + try { + s = next(); + } catch (e) { + closeAfterError(open); + throw e; + } + if (s === undefined) { + return emptyArray; + } + if (s !== emptyArray) { + return restOfEach(s, next, open); + } + } +} + +function* restOfEach( + first: Stream<'yield'>, + next: () => Stream<'yield'> | undefined, + open: Iterator | undefined, +): Stream<'yield'> { + let ended = false; + try { + yield* first; + for (;;) { + const s = next(); + if (s === undefined) { + break; + } + if (s !== emptyArray) { + yield* s; + } + } + ended = true; + } catch (e) { + ended = true; + closeAfterError(open); + throw e; + } finally { + // The consumer stopped reading. + if (!ended) { + open?.return?.(); + } + } +} + +/** + * Closes `it` on the way out with an error. An error from closing it is + * dropped, as a `for...of` drops it: the first error is the one to report. + */ +function closeAfterError(it: Iterator | undefined): void { + try { + it?.return?.(); + } catch { + // The first error wins. + } +} + +/** One 'yield'. Returned for a round of `eachStream` that was reached by one. */ +export const yieldOnce: Stream<'yield'> = Object.freeze(['yield'] as const); + +// The per-row steps of a fetch are iterators rather than generators, as +// `SkipYieldsStream` is: resuming a generator costs several times what a call +// to `next()` does, and a fetch pays it per row for each operator the row +// passes through. Each one is both the iterable and the iterator, passes +// 'yield' through, hands on the inner iterator's result object where the value +// is unchanged, and closes the inner iterator when it stops early or its +// callback throws, as a generator's `for...of` would. + +type NodeResult = IteratorResult; + +export const doneResult: IteratorReturnResult = Object.freeze({ + value: undefined, + done: true, +}); + +class MappedNodes implements IterableIterator { + readonly #it: Iterator; + readonly #fn: (node: Node) => Node; + #done = false; + + constructor(it: Iterator, fn: (node: Node) => Node) { + this.#it = it; + this.#fn = fn; + } + + [Symbol.iterator](): this { + return this; + } + + next(): NodeResult { + if (this.#done) { + return doneResult; + } + let r: NodeResult; + try { + r = this.#it.next(); + } catch (e) { + // A fetch that threw is over, as a generator's is. + this.#done = true; + throw e; + } + if (r.done) { + this.#done = true; + return r; + } + if (r.value === 'yield') { + return r; + } + let node: Node; + try { + node = this.#fn(r.value); + } catch (e) { + this.#done = true; + closeAfterError(this.#it); + throw e; + } + return {value: node, done: false}; + } + + return(): NodeResult { + if (!this.#done) { + this.#done = true; + this.#it.return?.(); + } + return doneResult; + } +} + +/** Yields `fn(node)` for each node of `stream`. */ +export function mapNodes( + stream: Stream, + fn: (node: Node) => Node, +): Stream { + return new MappedNodes(stream[Symbol.iterator](), fn); +} + +class TappedNodes implements IterableIterator { + readonly #it: Iterator; + readonly #fn: (node: Node) => void; + #done = false; + + constructor(it: Iterator, fn: (node: Node) => void) { + this.#it = it; + this.#fn = fn; + } + + [Symbol.iterator](): this { + return this; + } + + next(): NodeResult { + if (this.#done) { + return doneResult; + } + let r: NodeResult; + try { + r = this.#it.next(); + } catch (e) { + // A fetch that threw is over, as a generator's is. + this.#done = true; + throw e; + } + if (r.done) { + this.#done = true; + } else if (r.value !== 'yield') { + try { + this.#fn(r.value); + } catch (e) { + this.#done = true; + closeAfterError(this.#it); + throw e; + } + } + return r; + } + + return(): NodeResult { + if (!this.#done) { + this.#done = true; + this.#it.return?.(); + } + return doneResult; + } +} + +/** Yields the nodes of `stream`, calling `fn` on each as it goes by. */ +export function tapNodes( + stream: Stream, + fn: (node: Node) => void, +): Stream { + return new TappedNodes(stream[Symbol.iterator](), fn); +} + +class NodesWhile implements IterableIterator { + readonly #it: Iterator; + readonly #fn: (node: Node) => boolean; + #done = false; + + constructor(it: Iterator, fn: (node: Node) => boolean) { + this.#it = it; + this.#fn = fn; + } + + [Symbol.iterator](): this { + return this; + } + + next(): NodeResult { + if (this.#done) { + return doneResult; + } + let r: NodeResult; + try { + r = this.#it.next(); + } catch (e) { + // A fetch that threw is over, as a generator's is. + this.#done = true; + throw e; + } + if (r.done) { + this.#done = true; + return r; + } + if (r.value !== 'yield') { + let passes: boolean; + try { + passes = this.#fn(r.value); + } catch (e) { + this.#done = true; + closeAfterError(this.#it); + throw e; + } + if (!passes) { + return this.return(); + } + } + return r; + } + + return(): NodeResult { + if (!this.#done) { + this.#done = true; + this.#it.return?.(); + } + return doneResult; + } +} + +/** Yields the nodes of `stream` up to the first for which `fn` is false. */ +export function nodesWhile( + stream: Stream, + fn: (node: Node) => boolean, +): Stream { + return new NodesWhile(stream[Symbol.iterator](), fn); +} diff --git a/packages/zql/src/ivm/take-gate.ts b/packages/zql/src/ivm/take-gate.ts index aa7a21f5c9..5dfd5ae45c 100644 --- a/packages/zql/src/ivm/take-gate.ts +++ b/packages/zql/src/ivm/take-gate.ts @@ -1,4 +1,5 @@ import {assert} from '../../../shared/src/asserts.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {Row} from '../../../zero-protocol/src/data.ts'; import type {Change} from './change.ts'; import {constraintMatchesRow, type Constraint} from './constraint.ts'; @@ -12,7 +13,7 @@ import { type Output, } from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import type {Stream} from './stream.ts'; +import {nodesWhile, type Stream} from './stream.ts'; export interface TakeBoundProvider { getBound(constraint?: Constraint): Row | undefined; @@ -125,37 +126,28 @@ export class TakeGate implements Operator, TakeBoundProvider { this.#input.destroy(); } - *push(change: Change, _pusher: InputBase): Stream<'yield'> { - yield* this.#output.push(change, this); + push(change: Change, _pusher: InputBase): Stream<'yield'> { + return this.#output.push(change, this); } - *reconcile(_pusher: InputBase): Stream<'yield'> { - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); - } + reconcile(_pusher: InputBase): Stream<'yield'> { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; } - *fetch(req: FetchRequest): Stream { + fetch(req: FetchRequest): Stream { if (this.#openDepth > 0 || req.reverse) { - yield* this.#input.fetch(req); - return; + return this.#input.fetch(req); } const bound = this.#boundProvider?.getBound(req.constraint); if (!bound) { - yield* this.#input.fetch(req); - return; + return this.#input.fetch(req); } - for (const node of this.#input.fetch(req)) { - if (node === 'yield') { - yield 'yield'; - continue; - } - if (this.#comparator(node.row, bound) > 0) { - return; - } - yield node; - } + const comparator = this.#comparator; + return nodesWhile( + this.#input.fetch(req), + node => comparator(node.row, bound) <= 0, + ); } } diff --git a/packages/zql/src/ivm/take.ts b/packages/zql/src/ivm/take.ts index 5f07a8c2c7..c9941e1c9f 100644 --- a/packages/zql/src/ivm/take.ts +++ b/packages/zql/src/ivm/take.ts @@ -1,5 +1,6 @@ import {assert, unreachable} from '../../../shared/src/asserts.ts'; import {hasOwn} from '../../../shared/src/has-own.ts'; +import {emptyArray} from '../../../shared/src/sentinels.ts'; import type {Row, Value} from '../../../zero-protocol/src/data.ts'; import type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts'; import {assertOrderingIncludesPK} from '../query/complete-ordering.ts'; @@ -23,7 +24,13 @@ import { type Storage, } from './operator.ts'; import type {SourceSchema} from './schema.ts'; -import {type Stream} from './stream.ts'; +import { + andFinally, + andThen, + doneResult, + eachStream, + type Stream, +} from './stream.ts'; import type {TakeBoundProvider, TakeGate} from './take-gate.ts'; type TakeState = { @@ -137,7 +144,7 @@ export class Take implements Operator, TakeBoundProvider { return takeState.bound; } - *fetch(req: FetchRequest): Stream { + fetch(req: FetchRequest): Stream { assert( !this.#partitionKey || (req.constraint !== undefined && @@ -149,39 +156,25 @@ export class Take implements Operator, TakeBoundProvider { const takeState = this.#storage.get(takeStateKey); if (!takeState) { if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) { - yield* this.#initialFetch(req, takeStateKey); + return this.#initialFetch(req, takeStateKey); } - return; + return emptyArray; } if (takeState.bound === undefined) { - return; - } - let count = 0; - for (const inputNode of this.#input.fetch(req)) { - if (inputNode === 'yield') { - yield inputNode; - continue; - } - if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) { - return; - } - if ( - this.#rowHiddenFromFetch && - this.getSchema().compareRows( - this.#rowHiddenFromFetch, - inputNode.row, - ) === 0 - ) { - continue; - } - yield inputNode; - count++; - if (count >= this.#limit) { - return; - } + return emptyArray; } + return new TakeNodes( + this.#input.fetch(req)[Symbol.iterator](), + this.getSchema().compareRows, + takeState.bound, + this.#limit, + this.#getRowHiddenFromFetch, + ); } + readonly #getRowHiddenFromFetch = (): Row | undefined => + this.#rowHiddenFromFetch; + *#initialFetch( req: FetchRequest, takeStateKey: string, @@ -262,17 +255,16 @@ export class Take implements Operator, TakeBoundProvider { }; } - *push(change: Change): Stream<'yield'> { + push(change: Change): Stream<'yield'> { if (change[ChangeIndex.TYPE] === ChangeType.EDIT) { - yield* this.#pushEditChange(change); - return; + return this.#pushEditChange(change); } const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint( change[ChangeIndex.NODE].row, ); if (!takeState) { - return; + return emptyArray; } const {compareRows} = this.getSchema(); @@ -288,7 +280,7 @@ export class Take implements Operator, TakeBoundProvider { takeState.bound === undefined || compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0 ) { - return; + return emptyArray; } } const nextSize = takeState.size + 1; @@ -303,78 +295,65 @@ export class Take implements Operator, TakeBoundProvider { ? change[ChangeIndex.NODE].row : takeState.bound, ); - yield* this.#output.push(change, this); - return; + return this.#output.push(change, this); } // size === limit if ( takeState.bound === undefined || compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0 ) { - return; + return emptyArray; } // added row < bound - let beforeBoundNode: Node | undefined; - let boundNode: Node | undefined; - if (this.#limit === 1) { - for (const node of this.#input.fetch({ - start: { - row: takeState.bound, - basis: 'at', - }, - constraint, - })) { - if (node === 'yield') { - yield node; - continue; - } - boundNode = node; - break; - } - } else { - for (const node of this.#input.fetch({ - start: { - row: takeState.bound, - basis: 'at', - }, - constraint, - reverse: true, - })) { - if (node === 'yield') { - yield node; - continue; - } else if (boundNode === undefined) { - boundNode = node; - } else { - beforeBoundNode = node; - break; - } - } - } - assert( - boundNode !== undefined, - 'Take: boundNode must be found during fetch', - ); - const removeChange = makeRemoveChange(boundNode); - // Remove before add to maintain invariant that - // output size <= limit. - this.#setTakeState( - takeStateKey, - takeState.size, - beforeBoundNode === undefined || - compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0 - ? change[ChangeIndex.NODE].row - : beforeBoundNode.row, - ); - yield* this.#pushWithRowHiddenFromFetch( - change[ChangeIndex.NODE].row, - removeChange, + // The bound node and, unless the limit is 1, the node before it. + return this.#withFirst( + this.#limit === 1 + ? { + start: { + row: takeState.bound, + basis: 'at', + }, + constraint, + } + : { + start: { + row: takeState.bound, + basis: 'at', + }, + constraint, + reverse: true, + }, + this.#limit === 1 ? 1 : 2, + false, + ([boundNode, beforeBoundNode]) => { + assert( + boundNode !== undefined, + 'Take: boundNode must be found during fetch', + ); + const removeChange = makeRemoveChange(boundNode); + // Remove before add to maintain invariant that + // output size <= limit. + this.#setTakeState( + takeStateKey, + takeState.size, + beforeBoundNode === undefined || + compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0 + ? change[ChangeIndex.NODE].row + : beforeBoundNode.row, + ); + return andThen( + this.#pushWithRowHiddenFromFetch( + change[ChangeIndex.NODE].row, + removeChange, + ), + () => this.#output.push(change, this), + ); + }, ); - yield* this.#output.push(change, this); } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) { if (takeState.bound === undefined) { // change is after bound - return; + return emptyArray; } const compToBound = compareRows( change[ChangeIndex.NODE].row, @@ -382,55 +361,135 @@ export class Take implements Operator, TakeBoundProvider { ); if (compToBound > 0 || (compToBound < 0 && this.#limit === 1)) { // change is not in window - return; + return emptyArray; } - let beforeBoundNode: Node | undefined; - for (const node of this.#input.fetch({ - start: { - row: takeState.bound, - basis: 'after', + const {bound} = takeState; + return this.#withFirst( + { + start: { + row: bound, + basis: 'after', + }, + constraint, + reverse: true, }, - constraint, - reverse: true, - })) { - if (node === 'yield') { - yield node; - continue; + 1, + false, + ([beforeBoundNode]) => { + const finalBound = + takeState.size - 1 === 0 + ? undefined + : compToBound < 0 + ? bound + : beforeBoundNode?.row; + if (!this.#dirtyPartitions.has(takeStateKey)) { + // Snapshot the pre-removal bound as the gate bound on the first removal that + // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return + // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits. + this.#dirtyPartitions.set(takeStateKey, { + constraint, + gateBound: bound, + }); + } + this.#setTakeState(takeStateKey, takeState.size - 1, finalBound); + return this.#output.push(change, this); + }, + ); + } + // A 'child' change should be pushed to output if its row + // is <= bound. + if ( + takeState.bound && + compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0 + ) { + return this.#output.push(change, this); + } + return emptyArray; + } + + /** + * Reads the first `n` nodes of a fetch from the input and calls `rest` with + * them (fewer if the fetch has fewer). With `gated`, the take gate is open + * while the fetch is read. + * + * `rest` runs right away unless the fetch yields. Then the stream returned + * holds the yields, with what is left of the fetch and `rest` behind them + * (see `andThen` in stream.ts). + */ + #withFirst( + req: FetchRequest, + n: number, + gated: boolean, + rest: (nodes: Node[]) => Stream<'yield'>, + ): Stream<'yield'> { + const nodes: Node[] = []; + let closeGate = gated; + if (gated) { + this.#takeGate?.open(); + } + try { + const it = this.#input.fetch(req)[Symbol.iterator](); + for (;;) { + const r = it.next(); + if (r.done) { + break; + } + if (r.value === 'yield') { + closeGate = false; + return this.#withFirstRest(it, nodes, n, gated, rest); + } + nodes.push(r.value); + if (nodes.length === n) { + it.return?.(); + break; } - beforeBoundNode = node; - break; } + } finally { + if (closeGate) { + this.#takeGate?.close(); + } + } + return rest(nodes); + } - const finalBound = - takeState.size - 1 === 0 - ? undefined - : compToBound < 0 - ? takeState.bound - : beforeBoundNode?.row; - if (!this.#dirtyPartitions.has(takeStateKey)) { - // Snapshot the pre-removal bound as the gate bound on the first removal that - // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return - // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits. - this.#dirtyPartitions.set(takeStateKey, { - constraint, - gateBound: takeState.bound, - }); + /** #withFirst from the 'yield' it stopped at. */ + *#withFirstRest( + it: Iterator, + nodes: Node[], + n: number, + gated: boolean, + rest: (nodes: Node[]) => Stream<'yield'>, + ): Stream<'yield'> { + let finished = false; + try { + yield 'yield'; + for (;;) { + const r = it.next(); + if (r.done) { + finished = true; + break; + } + if (r.value === 'yield') { + yield 'yield'; + continue; + } + nodes.push(r.value); + if (nodes.length === n) { + break; + } } - this.#setTakeState(takeStateKey, takeState.size - 1, finalBound); - yield* this.#output.push(change, this); - } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) { - // A 'child' change should be pushed to output if its row - // is <= bound. - if ( - takeState.bound && - compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0 - ) { - yield* this.#output.push(change, this); + } finally { + if (!finished) { + it.return?.(); + } + if (gated) { + this.#takeGate?.close(); } } + yield* rest(nodes); } - *#pushEditChange(change: EditChange): Stream<'yield'> { + #pushEditChange(change: EditChange): Stream<'yield'> { assert( !this.#partitionKeyComparator || this.#partitionKeyComparator( @@ -444,16 +503,14 @@ export class Take implements Operator, TakeBoundProvider { change[ChangeIndex.OLD_NODE].row, ); if (!takeState) { - return; + return emptyArray; } assert(takeState.bound, 'Bound should be set'); + const {bound} = takeState; const {compareRows} = this.getSchema(); - const oldCmp = compareRows( - change[ChangeIndex.OLD_NODE].row, - takeState.bound, - ); - const newCmp = compareRows(change[ChangeIndex.NODE].row, takeState.bound); + const oldCmp = compareRows(change[ChangeIndex.OLD_NODE].row, bound); + const newCmp = compareRows(change[ChangeIndex.NODE].row, bound); const replaceBoundAndForwardChange = () => { this.#setTakeState( @@ -469,89 +526,82 @@ export class Take implements Operator, TakeBoundProvider { // The new row is the new bound. if (newCmp === 0) { // no need to update the state since we are keeping the bounds - yield* this.#output.push(change, this); - return; + return this.#output.push(change, this); } if (newCmp < 0) { if (this.#limit === 1) { - yield* replaceBoundAndForwardChange(); - return; + return replaceBoundAndForwardChange(); } // New row will be in the result but it might not be the bounds any // more. We need to find the row before the bounds to determine the new // bounds. - - let beforeBoundNode: Node | undefined; - for (const node of this.#input.fetch({ - start: { - row: takeState.bound, - basis: 'after', + return this.#withFirst( + { + start: { + row: bound, + basis: 'after', + }, + constraint, + reverse: true, + }, + 1, + false, + ([beforeBoundNode]) => { + assert( + beforeBoundNode !== undefined, + 'Take: beforeBoundNode must be found during fetch', + ); + + this.#setTakeState( + takeStateKey, + takeState.size, + beforeBoundNode.row, + ); + return this.#output.push(change, this); }, - constraint, - reverse: true, - })) { - if (node === 'yield') { - yield node; - continue; - } - beforeBoundNode = node; - break; - } - assert( - beforeBoundNode !== undefined, - 'Take: beforeBoundNode must be found during fetch', ); - - this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row); - yield* this.#output.push(change, this); - return; } assert(newCmp > 0, 'New comparison must be greater than 0'); // Find the first item at the old bounds. This will be the new bounds. - let newBoundNode: Node | undefined; - this.#takeGate?.open(); - try { - for (const node of this.#input.fetch({ + return this.#withFirst( + { start: { - row: takeState.bound, + row: bound, basis: 'at', }, constraint, - })) { - if (node === 'yield') { - yield node; - continue; + }, + 1, + true, + ([newBoundNode]) => { + assert( + newBoundNode !== undefined, + 'Take: newBoundNode must be found during fetch', + ); + + // The next row is the new row. We can replace the bounds and keep the + // edit change. + if ( + compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0 + ) { + return replaceBoundAndForwardChange(); } - newBoundNode = node; - break; - } - } finally { - this.#takeGate?.close(); - } - assert( - newBoundNode !== undefined, - 'Take: newBoundNode must be found during fetch', - ); - // The next row is the new row. We can replace the bounds and keep the - // edit change. - if (compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0) { - yield* replaceBoundAndForwardChange(); - return; - } - - // The new row is now outside the bounds, so we need to remove the old - // row and add the new bounds row. - this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row); - yield* this.#pushWithRowHiddenFromFetch( - newBoundNode.row, - makeRemoveChange(change[ChangeIndex.OLD_NODE]), + // The new row is now outside the bounds, so we need to remove the old + // row and add the new bounds row. + this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row); + return andThen( + this.#pushWithRowHiddenFromFetch( + newBoundNode.row, + makeRemoveChange(change[ChangeIndex.OLD_NODE]), + ), + () => this.#output.push(makeAddChange(newBoundNode), this), + ); + }, ); - yield* this.#output.push(makeAddChange(newBoundNode), this); - return; } if (oldCmp > 0) { @@ -559,51 +609,46 @@ export class Take implements Operator, TakeBoundProvider { // Both old and new outside of bounds if (newCmp > 0) { - return; + return emptyArray; } // old was outside, new is inside. Pushing out the old bounds assert(newCmp < 0, 'New comparison must be less than 0'); - let oldBoundNode: Node | undefined; - let newBoundNode: Node | undefined; - for (const node of this.#input.fetch({ - start: { - row: takeState.bound, - basis: 'at', + return this.#withFirst( + { + start: { + row: bound, + basis: 'at', + }, + constraint, + reverse: true, + }, + 2, + false, + ([oldBoundNode, newBoundNode]) => { + assert( + oldBoundNode !== undefined, + 'Take: oldBoundNode must be found during fetch', + ); + assert( + newBoundNode !== undefined, + 'Take: newBoundNode must be found during fetch', + ); + + // Remove before add to maintain invariant that + // output size <= limit. + this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row); + return andThen( + this.#pushWithRowHiddenFromFetch( + change[ChangeIndex.NODE].row, + makeRemoveChange(oldBoundNode), + ), + () => + this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this), + ); }, - constraint, - reverse: true, - })) { - if (node === 'yield') { - yield node; - continue; - } else if (oldBoundNode === undefined) { - oldBoundNode = node; - } else { - newBoundNode = node; - break; - } - } - assert( - oldBoundNode !== undefined, - 'Take: oldBoundNode must be found during fetch', - ); - assert( - newBoundNode !== undefined, - 'Take: newBoundNode must be found during fetch', - ); - - // Remove before add to maintain invariant that - // output size <= limit. - this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row); - yield* this.#pushWithRowHiddenFromFetch( - change[ChangeIndex.NODE].row, - makeRemoveChange(oldBoundNode), ); - yield* this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this); - - return; } if (oldCmp < 0) { @@ -611,8 +656,7 @@ export class Take implements Operator, TakeBoundProvider { // Both old and new inside of bounds if (newCmp < 0) { - yield* this.#output.push(change, this); - return; + return this.#output.push(change, this); } // old was inside, new is larger than old bound @@ -621,58 +665,66 @@ export class Take implements Operator, TakeBoundProvider { // at this point we need to find the row after the bound and use that or // the newRow as the new bound. - let afterBoundNode: Node | undefined; - this.#takeGate?.open(); - try { - for (const node of this.#input.fetch({ + return this.#withFirst( + { start: { - row: takeState.bound, + row: bound, basis: 'after', }, constraint, - })) { - if (node === 'yield') { - yield node; - continue; + }, + 1, + true, + ([afterBoundNode]) => { + assert( + afterBoundNode !== undefined, + 'Take: afterBoundNode must be found during fetch', + ); + + // The new row is the new bound. Use an edit change. + if ( + compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0 + ) { + return replaceBoundAndForwardChange(); } - afterBoundNode = node; - break; - } - } finally { - this.#takeGate?.close(); - } - assert( - afterBoundNode !== undefined, - 'Take: afterBoundNode must be found during fetch', - ); - // The new row is the new bound. Use an edit change. - if (compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0) { - yield* replaceBoundAndForwardChange(); - return; - } - - yield* this.#output.push( - makeRemoveChange(change[ChangeIndex.OLD_NODE]), - this, + return andThen( + this.#output.push( + makeRemoveChange(change[ChangeIndex.OLD_NODE]), + this, + ), + () => { + this.#setTakeState( + takeStateKey, + takeState.size, + afterBoundNode.row, + ); + return this.#output.push(makeAddChange(afterBoundNode), this); + }, + ); + }, ); - this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row); - yield* this.#output.push(makeAddChange(afterBoundNode), this); - return; } unreachable(); } - *#pushWithRowHiddenFromFetch(row: Row, change: Change) { + #pushWithRowHiddenFromFetch(row: Row, change: Change): Stream<'yield'> { this.#rowHiddenFromFetch = row; + let s: Stream<'yield'>; try { - yield* this.#output.push(change, this); - } finally { + s = this.#output.push(change, this); + } catch (e) { this.#rowHiddenFromFetch = undefined; + throw e; } + return andFinally(s, this.#showHiddenRow); } + readonly #showHiddenRow = (): void => { + this.#rowHiddenFromFetch = undefined; + }; + #setTakeState(takeStateKey: string, size: number, bound: Row | undefined) { this.#storage.set(takeStateKey, { size, @@ -685,59 +737,155 @@ export class Take implements Operator, TakeBoundProvider { this.#storage.destroy(); } - *reconcile(_pusher?: InputBase): Stream<'yield'> { - if (this.#dirtyPartitions.size > 0) { - const dirty = [...this.#dirtyPartitions.entries()]; - this.#dirtyPartitions.clear(); - - for (const [takeStateKey, {constraint}] of dirty) { - const takeState = this.#storage.get(takeStateKey); - assert( - takeState !== undefined, - 'Take: dirty partition must exist in storage', - ); - const deficit = this.#limit - takeState.size; - assert(deficit > 0, 'Take: dirty partition must have a deficit'); - - const toPush: Node[] = []; - this.#takeGate?.open(); - try { - const stream = this.#input.fetch({ - start: takeState.bound - ? { - row: takeState.bound, - basis: 'after', - } - : undefined, - constraint, - }); + reconcile(_pusher?: InputBase): Stream<'yield'> { + if (this.#dirtyPartitions.size === 0) { + return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray; + } + const dirty = [...this.#dirtyPartitions.entries()]; + this.#dirtyPartitions.clear(); + let i = 0; + return andThen( + eachStream(() => + i === dirty.length ? undefined : this.#refill(dirty[i++]), + ), + () => + this.#output.reconcile ? this.#output.reconcile(this) : emptyArray, + ); + } - for (const node of stream) { - if (node === 'yield') { - yield 'yield'; - continue; - } - toPush.push(node); - if (toPush.length === deficit) { - break; + /** Refills a dirty partition with the rows that follow its bound. */ + #refill([takeStateKey, {constraint}]: [ + string, + DirtyPartitionState, + ]): Stream<'yield'> { + const takeState = this.#storage.get(takeStateKey); + assert( + takeState !== undefined, + 'Take: dirty partition must exist in storage', + ); + const deficit = this.#limit - takeState.size; + assert(deficit > 0, 'Take: dirty partition must have a deficit'); + + return this.#withFirst( + { + start: takeState.bound + ? { + row: takeState.bound, + basis: 'after', } + : undefined, + constraint, + }, + deficit, + true, + toPush => { + let size = takeState.size; + let j = 0; + return eachStream(() => { + if (j === toPush.length) { + return undefined; } - } finally { - this.#takeGate?.close(); - } + const node = toPush[j++]; + this.#setTakeState(takeStateKey, ++size, node.row); + return this.#output.push(makeAddChange(node), this); + }); + }, + ); + } +} - let currentSize = takeState.size; - for (const node of toPush) { - currentSize++; - this.#setTakeState(takeStateKey, currentSize, node.row); - yield* this.#output.push(makeAddChange(node), this); +/** + * The rows of a hydrated Take: its input up to the bound or the limit. An + * iterator rather than a generator, see `mapNodes` in stream.ts. + */ +class TakeNodes implements IterableIterator { + readonly #it: Iterator; + readonly #compareRows: Comparator; + readonly #bound: Row; + readonly #limit: number; + readonly #getHidden: () => Row | undefined; + #count = 0; + #done = false; + + constructor( + it: Iterator, + compareRows: Comparator, + bound: Row, + limit: number, + getHidden: () => Row | undefined, + ) { + this.#it = it; + this.#compareRows = compareRows; + this.#bound = bound; + this.#limit = limit; + this.#getHidden = getHidden; + } + + [Symbol.iterator](): this { + return this; + } + + next(): IteratorResult { + if (this.#done) { + return doneResult; + } + if (this.#count >= this.#limit) { + return this.return(); + } + for (;;) { + let r: IteratorResult; + try { + r = this.#it.next(); + } catch (e) { + // A fetch that threw is over, as a generator's is. + this.#done = true; + throw e; + } + if (r.done) { + this.#done = true; + return r; + } + const node = r.value; + if (node === 'yield') { + return r; + } + let past: boolean; + let hidden: boolean; + try { + past = this.#compareRows(this.#bound, node.row) < 0; + const hiddenRow = this.#getHidden(); + hidden = + !past && + hiddenRow !== undefined && + this.#compareRows(hiddenRow, node.row) === 0; + } catch (e) { + // As a `for...of` does when its body throws: close the input, and + // report this error rather than one from closing it. + this.#done = true; + try { + this.#it.return?.(); + } catch { + // The first error wins. } + throw e; + } + if (past) { + return this.return(); } + if (hidden) { + continue; + } + this.#count++; + return r; } + } - if (this.#output.reconcile) { - yield* this.#output.reconcile(this); + return(): IteratorResult { + if (!this.#done) { + this.#done = true; + this.#it.return?.(); } + return doneResult; } } diff --git a/packages/zql/src/query/query.ts b/packages/zql/src/query/query.ts index bbdbca3ee6..f0cc4a206e 100644 --- a/packages/zql/src/query/query.ts +++ b/packages/zql/src/query/query.ts @@ -847,8 +847,8 @@ export interface Query< >; /** * The minimum of the number and string leaves at a path inside a `json()` - * column. Numbers order before strings (as in JS and SQLite), so a path - * holding both yields its smallest number. Other leaves are skipped. + * column. Strings order before numbers (as in Postgres's jsonb), so a path + * holding both yields its smallest string. Other leaves are skipped. */ min( selector: ( @@ -877,8 +877,8 @@ export interface Query< >; /** * The maximum of the number and string leaves at a path inside a `json()` - * column. Numbers order before strings, so a path holding both yields its - * largest string. Other leaves are skipped. + * column. Strings order before numbers, so a path holding both yields its + * largest number. Other leaves are skipped. */ max( selector: ( diff --git a/rust/zero-client/src/ivm/aggregate.rs b/rust/zero-client/src/ivm/aggregate.rs index 168e9e2a2e..feaf9864ab 100644 --- a/rust/zero-client/src/ivm/aggregate.rs +++ b/rust/zero-client/src/ivm/aggregate.rs @@ -301,15 +301,15 @@ impl Reducer { } } -/// `compareAggregateValues`: numbers before strings, as in JS and SQLite, -/// for the leaves of a JSON path; otherwise `compareValues`. Values JS can't -/// compare, such as the objects of a json column without a path, fail the -/// query as JS's throw does. Equal values compare equal first, as JS's +/// `compareAggregateValues`: strings before numbers, as in Postgres's jsonb +/// order, for the leaves of a JSON path; otherwise `compareValues`. Values JS +/// can't compare, such as the objects of a json column without a path, fail +/// the query as JS's throw does. Equal values compare equal first, as JS's /// `a === b` does for one row's own value. pub fn compare_aggregate_values(a: &Value, b: &Value) -> Ordering { match (a, b) { - (Value::Number(_), Value::String(_)) => Ordering::Less, - (Value::String(_), Value::Number(_)) => Ordering::Greater, + (Value::String(_), Value::Number(_)) => Ordering::Less, + (Value::Number(_), Value::String(_)) => Ordering::Greater, _ => match compare::compare_checked(a, b) { Ok(o) => o, Err(_) if a == b => Ordering::Equal, diff --git a/rust/zero-client/src/query.rs b/rust/zero-client/src/query.rs index af328eb0cc..10377fef6f 100644 --- a/rust/zero-client/src/query.rs +++ b/rust/zero-client/src/query.rs @@ -591,7 +591,7 @@ impl AnyQuery { self.aggregate(AggregateFunction::Min, Some(field.into()), None) } - /// The least number or string at a JSON path; numbers sort first. + /// The least number or string at a JSON path; strings sort first. pub fn min_json(self, column: impl Into, path: Vec) -> Self { self.aggregate(AggregateFunction::Min, Some(column.into()), Some(path)) } diff --git a/swift/Sources/Zero/IVM/Aggregate.swift b/swift/Sources/Zero/IVM/Aggregate.swift index 84cebc0f63..e0389488e8 100644 --- a/swift/Sources/Zero/IVM/Aggregate.swift +++ b/swift/Sources/Zero/IVM/Aggregate.swift @@ -224,15 +224,15 @@ struct Reducer { } } -/// `compareAggregateValues`: numbers before strings, as in JS and SQLite, -/// for the leaves of a JSON path; otherwise `compareValues`. Values JS can't -/// compare, such as the objects of a json column without a path, fail the -/// query as JS's throw does. Equal values compare equal first, as JS's +/// `compareAggregateValues`: strings before numbers, as in Postgres's jsonb +/// order, for the leaves of a JSON path; otherwise `compareValues`. Values JS +/// can't compare, such as the objects of a json column without a path, fail +/// the query as JS's throw does. Equal values compare equal first, as JS's /// `a === b` does for one row's own value. func compareAggregateValues(_ a: JSONValue, _ b: JSONValue) -> Int { switch (a, b) { - case (.number, .string): return -1 - case (.string, .number): return 1 + case (.string, .number): return -1 + case (.number, .string): return 1 default: do { return try ValueCompare.compareChecked(a, b) diff --git a/swift/Sources/Zero/Query/AnyQuery.swift b/swift/Sources/Zero/Query/AnyQuery.swift index 9905de04f2..178a74a540 100644 --- a/swift/Sources/Zero/Query/AnyQuery.swift +++ b/swift/Sources/Zero/Query/AnyQuery.swift @@ -359,7 +359,7 @@ public struct AnyQuery: Sendable { /// The least of `field`'s non-null values; null when there are none. public func min(_ field: String) -> AnyQuery { aggregate(.min, field: field, path: nil) } - /// The least number or string at a JSON path; numbers sort first. + /// The least number or string at a JSON path; strings sort first. public func min(json column: String, _ path: AST.JSONPathSegment...) -> AnyQuery { aggregate(.min, field: column, path: path) }