@mastra/duckdb 1.12.1 → 1.13.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.
Files changed (33) hide show
  1. package/dist/docs/SKILL.md +1 -1
  2. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  3. package/dist/{db-zp2htaur.cjs → features-BzhPmFg2.cjs} +63 -2
  4. package/dist/features-BzhPmFg2.cjs.map +1 -0
  5. package/dist/{db-BWgrbB_u.js → features-D3sUOsCL.js} +23 -3
  6. package/dist/features-D3sUOsCL.js.map +1 -0
  7. package/dist/index.cjs +16 -6
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.js +12 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/{observability-BmLO_YTO.js → observability-Bk_y3hof.js} +460 -43
  12. package/dist/observability-Bk_y3hof.js.map +1 -0
  13. package/dist/{observability-DlmDRPW3.cjs → observability-DNuNWZFX.cjs} +463 -68
  14. package/dist/observability-DNuNWZFX.cjs.map +1 -0
  15. package/dist/storage/db/index.d.ts +5 -1
  16. package/dist/storage/db/index.d.ts.map +1 -1
  17. package/dist/storage/domains/observability/index.d.ts +4 -2
  18. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  19. package/dist/storage/domains/observability/span-query.d.ts +43 -0
  20. package/dist/storage/domains/observability/span-query.d.ts.map +1 -0
  21. package/dist/storage/domains/observability/trace-aggregate.d.ts +17 -0
  22. package/dist/storage/domains/observability/trace-aggregate.d.ts.map +1 -0
  23. package/dist/storage/domains/observability/trace-query.d.ts +26 -1
  24. package/dist/storage/domains/observability/trace-query.d.ts.map +1 -1
  25. package/dist/storage/features.d.ts +2 -0
  26. package/dist/storage/features.d.ts.map +1 -0
  27. package/dist/storage/index.d.ts +2 -0
  28. package/dist/storage/index.d.ts.map +1 -1
  29. package/package.json +5 -5
  30. package/dist/db-BWgrbB_u.js.map +0 -1
  31. package/dist/db-zp2htaur.cjs.map +0 -1
  32. package/dist/observability-BmLO_YTO.js.map +0 -1
  33. package/dist/observability-DlmDRPW3.cjs.map +0 -1
@@ -1,29 +1,7 @@
1
- //#region \0rolldown/runtime.js
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
- //#endregion
23
- const require_db = require("./db-zp2htaur.cjs");
1
+ const require_features = require("./features-BzhPmFg2.cjs");
24
2
  let _mastra_core_error = require("@mastra/core/error");
25
3
  let _mastra_core_storage = require("@mastra/core/storage");
26
- _mastra_core_storage = __toESM(_mastra_core_storage, 1);
4
+ _mastra_core_storage = require_features.__toESM(_mastra_core_storage, 1);
27
5
  let _mastra_core_features = require("@mastra/core/features");
28
6
  let _mastra_core_observability = require("@mastra/core/observability");
29
7
  let _mastra_core_utils = require("@mastra/core/utils");
