@mastra/clickhouse 1.20.1 → 1.21.0-alpha.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.1"
6
+ version: "1.21.0-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.20.1",
2
+ "version": "1.21.0-alpha.0",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -6813,9 +6813,20 @@ function compileThreadPredicate(predicate, parameters) {
6813
6813
  if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileThreadPredicate(arg, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6814
6814
  return `NOT (${compileThreadPredicate(predicate.arg, parameters)})`;
6815
6815
  }
6816
- function compileClickHouseTraceScope(selection, relationCollections, parameters) {
6816
+ /**
6817
+ * Tenant conditions ANDed into every root and related-signal scan. Columns are
6818
+ * `Nullable(String)`, so rows without a tenant never match a scope.
6819
+ */
6820
+ function compileTenantScope(scope, parameters) {
6821
+ if (!scope) return "";
6822
+ let sql = `\n AND organizationId = ${parameters.add(scope.organizationId, "String")}`;
6823
+ if (scope.resourceId !== void 0) sql += `\n AND resourceId = ${parameters.add(scope.resourceId, "String")}`;
6824
+ return sql;
6825
+ }
6826
+ function compileClickHouseTraceScope(selection, relationCollections, parameters, scope) {
6817
6827
  const from = parameters.add(selection.timeRange.from, "DateTime64(3, 'UTC')");
6818
6828
  const to = parameters.add(selection.timeRange.to, "DateTime64(3, 'UTC')");
6829
+ const tenant = compileTenantScope(scope, parameters);
6819
6830
  const ctes = [`current_roots AS (
6820
6831
  SELECT * FROM (
6821
6832
  SELECT *
@@ -6829,7 +6840,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6829
6840
  SELECT *
6830
6841
  FROM current_roots
6831
6842
  WHERE startedAt >= ${from}
6832
- AND startedAt < ${to}
6843
+ AND startedAt < ${to}${tenant}
6833
6844
  )`];
6834
6845
  if (relationCollections.has("spans")) ctes.push(`current_spans AS (
6835
6846
  SELECT
@@ -6851,7 +6862,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6851
6862
  rootEntityVersionId
6852
6863
  FROM ${TABLE_SPAN_EVENTS}
6853
6864
  WHERE isNotNull(traceId)
6854
- AND traceId IN (SELECT traceId FROM root_scope)
6865
+ AND traceId IN (SELECT traceId FROM root_scope)${tenant}
6855
6866
  ORDER BY dedupeKey
6856
6867
  LIMIT 1 BY dedupeKey
6857
6868
  )`);
@@ -6869,7 +6880,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6869
6880
  rootEntityVersionId
6870
6881
  FROM ${currentScoresRelation()} AS current
6871
6882
  WHERE isNotNull(current.traceId)
6872
- AND current.traceId IN (SELECT traceId FROM root_scope)
6883
+ AND current.traceId IN (SELECT traceId FROM root_scope)${tenant}
6873
6884
  )`);
6874
6885
  if (relationCollections.has("feedback")) ctes.push(`current_feedback AS (
6875
6886
  SELECT
@@ -6892,7 +6903,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6892
6903
  LIMIT 1 BY feedbackId
6893
6904
  ) AS current
6894
6905
  WHERE isNotNull(traceId)
6895
- AND traceId IN (SELECT traceId FROM root_scope)
6906
+ AND traceId IN (SELECT traceId FROM root_scope)${tenant}
6896
6907
  )`);
6897
6908
  return ctes;
6898
6909
  }
@@ -6909,7 +6920,7 @@ function parseDeltaWatermark(value) {
6909
6920
  }
6910
6921
  function compileClickHouseTraceQuery(plan, deltaHead) {
6911
6922
  const parameters = new ParameterBuilder();
6912
- const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters);
6923
+ const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters, plan.scope);
6913
6924
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
6914
6925
  ctes.push(`candidates AS (
6915
6926
  SELECT ${TRACE_SELECT}
@@ -7012,7 +7023,7 @@ function compileClickHouseThreadQuery(plan) {
7012
7023
  const parameters = new ParameterBuilder();
7013
7024
  const relationCollections = collectRelationCollections(plan.traces.where);
7014
7025
  collectThreadRelationCollections(plan.where, relationCollections);
7015
- const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters);
7026
+ const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters, plan.scope);
7016
7027
  const eligibility = plan.traces.where ? compilePredicate(plan.traces.where, parameters) : "1";
7017
7028
  ctes.push(`eligible_roots AS (
7018
7029
  SELECT *
@@ -7060,7 +7071,7 @@ function discoveryCollections(scope) {
7060
7071
  }
7061
7072
  function compileClickHouseTraceQueryObservedFields(plan) {
7062
7073
  const parameters = new ParameterBuilder();
7063
- const ctes = compileClickHouseTraceScope(plan, /* @__PURE__ */ new Set(), parameters);
7074
+ const ctes = compileClickHouseTraceScope(plan, /* @__PURE__ */ new Set(), parameters, plan.scope);
7064
7075
  const search = plan.search ? `AND positionCaseInsensitiveUTF8(concat('metadata.', key), ${parameters.add(plan.search, "String")}) > 0` : "";
7065
7076
  const limit = parameters.add(plan.limit + 1, "UInt64");
7066
7077
  ctes.push(`metadata_entries AS (
@@ -7090,7 +7101,7 @@ LIMIT ${limit}`,
7090
7101
  }
7091
7102
  function compileClickHouseTraceQueryValues(plan) {
7092
7103
  const parameters = new ParameterBuilder();
7093
- const ctes = compileClickHouseTraceScope(plan, discoveryCollections(plan.predicateScope), parameters);
7104
+ const ctes = compileClickHouseTraceScope(plan, discoveryCollections(plan.predicateScope), parameters, plan.scope);
7094
7105
  let field;
7095
7106
  if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
7096
7107
  const key = parameters.add(plan.path.slice(9), "String");
@@ -8545,7 +8556,8 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
8545
8556
  "logs",
8546
8557
  "trace-query",
8547
8558
  "trace-query-discovery",
8548
- "thread-query"
8559
+ "thread-query",
8560
+ "trace-query-tenant-scope"
8549
8561
  ];
8550
8562
  return [
8551
8563
  "metrics",
@@ -8553,7 +8565,8 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
8553
8565
  "delta-polling",
8554
8566
  "trace-query",
8555
8567
  "trace-query-discovery",
8556
- "thread-query"
8568
+ "thread-query",
8569
+ "trace-query-tenant-scope"
8557
8570
  ];
8558
8571
  }
8559
8572
  async createSpan(args) {