@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.
package/dist/index.js CHANGED
@@ -6790,9 +6790,20 @@ function compileThreadPredicate(predicate, parameters) {
6790
6790
  if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileThreadPredicate(arg, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6791
6791
  return `NOT (${compileThreadPredicate(predicate.arg, parameters)})`;
6792
6792
  }
6793
- function compileClickHouseTraceScope(selection, relationCollections, parameters) {
6793
+ /**
6794
+ * Tenant conditions ANDed into every root and related-signal scan. Columns are
6795
+ * `Nullable(String)`, so rows without a tenant never match a scope.
6796
+ */
6797
+ function compileTenantScope(scope, parameters) {
6798
+ if (!scope) return "";
6799
+ let sql = `\n AND organizationId = ${parameters.add(scope.organizationId, "String")}`;
6800
+ if (scope.resourceId !== void 0) sql += `\n AND resourceId = ${parameters.add(scope.resourceId, "String")}`;
6801
+ return sql;
6802
+ }
6803
+ function compileClickHouseTraceScope(selection, relationCollections, parameters, scope) {
6794
6804
  const from = parameters.add(selection.timeRange.from, "DateTime64(3, 'UTC')");
6795
6805
  const to = parameters.add(selection.timeRange.to, "DateTime64(3, 'UTC')");
6806
+ const tenant = compileTenantScope(scope, parameters);
6796
6807
  const ctes = [`current_roots AS (
6797
6808
  SELECT * FROM (
6798
6809
  SELECT *
@@ -6806,7 +6817,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6806
6817
  SELECT *
6807
6818
  FROM current_roots
6808
6819
  WHERE startedAt >= ${from}
6809
- AND startedAt < ${to}
6820
+ AND startedAt < ${to}${tenant}
6810
6821
  )`];
6811
6822
  if (relationCollections.has("spans")) ctes.push(`current_spans AS (
6812
6823
  SELECT
@@ -6828,7 +6839,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6828
6839
  rootEntityVersionId
6829
6840
  FROM ${TABLE_SPAN_EVENTS}
6830
6841
  WHERE isNotNull(traceId)
6831
- AND traceId IN (SELECT traceId FROM root_scope)
6842
+ AND traceId IN (SELECT traceId FROM root_scope)${tenant}
6832
6843
  ORDER BY dedupeKey
6833
6844
  LIMIT 1 BY dedupeKey
6834
6845
  )`);
@@ -6846,7 +6857,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6846
6857
  rootEntityVersionId
6847
6858
  FROM ${currentScoresRelation()} AS current
6848
6859
  WHERE isNotNull(current.traceId)
6849
- AND current.traceId IN (SELECT traceId FROM root_scope)
6860
+ AND current.traceId IN (SELECT traceId FROM root_scope)${tenant}
6850
6861
  )`);
6851
6862
  if (relationCollections.has("feedback")) ctes.push(`current_feedback AS (
6852
6863
  SELECT
@@ -6869,7 +6880,7 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters)
6869
6880
  LIMIT 1 BY feedbackId
6870
6881
  ) AS current
6871
6882
  WHERE isNotNull(traceId)
6872
- AND traceId IN (SELECT traceId FROM root_scope)
6883
+ AND traceId IN (SELECT traceId FROM root_scope)${tenant}
6873
6884
  )`);
6874
6885
  return ctes;
6875
6886
  }
@@ -6886,7 +6897,7 @@ function parseDeltaWatermark(value) {
6886
6897
  }
6887
6898
  function compileClickHouseTraceQuery(plan, deltaHead) {
6888
6899
  const parameters = new ParameterBuilder();
6889
- const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters);
6900
+ const ctes = compileClickHouseTraceScope(plan, collectRelationCollections(plan.where), parameters, plan.scope);
6890
6901
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
6891
6902
  ctes.push(`candidates AS (
6892
6903
  SELECT ${TRACE_SELECT}
@@ -6989,7 +7000,7 @@ function compileClickHouseThreadQuery(plan) {
6989
7000
  const parameters = new ParameterBuilder();
6990
7001
  const relationCollections = collectRelationCollections(plan.traces.where);
6991
7002
  collectThreadRelationCollections(plan.where, relationCollections);
6992
- const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters);
7003
+ const ctes = compileClickHouseTraceScope(plan.traces, relationCollections, parameters, plan.scope);
6993
7004
  const eligibility = plan.traces.where ? compilePredicate(plan.traces.where, parameters) : "1";
6994
7005
  ctes.push(`eligible_roots AS (
6995
7006
  SELECT *
@@ -7037,7 +7048,7 @@ function discoveryCollections(scope) {
7037
7048
  }
7038
7049
  function compileClickHouseTraceQueryObservedFields(plan) {
7039
7050
  const parameters = new ParameterBuilder();
7040
- const ctes = compileClickHouseTraceScope(plan, /* @__PURE__ */ new Set(), parameters);
7051
+ const ctes = compileClickHouseTraceScope(plan, /* @__PURE__ */ new Set(), parameters, plan.scope);
7041
7052
  const search = plan.search ? `AND positionCaseInsensitiveUTF8(concat('metadata.', key), ${parameters.add(plan.search, "String")}) > 0` : "";
7042
7053
  const limit = parameters.add(plan.limit + 1, "UInt64");
7043
7054
  ctes.push(`metadata_entries AS (
@@ -7067,7 +7078,7 @@ LIMIT ${limit}`,
7067
7078
  }
7068
7079
  function compileClickHouseTraceQueryValues(plan) {
7069
7080
  const parameters = new ParameterBuilder();
7070
- const ctes = compileClickHouseTraceScope(plan, discoveryCollections(plan.predicateScope), parameters);
7081
+ const ctes = compileClickHouseTraceScope(plan, discoveryCollections(plan.predicateScope), parameters, plan.scope);
7071
7082
  let field;
7072
7083
  if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
7073
7084
  const key = parameters.add(plan.path.slice(9), "String");
@@ -8522,7 +8533,8 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
8522
8533
  "logs",
8523
8534
  "trace-query",
8524
8535
  "trace-query-discovery",
8525
- "thread-query"
8536
+ "thread-query",
8537
+ "trace-query-tenant-scope"
8526
8538
  ];
8527
8539
  return [
8528
8540
  "metrics",
@@ -8530,7 +8542,8 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
8530
8542
  "delta-polling",
8531
8543
  "trace-query",
8532
8544
  "trace-query-discovery",
8533
- "thread-query"
8545
+ "thread-query",
8546
+ "trace-query-tenant-scope"
8534
8547
  ];
8535
8548
  }
8536
8549
  async createSpan(args) {