@mastra/clickhouse 1.28.0 → 1.29.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.28.0"
6
+ version: "1.29.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.28.0",
2
+ "version": "1.29.0-alpha.1",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -6938,6 +6938,8 @@ const timingFeatures = [
6938
6938
  ...timeToFirstTokenSupported ? ["trace-query-time-to-first-token"] : [],
6939
6939
  ...timeToFirstTokenSupported && timeToFirstTokenSortSupported ? ["trace-query-time-to-first-token-sort"] : []
6940
6940
  ];
6941
+ /** Core before the fields rejects their trace and span predicates, so the feature is only declared when core plans them. */
6942
+ const contextFieldFeatures = "requestId" in (_mastra_core_storage.TRACE_QUERY_FIELD_REGISTRY?.trace ?? {}) ? ["trace-query-context-fields"] : [];
6941
6943
  /**
6942
6944
  * Outcome fields follow the same rule: core before them rejects their predicates and strict rows,
6943
6945
  * so they are only returned and declared when core knows them.
@@ -6983,6 +6985,18 @@ const TRACE_FIELDS = {
6983
6985
  sql: "r.organizationId",
6984
6986
  parameterType: "String"
6985
6987
  },
6988
+ requestId: {
6989
+ sql: "r.requestId",
6990
+ parameterType: "String"
6991
+ },
6992
+ serviceName: {
6993
+ sql: "r.serviceName",
6994
+ parameterType: "String"
6995
+ },
6996
+ experimentId: {
6997
+ sql: "r.experimentId",
6998
+ parameterType: "String"
6999
+ },
6986
7000
  startedAt: {
6987
7001
  sql: "r.startedAt",
6988
7002
  parameterType: "DateTime64(3, 'UTC')"
@@ -7136,6 +7150,18 @@ const SPAN_FIELDS = {
7136
7150
  organizationId: {
7137
7151
  sql: "s.organizationId",
7138
7152
  parameterType: "String"
7153
+ },
7154
+ requestId: {
7155
+ sql: "s.requestId",
7156
+ parameterType: "String"
7157
+ },
7158
+ serviceName: {
7159
+ sql: "s.serviceName",
7160
+ parameterType: "String"
7161
+ },
7162
+ experimentId: {
7163
+ sql: "s.experimentId",
7164
+ parameterType: "String"
7139
7165
  }
7140
7166
  };
7141
7167
  const SPAN_QUERY_FIELDS = {
@@ -7160,18 +7186,6 @@ const SPAN_QUERY_FIELDS = {
7160
7186
  sql: "s.resourceId",
7161
7187
  parameterType: "String"
7162
7188
  },
7163
- requestId: {
7164
- sql: "s.requestId",
7165
- parameterType: "String"
7166
- },
7167
- serviceName: {
7168
- sql: "s.serviceName",
7169
- parameterType: "String"
7170
- },
7171
- experimentId: {
7172
- sql: "s.experimentId",
7173
- parameterType: "String"
7174
- },
7175
7189
  environment: {
7176
7190
  sql: "s.environment",
7177
7191
  parameterType: "String"
@@ -7525,7 +7539,10 @@ function compileClickHouseTraceScope(selection, relationCollections, parameters,
7525
7539
  runId,
7526
7540
  sessionId,
7527
7541
  userId,
7528
- organizationId
7542
+ organizationId,
7543
+ requestId,
7544
+ serviceName,
7545
+ experimentId
7529
7546
  FROM ${TABLE_SPAN_EVENTS}
7530
7547
  WHERE isNotNull(traceId)
7531
7548
  AND traceId IN (${options.spanExistence && options.oneRootPerTrace ? windowTraces(bound) : "SELECT traceId FROM root_scope"})${tenant}${options.spanExistence ? "" : `
@@ -8843,6 +8860,27 @@ const PERCENTILES = {
8843
8860
  "duration.p95": .95,
8844
8861
  "duration.p99": .99
8845
8862
  };
8863
+ /** Quantile for each percentile statistic of a time-to-first measure. */
8864
+ const STATISTIC_PERCENTILES = {
8865
+ p50: .5,
8866
+ p90: .9,
8867
+ p95: .95,
8868
+ p99: .99
8869
+ };
8870
+ /**
8871
+ * Store features this compiler supports beyond `trace-aggregate` / `span-aggregate`. Core before
8872
+ * the time-to-first measures rejects them, so they are only declared when core plans them.
8873
+ */
8874
+ const aggregateTimeToFirstFeatures = "timeToFirstToken.p50" in _mastra_core_storage.TRACE_AGGREGATE_MEASURE_REGISTRY ? ["aggregate-time-to-first-measures"] : [];
8875
+ /** The nullable span columns read by the plan's time-to-first measures. */
8876
+ function timeToFirstFields(plan) {
8877
+ const fields = /* @__PURE__ */ new Set();
8878
+ for (const measure of plan.measures) {
8879
+ const field = measureRule(measure.name)?.field;
8880
+ if (field) fields.add(field);
8881
+ }
8882
+ return [...fields];
8883
+ }
8846
8884
  const COMPARISON_SQL = {
8847
8885
  eq: "=",
8848
8886
  ne: "!=",
@@ -8882,7 +8920,11 @@ function compileClickHouseTraceAggregate(plan) {
8882
8920
  function compileClickHouseSpanAggregate(plan) {
8883
8921
  const parameters = new ParameterBuilder();
8884
8922
  return compileClickHouseAggregate(plan, parameters, {
8885
- ctes: compileClickHouseSpanSelection(plan, [...plan.dimensions, ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : [])], parameters),
8923
+ ctes: compileClickHouseSpanSelection(plan, [
8924
+ ...plan.dimensions,
8925
+ ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : []),
8926
+ ...timeToFirstFields(plan)
8927
+ ], parameters),
8886
8928
  from: "selected",
8887
8929
  dimensionSql: (field) => lookupDimensionSql(SPAN_DIMENSION_SQL, field),
8888
8930
  isErrorSql: "r.status = 'error'",
@@ -8908,7 +8950,7 @@ function compileClickHouseAggregate(plan, parameters, source) {
8908
8950
  distinctColumns.set(measure.field, column);
8909
8951
  factColumns.push(`CAST(${distinctSql(measure.field)} AS Nullable(String)) AS ${column}`);
8910
8952
  }
8911
- factColumns.push(`toFloat64(${durationMsSql("r.startedAt", "r.endedAt")}) AS durationMs`, `${source.isErrorSql} AS isError`, `cityHash64(${source.usageKey.map((column) => `r.${column}`).join(", ")}) AS traceSeed`);
8953
+ factColumns.push(`toFloat64(${durationMsSql("r.startedAt", "r.endedAt")}) AS durationMs`, `${source.isErrorSql} AS isError`, `cityHash64(${source.usageKey.map((column) => `r.${column}`).join(", ")}) AS traceSeed`, ...timeToFirstFields(plan).map((field) => `toNullable(toFloat64(r.${field})) AS ${field}`));
8912
8954
  if (plan.interval !== void 0) {
8913
8955
  const intervalMs = _mastra_core_storage.TRACE_AGGREGATE_INTERVAL_MS[plan.interval];
8914
8956
  if (intervalMs === void 0) throw new Error(`Unsupported trusted trace-aggregate interval: ${plan.interval}`);
@@ -8939,6 +8981,14 @@ function compileClickHouseAggregate(plan, parameters, source) {
8939
8981
  const aggregate = rule.statistic === "avg" ? "avgOrNull" : "sumOrNull";
8940
8982
  return `if(${mixedUnitsSql}, NULL, toFloat64(${aggregate}(traceCost)))`;
8941
8983
  }
8984
+ if (rule?.field) {
8985
+ if (rule.statistic === "count") return `toFloat64(count(${rule.field}))`;
8986
+ if (rule.statistic === "avg") return `avgOrNull(${rule.field})`;
8987
+ if (rule.statistic === "min") return `minOrNull(${rule.field})`;
8988
+ if (rule.statistic === "max") return `maxOrNull(${rule.field})`;
8989
+ const quantile = rule.statistic && STATISTIC_PERCENTILES[rule.statistic];
8990
+ if (quantile !== void 0) return `quantileDeterministicOrNull(${quantile})(${rule.field}, traceSeed)`;
8991
+ }
8942
8992
  if (name === "errorCount") return "toFloat64(countIf(isError))";
8943
8993
  if (name === "errorRate") return "countIf(isError) / count()";
8944
8994
  if (name === "duration.avg") return "avg(durationMs)";
@@ -10441,11 +10491,13 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
10441
10491
  "trace-query-tenant-scope",
10442
10492
  "feedback",
10443
10493
  "trace-query-context-ids",
10494
+ ...contextFieldFeatures,
10444
10495
  ...timingFeatures,
10445
10496
  ...outcomeFeatures,
10446
10497
  "trace-query-root-span-fields",
10447
10498
  "trace-query-root-entity-id",
10448
10499
  "trace-aggregate",
10500
+ ...aggregateTimeToFirstFeatures,
10449
10501
  ...spanQueryFeatures,
10450
10502
  ...spanAggregateFeatures,
10451
10503
  ...threadQueryFeatures
@@ -10467,11 +10519,13 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
10467
10519
  "trace-query-tenant-scope",
10468
10520
  "feedback",
10469
10521
  "trace-query-context-ids",
10522
+ ...contextFieldFeatures,
10470
10523
  ...timingFeatures,
10471
10524
  ...outcomeFeatures,
10472
10525
  "trace-query-root-span-fields",
10473
10526
  "trace-query-root-entity-id",
10474
10527
  "trace-aggregate",
10528
+ ...aggregateTimeToFirstFeatures,
10475
10529
  ...spanQueryFeatures,
10476
10530
  ...spanAggregateFeatures,
10477
10531
  ...threadQueryFeatures