@mastra/clickhouse 1.20.0-alpha.3 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ name: mastra-clickhouse
3
3
  description: Documentation for @mastra/clickhouse. Use when working with @mastra/clickhouse APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/clickhouse"
6
- version: "1.20.0-alpha.3"
6
+ version: "1.20.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.20.0-alpha.3",
2
+ "version": "1.20.0",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -30,6 +30,7 @@ let _mastra_core_base = require("@mastra/core/base");
30
30
  let crypto$1 = require("crypto");
31
31
  let _mastra_core_utils = require("@mastra/core/utils");
32
32
  let _mastra_core_features = require("@mastra/core/features");
33
+ let zod_v4 = require("zod/v4");
33
34
  let _mastra_core_evals = require("@mastra/core/evals");
34
35
  //#region src/storage/db/replication.ts
35
36
  const DEFAULT_ZOOKEEPER_PATH = "/clickhouse/tables/{shard}/{database}/{table}";
@@ -6872,7 +6873,18 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6872
6873
  )`);
6873
6874
  return ctes;
6874
6875
  }
6875
- function compileClickHouseTraceQuery(plan) {
6876
+ const deltaWatermarkSchema = zod_v4.z.object({
6877
+ cursorId: zod_v4.z.string().regex(/^\d+$/).refine((value) => BigInt(value) <= 18446744073709551615n),
6878
+ traceId: zod_v4.z.string()
6879
+ }).strict();
6880
+ function parseDeltaWatermark(value) {
6881
+ try {
6882
+ return deltaWatermarkSchema.parse(JSON.parse(value));
6883
+ } catch {
6884
+ throw new _mastra_core_storage.TraceQueryCursorError("TRACE_QUERY_CURSOR_MALFORMED");
6885
+ }
6886
+ }
6887
+ function compileClickHouseTraceQuery(plan, deltaHead) {
6876
6888
  const parameters = new ParameterBuilder();
6877
6889
  const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters);
6878
6890
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
@@ -6892,6 +6904,28 @@ ORDER BY threadId ASC
6892
6904
  LIMIT ${parameters.add(plan.limit + 1, "UInt64")}`,
6893
6905
  query_params: parameters.params
6894
6906
  };
6907
+ if (plan.paginationMode === "delta") {
6908
+ const watermark = _mastra_core_storage.getTraceQueryDeltaWatermark(plan, "clickhouse");
6909
+ const after = watermark ? parseDeltaWatermark(watermark) : {
6910
+ cursorId: "0",
6911
+ traceId: ""
6912
+ };
6913
+ return {
6914
+ query: `${candidates}, delta_candidates AS (
6915
+ SELECT traceId, max(cursorId) AS latestCursorId
6916
+ FROM ${TABLE_TRACE_ROOTS_DELTA}
6917
+ WHERE tuple(cursorId, traceId) > ${`tuple(${parameters.add(after.cursorId, "UInt64")}, ${parameters.add(after.traceId, "String")})`} ${deltaHead ? `AND tuple(cursorId, traceId) <= tuple(${parameters.add(deltaHead.cursorId, "UInt64")}, ${parameters.add(deltaHead.traceId, "String")})` : ""}
6918
+ GROUP BY traceId
6919
+ )
6920
+ SELECT c.*, toString(d.latestCursorId) AS __delta_cursor
6921
+ FROM candidates c
6922
+ INNER JOIN delta_candidates d ON c.traceId = d.traceId
6923
+ ORDER BY d.latestCursorId ASC, c.traceId ASC
6924
+ LIMIT ${parameters.add(plan.limit + 1, "UInt64")}`,
6925
+ query_params: parameters.params,
6926
+ sharedSnapshot: true
6927
+ };
6928
+ }
6895
6929
  const orderField = resolveOrderField(plan.orderBy.field);
6896
6930
  const direction = plan.orderBy.direction === "asc" ? "ASC" : "DESC";
6897
6931
  if (plan.paginationMode === "page") return {
@@ -7111,9 +7145,39 @@ async function getTraceQueryValues(client, plan, limits) {
7111
7145
  valuesTruncated: rows.length > plan.limit
7112
7146
  });
7113
7147
  }
