@mastra/clickhouse 1.18.0 → 1.19.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.18.0"
6
+ version: "1.19.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.18.0",
2
+ "version": "1.19.0",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -6601,6 +6601,13 @@ function collectRelationCollections(predicate, collections = /* @__PURE__ */ new
6601
6601
  else if (predicate.type === "not") collectRelationCollections(predicate.arg, collections);
6602
6602
  return collections;
6603
6603
  }
6604
+ function collectThreadRelationCollections(predicate, collections) {
6605
+ if (!predicate) return collections;
6606
+ if (predicate.type === "relation") collectRelationCollections(predicate.predicate, collections);
6607
+ else if (predicate.type === "boolean") for (const arg of predicate.args) collectThreadRelationCollections(arg, collections);
6608
+ else collectThreadRelationCollections(predicate.arg, collections);
6609
+ return collections;
6610
+ }
6604
6611
  function compilePredicate(predicate, parameters) {
6605
6612
  if (predicate.type === "relation") {
6606
6613
  const existence = `EXISTS (
@@ -6615,11 +6622,21 @@ function compilePredicate(predicate, parameters) {
6615
6622
  if (predicate.type === "not") return `NOT (${compilePredicate(predicate.arg, parameters)})`;
6616
6623
  return compileScalarPredicate(predicate, TRACE_FIELDS, parameters, true);
6617
6624
  }
6618
- function compileClickHouseTraceQuery(plan) {
6619
- const parameters = new ParameterBuilder();
6620
- const from = parameters.add(plan.timeRange.from, "DateTime64(3, 'UTC')");
6621
- const to = parameters.add(plan.timeRange.to, "DateTime64(3, 'UTC')");
6622
- const relationCollections = collectRelationCollections(plan.where);
6625
+ function compileThreadPredicate(predicate, parameters) {
6626
+ if (predicate.type === "relation") {
6627
+ const existence = `EXISTS (
6628
+ SELECT 1 FROM eligible_roots r
6629
+ WHERE r.threadId = t.threadId
6630
+ AND (${compilePredicate(predicate.predicate, parameters)})
6631
+ )`;
6632
+ return predicate.quantifier === "some" ? existence : `NOT ${existence}`;
6633
+ }
6634
+ if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileThreadPredicate(arg, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6635
+ return `NOT (${compileThreadPredicate(predicate.arg, parameters)})`;
6636
+ }
6637
+ function compileClickHouseTraceScope(selection, relationCollections, parameters) {
6638
+ const from = parameters.add(selection.timeRange.from, "DateTime64(3, 'UTC')");
6639
+ const to = parameters.add(selection.timeRange.to, "DateTime64(3, 'UTC')");
6623
6640
  const ctes = [`current_roots AS (
6624
6641
  SELECT * FROM (
6625
6642
  SELECT *
@@ -6700,6 +6717,11 @@ function compileClickHouseTraceQuery(plan) {
6700
6717
  WHERE isNotNull(traceId)
6701
6718
  AND traceId IN (SELECT traceId FROM root_scope)
6702
6719
  )`);
6720
+ return ctes;
6721
+ }
6722
+ function compileClickHouseTraceQuery(plan) {
6723
+ const parameters = new ParameterBuilder();
6724
+ const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters);
6703
6725
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
6704
6726
  ctes.push(`candidates AS (
6705
6727
  SELECT ${TRACE_SELECT}
@@ -6736,6 +6758,41 @@ LIMIT ${limit}`,
6736
6758
  query_params: parameters.params
6737
6759
  };
6738
6760
  }
6761
+ function compileClickHouseThreadQuery(plan) {
6762
+ const parameters = new ParameterBuilder();
6763
+ const relationCollections = collectRelationCollections(plan.traces.where);
6764
+ collectThreadRelationCollections(plan.where, relationCollections);
6765
+ const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters);
6766
+ const eligibility = plan.traces.where ? compilePredicate(plan.traces.where, parameters) : "1";
6767
+ ctes.push(`eligible_roots AS (
6768
+ SELECT *
6769
+ FROM root_scope r
6770
+ WHERE ${eligibility}
6771
+ )`);
6772
+ ctes.push(`thread_ids AS (
6773
+ SELECT threadId
6774
+ FROM eligible_roots
6775
+ WHERE isNotNull(threadId)
6776
+ GROUP BY threadId
6777
+ )`);
6778
+ const threadPredicate = plan.where ? compileThreadPredicate(plan.where, parameters) : "1";
6779
+ ctes.push(`qualified_threads AS (
6780
+ SELECT t.threadId
6781
+ FROM thread_ids t
6782
+ WHERE ${threadPredicate}
6783
+ )`);
6784
+ const pageCondition = plan.cursor ? `WHERE threadId > ${parameters.add(plan.cursor.threadId, "String")}` : "";
6785
+ const limit = parameters.add(plan.limit + 1, "UInt64");
6786
+ return {
6787
+ query: `WITH ${ctes.join(",\n")}
6788
+ SELECT threadId
6789
+ FROM qualified_threads
6790
+ ${pageCondition}
6791
+ ORDER BY threadId ASC
6792
+ LIMIT ${limit}`,
6793
+ query_params: parameters.params
6794
+ };
6795
+ }
6739
6796
  function asIsoTimestamp(value) {
6740
6797
  return new Date(value).toISOString();
6741
6798
  }
@@ -6798,6 +6855,18 @@ async function queryTraces(client, plan, timeoutMs) {
6798
6855
  }) : null }
6799
6856
  });
6800
6857
  }
6858
+ async function queryThreads(client, plan, timeoutMs) {
6859
+ const rows = await runWithClickHouseTraceQueryTimeout(client, timeoutMs, compileClickHouseThreadQuery(plan));
6860
+ const threads = rows.slice(0, plan.limit).map((row) => ({ threadId: String(row.threadId) }));
6861
+ const last = threads.at(-1);
6862
+ return _mastra_core_storage.queryThreadsResultSchema.parse({
6863
+ threads,
6864
+ page: { next: rows.length > plan.limit && last ? _mastra_core_storage.encodeTraceQueryCursor(plan, {
6865
+ result: "threads",
6866
+ threadId: last.threadId
6867
+ }) : null }
6868
+ });
6869
+ }
6801
6870
  //#endregion
6802
6871
  //#region src/storage/domains/observability/v-next/trace-roots.ts
6803
6872
  /**
@@ -7914,13 +7983,15 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
7914
7983
  if (!deltaPollingSupported(this.#deltaCursorStrategy)) return [
7915
7984
  "metrics",
7916
7985
  "logs",
7917
- "trace-query"
7986
+ "trace-query",
7987
+ "thread-query"
7918
7988
  ];
7919
7989
  return [
7920
7990
  "metrics",
7921
7991
  "logs",
7922
7992
  "delta-polling",
7923
- "trace-query"
7993
+ "trace-query",
7994
+ "thread-query"
7924
7995
  ];
7925
7996
  }
7926
7997
  async createSpan(args) {
@@ -8047,6 +8118,18 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
8047
8118
  }, error);
8048
8119
  }
8049
8120
  }
8121
+ async queryThreads(plan) {
8122
+ try {
8123
+ return await queryThreads(this.#client, plan, this.#traceQueryTimeoutMs);
8124
+ } catch (error) {
8125
+ if (error instanceof _mastra_core_error.MastraError || error instanceof _mastra_core_storage.TraceQueryExecutionError) throw error;
8126
+ throw new _mastra_core_error.MastraError({
8127
+ id: (0, _mastra_core_storage.createStorageErrorId)("CLICKHOUSE", "QUERY_THREADS", "FAILED"),
8128
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
8129
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
8130
+ }, error);
8131
+ }
8132
+ }
8050
8133
  async listTracesLight(args) {
8051
8134
  try {
8052
8135
  return await listTracesLight(this.#client, args, this.#deltaCursorStrategy);