@@ -703,11 +681,11 @@ function buildPaginationClause(pagination) {
703
681
  //#endregion
704
682
  //#region src/storage/domains/observability/helpers.ts
705
683
  /** Shorthand for {@link DuckDBConnection.sqlValue}. */
706
- const v = require_db.DuckDBConnection.sqlValue;
684
+ const v = require_features.DuckDBConnection.sqlValue;
707
685
  /** Serialize a value to JSON then SQL-escape it, or return 'NULL'. */
708
686
  function jsonV(val) {
709
687
  if (val === null || val === void 0) return "NULL";
710
- return require_db.DuckDBConnection.sqlValue(JSON.stringify(val));
688
+ return require_features.DuckDBConnection.sqlValue(JSON.stringify(val));
711
689
  }
712
690
  /**
713
691
  * Trim, drop blank, and dedupe tags before they are written, matching the
@@ -2725,6 +2703,15 @@ const TRACE_STATUS_SQL = `CASE WHEN r.error IS NOT NULL THEN 'error' ELSE 'succe
2725
2703
  function durationMsSql(startedAt, endedAt) {
2726
2704
  return `date_diff('millisecond', ${startedAt}, ${endedAt})`;
2727
2705
  }
2706
+ /**
2707
+ * Top-level string metadata value of the trace root (alias `r`). The SQL binds the JSON path
2708
+ * twice; use `traceMetadataPathValues` for the matching parameters.
2709
+ */
2710
+ const TRACE_METADATA_VALUE_SQL = `NULLIF(trim(CASE WHEN json_type(r.metadata, ?) = 'VARCHAR' THEN json_extract_string(r.metadata, ?) END), '')`;
2711
+ function traceMetadataPathValues(key) {
2712
+ const path = `$.${JSON.stringify(key)}`;
2713
+ return [path, path];
2714
+ }
2728
2715
  const TRACE_FIELDS = {
2729
2716
  traceId: {
2730
2717
  sql: "r.traceId",
@@ -2991,13 +2978,11 @@ function compileScalarPredicate(predicate, registry, allowMetadata = false) {
2991
2978
  let fieldValues = [];
2992
2979
  if (isMetadataField(predicate.field)) {
2993
2980
  if (!allowMetadata) throw new Error(`Unsupported trusted trace-query field: ${predicate.field}`);
2994
- const key = predicate.field.slice(9);
2995
- const path = `$.${JSON.stringify(key)}`;
2996
2981
  field = {
2997
- sql: `NULLIF(trim(CASE WHEN json_type(r.metadata, ?) = 'VARCHAR' THEN json_extract_string(r.metadata, ?) END), '')`,
2982
+ sql: TRACE_METADATA_VALUE_SQL,
2998
2983
  parameterType: "scalar"
2999
2984
  };
3000
- fieldValues = [path, path];
2985
+ fieldValues = traceMetadataPathValues(predicate.field.slice(9));
3001
2986
  } else field = fieldDefinition(registry, predicate.field);
3002
2987
  if (predicate.type === "presence") return {
3003
2988
  sql: `${field.sql} IS ${predicate.operator === "exists" ? "NOT " : ""}NULL`,
@@ -3174,15 +3159,66 @@ function collectThreadRelatedCollections(predicate, collections) {
3174
3159
  else collectThreadRelatedCollections(predicate.arg, collections);
3175
3160
  return collections;
3176
3161
  }
3177
- function compileDuckDBTraceScope(relatedCollections, scope) {
3162
+ const ROOT_PUSHDOWN_FIELDS = /* @__PURE__ */ new Set([
3163
+ "traceId",
3164
+ "threadId",
3165
+ "resourceId",
3166
+ "runId",
3167
+ "sessionId",
3168
+ "userId",
3169
+ "organizationId",
3170
+ "endedAt",
3171
+ "entityName",
3172
+ "entityType",
3173
+ "environment"
3174
+ ]);
3175
+ function isRootPushdownPredicate(predicate) {
3176
+ if (predicate.type === "relation") return false;
3177
+ if (predicate.type === "boolean") return predicate.args.every(isRootPushdownPredicate);
3178
+ if (predicate.type === "not") return isRootPushdownPredicate(predicate.arg);
3179
+ return ROOT_PUSHDOWN_FIELDS.has(predicate.field);
3180
+ }
3181
+ /** Top-level AND conjuncts of `where` that only read root-row columns. */
3182
+ function rootPushdownPredicates(predicate) {
3183
+ if (!predicate) return [];
3184
+ return (predicate.type === "boolean" && predicate.operator === "and" ? predicate.args : [predicate]).filter(isRootPushdownPredicate);
3185
+ }
3186
+ /**
3187
+ * Builds the selection CTEs. `root_trace_ids` narrows the root scan to traces that could have a
3188
+ * qualifying current root before the window functions run: some root event carries the current
3189
+ * root's own conditions (ended, tenant, pushed `where` conjuncts) and some root event has a
3190
+ * timestamp in the window (the current root's `startedAt` is a root event's timestamp in its
3191
+ * trace). This only removes traces that cannot qualify; `root_events` / `current_roots` still see
3192
+ * every root event of the remaining traces, and the full `where` is applied in `candidates`.
3193
+ */
3194
+ function compileDuckDBTraceScope(relatedCollections, scope, timeRange, rootPredicates = []) {
3178
3195
  const tenantConditions = (alias) => scope ? [`${alias}.organizationId = ?`, ...scope.resourceId === void 0 ? [] : [`${alias}.resourceId = ?`]] : [];
3179
3196
  const tenantValues = scope ? scope.resourceId === void 0 ? [scope.organizationId] : [scope.organizationId, scope.resourceId] : [];
3180
3197
  const tenantWhere = (alias) => {
3181
3198
  const conditions = tenantConditions(alias);
3182
3199
  return conditions.length ? `\n WHERE ${conditions.join(" AND ")}` : "";
3183
3200
  };
3184
- const values = [...tenantValues];
3201
+ const rowConditions = ["r.endedAt IS NOT NULL", ...tenantConditions("r")];
3202
+ const values = [
3203
+ timeRange.from,
3204
+ timeRange.to,
3205
+ ...tenantValues
3206
+ ];
3207
+ for (const predicate of rootPredicates) {
3208
+ const compiled = compilePredicate(predicate);
3209
+ rowConditions.push(`(${compiled.sql})`);
3210
+ values.push(...compiled.values);
3211
+ }
3212
+ values.push(timeRange.from, timeRange.to, ...tenantValues);
3185
3213
  const ctes = [
3214
+ `root_trace_ids AS (
3215
+ SELECT r.traceId
3216
+ FROM span_events r
3217
+ WHERE r.parentSpanId IS NULL
3218
+ GROUP BY r.traceId
3219
+ HAVING bool_or(r.timestamp >= CAST(? AS TIMESTAMP) AND r.timestamp < CAST(? AS TIMESTAMP))
3220
+ AND bool_or(${rowConditions.join("\n AND ")})
3221
+ )`,
3186
3222
  `root_events AS (
3187
3223
  SELECT
3188
3224
  *,
@@ -3192,6 +3228,7 @@ function compileDuckDBTraceScope(relatedCollections, scope) {
3192
3228
  END AS startedAt
3193
3229
  FROM span_events
3194
3230
  WHERE parentSpanId IS NULL
3231
+ AND traceId IN (SELECT traceId FROM root_trace_ids)
3195
3232
  )`,
3196
3233
  `current_roots AS (
3197
3234
  SELECT * EXCLUDE (rootRank)
@@ -3279,28 +3316,30 @@ function compileDuckDBTraceScope(relatedCollections, scope) {
3279
3316
  values
3280
3317
  };
3281
3318
  }
3282
- function compileDuckDBTraceQuery(plan) {
3283
- const { ctes, values: scopeValues } = compileDuckDBTraceScope(collectRelatedCollections(plan.where), plan.scope);
3284
- const values = [
3285
- plan.timeRange.from,
3286
- plan.timeRange.to,
3287
- ...scopeValues
3288
- ];
3289
- const conditions = [
3290
- `r.endedAt IS NOT NULL`,
3291
- `r.startedAt >= CAST(? AS TIMESTAMP)`,
3292
- `r.startedAt < CAST(? AS TIMESTAMP)`
3293
- ];
3319
+ /**
3320
+ * Builds the trace selection stages shared by trace queries and trace aggregates: root scope,
3321
+ * related-collection scopes, and a `candidates` CTE projecting `columns` from matching roots.
3322
+ */
3323
+ function compileDuckDBTraceCandidates(plan, columns) {
3324
+ const { ctes, values } = compileDuckDBTraceScope(collectRelatedCollections(plan.where), plan.scope, plan.timeRange, rootPushdownPredicates(plan.where));
3325
+ let predicateSql = "TRUE";
3294
3326
  if (plan.where) {
3295
3327
  const predicate = compilePredicate(plan.where);
3296
- conditions.push(`(${predicate.sql})`);
3328
+ predicateSql = `(${predicate.sql})`;
3297
3329
  values.push(...predicate.values);
3298
3330
  }
3299
3331
  ctes.push(`candidates AS (
3300
- SELECT ${TRACE_SELECT}${plan.paginationMode === "delta" ? ", r.cursorId AS deltaWatermark" : ""}
3332
+ SELECT ${columns}
3301
3333
  FROM root_scope r
3302
- WHERE ${conditions.slice(3).join("\n AND ") || "TRUE"}
3334
+ WHERE ${predicateSql}
3303
3335
  )`);
3336
+ return {
3337
+ ctes,
3338
+ values
3339
+ };
3340
+ }
3341
+ function compileDuckDBTraceQuery(plan) {
3342
+ const { ctes, values } = compileDuckDBTraceCandidates(plan, `${TRACE_SELECT}${plan.paginationMode === "delta" ? ", r.cursorId AS deltaWatermark" : ""}`);
3304
3343
  const candidates = `WITH ${ctes.join(",\n ")}`;
3305
3344
  if (plan.result === "groups") {
3306
3345
  const pageCondition = plan.cursor ? `AND threadId > ?` : "";
@@ -3323,7 +3362,7 @@ LIMIT ?`,
3323
3362
  values.push(watermark ?? "0", plan.limit + 1);
3324
3363
  return {
3325
3364
  sql: `${candidates},
3326
- delta_head AS (SELECT coalesce(max(cursorId), 0) AS streamHead FROM root_events),
3365
+ delta_head AS (SELECT coalesce(max(cursorId), 0) AS streamHead FROM span_events WHERE parentSpanId IS NULL),
3327
3366
  delta_rows AS (
3328
3367
  SELECT * FROM candidates
3329
3368
  WHERE deltaWatermark > CAST(? AS BIGINT) ${watermark === void 0 ? "AND FALSE" : ""}
@@ -3353,7 +3392,7 @@ ORDER BY delta_rows.deltaWatermark ASC NULLS LAST, delta_rows.traceId ASC`,
3353
3392
  SELECT COUNT(*) AS total
3354
3393
  FROM candidates
3355
3394
  )
3356
- SELECT page_rows.*, page_total.total${deltaPollingFeatureEnabled() ? ", (SELECT coalesce(max(cursorId), 0) FROM root_events) AS streamHead" : ""}
3395
+ SELECT page_rows.*, page_total.total${deltaPollingFeatureEnabled() ? ", (SELECT coalesce(max(cursorId), 0) FROM span_events WHERE parentSpanId IS NULL) AS streamHead" : ""}
3357
3396
  FROM page_total
3358
3397
  LEFT JOIN page_rows ON TRUE
3359
3398
  ORDER BY page_rows.__row_position ASC NULLS LAST`,
@@ -3379,12 +3418,7 @@ LIMIT ?`,
3379
3418
  function compileDuckDBThreadQuery(plan) {
3380
3419
  const relatedCollections = collectRelatedCollections(plan.traces.where);
3381
3420
  collectThreadRelatedCollections(plan.where, relatedCollections);
3382
- const { ctes, values: scopeValues } = compileDuckDBTraceScope(relatedCollections, plan.scope);
3383
- const values = [
3384
- plan.traces.timeRange.from,
3385
- plan.traces.timeRange.to,
3386
- ...scopeValues
3387
- ];
3421
+ const { ctes, values } = compileDuckDBTraceScope(relatedCollections, plan.scope, plan.traces.timeRange);
3388
3422
  let eligibilitySql = "TRUE";
3389
3423
  if (plan.traces.where) {
3390
3424
  const eligibility = compilePredicate(plan.traces.where);
@@ -3442,12 +3476,7 @@ function discoveryCollections(scope) {
3442
3476
  return scope === "trace" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([scope]);
3443
3477
  }
3444
3478
  function compileDuckDBTraceQueryObservedFields(plan) {
3445
- const { ctes, values: scopeValues } = compileDuckDBTraceScope(/* @__PURE__ */ new Set(), plan.scope);
3446
- const values = [
3447
- plan.timeRange.from,
3448
- plan.timeRange.to,
3449
- ...scopeValues
3450
- ];
3479
+ const { ctes, values } = compileDuckDBTraceScope(/* @__PURE__ */ new Set(), plan.scope, plan.timeRange);
3451
3480
  if (plan.search) values.push(plan.search);
3452
3481
  values.push(plan.limit + 1);
3453
3482
  const search = plan.search ? `AND strpos(lower('metadata.' || entry.key), lower(?)) > 0` : "";
@@ -3469,12 +3498,7 @@ LIMIT ?`,
3469
3498
  };
3470
3499
  }
3471
3500
  function compileDuckDBTraceQueryValues(plan) {
3472
- const { ctes, values: scopeValues } = compileDuckDBTraceScope(discoveryCollections(plan.predicateScope), plan.scope);
3473
- const values = [
3474
- plan.timeRange.from,
3475
- plan.timeRange.to,
3476
- ...scopeValues
3477
- ];
3501
+ const { ctes, values } = compileDuckDBTraceScope(discoveryCollections(plan.predicateScope), plan.scope, plan.timeRange);
3478
3502
  let fieldSql;
3479
3503
  let source = discoverySource(plan.predicateScope);
3480
3504
  if (plan.predicateScope === "trace" && plan.path === "tags") {
@@ -3642,6 +3666,367 @@ async function queryThreads(db, plan) {
3642
3666
  }) : null }
3643
3667
  });
3644
3668
  }
3669
+ /** Compile a span-row filter with exactly the same rules as trace span predicates. */
3670
+ function compileSpanQueryPredicate(predicate) {
3671
+ return compileScalarPredicate(predicate, SPAN_FIELDS);
3672
+ }
3673
+ //#endregion
3674
+ //#region src/storage/domains/observability/span-query.ts
3675
+ const identityFields = [
3676
+ "organizationId",
3677
+ "resourceId",
3678
+ "traceId",
3679
+ "spanId"
3680
+ ];
3681
+ const identities = identityFields.join(", ");
3682
+ const fields = [
3683
+ ...identityFields,
3684
+ "parentSpanId",
3685
+ "name",
3686
+ "spanType",
3687
+ "entityType",
3688
+ "entityId",
3689
+ "entityName",
3690
+ "entityVersionId",
3691
+ "parentEntityVersionId",
3692
+ "rootEntityVersionId",
3693
+ "runId",
3694
+ "sessionId",
3695
+ "userId"
3696
+ ];
3697
+ function keys(rows, values, alias = "") {
3698
+ return rows.map((row) => `(${identityFields.map((field) => {
3699
+ values.push(row[field]);
3700
+ return `${alias}${field} IS NOT DISTINCT FROM ?`;
3701
+ }).join(" AND ")})`).join(" OR ");
3702
+ }
3703
+ function afterCursor(plan) {
3704
+ if (!plan.cursor) return {
3705
+ sql: "TRUE",
3706
+ values: []
3707
+ };
3708
+ const cursor = plan.cursor;
3709
+ const comparisons = [`s.${plan.orderBy.field} ${plan.orderBy.direction === "asc" ? ">" : "<"} CAST(? AS TIMESTAMP)`];
3710
+ const values = [cursor.sortValue];
3711
+ for (let i = 0; i < identityFields.length; i++) {
3712
+ const field = identityFields[i];
3713
+ if (cursor[field] === null) continue;
3714
+ const equal = [`s.${plan.orderBy.field} = CAST(? AS TIMESTAMP)`];
3715
+ values.push(cursor.sortValue);
3716
+ for (const prior of identityFields.slice(0, i)) {
3717
+ equal.push(`s.${prior} IS NOT DISTINCT FROM ?`);
3718
+ values.push(cursor[prior]);
3719
+ }
3720
+ values.push(cursor[field]);
3721
+ comparisons.push(`(${equal.join(" AND ")} AND (s.${field} > ? OR s.${field} IS NULL))`);
3722
+ }
3723
+ return {
3724
+ sql: `(${comparisons.join(" OR ")})`,
3725
+ values
3726
+ };
3727
+ }
3728
+ function compileDuckDBSpanQuery(plan) {
3729
+ const values = [plan.timeRange.from, plan.timeRange.to];
3730
+ const scope = [];
3731
+ if (plan.scope) {
3732
+ scope.push("organizationId = ?");
3733
+ values.push(plan.scope.organizationId);
3734
+ if (plan.scope.resourceId !== void 0) {
3735
+ scope.push("resourceId = ?");
3736
+ values.push(plan.scope.resourceId);
3737
+ }
3738
+ }
3739
+ const predicate = plan.where ? compileSpanQueryPredicate(plan.where) : {
3740
+ sql: "TRUE",
3741
+ values: []
3742
+ };
3743
+ const after = afterCursor(plan);
3744
+ values.push(plan.timeRange.from, plan.timeRange.to, ...predicate.values, ...after.values, plan.limit + 1);
3745
+ const join = identityFields.map((field) => `e.${field} IS NOT DISTINCT FROM c.${field}`).join(" AND ");
3746
+ return {
3747
+ sql: `WITH candidates AS MATERIALIZED (
3748
+ SELECT ${identities} FROM span_events
3749
+ WHERE eventType = 'start' AND timestamp >= CAST(? AS TIMESTAMP) AND timestamp < CAST(? AS TIMESTAMP)
3750
+ ${scope.length ? `AND ${scope.join(" AND ")}` : ""}
3751
+ GROUP BY ${identities}
3752
+ ), ranked AS (
3753
+ SELECT ${fields.map((field) => `e.${field}`).join(", ")},
3754
+ min(e.timestamp) FILTER (WHERE e.eventType = 'start') OVER (PARTITION BY ${identityFields.map((field) => `e.${field}`).join(", ")}) AS startedAt, e.endedAt, e.cursorId,
3755
+ CASE WHEN json_type(e.attributes, '$.model') = 'VARCHAR' THEN json_extract_string(e.attributes, '$.model') END AS model,
3756
+ CASE WHEN json_type(e.attributes, '$.provider') = 'VARCHAR' THEN json_extract_string(e.attributes, '$.provider') END AS provider,
3757
+ date_diff('millisecond', min(e.timestamp) FILTER (WHERE e.eventType = 'start') OVER (PARTITION BY ${identityFields.map((field) => `e.${field}`).join(", ")}), e.endedAt) AS durationMs,
3758
+ CASE WHEN e.error IS NULL THEN 'success' ELSE 'error' END AS status,
3759
+ CASE WHEN e.error IS NULL THEN NULL ELSE 'error' END AS error,
3760
+ row_number() OVER (PARTITION BY ${identityFields.map((field) => `e.${field}`).join(", ")}
3761
+ ORDER BY CASE WHEN e.endedAt IS NULL THEN 1 ELSE 0 END, e.cursorId DESC NULLS LAST, e.endedAt DESC) AS rank
3762
+ FROM span_events e INNER JOIN candidates c ON ${join}
3763
+ )
3764
+ SELECT ${[
3765
+ ...identityFields,
3766
+ "parentSpanId",
3767
+ "name",
3768
+ "spanType",
3769
+ "entityType",
3770
+ "entityId",
3771
+ "entityName",
3772
+ "model",
3773
+ "provider",
3774
+ "startedAt",
3775
+ "endedAt",
3776
+ "durationMs",
3777
+ "status",
3778
+ "cursorId"
3779
+ ].map((field) => `s.${field}`).join(", ")}
3780
+ FROM ranked s WHERE rank = 1 AND endedAt IS NOT NULL AND startedAt >= CAST(? AS TIMESTAMP) AND startedAt < CAST(? AS TIMESTAMP) AND (${predicate.sql}) AND ${after.sql}
3781
+ ORDER BY s.${plan.orderBy.field} ${plan.orderBy.direction === "asc" ? "ASC" : "DESC"}, ${identityFields.map((field) => `s.${field} ASC NULLS LAST`).join(", ")}
3782
+ LIMIT ?`,
3783
+ values
3784
+ };
3785
+ }
3786
+ async function querySpans(db, plan) {
3787
+ const deadline = performance.now() + _mastra_core_storage.TRACE_QUERY_DEFAULT_TIMEOUT_MS;
3788
+ const query = async (sql, values) => {
3789
+ const remaining = Math.floor(deadline - performance.now());
3790
+ if (remaining <= 0) throw new _mastra_core_storage.TraceQueryExecutionError();
3791
+ try {
3792
+ return await db.query(sql, values, remaining);
3793
+ } catch (error) {
3794
+ if (error instanceof require_features.DuckDBQueryTimeoutError) throw new _mastra_core_storage.TraceQueryExecutionError();
3795
+ if (error instanceof Error && /out of memory/i.test(error.message)) throw new _mastra_core_storage.TraceQueryResourceLimitError();
3796
+ throw error;
3797
+ }
3798
+ };
3799
+ const compiled = compileDuckDBSpanQuery(plan);
3800
+ const rows = await query(compiled.sql, compiled.values);
3801
+ const selected = rows.map(({ cursorId: _cursorId, startedAt, endedAt, ...row }) => ({
3802
+ ...row,
3803
+ startedAt: startedAt.toISOString(),
3804
+ endedAt: endedAt.toISOString()
3805
+ }));
3806
+ const page = rows.slice(0, plan.limit);
3807
+ if (!page.length) return _mastra_core_storage.buildSpanQueryResponse(plan, selected, [], []);
3808
+ const payloadValues = [];
3809
+ const payloadWhere = page.map((row) => {
3810
+ const key = keys([row], payloadValues);
3811
+ payloadValues.push(row.cursorId, row.endedAt);
3812
+ return `(${key} AND cursorId IS NOT DISTINCT FROM ? AND endedAt = ?)`;
3813
+ }).join(" OR ");
3814
+ const payloads = await query(`SELECT DISTINCT ${identities},
3815
+ substring(CAST(input AS VARCHAR), 1, ${_mastra_core_storage.SPAN_QUERY_MAX_PREVIEW_CHARACTERS + 1}) AS inputPreview, substring(CAST(output AS VARCHAR), 1, ${_mastra_core_storage.SPAN_QUERY_MAX_PREVIEW_CHARACTERS + 1}) AS outputPreview
3816
+ FROM span_events WHERE ${payloadWhere}`, payloadValues);
3817
+ const metricValues = [];
3818
+ const metricWhere = keys(page, metricValues);
3819
+ const metrics = await query(`SELECT ${identities}, name, estimatedCost, costUnit,
3820
+ json_extract_string(costMetadata, '$.allocation') AS allocation,
3821
+ COALESCE(json_exists(costMetadata, '$.error'), false) AS costError
3822
+ FROM metric_events WHERE (${metricWhere}) AND name LIKE 'mastra_model_%'
3823
+ LIMIT ${_mastra_core_storage.SPAN_QUERY_MAX_COST_METRICS + 1}`, metricValues);
3824
+ if (metrics.length > _mastra_core_storage.SPAN_QUERY_MAX_COST_METRICS) throw new _mastra_core_storage.TraceQueryResourceLimitError();
3825
+ return _mastra_core_storage.buildSpanQueryResponse(plan, selected, payloads, metrics);
3826
+ }
3827
+ //#endregion
3828
+ //#region src/storage/domains/observability/trace-aggregate.ts
3829
+ /** Trace-root column expressions (alias `r`) for each groupable dimension; same values `where` sees. */
3830
+ const DIMENSION_SQL = {
3831
+ entityType: "r.entityType",
3832
+ entityName: "r.entityName",
3833
+ environment: "r.environment",
3834
+ status: TRACE_STATUS_SQL,
3835
+ serviceName: "r.serviceName",
3836
+ executionSource: "r.executionSource",
3837
+ threadId: "r.threadId",
3838
+ resourceId: "r.resourceId",
3839
+ userId: "r.userId",
3840
+ sessionId: "r.sessionId",
3841
+ organizationId: "r.organizationId",
3842
+ experimentId: "r.experimentId"
3843
+ };
3844
+ const PERCENTILES = {
3845
+ "duration.p50": .5,
3846
+ "duration.p90": .9,
3847
+ "duration.p95": .95,
3848
+ "duration.p99": .99
3849
+ };
3850
+ const COMPARISON_SQL = {
3851
+ eq: "=",
3852
+ ne: "<>",
3853
+ lt: "<",
3854
+ lte: "<=",
3855
+ gt: ">",
3856
+ gte: ">="
3857
+ };
3858
+ /**
3859
+ * Compiles a trusted aggregate plan. The plan's caps are enforced by the planner and trusted here.
3860
+ *
3861
+ * Every candidate root is first reduced to a `facts` row (dimension values, duration, error flag,
3862
+ * countDistinct inputs, bucket) so grouping, measures, and joins only reference plain columns.
3863
+ * Without `interval`, groups are filtered, ordered, and limited in one grouped select. With
3864
+ * `interval`, a `ranked` CTE ranks groups on whole-window measures, then only the top `limit`
3865
+ * groups are expanded into per-bucket rows.
3866
+ *
3867
+ * Parameters are positional, so `values` is appended in the order placeholders appear in the text.
3868
+ */
3869
+ function compileDuckDBTraceAggregate(plan) {
3870
+ const { ctes, values } = compileDuckDBTraceCandidates(plan, "r.*");
3871
+ const dimensionSql = (field) => {
3872
+ if (field.startsWith("metadata.")) return {
3873
+ sql: TRACE_METADATA_VALUE_SQL,
3874
+ values: traceMetadataPathValues(field.slice(9))
3875
+ };
3876
+ const sql = Object.hasOwn(DIMENSION_SQL, field) ? DIMENSION_SQL[field] : void 0;
3877
+ if (sql === void 0) throw new Error(`Unsupported trusted trace-aggregate field: ${field}`);
3878
+ return {
3879
+ sql,
3880
+ values: []
3881
+ };
3882
+ };
3883
+ const distinctSql = (field) => field === "traceId" ? {
3884
+ sql: "r.traceId",
3885
+ values: []
3886
+ } : dimensionSql(field);
3887
+ const dimensionColumns = plan.dimensions.map((_, index) => `d${index}`);
3888
+ const distinctColumns = /* @__PURE__ */ new Map();
3889
+ const factColumns = [];
3890
+ plan.dimensions.forEach((dimension, index) => {
3891
+ const compiled = dimensionSql(dimension);
3892
+ factColumns.push(`CAST(${compiled.sql} AS VARCHAR) AS d${index}`);
3893
+ values.push(...compiled.values);
3894
+ });
3895
+ for (const measure of plan.measures) {
3896
+ if (measure.type !== "countDistinct" || distinctColumns.has(measure.field)) continue;
3897
+ const column = `cd${distinctColumns.size}`;
3898
+ distinctColumns.set(measure.field, column);
3899
+ const compiled = distinctSql(measure.field);
3900
+ factColumns.push(`CAST(${compiled.sql} AS VARCHAR) AS ${column}`);
3901
+ values.push(...compiled.values);
3902
+ }
3903
+ factColumns.push(`CAST(${durationMsSql("r.startedAt", "r.endedAt")} AS DOUBLE) AS durationMs`, `r.error IS NOT NULL AS isError`);
3904
+ if (plan.interval !== void 0) {
3905
+ const intervalMs = _mastra_core_storage.TRACE_AGGREGATE_INTERVAL_MS[plan.interval];
3906
+ if (intervalMs === void 0) throw new Error(`Unsupported trusted trace-aggregate interval: ${plan.interval}`);
3907
+ factColumns.push(`epoch_ms((epoch_ms(r.startedAt) // ${intervalMs}) * ${intervalMs}) AS bucket`);
3908
+ }
3909
+ ctes.push(`facts AS (
3910
+ SELECT ${factColumns.join(",\n ")}
3911
+ FROM candidates r
3912
+ )`);
3913
+ const measureSql = (name, alias = "") => {
3914
+ const column = (name) => `${alias}${name}`;
3915
+ if (name === "count") return "CAST(count(*) AS DOUBLE)";
3916
+ if (name === "errorCount") return `CAST(count_if(${column("isError")}) AS DOUBLE)`;
3917
+ if (name === "errorRate") return `CAST(count_if(${column("isError")}) AS DOUBLE) / count(*)`;
3918
+ if (name === "duration.avg") return `avg(${column("durationMs")})`;
3919
+ if (name === "duration.min") return `min(${column("durationMs")})`;
3920
+ if (name === "duration.max") return `max(${column("durationMs")})`;
3921
+ const percentile = PERCENTILES[name];
3922
+ if (percentile !== void 0) return `quantile_cont(${column("durationMs")}, ${percentile})`;
3923
+ const measure = plan.measures.find((candidate) => candidate.type === "countDistinct" && candidate.name === name);
3924
+ const distinctColumn = measure && distinctColumns.get(measure.field);
3925
+ if (distinctColumn === void 0) throw new Error(`Unsupported trusted trace-aggregate measure: ${name}`);
3926
+ return `CAST(count(DISTINCT ${alias}${distinctColumn}) AS DOUBLE)`;
3927
+ };
3928
+ const havingValues = [];
3929
+ const compileHaving = (predicate) => {
3930
+ if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileHaving(arg)})`).join(predicate.operator === "and" ? " AND " : " OR ");
3931
+ if (predicate.type === "not") return `NOT (${compileHaving(predicate.arg)})`;
3932
+ const measure = measureSql(predicate.measure);
3933
+ if (predicate.type === "membership") {
3934
+ if (predicate.values.length === 0) return predicate.operator === "in" ? "FALSE" : "TRUE";
3935
+ havingValues.push(...predicate.values);
3936
+ const list = predicate.values.map(() => "CAST(? AS DOUBLE)").join(", ");
3937
+ return `${measure} ${predicate.operator === "in" ? "IN" : "NOT IN"} (${list})`;
3938
+ }
3939
+ const operator = COMPARISON_SQL[predicate.operator];
3940
+ if (operator === void 0) throw new Error(`Unsupported trusted trace-aggregate operator: ${predicate.operator}`);
3941
+ havingValues.push(predicate.value);
3942
+ return `${measure} ${operator} CAST(? AS DOUBLE)`;
3943
+ };
3944
+ const havingSql = ["count(*) > 0", ...plan.having ? [`(${compileHaving(plan.having)})`] : []].join(" AND ");
3945
+ const direction = plan.orderBy.direction === "asc" ? "ASC" : "DESC";
3946
+ let orderTarget;
3947
+ if (plan.orderBy.target === "measure") orderTarget = measureSql(plan.orderBy.measure);
3948
+ else {
3949
+ const index = plan.dimensions.indexOf(plan.orderBy.dimension);
3950
+ if (index === -1) throw new Error(`Unsupported trusted trace-aggregate order dimension: ${plan.orderBy.dimension}`);
3951
+ orderTarget = dimensionColumns[index];
3952
+ }
3953
+ const orderSql = [`${orderTarget} ${direction} NULLS LAST`, ...dimensionColumns.map((column) => `${column} ASC NULLS LAST`)].join(", ");
3954
+ const groupSql = dimensionColumns.length > 0 ? `\nGROUP BY ${dimensionColumns.join(", ")}` : "";
3955
+ if (plan.interval === void 0) {
3956
+ const projected = [...dimensionColumns, ...plan.measures.map((measure, index) => `${measureSql(measure.name)} AS m${index}`)];
3957
+ values.push(...havingValues, plan.limit + 1);
3958
+ return {
3959
+ sql: `WITH ${ctes.join(",\n ")}
3960
+ SELECT ${projected.join(", ")}
3961
+ FROM facts${groupSql}
3962
+ HAVING ${havingSql}
3963
+ ORDER BY ${orderSql}
3964
+ LIMIT ?`,
3965
+ values
3966
+ };
3967
+ }
3968
+ ctes.push(`ranked AS (
3969
+ SELECT ${[...dimensionColumns, `row_number() OVER (ORDER BY ${orderSql}) AS rank`].join(", ")}
3970
+ FROM facts${groupSql}
3971
+ HAVING ${havingSql}
3972
+ ORDER BY ${orderSql}
3973
+ LIMIT ?
3974
+ )`);
3975
+ values.push(...havingValues, plan.limit + 1);
3976
+ const joinSql = dimensionColumns.length > 0 ? dimensionColumns.map((column) => `f.${column} IS NOT DISTINCT FROM g.${column}`).join(" AND ") : "TRUE";
3977
+ const projected = [
3978
+ ...dimensionColumns.map((column) => `f.${column}`),
3979
+ "f.bucket",
3980
+ ...plan.measures.map((measure, index) => `${measureSql(measure.name, "f.")} AS m${index}`),
3981
+ `(SELECT count(*) FROM ranked) > ? AS truncated`
3982
+ ];
3983
+ values.push(plan.limit, plan.limit);
3984
+ return {
3985
+ sql: `WITH ${ctes.join(",\n ")}
3986
+ SELECT ${projected.join(", ")}
3987
+ FROM facts f
3988
+ JOIN ranked g ON ${joinSql}
3989
+ WHERE g.rank <= ?
3990
+ GROUP BY ${[
3991
+ "g.rank",
3992
+ ...dimensionColumns.map((column) => `f.${column}`),
3993
+ "f.bucket"
3994
+ ].join(", ")}
3995
+ ORDER BY g.rank ASC, f.bucket ASC`,
3996
+ values
3997
+ };
3998
+ }
3999
+ async function aggregateTraces(db, plan) {
4000
+ const compiled = compileDuckDBTraceAggregate(plan);
4001
+ let result;
4002
+ try {
4003
+ result = await db.query(compiled.sql, compiled.values);
4004
+ } catch (error) {
4005
+ if (isDuckDBResourceLimit(error)) throw new _mastra_core_storage.TraceQueryResourceLimitError();
4006
+ throw error;
4007
+ }
4008
+ let truncated;
4009
+ let resultRows;
4010
+ if (plan.interval === void 0) {
4011
+ truncated = result.length > plan.limit;
4012
+ resultRows = result.slice(0, plan.limit);
4013
+ } else {
4014
+ truncated = result[0]?.truncated === true;
4015
+ resultRows = result;
4016
+ }
4017
+ return {
4018
+ rows: resultRows.map((row) => {
4019
+ const shaped = { measures: Object.fromEntries(plan.measures.map((measure, index) => [measure.name, Number(row[`m${index}`])])) };
4020
+ if (plan.dimensions.length > 0) shaped.dimensions = Object.fromEntries(plan.dimensions.map((dimension, index) => {
4021
+ const value = row[`d${index}`];
4022
+ return [dimension, value == null ? null : String(value)];
4023
+ }));
4024
+ if (plan.interval !== void 0) shaped.bucket = asIsoTimestamp(row.bucket);
4025
+ return shaped;
4026
+ }),
4027
+ truncated
4028
+ };
4029
+ }
3645
4030
  //#endregion
3646
4031
  //#region src/storage/domains/observability/tracing.ts
3647
4032
  const COLUMNS_SQL = [
@@ -4970,6 +5355,8 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends _mastr
4970
5355
  "tag-discovery",
4971
5356
  "metric-discovery",
4972
5357
  "trace-query",
5358
+ "trace-aggregate",
5359
+ ...require_features.spanQueryFeatures,
4973
5360
  "trace-query-root-duration",
4974
5361
  "trace-query-discovery",
4975
5362
  "thread-query",
@@ -4988,6 +5375,8 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends _mastr
4988
5375
  "metric-discovery",
4989
5376
  "delta-polling",
4990
5377
  "trace-query",
5378
+ "trace-aggregate",
5379
+ ...require_features.spanQueryFeatures,
4991
5380
  "trace-query-root-duration",
4992
5381
  "trace-query-discovery",
4993
5382
  "thread-query",
@@ -5023,9 +5412,15 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends _mastr
5023
5412
  async listTraces(args) {
5024
5413
  return listTraces(this.db, args);
5025
5414
  }
5415
+ async querySpans(plan) {
5416
+ return querySpans(this.db, plan);
5417
+ }
5026
5418
  async queryTraces(plan) {
5027
5419
  return queryTraces(this.db, plan);
5028
5420
  }
5421
+ async aggregateTraces(plan) {
5422
+ return aggregateTraces(this.db, plan);
5423
+ }
5029
5424
  async getTraceQueryObservedFields(plan) {
5030
5425
  return getTraceQueryObservedFields(this.db, plan);
5031
5426
  }
@@ -5148,4 +5543,4 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends _mastr
5148
5543
  //#endregion
5149
5544
  exports.ObservabilityStorageDuckDB = ObservabilityStorageDuckDB;
5150
5545
 
5151
- //# sourceMappingURL=observability-DlmDRPW3.cjs.map
5546
+ //# sourceMappingURL=observability-DNuNWZFX.cjs.map