@mastra/clickhouse 1.19.0 → 1.20.0-alpha.1

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.19.0"
6
+ version: "1.20.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.19.0",
2
+ "version": "1.20.0-alpha.1",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -3279,7 +3279,10 @@ CREATE TABLE IF NOT EXISTS ${TABLE_DELETION_REQUESTS} (
3279
3279
  requestedBy String DEFAULT '',
3280
3280
  lastAppliedAt DateTime64(3) DEFAULT 0,
3281
3281
  purgeVerifiedAt DateTime64(3) DEFAULT 0,
3282
- updatedAt DateTime64(3)
3282
+ updatedAt DateTime64(3),
3283
+ -- Bloom-filter skip index so has(predicateValues, id) mutation guards
3284
+ -- can skip granules instead of scanning every request in a tenant scope.
3285
+ INDEX idx_predicateValues predicateValues TYPE bloom_filter(0.01) GRANULARITY 2
3283
3286
  )
3284
3287
  ENGINE = ReplacingMergeTree(updatedAt)
3285
3288
  ORDER BY (organizationId, resourceId, requestId)
@@ -3507,7 +3510,8 @@ const ALL_MIGRATIONS = [
3507
3510
  addBloomIndex(TABLE_METRIC_EVENTS, "idx_experimentId", "experimentId"),
3508
3511
  addBloomIndex(TABLE_METRIC_EVENTS, "idx_runId", "runId"),
3509
3512
  addBloomIndex(TABLE_METRIC_EVENTS, "idx_sessionId", "sessionId"),
3510
- addBloomIndex(TABLE_METRIC_EVENTS, "idx_requestId", "requestId")
3513
+ addBloomIndex(TABLE_METRIC_EVENTS, "idx_requestId", "requestId"),
3514
+ addBloomIndex(TABLE_DELETION_REQUESTS, "idx_predicateValues", "predicateValues")
3511
3515
  ];
