@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.
package/dist/index.js CHANGED
@@ -6578,6 +6578,13 @@ function collectRelationCollections(predicate, collections = /* @__PURE__ */ new
6578
6578
  else if (predicate.type === "not") collectRelationCollections(predicate.arg, collections);
6579
6579
  return collections;
6580
6580
  }
6581
+ function collectThreadRelationCollections(predicate, collections) {
6582
+ if (!predicate) return collections;
6583
+ if (predicate.type === "relation") collectRelationCollections(predicate.predicate, collections);
6584
+ else if (predicate.type === "boolean") for (const arg of predicate.args) collectThreadRelationCollections(arg, collections);
6585
+ else collectThreadRelationCollections(predicate.arg, collections);
6586
+ return collections;
6587
+ }
6581
6588
  function compilePredicate(predicate, parameters) {
6582
6589
  if (predicate.type === "relation") {
6583
6590
  const existence = `EXISTS (
@@ -6592,11 +6599,21 @@ function compilePredicate(predicate, parameters) {
6592
6599
  if (predicate.type === "not") return `NOT (${compilePredicate(predicate.arg, parameters)})`;
6593
6600
  return compileScalarPredicate(predicate, TRACE_FIELDS, parameters, true);
6594
6601
  }
6595
- function compileClickHouseTraceQuery(plan) {
6596
- const parameters = new ParameterBuilder();
6597
- const from = parameters.add(plan.timeRange.from, "DateTime64(3, 'UTC')");
6598
- const to = parameters.add(plan.timeRange.to, "DateTime64(3, 'UTC')");
6599
- const relationCollections = collectRelationCollections(plan.where);
6602
+ function compileThreadPredicate(predicate, parameters) {
6603
+ if (predicate.type === "relation") {
6604
+ const existence = `EXISTS (
6605
+ SELECT 1 FROM eligible_roots r
6606
+ WHERE r.threadId = t.threadId
6607
+ AND (${compilePredicate(predicate.predicate, parameters)})
6608
+ )`;
6609
+ return predicate.quantifier === "some" ? existence : `NOT ${existence}`;
6610
+ }
6611
+ if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileThreadPredicate(arg, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6612
+ return `NOT (${compileThreadPredicate(predicate.arg, parameters)})`;
6613
+ }
6614
+ function compileClickHouseTraceScope(selection, relationCollections, parameters) {
6615
+ const from = parameters.add(selection.timeRange.from, "DateTime64(3, 'UTC')");
6616
+ const to = parameters.add(selection.timeRange.to, "DateTime64(3, 'UTC')");
6600
6617
  const ctes = [`current_roots AS (
6601
6618
  SELECT * FROM (
6602
6619
  SELECT *
@@ -6677,6 +6694,11 @@ function compileClickHouseTraceQuery(plan) {
6677
6694
  WHERE isNotNull(traceId)
6678
6695
  AND traceId IN (SELECT traceId FROM root_scope)
6679
6696
  )`);
6697
+ return ctes;
6698
+ }
6699
+ function compileClickHouseTraceQuery(plan) {
6700
+ const parameters = new ParameterBuilder();
6701
+ const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters);
6680
6702
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
6681
6703
  ctes.push(`candidates AS (
6682
6704
  SELECT ${TRACE_SELECT}
@@ -6713,6 +6735,41 @@ LIMIT ${limit}`,
6713
6735
  query_params: parameters.params
6714
6736
  };
6715
6737
  }
6738
+ function compileClickHouseThreadQuery(plan) {
6739
+ const parameters = new ParameterBuilder();
6740
+ const relationCollections = collectRelationCollections(plan.traces.where);
6741
+ collectThreadRelationCollections(plan.where, relationCollections);
6742
+ const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters);
6743
+ const eligibility = plan.traces.where ? compilePredicate(plan.traces.where, parameters) : "1";
6744
+ ctes.push(`eligible_roots AS (
6745
+ SELECT *
6746
+ FROM root_scope r
6747
+ WHERE ${eligibility}
6748
+ )`);
6749
+ ctes.push(`thread_ids AS (
6750
+ SELECT threadId
6751
+ FROM eligible_roots
6752
+ WHERE isNotNull(threadId)
6753
+ GROUP BY threadId
6754
+ )`);
6755
+ const threadPredicate = plan.where ? compileThreadPredicate(plan.where, parameters) : "1";
6756
+ ctes.push(`qualified_threads AS (
6757
+ SELECT t.threadId
6758
+ FROM thread_ids t
6759
+ WHERE ${threadPredicate}
6760
+ )`);
6761
+ const pageCondition = plan.cursor ? `WHERE threadId > ${parameters.add(plan.cursor.threadId, "String")}` : "";
6762
+ const limit = parameters.add(plan.limit + 1, "UInt64");
6763
+ return {
6764
+ query: `WITH ${ctes.join(",\n")}
6765
+ SELECT threadId
6766
+ FROM qualified_threads
6767
+ ${pageCondition}
6768
+ ORDER BY threadId ASC
6769
+ LIMIT ${limit}`,
6770
+ query_params: parameters.params
6771
+ };
6772
+ }
6716
6773
  function asIsoTimestamp(value) {
6717
6774
  return new Date(value).toISOString();
6718
6775
  }
@@ -6775,6 +6832,18 @@ async function queryTraces(client, plan, timeoutMs) {
6775
6832
  }) : null }
6776
6833
  });
6777
6834
  }
6835
+ async function queryThreads(client, plan, timeoutMs) {
6836
+ const rows = await runWithClickHouseTraceQueryTimeout(client, timeoutMs, compileClickHouseThreadQuery(plan));
6837
+ const threads = rows.slice(0, plan.limit).map((row) => ({ threadId: String(row.threadId) }));
6838
+ const last = threads.at(-1);
6839
+ return coreStorage.queryThreadsResultSchema.parse({
6840
+ threads,
6841
+ page: { next: rows.length > plan.limit && last ? coreStorage.encodeTraceQueryCursor(plan, {
6842
+ result: "threads",
6843
+ threadId: last.threadId
6844
+ }) : null }
6845
+ });
6846
+ }
6778
6847
  //#endregion
6779
6848
  //#region src/storage/domains/observability/v-next/trace-roots.ts
6780
6849
  /**
@@ -7891,13 +7960,15 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
7891
7960
  if (!deltaPollingSupported(this.#deltaCursorStrategy)) return [
7892
7961
  "metrics",
7893
7962
  "logs",
7894
- "trace-query"
7963
+ "trace-query",
7964
+ "thread-query"
7895
7965
  ];
7896
7966
  return [
7897
7967
  "metrics",
7898
7968
  "logs",
7899
7969
  "delta-polling",
7900
- "trace-query"
7970
+ "trace-query",
7971
+ "thread-query"
7901
7972
  ];
7902
7973
  }
7903
7974
  async createSpan(args) {
@@ -8024,6 +8095,18 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
8024
8095
  }, error);
8025
8096
  }
8026
8097
  }
8098
+ async queryThreads(plan) {
8099
+ try {
8100
+ return await queryThreads(this.#client, plan, this.#traceQueryTimeoutMs);
8101
+ } catch (error) {
8102
+ if (error instanceof MastraError || error instanceof coreStorage.TraceQueryExecutionError) throw error;
8103
+ throw new MastraError({
8104
+ id: createStorageErrorId("CLICKHOUSE", "QUERY_THREADS", "FAILED"),
8105
+ domain: ErrorDomain.STORAGE,
8106
+ category: ErrorCategory.THIRD_PARTY
8107
+ }, error);
8108
+ }
8109
+ }
8027
8110
  async listTracesLight(args) {
8028
8111
  try {
8029
8112
  return await listTracesLight(this.#client, args, this.#deltaCursorStrategy);