@mastra/clickhouse 1.28.0 → 1.29.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
@@ -8821,6 +8821,27 @@ const PERCENTILES = {
8821
8821
  "duration.p95": .95,
8822
8822
  "duration.p99": .99
8823
8823
  };
8824
+ /** Quantile for each percentile statistic of a time-to-first measure. */
8825
+ const STATISTIC_PERCENTILES = {
8826
+ p50: .5,
8827
+ p90: .9,
8828
+ p95: .95,
8829
+ p99: .99
8830
+ };
8831
+ /**
8832
+ * Store features this compiler supports beyond `trace-aggregate` / `span-aggregate`. Core before
8833
+ * the time-to-first measures rejects them, so they are only declared when core plans them.
8834
+ */
8835
+ const aggregateTimeToFirstFeatures = "timeToFirstToken.p50" in coreStorage.TRACE_AGGREGATE_MEASURE_REGISTRY ? ["aggregate-time-to-first-measures"] : [];
8836
+ /** The nullable span columns read by the plan's time-to-first measures. */
8837
+ function timeToFirstFields(plan) {
8838
+ const fields = /* @__PURE__ */ new Set();
8839
+ for (const measure of plan.measures) {
8840
+ const field = measureRule(measure.name)?.field;
8841
+ if (field) fields.add(field);
8842
+ }
8843
+ return [...fields];
8844
+ }
8824
8845
  const COMPARISON_SQL = {
8825
8846
  eq: "=",
8826
8847
  ne: "!=",
@@ -8860,7 +8881,11 @@ function compileClickHouseTraceAggregate(plan) {
8860
8881
  function compileClickHouseSpanAggregate(plan) {
8861
8882
  const parameters = new ParameterBuilder();
8862
8883
  return compileClickHouseAggregate(plan, parameters, {
8863
- ctes: compileClickHouseSpanSelection(plan, [...plan.dimensions, ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : [])], parameters),
8884
+ ctes: compileClickHouseSpanSelection(plan, [
8885
+ ...plan.dimensions,
8886
+ ...plan.measures.flatMap((measure) => measure.type === "countDistinct" && measure.field !== "traceId" ? [measure.field] : []),
8887
+ ...timeToFirstFields(plan)
8888
+ ], parameters),
8864
8889
  from: "selected",
8865
8890
  dimensionSql: (field) => lookupDimensionSql(SPAN_DIMENSION_SQL, field),
8866
8891
  isErrorSql: "r.status = 'error'",
@@ -8886,7 +8911,7 @@ function compileClickHouseAggregate(plan, parameters, source) {
8886
8911
  distinctColumns.set(measure.field, column);
8887
8912
  factColumns.push(`CAST(${distinctSql(measure.field)} AS Nullable(String)) AS ${column}`);
8888
8913
  }
8889
- factColumns.push(`toFloat64(${durationMsSql("r.startedAt", "r.endedAt")}) AS durationMs`, `${source.isErrorSql} AS isError`, `cityHash64(${source.usageKey.map((column) => `r.${column}`).join(", ")}) AS traceSeed`);
8914
+ 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}`));
8890
8915
  if (plan.interval !== void 0) {
8891
8916
  const intervalMs = coreStorage.TRACE_AGGREGATE_INTERVAL_MS[plan.interval];
8892
8917
  if (intervalMs === void 0) throw new Error(`Unsupported trusted trace-aggregate interval: ${plan.interval}`);
@@ -8917,6 +8942,14 @@ function compileClickHouseAggregate(plan, parameters, source) {
8917
8942
  const aggregate = rule.statistic === "avg" ? "avgOrNull" : "sumOrNull";
8918
8943
  return `if(${mixedUnitsSql}, NULL, toFloat64(${aggregate}(traceCost)))`;
8919
8944
  }
8945
+ if (rule?.field) {
8946
+ if (rule.statistic === "count") return `toFloat64(count(${rule.field}))`;
8947
+ if (rule.statistic === "avg") return `avgOrNull(${rule.field})`;
8948
+ if (rule.statistic === "min") return `minOrNull(${rule.field})`;
8949
+ if (rule.statistic === "max") return `maxOrNull(${rule.field})`;
8950
+ const quantile = rule.statistic && STATISTIC_PERCENTILES[rule.statistic];
8951
+ if (quantile !== void 0) return `quantileDeterministicOrNull(${quantile})(${rule.field}, traceSeed)`;
8952
+ }
8920
8953
  if (name === "errorCount") return "toFloat64(countIf(isError))";
8921
8954
  if (name === "errorRate") return "countIf(isError) / count()";
8922
8955
  if (name === "duration.avg") return "avg(durationMs)";
@@ -10424,6 +10457,7 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
10424
10457
  "trace-query-root-span-fields",
10425
10458
  "trace-query-root-entity-id",
10426
10459
  "trace-aggregate",
10460
+ ...aggregateTimeToFirstFeatures,
10427
10461
  ...spanQueryFeatures,
10428
10462
  ...spanAggregateFeatures,
10429
10463
  ...threadQueryFeatures
@@ -10450,6 +10484,7 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
10450
10484
  "trace-query-root-span-fields",
10451
10485
  "trace-query-root-entity-id",
10452
10486
  "trace-aggregate",
10487
+ ...aggregateTimeToFirstFeatures,
10453
10488
  ...spanQueryFeatures,
10454
10489
  ...spanAggregateFeatures,
10455
10490
  ...threadQueryFeatures