7114
- async function queryTraces(client, plan, timeoutMs) {
7148
+ async function queryTraces(client, plan, timeoutMs, strategy = null) {
7149
+ const deadline = performance.now() + _mastra_core_storage.resolveTraceQueryTimeoutMs(timeoutMs);
7150
+ const remaining = () => {
7151
+ const value = Math.floor(deadline - performance.now());
7152
+ if (value <= 0) throw new _mastra_core_storage.TraceQueryExecutionError();
7153
+ return value;
7154
+ };
7155
+ let deltaHead;
7156
+ if (plan.paginationMode === "delta") {
7157
+ assertDeltaPollingSupported(strategy);
7158
+ const watermark = _mastra_core_storage.getTraceQueryDeltaWatermark(plan, "clickhouse");
7159
+ if (watermark) parseDeltaWatermark(watermark);
7160
+ }
7161
+ if (plan.paginationMode === "delta" || plan.paginationMode === "page" && deltaPollingSupported(strategy) && typeof _mastra_core_storage.encodeTraceQueryDeltaCursor === "function") {
7162
+ const head = await runWithClickHouseTraceQueryTimeout(client, { timeoutMs: remaining() }, {
7163
+ query: `SELECT toString(cursorId) AS cursorId, traceId FROM ${TABLE_TRACE_ROOTS_DELTA} ORDER BY cursorId DESC, traceId DESC LIMIT 1`,
7164
+ query_params: {}
7165
+ });
7166
+ deltaHead = head[0] ? parseDeltaWatermark(JSON.stringify(head[0])) : {
7167
+ cursorId: "0",
7168
+ traceId: ""
7169
+ };
7170
+ if (plan.paginationMode === "delta" && !plan.deltaCursor) return _mastra_core_storage.traceQueryResponseSchema.parse({
7171
+ traces: [],
7172
+ delta: {
7173
+ limit: plan.limit,
7174
+ hasMore: false
7175
+ },
7176
+ deltaCursor: _mastra_core_storage.encodeTraceQueryDeltaCursor(plan, "clickhouse", JSON.stringify(deltaHead))
7177
+ });
7178
+ }
7115
7179
  if (plan.paginationMode === "page") {
7116
- const rows = await runWithClickHouseTraceQueryTimeout(client, { timeoutMs }, compileClickHouseTraceQuery(plan));
7180
+ const rows = await runWithClickHouseTraceQueryTimeout(client, { timeoutMs: deltaHead ? remaining() : timeoutMs }, compileClickHouseTraceQuery(plan, deltaHead));
7117
7181
  const total = Number(rows.at(-1)?.total ?? 0);
7118
7182
  const traces = rows.filter((row) => Number(row.__metadata) === 0).map((row) => ({
7119
7183
  traceId: String(row.traceId),
@@ -7135,6 +7199,7 @@ async function queryTraces(client, plan, timeoutMs) {
7135
7199
  }));
7136
7200
  return _mastra_core_storage.traceQueryResponseSchema.parse({
7137
7201
  traces,
7202
+ ...deltaHead ? { deltaCursor: _mastra_core_storage.encodeTraceQueryDeltaCursor(plan, "clickhouse", JSON.stringify(deltaHead)) } : {},
7138
7203
  pagination: {
7139
7204
  total,
7140
7205
  page: plan.page,
@@ -7143,7 +7208,7 @@ async function queryTraces(client, plan, timeoutMs) {
7143
7208
  }
7144
7209
  });
7145
7210
  }
7146
- const rows = await runWithClickHouseTraceQueryTimeout(client, { timeoutMs }, compileClickHouseTraceQuery(plan));
7211
+ const rows = await runWithClickHouseTraceQueryTimeout(client, { timeoutMs: deltaHead ? remaining() : timeoutMs }, compileClickHouseTraceQuery(plan, deltaHead));
7147
7212
  const visibleRows = rows.slice(0, plan.limit);
7148
7213
  if (plan.result === "groups") {
7149
7214
  const groups = visibleRows.map((row) => ({ threadId: String(row.threadId) }));
@@ -7175,6 +7240,23 @@ async function queryTraces(client, plan, timeoutMs) {
7175
7240
  status: row.status
7176
7241
  }));
7177
7242
  const last = traces.at(-1);
7243
+ if (plan.paginationMode === "delta") {
7244
+ const lastRow = visibleRows.at(-1);
7245
+ let watermark = lastRow ? {
7246
+ cursorId: String(lastRow.__delta_cursor),
7247
+ traceId: String(lastRow.traceId)
7248
+ } : deltaHead;
7249
+ const previous = parseDeltaWatermark(_mastra_core_storage.getTraceQueryDeltaWatermark(plan, "clickhouse"));
7250
+ if (BigInt(previous.cursorId) > BigInt(watermark.cursorId) || previous.cursorId === watermark.cursorId && previous.traceId > watermark.traceId) watermark = previous;
7251
+ return _mastra_core_storage.traceQueryResponseSchema.parse({
7252
+ traces,
7253
+ delta: {
7254
+ limit: plan.limit,
7255
+ hasMore: rows.length > plan.limit
7256
+ },
7257
+ deltaCursor: _mastra_core_storage.encodeTraceQueryDeltaCursor(plan, "clickhouse", JSON.stringify(watermark))
7258
+ });
7259
+ }
7178
7260
  return _mastra_core_storage.traceQueryResponseSchema.parse({
7179
7261
  traces,
7180
7262
  page: { next: rows.length > plan.limit && last ? _mastra_core_storage.encodeTraceQueryCursor(plan, {
@@ -8533,9 +8615,9 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
8533
8615
  }
8534
8616
  async queryTraces(plan) {
8535
8617
  try {
8536
- return await queryTraces(this.#client, plan, this.#traceQueryTimeoutMs);
8618
+ return await queryTraces(this.#client, plan, this.#traceQueryTimeoutMs, this.#deltaCursorStrategy);
8537
8619
  } catch (error) {
8538
- if (error instanceof _mastra_core_error.MastraError || error instanceof _mastra_core_storage.TraceQueryExecutionError || error instanceof _mastra_core_storage.TraceQueryResourceLimitError) throw error;
8620
+ if (error instanceof _mastra_core_error.MastraError || error instanceof _mastra_core_storage.TraceQueryExecutionError || error instanceof _mastra_core_storage.TraceQueryCursorError || error instanceof _mastra_core_storage.TraceQueryResourceLimitError) throw error;
8539
8621
  throw new _mastra_core_error.MastraError({
8540
8622
  id: (0, _mastra_core_storage.createStorageErrorId)("CLICKHOUSE", "QUERY_TRACES", "FAILED"),
8541
8623
  domain: _mastra_core_error.ErrorDomain.STORAGE,