3512
3516
  const ALL_TABLE_NAMES = [
3513
3517
  TABLE_SPAN_EVENTS,
@@ -6793,6 +6797,76 @@ LIMIT ${limit}`,
6793
6797
  query_params: parameters.params
6794
6798
  };
6795
6799
  }
6800
+ function discoveryRegistry(scope) {
6801
+ if (scope === "trace") return TRACE_FIELDS;
6802
+ if (scope === "spans") return SPAN_FIELDS;
6803
+ if (scope === "scores") return SCORE_FIELDS;
6804
+ return FEEDBACK_FIELDS;
6805
+ }
6806
+ function discoverySource(scope) {
6807
+ if (scope === "trace") return "root_scope r";
6808
+ if (scope === "spans") return "current_spans s";
6809
+ if (scope === "scores") return "current_scores s";
6810
+ return "current_feedback s";
6811
+ }
6812
+ function discoveryCollections(scope) {
6813
+ return scope === "trace" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([scope]);
6814
+ }
6815
+ function compileClickHouseTraceQueryObservedFields(plan) {
6816
+ const parameters = new ParameterBuilder();
6817
+ const ctes = compileClickHouseTraceScope(plan, /* @__PURE__ */ new Set(), parameters);
6818
+ const search = plan.search ? `AND positionCaseInsensitiveUTF8(concat('metadata.', key), ${parameters.add(plan.search, "String")}) > 0` : "";
6819
+ const limit = parameters.add(plan.limit + 1, "UInt64");
6820
+ ctes.push(`metadata_entries AS (
6821
+ SELECT
6822
+ entry.1 AS key,
6823
+ entry.2 AS rawValue,
6824
+ JSONExtractString(entry.2) AS value
6825
+ FROM root_scope r
6826
+ ARRAY JOIN JSONExtractKeysAndValuesRaw(ifNull(r.metadataRaw, '{}')) AS entry
6827
+ )`);
6828
+ return {
6829
+ query: `WITH ${ctes.join(",\n")}
6830
+ SELECT concat('metadata.', key) AS path, count() AS occurrences
6831
+ FROM metadata_entries
6832
+ WHERE JSONType(rawValue) = 'String'
6833
+ AND trim(value) != ''
6834
+ AND key != ''
6835
+ AND position(key, '.') = 0
6836
+ AND length(concat('metadata.', key)) <= ${_mastra_core_storage.TRACE_QUERY_MAX_PATH_BYTES}
6837
+ AND length(value) <= ${_mastra_core_storage.TRACE_QUERY_MAX_STRING_BYTES}
6838
+ ${search}
6839
+ GROUP BY key
6840
+ ORDER BY occurrences DESC, path ASC
6841
+ LIMIT ${limit}`,
6842
+ query_params: parameters.params
6843
+ };
6844
+ }
6845
+ function compileClickHouseTraceQueryValues(plan) {
6846
+ const parameters = new ParameterBuilder();
6847
+ const ctes = compileClickHouseTraceScope(plan, discoveryCollections(plan.predicateScope), parameters);
6848
+ let field;
6849
+ if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
6850
+ const key = parameters.add(plan.path.slice(9), "String");
6851
+ field = `coalesce(if(mapContains(r.metadataSearch, ${key}), r.metadataSearch[${key}], NULL), nullIf(trim(JSONExtractString(r.metadataRaw, ${key})), ''))`;
6852
+ } else field = fieldDefinition(discoveryRegistry(plan.predicateScope), plan.path).sql;
6853
+ const search = plan.search ? `AND positionCaseInsensitiveUTF8(value, ${parameters.add(plan.search, "String")}) > 0` : "";
6854
+ const limit = parameters.add(plan.limit + 1, "UInt64");
6855
+ return {
6856
+ query: `WITH ${ctes.join(",\n")}, extracted AS (
6857
+ SELECT toString(${field}) AS value FROM ${discoverySource(plan.predicateScope)}
6858
+ )
6859
+ SELECT value, count() AS count
6860
+ FROM extracted
6861
+ WHERE value IS NOT NULL
6862
+ AND length(value) <= ${_mastra_core_storage.TRACE_QUERY_MAX_STRING_BYTES}
6863
+ ${search}
6864
+ GROUP BY value
6865
+ ORDER BY count DESC, value ASC
6866
+ LIMIT ${limit}`,
6867
+ query_params: parameters.params
6868
+ };
6869
+ }
6796
6870
  function asIsoTimestamp(value) {
6797
6871
  return new Date(value).toISOString();
6798
6872
  }
@@ -6819,6 +6893,27 @@ async function runWithClickHouseTraceQueryTimeout(client, timeoutMs, compiled, q
6819
6893
  throw error;
6820
6894
  }
6821
6895
  }
6896
+ async function getTraceQueryObservedFields(client, plan, timeoutMs) {
6897
+ if (plan.predicateScope !== "trace") return {
6898
+ observedFields: [],
6899
+ observedFieldsTruncated: false
6900
+ };
6901
+ const rows = await runWithClickHouseTraceQueryTimeout(client, timeoutMs, compileClickHouseTraceQueryObservedFields(plan));
6902
+ return {
6903
+ observedFields: rows.slice(0, plan.limit).map((row) => _mastra_core_storage.createTraceQueryObservedFieldDescriptor(String(row.path), Number(row.occurrences))),
6904
+ observedFieldsTruncated: rows.length > plan.limit
6905
+ };
6906
+ }
6907
+ async function getTraceQueryValues(client, plan, timeoutMs) {
6908
+ const rows = await runWithClickHouseTraceQueryTimeout(client, timeoutMs, compileClickHouseTraceQueryValues(plan));
6909
+ return _mastra_core_storage.getTraceQueryValuesResponseSchema.parse({
6910
+ values: rows.slice(0, plan.limit).map((row) => ({
6911
+ value: String(row.value),
6912
+ count: Number(row.count)
6913
+ })),
6914
+ valuesTruncated: rows.length > plan.limit
6915
+ });
6916
+ }
6822
6917
  async function queryTraces(client, plan, timeoutMs) {
6823
6918
  const rows = await runWithClickHouseTraceQueryTimeout(client, timeoutMs, compileClickHouseTraceQuery(plan));
6824
6919
  const visibleRows = rows.slice(0, plan.limit);
@@ -7984,6 +8079,7 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
7984
8079
  "metrics",
7985
8080
  "logs",
7986
8081
  "trace-query",
8082
+ "trace-query-discovery",
7987
8083
  "thread-query"
7988
8084
  ];
7989
8085
  return [
@@ -7991,6 +8087,7 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
7991
8087
  "logs",
7992
8088
  "delta-polling",
7993
8089
  "trace-query",
8090
+ "trace-query-discovery",
7994
8091
  "thread-query"
7995
8092
  ];
7996
8093
  }
@@ -8118,6 +8215,30 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
8118
8215
  }, error);
8119
8216
  }
8120
8217
  }
8218
+ async getTraceQueryObservedFields(plan) {
8219
+ try {
8220
+ return await getTraceQueryObservedFields(this.#client, plan, this.#traceQueryTimeoutMs);
8221
+ } catch (error) {
8222
+ if (error instanceof _mastra_core_error.MastraError || error instanceof _mastra_core_storage.TraceQueryExecutionError) throw error;
8223
+ throw new _mastra_core_error.MastraError({
8224
+ id: (0, _mastra_core_storage.createStorageErrorId)("CLICKHOUSE", "GET_TRACE_QUERY_OBSERVED_FIELDS", "FAILED"),
8225
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
8226
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
8227
+ }, error);
8228
+ }
8229
+ }
8230
+ async getTraceQueryValues(plan) {
8231
+ try {
8232
+ return await getTraceQueryValues(this.#client, plan, this.#traceQueryTimeoutMs);
8233
+ } catch (error) {
8234
+ if (error instanceof _mastra_core_error.MastraError || error instanceof _mastra_core_storage.TraceQueryExecutionError) throw error;
8235
+ throw new _mastra_core_error.MastraError({
8236
+ id: (0, _mastra_core_storage.createStorageErrorId)("CLICKHOUSE", "GET_TRACE_QUERY_VALUES", "FAILED"),
8237
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
8238
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
8239
+ }, error);
8240
+ }
8241
+ }
8121
8242
  async queryThreads(plan) {
8122
8243
  try {
8123
8244
  return await queryThreads(this.#client, plan, this.#traceQueryTimeoutMs);