@mastra/duckdb 1.10.0 → 1.11.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.
@@ -3073,7 +3073,14 @@ function collectThreadRelatedCollections(predicate, collections) {
3073
3073
  else collectThreadRelatedCollections(predicate.arg, collections);
3074
3074
  return collections;
3075
3075
  }
3076
- function compileDuckDBTraceScope(relatedCollections) {
3076
+ function compileDuckDBTraceScope(relatedCollections, scope) {
3077
+ const tenantConditions = (alias) => scope ? [`${alias}.organizationId = ?`, ...scope.resourceId === void 0 ? [] : [`${alias}.resourceId = ?`]] : [];
3078
+ const tenantValues = scope ? scope.resourceId === void 0 ? [scope.organizationId] : [scope.organizationId, scope.resourceId] : [];
3079
+ const tenantWhere = (alias) => {
3080
+ const conditions = tenantConditions(alias);
3081
+ return conditions.length ? `\n WHERE ${conditions.join(" AND ")}` : "";
3082
+ };
3083
+ const values = [...tenantValues];
3077
3084
  const ctes = [
3078
3085
  `root_events AS (
3079
3086
  SELECT
@@ -3096,12 +3103,16 @@ function compileDuckDBTraceScope(relatedCollections) {
3096
3103
  `root_scope AS (
3097
3104
  SELECT *
3098
3105
  FROM current_roots r
3099
- WHERE r.endedAt IS NOT NULL
3100
- AND r.startedAt >= CAST(? AS TIMESTAMP)
3101
- AND r.startedAt < CAST(? AS TIMESTAMP)
3106
+ WHERE ${[
3107
+ "r.endedAt IS NOT NULL",
3108
+ "r.startedAt >= CAST(? AS TIMESTAMP)",
3109
+ "r.startedAt < CAST(? AS TIMESTAMP)",
3110
+ ...tenantConditions("r")
3111
+ ].join("\n AND ")}
3102
3112
  )`
3103
3113
  ];
3104
- if (relatedCollections.has("spans")) ctes.push(`current_span_rows AS (
3114
+ if (relatedCollections.has("spans")) {
3115
+ ctes.push(`current_span_rows AS (
3105
3116
  SELECT
3106
3117
  e.*,
3107
3118
  coalesce(
@@ -3113,7 +3124,7 @@ function compileDuckDBTraceScope(relatedCollections) {
3113
3124
  ORDER BY CASE WHEN e.endedAt IS NULL THEN 1 ELSE 0 END ASC, e.cursorId DESC
3114
3125
  ) AS currentRank
3115
3126
  FROM span_events e
3116
- INNER JOIN root_scope roots ON roots.traceId = e.traceId
3127
+ INNER JOIN root_scope roots ON roots.traceId = e.traceId${tenantWhere("e")}
3117
3128
  ),
3118
3129
  current_spans AS (
3119
3130
  SELECT
@@ -3140,20 +3151,36 @@ function compileDuckDBTraceScope(relatedCollections) {
3140
3151
  FROM current_span_rows
3141
3152
  WHERE currentRank = 1
3142
3153
  )`);
3143
- if (relatedCollections.has("scores")) ctes.push(`current_scores AS (
3154
+ values.push(...tenantValues);
3155
+ }
3156
+ if (relatedCollections.has("scores")) {
3157
+ ctes.push(`current_scores AS (
3144
3158
  SELECT s.*
3145
3159
  FROM score_events s
3146
- INNER JOIN root_scope roots ON roots.traceId = s.traceId
3160
+ INNER JOIN root_scope roots ON roots.traceId = s.traceId${tenantWhere("s")}
3147
3161
  )`);
3148
- if (relatedCollections.has("feedback")) ctes.push(`current_feedback AS (
3162
+ values.push(...tenantValues);
3163
+ }
3164
+ if (relatedCollections.has("feedback")) {
3165
+ ctes.push(`current_feedback AS (
3149
3166
  SELECT f.*
3150
3167
  FROM feedback_events f
3151
- INNER JOIN root_scope roots ON roots.traceId = f.traceId
3168
+ INNER JOIN root_scope roots ON roots.traceId = f.traceId${tenantWhere("f")}
3152
3169
  )`);
3153
- return ctes;
3170
+ values.push(...tenantValues);
3171
+ }
3172
+ return {
3173
+ ctes,
3174
+ values
3175
+ };
3154
3176
  }
3155
3177
  function compileDuckDBTraceQuery(plan) {
3156
- const values = [plan.timeRange.from, plan.timeRange.to];
3178
+ const { ctes, values: scopeValues } = compileDuckDBTraceScope(collectRelatedCollections(plan.where), plan.scope);
3179
+ const values = [
3180
+ plan.timeRange.from,
3181
+ plan.timeRange.to,
3182
+ ...scopeValues
3183
+ ];
3157
3184
  const conditions = [
3158
3185
  `r.endedAt IS NOT NULL`,
3159
3186
  `r.startedAt >= CAST(? AS TIMESTAMP)`,
@@ -3164,7 +3191,6 @@ function compileDuckDBTraceQuery(plan) {
3164
3191
  conditions.push(`(${predicate.sql})`);
3165
3192
  values.push(...predicate.values);
3166
3193
  }
3167
- const ctes = compileDuckDBTraceScope(collectRelatedCollections(plan.where));
3168
3194
  ctes.push(`candidates AS (
3169
3195
  SELECT ${TRACE_SELECT}${plan.paginationMode === "delta" ? ", r.cursorId AS deltaWatermark" : ""}
3170
3196
  FROM root_scope r
@@ -3246,10 +3272,14 @@ LIMIT ?`,
3246
3272
  };
3247
3273
  }
3248
3274
  function compileDuckDBThreadQuery(plan) {
3249
- const values = [plan.traces.timeRange.from, plan.traces.timeRange.to];
3250
3275
  const relatedCollections = collectRelatedCollections(plan.traces.where);
3251
3276
  collectThreadRelatedCollections(plan.where, relatedCollections);
3252
- const ctes = compileDuckDBTraceScope(relatedCollections);
3277
+ const { ctes, values: scopeValues } = compileDuckDBTraceScope(relatedCollections, plan.scope);
3278
+ const values = [
3279
+ plan.traces.timeRange.from,
3280
+ plan.traces.timeRange.to,
3281
+ ...scopeValues
3282
+ ];
3253
3283
  let eligibilitySql = "TRUE";
3254
3284
  if (plan.traces.where) {
3255
3285
  const eligibility = compilePredicate(plan.traces.where);
@@ -3307,12 +3337,17 @@ function discoveryCollections(scope) {
3307
3337
  return scope === "trace" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([scope]);
3308
3338
  }
3309
3339
  function compileDuckDBTraceQueryObservedFields(plan) {
3310
- const values = [plan.timeRange.from, plan.timeRange.to];
3340
+ const { ctes, values: scopeValues } = compileDuckDBTraceScope(/* @__PURE__ */ new Set(), plan.scope);
3341
+ const values = [
3342
+ plan.timeRange.from,
3343
+ plan.timeRange.to,
3344
+ ...scopeValues
3345
+ ];
3311
3346
  if (plan.search) values.push(plan.search);
3312
3347
  values.push(plan.limit + 1);
3313
3348
  const search = plan.search ? `AND strpos(lower('metadata.' || entry.key), lower(?)) > 0` : "";
3314
3349
  return {
3315
- sql: `WITH ${compileDuckDBTraceScope(/* @__PURE__ */ new Set()).join(",\n ")}
3350
+ sql: `WITH ${ctes.join(",\n ")}
3316
3351
  SELECT 'metadata.' || entry.key AS path, count(*) AS occurrences
3317
3352
  FROM root_scope r, LATERAL json_each(r.metadata) entry
3318
3353
  WHERE entry.type = 'VARCHAR'
@@ -3329,8 +3364,12 @@ LIMIT ?`,
3329
3364
  };
3330
3365
  }
3331
3366
  function compileDuckDBTraceQueryValues(plan) {
3332
- const values = [plan.timeRange.from, plan.timeRange.to];
3333
- const ctes = compileDuckDBTraceScope(discoveryCollections(plan.predicateScope));
3367
+ const { ctes, values: scopeValues } = compileDuckDBTraceScope(discoveryCollections(plan.predicateScope), plan.scope);
3368
+ const values = [
3369
+ plan.timeRange.from,
3370
+ plan.timeRange.to,
3371
+ ...scopeValues
3372
+ ];
3334
3373
  let fieldSql;
3335
3374
  if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
3336
3375
  const jsonPath = `$.${JSON.stringify(plan.path.slice(9))}`;
@@ -4782,7 +4821,8 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends Observ
4782
4821
  "logs",
4783
4822
  "trace-query",
4784
4823
  "trace-query-discovery",
4785
- "thread-query"
4824
+ "thread-query",
4825
+ "trace-query-tenant-scope"
4786
4826
  ];
4787
4827
  return [
4788
4828
  "metrics",
@@ -4790,7 +4830,8 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends Observ
4790
4830
  "delta-polling",
4791
4831
  "trace-query",
4792
4832
  "trace-query-discovery",
4793
- "thread-query"
4833
+ "thread-query",
4834
+ "trace-query-tenant-scope"
4794
4835
  ];
4795
4836
  }
4796
4837
  async createSpan(args) {
@@ -4945,4 +4986,4 @@ var ObservabilityStorageDuckDB = class ObservabilityStorageDuckDB extends Observ
4945
4986
  //#endregion
4946
4987
  export { ObservabilityStorageDuckDB };
4947
4988
 
4948
- //# sourceMappingURL=observability-D3311DPa.js.map
4989
+ //# sourceMappingURL=observability-DyP55NQC.js.map