drizzle-cube 0.3.32 → 0.4.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.
Files changed (40) hide show
  1. package/dist/adapters/express/index.cjs +1 -1
  2. package/dist/adapters/express/index.js +1 -1
  3. package/dist/adapters/fastify/index.cjs +1 -1
  4. package/dist/adapters/fastify/index.js +1 -1
  5. package/dist/adapters/hono/index.cjs +1 -1
  6. package/dist/adapters/hono/index.js +1 -1
  7. package/dist/adapters/{mcp-transport-B2rGcu1X.js → mcp-transport-Bbz3qrIy.js} +4324 -3230
  8. package/dist/adapters/mcp-transport-CXF4E5QJ.cjs +257 -0
  9. package/dist/adapters/nextjs/index.cjs +1 -1
  10. package/dist/adapters/nextjs/index.js +1 -1
  11. package/dist/adapters/utils.cjs +8 -8
  12. package/dist/adapters/utils.d.ts +2 -137
  13. package/dist/adapters/utils.js +1347 -1356
  14. package/dist/client/chunks/{DashboardEditModal-rLcmZpe_.js → DashboardEditModal-Bv7e3Q7O.js} +2 -2
  15. package/dist/client/chunks/{DashboardEditModal-rLcmZpe_.js.map → DashboardEditModal-Bv7e3Q7O.js.map} +1 -1
  16. package/dist/client/chunks/{analysis-builder-DCt5C58c.js → analysis-builder-BfH-w92z.js} +2962 -3019
  17. package/dist/client/chunks/analysis-builder-BfH-w92z.js.map +1 -0
  18. package/dist/client/chunks/{analysis-builder-shared-ysrRYGiU.js → analysis-builder-shared-DsbdRCzz.js} +361 -361
  19. package/dist/client/chunks/{analysis-builder-shared-ysrRYGiU.js.map → analysis-builder-shared-DsbdRCzz.js.map} +1 -1
  20. package/dist/client/chunks/useDirtyStateTracking-CTS_m9mg.js.map +1 -1
  21. package/dist/client/components/AnalysisBuilder/AnalysisResultsPanel.d.ts +2 -1
  22. package/dist/client/components/AnalysisBuilder/types.d.ts +18 -6
  23. package/dist/client/components.js +1 -1
  24. package/dist/client/hooks/queries/useDryRunQuery.d.ts +14 -48
  25. package/dist/client/hooks/useAnalysisBuilderHook.d.ts +6 -33
  26. package/dist/client/hooks/useAnalysisQueryExecution.d.ts +7 -7
  27. package/dist/client/hooks.js +194 -22
  28. package/dist/client/hooks.js.map +1 -1
  29. package/dist/client/index.js +3 -3
  30. package/dist/client/shared/types.d.ts +27 -0
  31. package/dist/client/styles.css +1 -1
  32. package/dist/client-bundle-stats.html +1 -1
  33. package/dist/server/index.cjs +49 -49
  34. package/dist/server/index.d.ts +1727 -547
  35. package/dist/server/index.js +4836 -3715
  36. package/package.json +1 -1
  37. package/dist/adapters/mcp-transport-DeD7YevT.cjs +0 -257
  38. package/dist/client/chunks/analysis-builder-DCt5C58c.js.map +0 -1
  39. package/dist/client/chunks/hooks-CdyIO1-j.js +0 -236
  40. package/dist/client/chunks/hooks-CdyIO1-j.js.map +0 -1
@@ -1,257 +0,0 @@
1
- "use strict";const Q=require("./utils.cjs"),k=Symbol.for("drizzle:entityKind");function L(d,e){if(!d||typeof d!="object")return!1;if(d instanceof e)return!0;if(!Object.prototype.hasOwnProperty.call(e,k))throw new Error(`Class "${e.name??"<unknown>"}" doesn't look like a Drizzle entity. If this is incorrect and the class is provided by Drizzle, please report this as a bug.`);let t=Object.getPrototypeOf(d).constructor;if(t)for(;t;){if(k in t&&t[k]===e[k])return!0;t=Object.getPrototypeOf(t)}return!1}class se{constructor(e,t){this.table=e,this.config=t,this.name=t.name,this.keyAsName=t.keyAsName,this.notNull=t.notNull,this.default=t.default,this.defaultFn=t.defaultFn,this.onUpdateFn=t.onUpdateFn,this.hasDefault=t.hasDefault,this.primary=t.primaryKey,this.isUnique=t.isUnique,this.uniqueName=t.uniqueName,this.uniqueType=t.uniqueType,this.dataType=t.dataType,this.columnType=t.columnType,this.generated=t.generated,this.generatedIdentity=t.generatedIdentity}static[k]="Column";name;keyAsName;primary;notNull;default;defaultFn;onUpdateFn;hasDefault;isUnique;uniqueName;uniqueType;dataType;columnType;enumValues=void 0;generated=void 0;generatedIdentity=void 0;config;mapFromDriverValue(e){return e}mapToDriverValue(e){return e}shouldDisableInsert(){return this.config.generated!==void 0&&this.config.generated.type!=="byDefault"}}const be=Symbol.for("drizzle:Name"),Me=Symbol.for("drizzle:isPgEnum");function bt(d){return!!d&&typeof d=="function"&&Me in d&&d[Me]===!0}class et{static[k]="Subquery";constructor(e,t,n,s=!1,i=[]){this._={brand:"Subquery",sql:e,selectedFields:t,alias:n,isWith:s,usedTables:i}}}const yt={startActiveSpan(d,e){return e()}},ne=Symbol.for("drizzle:ViewBaseConfig"),ye=Symbol.for("drizzle:Schema"),je=Symbol.for("drizzle:Columns"),Ue=Symbol.for("drizzle:ExtraConfigColumns"),Ce=Symbol.for("drizzle:OriginalName"),we=Symbol.for("drizzle:BaseName"),le=Symbol.for("drizzle:IsAlias"),xe=Symbol.for("drizzle:ExtraConfigBuilder"),Ct=Symbol.for("drizzle:IsDrizzleTable");class P{static[k]="Table";static Symbol={Name:be,Schema:ye,OriginalName:Ce,Columns:je,ExtraConfigColumns:Ue,BaseName:we,IsAlias:le,ExtraConfigBuilder:xe};[be];[Ce];[ye];[je];[Ue];[we];[le]=!1;[Ct]=!0;[xe]=void 0;constructor(e,t,n){this[be]=this[Ce]=e,this[ye]=t,this[we]=n}}function tt(d){return d!=null&&typeof d.getSQL=="function"}function wt(d){const e={sql:"",params:[]};for(const t of d)e.sql+=t.sql,e.params.push(...t.params),t.typings?.length&&(e.typings||(e.typings=[]),e.typings.push(...t.typings));return e}class M{static[k]="StringChunk";value;constructor(e){this.value=Array.isArray(e)?e:[e]}getSQL(){return new O([this])}}class O{constructor(e){this.queryChunks=e;for(const t of e)if(L(t,P)){const n=t[P.Symbol.Schema];this.usedTables.push(n===void 0?t[P.Symbol.Name]:n+"."+t[P.Symbol.Name])}}static[k]="SQL";decoder=nt;shouldInlineParams=!1;usedTables=[];append(e){return this.queryChunks.push(...e.queryChunks),this}toQuery(e){return yt.startActiveSpan("drizzle.buildSQL",t=>{const n=this.buildQueryFromSourceParams(this.queryChunks,e);return t?.setAttributes({"drizzle.query.text":n.sql,"drizzle.query.params":JSON.stringify(n.params)}),n})}buildQueryFromSourceParams(e,t){const n=Object.assign({},t,{inlineParams:t.inlineParams||this.shouldInlineParams,paramStartIndex:t.paramStartIndex||{value:0}}),{casing:s,escapeName:i,escapeParam:r,prepareTyping:a,inlineParams:u,paramStartIndex:c}=n;return wt(e.map(l=>{if(L(l,M))return{sql:l.value.join(""),params:[]};if(L(l,De))return{sql:i(l.value),params:[]};if(l===void 0)return{sql:"",params:[]};if(Array.isArray(l)){const m=[new M("(")];for(const[p,f]of l.entries())m.push(f),p<l.length-1&&m.push(new M(", "));return m.push(new M(")")),this.buildQueryFromSourceParams(m,n)}if(L(l,O))return this.buildQueryFromSourceParams(l.queryChunks,{...n,inlineParams:u||l.shouldInlineParams});if(L(l,P)){const m=l[P.Symbol.Schema],p=l[P.Symbol.Name];return{sql:m===void 0||l[le]?i(p):i(m)+"."+i(p),params:[]}}if(L(l,se)){const m=s.getColumnCasing(l);if(t.invokeSource==="indexes")return{sql:i(m),params:[]};const p=l.table[P.Symbol.Schema];return{sql:l.table[le]||p===void 0?i(l.table[P.Symbol.Name])+"."+i(m):i(p)+"."+i(l.table[P.Symbol.Name])+"."+i(m),params:[]}}if(L(l,it)){const m=l[ne].schema,p=l[ne].name;return{sql:m===void 0||l[ne].isAlias?i(p):i(m)+"."+i(p),params:[]}}if(L(l,ce)){if(L(l.value,de))return{sql:r(c.value++,l),params:[l],typings:["none"]};const m=l.value===null?null:l.encoder.mapToDriverValue(l.value);if(L(m,O))return this.buildQueryFromSourceParams([m],n);if(u)return{sql:this.mapInlineParam(m,n),params:[]};let p=["none"];return a&&(p=[a(l.encoder)]),{sql:r(c.value++,m),params:[m],typings:p}}return L(l,de)?{sql:r(c.value++,l),params:[l],typings:["none"]}:L(l,O.Aliased)&&l.fieldAlias!==void 0?{sql:i(l.fieldAlias),params:[]}:L(l,et)?l._.isWith?{sql:i(l._.alias),params:[]}:this.buildQueryFromSourceParams([new M("("),l._.sql,new M(") "),new De(l._.alias)],n):bt(l)?l.schema?{sql:i(l.schema)+"."+i(l.enumName),params:[]}:{sql:i(l.enumName),params:[]}:tt(l)?l.shouldOmitSQLParens?.()?this.buildQueryFromSourceParams([l.getSQL()],n):this.buildQueryFromSourceParams([new M("("),l.getSQL(),new M(")")],n):u?{sql:this.mapInlineParam(l,n),params:[]}:{sql:r(c.value++,l),params:[l],typings:["none"]}}))}mapInlineParam(e,{escapeString:t}){if(e===null)return"null";if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="string")return t(e);if(typeof e=="object"){const n=e.toString();return t(n==="[object Object]"?JSON.stringify(e):n)}throw new Error("Unexpected param value: "+e)}getSQL(){return this}as(e){return e===void 0?this:new O.Aliased(this,e)}mapWith(e){return this.decoder=typeof e=="function"?{mapFromDriverValue:e}:e,this}inlineParams(){return this.shouldInlineParams=!0,this}if(e){return e?this:void 0}}class De{constructor(e){this.value=e}static[k]="Name";brand;getSQL(){return new O([this])}}function $t(d){return typeof d=="object"&&d!==null&&"mapToDriverValue"in d&&typeof d.mapToDriverValue=="function"}const nt={mapFromDriverValue:d=>d},st={mapToDriverValue:d=>d};({...nt,...st});class ce{constructor(e,t=st){this.value=e,this.encoder=t}static[k]="Param";brand;getSQL(){return new O([this])}}function o(d,...e){const t=[];(e.length>0||d.length>0&&d[0]!=="")&&t.push(new M(d[0]));for(const[n,s]of e.entries())t.push(s,new M(d[n+1]));return new O(t)}(d=>{function e(){return new O([])}d.empty=e;function t(u){return new O(u)}d.fromList=t;function n(u){return new O([new M(u)])}d.raw=n;function s(u,c){const l=[];for(const[m,p]of u.entries())m>0&&c!==void 0&&l.push(c),l.push(p);return new O(l)}d.join=s;function i(u){return new De(u)}d.identifier=i;function r(u){return new de(u)}d.placeholder=r;function a(u,c){return new ce(u,c)}d.param=a})(o||(o={}));(d=>{class e{constructor(n,s){this.sql=n,this.fieldAlias=s}static[k]="SQL.Aliased";isSelectionField=!1;getSQL(){return this.sql}clone(){return new e(this.sql,this.fieldAlias)}}d.Aliased=e})(O||(O={}));class de{constructor(e){this.name=e}static[k]="Placeholder";getSQL(){return new O([this])}}const Et=Symbol.for("drizzle:IsDrizzleView");class it{static[k]="View";[ne];[Et]=!0;constructor({name:e,schema:t,selectedFields:n,query:s}){this[ne]={name:e,originalName:e,schema:t,selectedFields:n,query:s,isExisting:!s,isAlias:!1}}getSQL(){return new O([this])}}se.prototype.getSQL=function(){return new O([this])};P.prototype.getSQL=function(){return new O([this])};et.prototype.getSQL=function(){return new O([this])};function x(d,e){return $t(e)&&!tt(d)&&!L(d,ce)&&!L(d,de)&&!L(d,se)&&!L(d,P)&&!L(d,it)?new ce(d,e):d}const W=(d,e)=>o`${d} = ${x(e,d)}`,ke=(d,e)=>o`${d} <> ${x(e,d)}`;function A(...d){const e=d.filter(t=>t!==void 0);if(e.length!==0)return e.length===1?new O(e):new O([new M("("),o.join(e,new M(" and ")),new M(")")])}function re(...d){const e=d.filter(t=>t!==void 0);if(e.length!==0)return e.length===1?new O(e):new O([new M("("),o.join(e,new M(" or ")),new M(")")])}const $e=(d,e)=>o`${d} > ${x(e,d)}`,ee=(d,e)=>o`${d} >= ${x(e,d)}`,Ee=(d,e)=>o`${d} < ${x(e,d)}`,te=(d,e)=>o`${d} <= ${x(e,d)}`;function Te(d,e){return Array.isArray(e)?e.length===0?o`false`:o`${d} in ${e.map(t=>x(t,d))}`:o`${d} in ${x(e,d)}`}function Be(d,e){return Array.isArray(e)?e.length===0?o`true`:o`${d} not in ${e.map(t=>x(t,d))}`:o`${d} not in ${x(e,d)}`}function Pe(d){return o`${d} is null`}function Qe(d){return o`${d} is not null`}function Tt(d,e){if(Array.isArray(e)){if(e.length===0)throw new Error("arrayContains requires at least one value");const t=o`${x(e,d)}`;return o`${d} @> ${t}`}return o`${d} @> ${x(e,d)}`}function St(d,e){if(Array.isArray(e)){if(e.length===0)throw new Error("arrayContained requires at least one value");const t=o`${x(e,d)}`;return o`${d} <@ ${t}`}return o`${d} <@ ${x(e,d)}`}function Nt(d,e){if(Array.isArray(e)){if(e.length===0)throw new Error("arrayOverlaps requires at least one value");const t=o`${x(e,d)}`;return o`${d} && ${t}`}return o`${d} && ${x(e,d)}`}function We(d){return o`${d} asc`}function Dt(d){return o`${d} desc`}function Ae(d){return o`count(${d||o.raw("*")})`.mapWith(Number)}function At(d){return o`count(distinct ${d})`.mapWith(Number)}function V(d){return o`sum(${d})`.mapWith(String)}function H(d){return o`max(${d})`.mapWith(L(d,se)?d:String)}function oe(d){return o`min(${d})`.mapWith(L(d,se)?d:String)}class pe{preprocessCalculatedTemplate(e){return e}buildPattern(e,t){switch(e){case"contains":case"notContains":return`%${t}%`;case"startsWith":return`${t}%`;case"endsWith":return`%${t}`;default:return t}}parseISODuration(e){const t={years:0,months:0,days:0,hours:0,minutes:0,seconds:0},n=/^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/,s=e.match(n);if(!s)throw new Error(`Invalid ISO 8601 duration format: ${e}`);return t.years=parseInt(s[1]||"0",10),t.months=parseInt(s[2]||"0",10),t.days=parseInt(s[3]||"0",10),t.hours=parseInt(s[4]||"0",10),t.minutes=parseInt(s[5]||"0",10),t.seconds=parseFloat(s[6]||"0"),t}durationToSeconds(e){const t=this.parseISODuration(e);return t.years*365*24*60*60+t.months*30*24*60*60+t.days*24*60*60+t.hours*60*60+t.minutes*60+t.seconds}}class _t extends pe{getEngineType(){return"postgres"}supportsLateralJoins(){return!0}buildIntervalFromISO(e){const t=this.parseISODuration(e),n=[];t.years&&n.push(`${t.years} years`),t.months&&n.push(`${t.months} months`),t.days&&n.push(`${t.days} days`),t.hours&&n.push(`${t.hours} hours`),t.minutes&&n.push(`${t.minutes} minutes`),t.seconds&&n.push(`${t.seconds} seconds`);const s=n.join(" ")||"0 seconds";return o`INTERVAL '${o.raw(s)}'`}buildTimeDifferenceSeconds(e,t){return o`EXTRACT(EPOCH FROM (${e} - ${t}))`}buildDateAddInterval(e,t){const n=this.buildIntervalFromISO(t);return o`(${e} + ${n})`}buildConditionalAggregation(e,t,n){const s=e.toUpperCase();return e==="count"&&!t?o`COUNT(*) FILTER (WHERE ${n})`:o`${o.raw(s)}(${t}) FILTER (WHERE ${n})`}buildDateDiffPeriods(e,t,n){switch(n){case"day":return o`(${t}::date - ${e}::date)`;case"week":return o`FLOOR((${t}::date - ${e}::date) / 7)`;case"month":return o`(EXTRACT(YEAR FROM AGE(${t}::timestamp, ${e}::timestamp)) * 12 + EXTRACT(MONTH FROM AGE(${t}::timestamp, ${e}::timestamp)))::integer`;default:throw new Error(`Unsupported date diff unit: ${n}`)}}buildPeriodSeriesSubquery(e){return o`(SELECT generate_series(0, ${e}) as period_number) p`}buildTimeDimension(e,t){switch(e){case"year":return o`DATE_TRUNC('year', ${t}::timestamp)`;case"quarter":return o`DATE_TRUNC('quarter', ${t}::timestamp)`;case"month":return o`DATE_TRUNC('month', ${t}::timestamp)`;case"week":return o`DATE_TRUNC('week', ${t}::timestamp)`;case"day":return o`DATE_TRUNC('day', ${t}::timestamp)::timestamp`;case"hour":return o`DATE_TRUNC('hour', ${t}::timestamp)`;case"minute":return o`DATE_TRUNC('minute', ${t}::timestamp)`;case"second":return o`DATE_TRUNC('second', ${t}::timestamp)`;default:return t}}buildStringCondition(e,t,n){switch(t){case"contains":return o`${e} ILIKE ${`%${n}%`}`;case"notContains":return o`${e} NOT ILIKE ${`%${n}%`}`;case"startsWith":return o`${e} ILIKE ${`${n}%`}`;case"endsWith":return o`${e} ILIKE ${`%${n}`}`;case"like":return o`${e} LIKE ${n}`;case"notLike":return o`${e} NOT LIKE ${n}`;case"ilike":return o`${e} ILIKE ${n}`;case"regex":return o`${e} ~* ${n}`;case"notRegex":return o`${e} !~* ${n}`;default:throw new Error(`Unsupported string operator: ${t}`)}}castToType(e,t){switch(t){case"timestamp":return o`${e}::timestamp`;case"decimal":return o`${e}::decimal`;case"integer":return o`${e}::integer`;default:throw new Error(`Unsupported cast type: ${t}`)}}buildAvg(e){return o`COALESCE(AVG(${e}), 0)`}buildCaseWhen(e,t){const n=e.map(s=>o`WHEN ${s.when} THEN ${s.then}`).reduce((s,i)=>o`${s} ${i}`);return t!==void 0?o`CASE ${n} ELSE ${t} END`:o`CASE ${n} END`}buildBooleanLiteral(e){return e?o`TRUE`:o`FALSE`}convertFilterValue(e){return e}prepareDateValue(e){return e}isTimestampInteger(){return!1}convertTimeDimensionResult(e){return e}getCapabilities(){return{supportsStddev:!0,supportsVariance:!0,supportsPercentile:!0,supportsWindowFunctions:!0,supportsFrameClause:!0,supportsLateralJoins:!0,supportsPercentileSubqueries:!0}}buildStddev(e,t=!1){const n=t?"STDDEV_SAMP":"STDDEV_POP";return o`COALESCE(${o.raw(n)}(${e}), 0)`}buildVariance(e,t=!1){const n=t?"VAR_SAMP":"VAR_POP";return o`COALESCE(${o.raw(n)}(${e}), 0)`}buildPercentile(e,t){const n=t/100;return o`PERCENTILE_CONT(${n}) WITHIN GROUP (ORDER BY ${e})`}buildWindowFunction(e,t,n,s,i){const r=n&&n.length>0?o`PARTITION BY ${o.join(n,o`, `)}`:o``,a=s&&s.length>0?o`ORDER BY ${o.join(s.map(p=>p.direction==="desc"?o`${p.field} DESC`:o`${p.field} ASC`),o`, `)}`:o``;let u=o``;if(i?.frame){const{type:p,start:f,end:h}=i.frame,g=p.toUpperCase(),y=f==="unbounded"?"UNBOUNDED PRECEDING":typeof f=="number"?`${f} PRECEDING`:"CURRENT ROW",b=h==="unbounded"?"UNBOUNDED FOLLOWING":h==="current"?"CURRENT ROW":typeof h=="number"?`${h} FOLLOWING`:"CURRENT ROW";u=o`${o.raw(g)} BETWEEN ${o.raw(y)} AND ${o.raw(b)}`}const c=[];n&&n.length>0&&c.push(r),s&&s.length>0&&c.push(a),i?.frame&&c.push(u);const l=c.length>0?o.join(c,o` `):o``,m=o`OVER (${l})`;switch(e){case"lag":return o`LAG(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"lead":return o`LEAD(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"rank":return o`RANK() ${m}`;case"denseRank":return o`DENSE_RANK() ${m}`;case"rowNumber":return o`ROW_NUMBER() ${m}`;case"ntile":return o`NTILE(${i?.nTile??4}) ${m}`;case"firstValue":return o`FIRST_VALUE(${t}) ${m}`;case"lastValue":return o`LAST_VALUE(${t}) ${m}`;case"movingAvg":return o`AVG(${t}) ${m}`;case"movingSum":return o`SUM(${t}) ${m}`;default:throw new Error(`Unsupported window function: ${e}`)}}}class rt extends pe{getEngineType(){return"mysql"}supportsLateralJoins(){return!0}buildIntervalFromISO(e){const t=this.parseISODuration(e),n=[];t.years&&n.push(`${t.years} YEAR`),t.months&&n.push(`${t.months} MONTH`),t.days&&n.push(`${t.days} DAY`),t.hours&&n.push(`${t.hours} HOUR`),t.minutes&&n.push(`${t.minutes} MINUTE`),t.seconds&&n.push(`${t.seconds} SECOND`);const s=this.durationToSeconds(e);return o`${s}`}buildTimeDifferenceSeconds(e,t){return o`TIMESTAMPDIFF(SECOND, ${t}, ${e})`}buildDateAddInterval(e,t){const n=this.parseISODuration(t);let s=e;return n.years&&(s=o`DATE_ADD(${s}, INTERVAL ${n.years} YEAR)`),n.months&&(s=o`DATE_ADD(${s}, INTERVAL ${n.months} MONTH)`),n.days&&(s=o`DATE_ADD(${s}, INTERVAL ${n.days} DAY)`),n.hours&&(s=o`DATE_ADD(${s}, INTERVAL ${n.hours} HOUR)`),n.minutes&&(s=o`DATE_ADD(${s}, INTERVAL ${n.minutes} MINUTE)`),n.seconds&&(s=o`DATE_ADD(${s}, INTERVAL ${n.seconds} SECOND)`),s}buildConditionalAggregation(e,t,n){const s=e.toUpperCase();return e==="count"&&!t?o`COUNT(CASE WHEN ${n} THEN 1 END)`:o`${o.raw(s)}(CASE WHEN ${n} THEN ${t} END)`}buildDateDiffPeriods(e,t,n){const s=n.toUpperCase();return o`TIMESTAMPDIFF(${o.raw(s)}, ${e}, ${t})`}buildPeriodSeriesSubquery(e){return o`(
2
- WITH RECURSIVE periods(period_number) AS (
3
- SELECT 0
4
- UNION ALL
5
- SELECT period_number + 1 FROM periods WHERE period_number < ${e}
6
- )
7
- SELECT period_number FROM periods
8
- ) p`}buildTimeDimension(e,t){const n={year:"%Y-01-01 00:00:00",quarter:"%Y-%q-01 00:00:00",month:"%Y-%m-01 00:00:00",week:"%Y-%u-01 00:00:00",day:"%Y-%m-%d 00:00:00",hour:"%Y-%m-%d %H:00:00",minute:"%Y-%m-%d %H:%i:00",second:"%Y-%m-%d %H:%i:%s"};switch(e){case"quarter":return o`DATE_ADD(MAKEDATE(YEAR(${t}), 1), INTERVAL (QUARTER(${t}) - 1) * 3 MONTH)`;case"week":return o`DATE_SUB(${t}, INTERVAL WEEKDAY(${t}) DAY)`;default:{const s=n[e];return s?o`STR_TO_DATE(DATE_FORMAT(${t}, ${s}), '%Y-%m-%d %H:%i:%s')`:t}}}buildStringCondition(e,t,n){switch(t){case"contains":return o`LOWER(${e}) LIKE ${`%${n.toLowerCase()}%`}`;case"notContains":return o`LOWER(${e}) NOT LIKE ${`%${n.toLowerCase()}%`}`;case"startsWith":return o`LOWER(${e}) LIKE ${`${n.toLowerCase()}%`}`;case"endsWith":return o`LOWER(${e}) LIKE ${`%${n.toLowerCase()}`}`;case"like":return o`${e} LIKE ${n}`;case"notLike":return o`${e} NOT LIKE ${n}`;case"ilike":return o`LOWER(${e}) LIKE ${n.toLowerCase()}`;case"regex":return o`${e} REGEXP ${n}`;case"notRegex":return o`${e} NOT REGEXP ${n}`;default:throw new Error(`Unsupported string operator: ${t}`)}}castToType(e,t){switch(t){case"timestamp":return o`CAST(${e} AS DATETIME)`;case"decimal":return o`CAST(${e} AS DECIMAL(10,2))`;case"integer":return o`CAST(${e} AS SIGNED INTEGER)`;default:throw new Error(`Unsupported cast type: ${t}`)}}buildAvg(e){return o`IFNULL(AVG(${e}), 0)`}buildCaseWhen(e,t){const n=e.map(s=>o`WHEN ${s.when} THEN ${s.then}`).reduce((s,i)=>o`${s} ${i}`);return t!==void 0?o`CASE ${n} ELSE ${t} END`:o`CASE ${n} END`}buildBooleanLiteral(e){return e?o`TRUE`:o`FALSE`}convertFilterValue(e){return e}prepareDateValue(e){return e}isTimestampInteger(){return!1}convertTimeDimensionResult(e){return e}getCapabilities(){return{supportsStddev:!0,supportsVariance:!0,supportsPercentile:!1,supportsWindowFunctions:!0,supportsFrameClause:!0,supportsLateralJoins:!0,supportsPercentileSubqueries:!1}}buildStddev(e,t=!1){const n=t?"STDDEV_SAMP":"STDDEV_POP";return o`IFNULL(${o.raw(n)}(${e}), 0)`}buildVariance(e,t=!1){const n=t?"VAR_SAMP":"VAR_POP";return o`IFNULL(${o.raw(n)}(${e}), 0)`}buildPercentile(e,t){return null}buildWindowFunction(e,t,n,s,i){const r=n&&n.length>0?o`PARTITION BY ${o.join(n,o`, `)}`:o``,a=s&&s.length>0?o`ORDER BY ${o.join(s.map(p=>p.direction==="desc"?o`${p.field} DESC`:o`${p.field} ASC`),o`, `)}`:o``;let u=o``;if(i?.frame){const{type:p,start:f,end:h}=i.frame,g=p.toUpperCase(),y=f==="unbounded"?"UNBOUNDED PRECEDING":typeof f=="number"?`${f} PRECEDING`:"CURRENT ROW",b=h==="unbounded"?"UNBOUNDED FOLLOWING":h==="current"?"CURRENT ROW":typeof h=="number"?`${h} FOLLOWING`:"CURRENT ROW";u=o`${o.raw(g)} BETWEEN ${o.raw(y)} AND ${o.raw(b)}`}const c=[];n&&n.length>0&&c.push(r),s&&s.length>0&&c.push(a),i?.frame&&c.push(u);const l=c.length>0?o.join(c,o` `):o``,m=o`OVER (${l})`;switch(e){case"lag":return o`LAG(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"lead":return o`LEAD(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"rank":return o`RANK() ${m}`;case"denseRank":return o`DENSE_RANK() ${m}`;case"rowNumber":return o`ROW_NUMBER() ${m}`;case"ntile":return o`NTILE(${i?.nTile??4}) ${m}`;case"firstValue":return o`FIRST_VALUE(${t}) ${m}`;case"lastValue":return o`LAST_VALUE(${t}) ${m}`;case"movingAvg":return o`AVG(${t}) ${m}`;case"movingSum":return o`SUM(${t}) ${m}`;default:throw new Error(`Unsupported window function: ${e}`)}}}class vt extends pe{getEngineType(){return"sqlite"}supportsLateralJoins(){return!1}buildIntervalFromISO(e){const t=this.durationToSeconds(e);return o`${t}`}buildTimeDifferenceSeconds(e,t){return o`(${e} - ${t})`}buildDateAddInterval(e,t){const n=this.durationToSeconds(t);return o`(${e} + ${n})`}buildConditionalAggregation(e,t,n){const s=e.toUpperCase();return e==="count"&&!t?o`COUNT(CASE WHEN ${n} THEN 1 END)`:o`${o.raw(s)}(CASE WHEN ${n} THEN ${t} END)`}buildDateDiffPeriods(e,t,n){switch(n){case"day":return o`CAST((julianday(datetime(${t}, 'unixepoch')) - julianday(datetime(${e}, 'unixepoch'))) AS INTEGER)`;case"week":return o`CAST((julianday(datetime(${t}, 'unixepoch')) - julianday(datetime(${e}, 'unixepoch'))) / 7 AS INTEGER)`;case"month":return o`((CAST(strftime('%Y', datetime(${t}, 'unixepoch')) AS INTEGER) - CAST(strftime('%Y', datetime(${e}, 'unixepoch')) AS INTEGER)) * 12 + (CAST(strftime('%m', datetime(${t}, 'unixepoch')) AS INTEGER) - CAST(strftime('%m', datetime(${e}, 'unixepoch')) AS INTEGER)))`;default:throw new Error(`Unsupported date diff unit for SQLite: ${n}`)}}buildPeriodSeriesSubquery(e){return o`(
9
- WITH RECURSIVE periods(period_number) AS (
10
- SELECT 0
11
- UNION ALL
12
- SELECT period_number + 1 FROM periods WHERE period_number < ${e}
13
- )
14
- SELECT period_number FROM periods
15
- ) p`}buildTimeDimension(e,t){switch(e){case"year":return o`datetime(${t}, 'unixepoch', 'start of year')`;case"quarter":{const n=o`datetime(${t}, 'unixepoch')`;return o`datetime(${n}, 'start of year',
16
- '+' || (((CAST(strftime('%m', ${n}) AS INTEGER) - 1) / 3) * 3) || ' months')`}case"month":return o`datetime(${t}, 'unixepoch', 'start of month')`;case"week":return o`date(datetime(${t}, 'unixepoch'), 'weekday 1', '-6 days')`;case"day":return o`datetime(${t}, 'unixepoch', 'start of day')`;case"hour":{const n=o`datetime(${t}, 'unixepoch')`;return o`datetime(strftime('%Y-%m-%d %H:00:00', ${n}))`}case"minute":{const n=o`datetime(${t}, 'unixepoch')`;return o`datetime(strftime('%Y-%m-%d %H:%M:00', ${n}))`}case"second":{const n=o`datetime(${t}, 'unixepoch')`;return o`datetime(strftime('%Y-%m-%d %H:%M:%S', ${n}))`}default:return o`datetime(${t}, 'unixepoch')`}}buildStringCondition(e,t,n){switch(t){case"contains":return o`LOWER(${e}) LIKE ${`%${n.toLowerCase()}%`}`;case"notContains":return o`LOWER(${e}) NOT LIKE ${`%${n.toLowerCase()}%`}`;case"startsWith":return o`LOWER(${e}) LIKE ${`${n.toLowerCase()}%`}`;case"endsWith":return o`LOWER(${e}) LIKE ${`%${n.toLowerCase()}`}`;case"like":return o`${e} LIKE ${n}`;case"notLike":return o`${e} NOT LIKE ${n}`;case"ilike":return o`LOWER(${e}) LIKE ${n.toLowerCase()}`;case"regex":return o`${e} GLOB ${n}`;case"notRegex":return o`${e} NOT GLOB ${n}`;default:throw new Error(`Unsupported string operator: ${t}`)}}castToType(e,t){switch(t){case"timestamp":return o`datetime(${e} / 1000, 'unixepoch')`;case"decimal":return o`CAST(${e} AS REAL)`;case"integer":return o`CAST(${e} AS INTEGER)`;default:throw new Error(`Unsupported cast type: ${t}`)}}buildAvg(e){return o`IFNULL(AVG(${e}), 0)`}buildCaseWhen(e,t){const n=e.map(s=>s.then&&typeof s.then=="object"&&(s.then.queryChunks||s.then._||s.then.sql)?o`WHEN ${s.when} THEN ${o.raw("(")}${s.then}${o.raw(")")}`:o`WHEN ${s.when} THEN ${s.then}`).reduce((s,i)=>o`${s} ${i}`);return t!==void 0?t&&typeof t=="object"&&(t.queryChunks||t._||t.sql)?o`CASE ${n} ELSE ${o.raw("(")}${t}${o.raw(")")} END`:o`CASE ${n} ELSE ${t} END`:o`CASE ${n} END`}buildBooleanLiteral(e){return e?o`1`:o`0`}preprocessCalculatedTemplate(e){const t=/(\{[^}]+\})\s*\/\s*/g;return e.replace(t,(n,s)=>`${s.replace(/\{([^}]+)\}/,"CAST({$1} AS REAL)")} / `)}convertFilterValue(e){return typeof e=="boolean"?e?1:0:e instanceof Date?e.getTime():Array.isArray(e)?e.map(t=>this.convertFilterValue(t)):e}prepareDateValue(e){if(!(e instanceof Date)){if(typeof e=="number")return e;if(typeof e=="string")return new Date(e).getTime();throw new Error(`prepareDateValue expects a Date object, got ${typeof e}`)}return e.getTime()}isTimestampInteger(){return!0}convertTimeDimensionResult(e){return e}getCapabilities(){return{supportsStddev:!1,supportsVariance:!1,supportsPercentile:!1,supportsWindowFunctions:!0,supportsFrameClause:!0,supportsLateralJoins:!1,supportsPercentileSubqueries:!1}}buildStddev(e,t=!1){return null}buildVariance(e,t=!1){return null}buildPercentile(e,t){return null}buildWindowFunction(e,t,n,s,i){const r=n&&n.length>0?o`PARTITION BY ${o.join(n,o`, `)}`:o``,a=s&&s.length>0?o`ORDER BY ${o.join(s.map(p=>p.direction==="desc"?o`${p.field} DESC`:o`${p.field} ASC`),o`, `)}`:o``;let u=o``;if(i?.frame){const{type:p,start:f,end:h}=i.frame,g=p.toUpperCase(),y=f==="unbounded"?"UNBOUNDED PRECEDING":typeof f=="number"?`${f} PRECEDING`:"CURRENT ROW",b=h==="unbounded"?"UNBOUNDED FOLLOWING":h==="current"?"CURRENT ROW":typeof h=="number"?`${h} FOLLOWING`:"CURRENT ROW";u=o`${o.raw(g)} BETWEEN ${o.raw(y)} AND ${o.raw(b)}`}const c=[];n&&n.length>0&&c.push(r),s&&s.length>0&&c.push(a),i?.frame&&c.push(u);const l=c.length>0?o.join(c,o` `):o``,m=o`OVER (${l})`;switch(e){case"lag":return o`LAG(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"lead":return o`LEAD(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"rank":return o`RANK() ${m}`;case"denseRank":return o`DENSE_RANK() ${m}`;case"rowNumber":return o`ROW_NUMBER() ${m}`;case"ntile":return o`NTILE(${i?.nTile??4}) ${m}`;case"firstValue":return o`FIRST_VALUE(${t}) ${m}`;case"lastValue":return o`LAST_VALUE(${t}) ${m}`;case"movingAvg":return o`AVG(${t}) ${m}`;case"movingSum":return o`SUM(${t}) ${m}`;default:throw new Error(`Unsupported window function: ${e}`)}}}class Rt extends rt{getEngineType(){return"singlestore"}}class Ft extends pe{getEngineType(){return"duckdb"}supportsLateralJoins(){return!1}buildIntervalFromISO(e){const t=this.parseISODuration(e),n=[];t.years&&n.push(`${t.years} years`),t.months&&n.push(`${t.months} months`),t.days&&n.push(`${t.days} days`),t.hours&&n.push(`${t.hours} hours`),t.minutes&&n.push(`${t.minutes} minutes`),t.seconds&&n.push(`${t.seconds} seconds`);const s=n.join(" ")||"0 seconds";return o`INTERVAL '${o.raw(s)}'`}buildTimeDifferenceSeconds(e,t){return o`(EPOCH(${e}) - EPOCH(${t}))`}buildDateAddInterval(e,t){const n=this.buildIntervalFromISO(t);return o`(${e} + ${n})`}buildConditionalAggregation(e,t,n){const s=e.toUpperCase();return e==="count"&&!t?o`COUNT(*) FILTER (WHERE ${n})`:o`${o.raw(s)}(${t}) FILTER (WHERE ${n})`}buildDateDiffPeriods(e,t,n){return o`DATE_DIFF('${o.raw(n)}', ${e}::timestamp, ${t}::timestamp)`}buildPeriodSeriesSubquery(e){return o`(SELECT UNNEST(generate_series(0, ${e})) as period_number) p`}buildTimeDimension(e,t){switch(e){case"year":return o`DATE_TRUNC('year', ${t}::timestamp)`;case"quarter":return o`DATE_TRUNC('quarter', ${t}::timestamp)`;case"month":return o`DATE_TRUNC('month', ${t}::timestamp)`;case"week":return o`DATE_TRUNC('week', ${t}::timestamp)`;case"day":return o`DATE_TRUNC('day', ${t}::timestamp)::timestamp`;case"hour":return o`DATE_TRUNC('hour', ${t}::timestamp)`;case"minute":return o`DATE_TRUNC('minute', ${t}::timestamp)`;case"second":return o`DATE_TRUNC('second', ${t}::timestamp)`;default:return t}}buildStringCondition(e,t,n){switch(t){case"contains":return o`${e} ILIKE ${`%${n}%`}`;case"notContains":return o`${e} NOT ILIKE ${`%${n}%`}`;case"startsWith":return o`${e} ILIKE ${`${n}%`}`;case"endsWith":return o`${e} ILIKE ${`%${n}`}`;case"like":return o`${e} LIKE ${n}`;case"notLike":return o`${e} NOT LIKE ${n}`;case"ilike":return o`${e} ILIKE ${n}`;case"regex":return o`regexp_matches(${e}, ${n})`;case"notRegex":return o`NOT regexp_matches(${e}, ${n})`;default:throw new Error(`Unsupported string operator: ${t}`)}}castToType(e,t){switch(t){case"timestamp":return o`${e}::timestamp`;case"decimal":return o`${e}::decimal`;case"integer":return o`${e}::integer`;default:throw new Error(`Unsupported cast type: ${t}`)}}buildAvg(e){return o`COALESCE(AVG(${e}), 0)`}buildCaseWhen(e,t){const n=e.map(s=>o`WHEN ${s.when} THEN ${s.then}`).reduce((s,i)=>o`${s} ${i}`);return t!==void 0?o`CASE ${n} ELSE ${t} END`:o`CASE ${n} END`}buildBooleanLiteral(e){return e?o`TRUE`:o`FALSE`}convertFilterValue(e){return e}prepareDateValue(e){return e}isTimestampInteger(){return!1}convertTimeDimensionResult(e){return e}getCapabilities(){return{supportsStddev:!0,supportsVariance:!0,supportsPercentile:!0,supportsWindowFunctions:!0,supportsFrameClause:!0,supportsLateralJoins:!1,supportsPercentileSubqueries:!1}}buildStddev(e,t=!1){const n=t?"STDDEV_SAMP":"STDDEV_POP";return o`COALESCE(${o.raw(n)}(${e}), 0)`}buildVariance(e,t=!1){const n=t?"VAR_SAMP":"VAR_POP";return o`COALESCE(${o.raw(n)}(${e}), 0)`}buildPercentile(e,t){const n=t/100;return o`QUANTILE_CONT(${e}, ${n})`}buildWindowFunction(e,t,n,s,i){const r=n&&n.length>0?o`PARTITION BY ${o.join(n,o`, `)}`:o``,a=s&&s.length>0?o`ORDER BY ${o.join(s.map(p=>p.direction==="desc"?o`${p.field} DESC`:o`${p.field} ASC`),o`, `)}`:o``;let u=o``;if(i?.frame){const{type:p,start:f,end:h}=i.frame,g=p.toUpperCase(),y=f==="unbounded"?"UNBOUNDED PRECEDING":typeof f=="number"?`${f} PRECEDING`:"CURRENT ROW",b=h==="unbounded"?"UNBOUNDED FOLLOWING":h==="current"?"CURRENT ROW":typeof h=="number"?`${h} FOLLOWING`:"CURRENT ROW";u=o`${o.raw(g)} BETWEEN ${o.raw(y)} AND ${o.raw(b)}`}const c=[];n&&n.length>0&&c.push(r),s&&s.length>0&&c.push(a),i?.frame&&c.push(u);const l=c.length>0?o.join(c,o` `):o``,m=o`OVER (${l})`;switch(e){case"lag":return o`LAG(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"lead":return o`LEAD(${t}, ${i?.offset??1}${i?.defaultValue!==void 0?o`, ${i.defaultValue}`:o``}) ${m}`;case"rank":return o`RANK() ${m}`;case"denseRank":return o`DENSE_RANK() ${m}`;case"rowNumber":return o`ROW_NUMBER() ${m}`;case"ntile":return o`NTILE(${i?.nTile??4}) ${m}`;case"firstValue":return o`FIRST_VALUE(${t}) ${m}`;case"lastValue":return o`LAST_VALUE(${t}) ${m}`;case"movingAvg":return o`AVG(${t}) ${m}`;case"movingSum":return o`SUM(${t}) ${m}`;default:throw new Error(`Unsupported window function: ${e}`)}}}function Ot(d){switch(d){case"postgres":return new _t;case"mysql":return new rt;case"sqlite":return new vt;case"singlestore":return new Rt;case"duckdb":return new Ft;default:throw new Error(`Unsupported database engine: ${d}`)}}class he{constructor(e,t,n){this.db=e,this.schema=t;const s=n||this.getEngineType();this.databaseAdapter=Ot(s)}databaseAdapter}function It(d,e){const t=[],n=[];let s=!1,i,r,a;const u=[];for(const l of d){const m=l.match(/Planning Time:\s*([\d.]+)\s*ms/i);if(m){i=parseFloat(m[1]);continue}const p=l.match(/Execution Time:\s*([\d.]+)\s*ms/i);if(p){r=parseFloat(p[1]);continue}const f=Lt(l);if(f){f.type.includes("Seq Scan")&&(s=!0),f.index&&n.push(f.index),t.length===0&&f.estimatedCost!==void 0&&(a=f.estimatedCost);const h=l.search(/\S/);for(;u.length>0&&u[u.length-1].indent>=h;)u.pop();if(u.length===0)t.push(f);else{const g=u[u.length-1].op;g.children||(g.children=[]),g.children.push(f)}u.push({indent:h,op:f})}}const c={database:"postgres",planningTime:i,executionTime:r,totalCost:a,hasSequentialScans:s,usedIndexes:[...new Set(n)]};return{operations:t,summary:c,raw:d.join(`
17
- `),sql:e}}function Lt(d){const e=d.replace(/^[\s->]+/,"").trim();if(!e)return null;const t=e.match(/^([A-Za-z][A-Za-z0-9 ]+?)(?:\s+using\s+(\S+))?(?:\s+on\s+(\S+))?(?:\s+\w+)?(?:\s+\(cost=([\d.]+)\.\.([\d.]+)\s+rows=(\d+)(?:\s+width=\d+)?\))?(?:\s+\(actual time=([\d.]+)\.\.([\d.]+)\s+rows=(\d+)\s+loops=(\d+)\))?/i);if(!t)return e.match(/^Filter:\s*(.+)$/i)||e.match(/^(Hash Cond|Join Filter|Index Cond):\s*(.+)$/i),null;const n=t[1].trim(),s=t[2]||void 0,i=t[3]||void 0,r=t[5]?parseFloat(t[5]):void 0,a=t[6]?parseInt(t[6],10):void 0,u=t[9]?parseInt(t[9],10):void 0,c={type:n,table:i,index:s,estimatedRows:a,estimatedCost:r};u!==void 0&&(c.actualRows=u);const l=d.match(/Filter:\s*(.+?)(?:\)|$)/i);return l&&(c.filter=l[1].trim()),c}class Mt extends he{async execute(e,t){if(e&&typeof e=="object"&&typeof e.execute=="function"){const s=await e.execute();return Array.isArray(s)?s.map(i=>this.convertNumericFields(i,t)):s}if(!this.db.execute)throw new Error("PostgreSQL database instance must have an execute method");const n=await this.db.execute(e);return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}convertNumericFields(e,t){if(!e||typeof e!="object")return e;const n={};for(const[s,i]of Object.entries(e))t&&t.includes(s)?n[s]=this.coerceToNumber(i):n[s]=i;return n}coerceToNumber(e){if(e==null||typeof e=="number")return e;if(typeof e=="bigint")return Number(e);if(e&&typeof e=="object"){if(typeof e.toString=="function"){const t=e.toString();if(/^-?\d+(\.\d+)?$/.test(t))return t.includes(".")?parseFloat(t):parseInt(t,10)}if(e.constructor?.name==="Numeric"||e.constructor?.name==="Decimal"||"digits"in e||"sign"in e){const t=e.toString();return parseFloat(t)}return e}if(typeof e=="string"){if(/^-?\d+(\.\d+)?$/.test(e))return e.includes(".")?parseFloat(e):parseInt(e,10);if(!isNaN(parseFloat(e))&&isFinite(parseFloat(e)))return parseFloat(e)}return e}getEngineType(){return"postgres"}async explainQuery(e,t,n){const s=n?.analyze?"EXPLAIN ANALYZE":"EXPLAIN";if(!this.db.execute)throw new Error("PostgreSQL database instance must have an execute method");const i=await this.db.execute(o`${o.raw(s)} ${o.raw(e.replace(/\$(\d+)/g,(a,u)=>{const c=parseInt(u,10)-1,l=t[c];return l===null?"NULL":typeof l=="number"?String(l):typeof l=="boolean"?l?"TRUE":"FALSE":l instanceof Date?`'${l.toISOString()}'`:`'${String(l).replace(/'/g,"''")}'`}))}`),r=[];if(Array.isArray(i)){for(const a of i)if(a&&typeof a=="object"){const u=a["QUERY PLAN"]||a["query plan"]||a.queryplan;typeof u=="string"&&r.push(u)}}return It(r,{sql:e,params:t})}async getTableIndexes(e){if(!e||e.length===0)return[];if(!this.db.execute)throw new Error("PostgreSQL database instance must have an execute method");try{const t=e.map(s=>`'${s.toLowerCase()}'`).join(","),n=await this.db.execute(o`
18
- SELECT
19
- t.relname as table_name,
20
- i.relname as index_name,
21
- array_to_string(array_agg(a.attname ORDER BY k.n), ',') as columns,
22
- ix.indisunique as is_unique,
23
- ix.indisprimary as is_primary
24
- FROM pg_index ix
25
- JOIN pg_class t ON t.oid = ix.indrelid
26
- JOIN pg_class i ON i.oid = ix.indexrelid
27
- JOIN pg_namespace n ON n.oid = t.relnamespace
28
- JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, n) ON true
29
- JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
30
- WHERE n.nspname = 'public'
31
- AND t.relname IN (${o.raw(t)})
32
- GROUP BY t.relname, i.relname, ix.indisunique, ix.indisprimary
33
- ORDER BY t.relname, i.relname
34
- `);return Array.isArray(n)?n.map(s=>({table_name:s.table_name,index_name:s.index_name,columns:s.columns.split(","),is_unique:s.is_unique,is_primary:s.is_primary})):[]}catch(t){return console.warn("Failed to get table indexes:",t),[]}}}function Ke(d,e){return new Mt(d,e,"postgres")}function jt(d,e){const t=e?.toLowerCase()||"";switch(d.toLowerCase()){case"all":return"Seq Scan";case"index":return t.includes("using index")?"Index Only Scan":"Index Scan";case"range":return"Index Range Scan";case"ref":case"eq_ref":return"Index Lookup";case"const":case"system":return"Const Lookup";case"null":return"No Table";default:return`MySQL ${d}`}}function Ut(d,e){const t=[],n=[];let s=!1,i=0;for(const u of d){const c=jt(u.type,u.Extra);u.type.toLowerCase()==="all"&&(s=!0),u.key&&n.push(u.key);const l={type:c,table:u.table||void 0,index:u.key||void 0,estimatedRows:u.rows,estimatedCost:u.rows};if(u.Extra){const m=[];u.Extra.includes("Using where")&&m.push("WHERE filter applied"),u.Extra.includes("Using filesort")&&m.push("Filesort required"),u.Extra.includes("Using temporary")&&m.push("Temporary table required"),u.Extra.includes("Using join buffer")&&m.push("Join buffer used"),m.length>0&&(l.details=m.join("; ")),l.filter=u.Extra}t.push(l),i+=u.rows}const r={database:"mysql",planningTime:void 0,executionTime:void 0,totalCost:i,hasSequentialScans:s,usedIndexes:[...new Set(n)]},a=["id select_type table type possible_keys key rows Extra",...d.map(u=>`${u.id} ${u.select_type} ${u.table||"NULL"} ${u.type} ${u.possible_keys||"NULL"} ${u.key||"NULL"} ${u.rows} ${u.Extra||""}`)];return{operations:t,summary:r,raw:a.join(`
35
- `),sql:e}}class ot extends he{async execute(e,t){if(e&&typeof e=="object"&&typeof e.execute=="function"){const s=await e.execute();return Array.isArray(s)?s.map(i=>this.convertNumericFields(i,t)):s}if(!this.db.execute)throw new Error("MySQL database instance must have an execute method");const n=await this.db.execute(e);return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}convertNumericFields(e,t){if(!e||typeof e!="object")return e;const n={};for(const[s,i]of Object.entries(e))t&&t.includes(s)?n[s]=this.coerceToNumber(i):n[s]=i;return n}coerceToNumber(e){if(e==null||typeof e=="number")return e;if(typeof e=="string"){if(/^-?\d+(\.\d+)?$/.test(e))return e.includes(".")?parseFloat(e):parseInt(e,10);if(!isNaN(parseFloat(e))&&isFinite(parseFloat(e)))return parseFloat(e)}return e}getEngineType(){return"mysql"}async explainQuery(e,t,n){let s=e,i=0;s=s.replace(/\?/g,()=>{const c=t[i++];return c===null?"NULL":typeof c=="number"?String(c):typeof c=="boolean"?c?"1":"0":c instanceof Date?`'${c.toISOString().slice(0,19).replace("T"," ")}'`:`'${String(c).replace(/'/g,"''")}'`});const r=n?.analyze?"EXPLAIN ANALYZE":"EXPLAIN";if(!this.db.execute)throw new Error("MySQL database instance must have an execute method");const a=await this.db.execute(o.raw(`${r} ${s}`)),u=[];if(Array.isArray(a))for(const c of a)c&&typeof c=="object"&&u.push({id:c.id||1,select_type:c.select_type||"SIMPLE",table:c.table||null,partitions:c.partitions||null,type:c.type||"ALL",possible_keys:c.possible_keys||null,key:c.key||null,key_len:c.key_len||null,ref:c.ref||null,rows:Number(c.rows)||0,filtered:Number(c.filtered)||100,Extra:c.Extra||null});return Ut(u,{sql:e,params:t})}async getTableIndexes(e){if(!e||e.length===0)return[];if(!this.db.execute)throw new Error("MySQL database instance must have an execute method");try{const t=e.map(s=>`'${s.toLowerCase()}'`).join(","),n=await this.db.execute(o`
36
- SELECT
37
- TABLE_NAME as table_name,
38
- INDEX_NAME as index_name,
39
- GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) as columns,
40
- CASE WHEN NON_UNIQUE = 0 THEN TRUE ELSE FALSE END as is_unique,
41
- CASE WHEN INDEX_NAME = 'PRIMARY' THEN TRUE ELSE FALSE END as is_primary
42
- FROM INFORMATION_SCHEMA.STATISTICS
43
- WHERE TABLE_SCHEMA = DATABASE()
44
- AND LOWER(TABLE_NAME) IN (${o.raw(t)})
45
- GROUP BY TABLE_NAME, INDEX_NAME, NON_UNIQUE
46
- ORDER BY TABLE_NAME, INDEX_NAME
47
- `);return Array.isArray(n)?n.map(s=>({table_name:s.table_name,index_name:s.index_name,columns:s.columns.split(","),is_unique:!!s.is_unique,is_primary:!!s.is_primary})):[]}catch(t){return console.warn("Failed to get table indexes:",t),[]}}}function xt(d,e){return new ot(d,e,"mysql")}function kt(d){const e=d.toLowerCase(),t=d.match(/^SCAN\s+(\S+)/i);if(t)return{type:"Seq Scan",table:t[1]};const n=d.match(/^SEARCH\s+(\S+)\s+USING\s+(?:COVERING\s+)?INDEX\s+(\S+)(?:\s+\((.+)\))?/i);if(n)return{type:"Index Scan",table:n[1],index:n[2],filter:n[3]};const s=d.match(/^SEARCH\s+(\S+)\s+USING\s+INTEGER\s+PRIMARY\s+KEY\s+\((.+)\)/i);if(s)return{type:"Primary Key Lookup",table:s[1],filter:s[2]};const i=d.match(/^SEARCH\s+(\S+)/i);return i?{type:"Search",table:i[1]}:e.includes("temp b-tree")?e.includes("order by")?{type:"Sort"}:e.includes("group by")?{type:"Group"}:e.includes("distinct")?{type:"Distinct"}:{type:"Temp B-Tree"}:e.includes("compound")?{type:"Compound Query"}:e.includes("subquery")?{type:"Subquery"}:e.includes("co-routine")?{type:"Coroutine"}:{type:d}}function Bt(d,e){const t=[],n=[];let s=!1;const i=new Map;for(const u of d){const c=kt(u.detail);c.type==="Seq Scan"&&(s=!0),c.index&&n.push(c.index);const l={type:c.type,table:c.table,index:c.index,filter:c.filter,details:u.detail};if(i.set(u.id,l),u.parent===0)t.push(l);else{const m=i.get(u.parent);m?(m.children||(m.children=[]),m.children.push(l)):t.push(l)}}const r={database:"sqlite",planningTime:void 0,executionTime:void 0,totalCost:void 0,hasSequentialScans:s,usedIndexes:[...new Set(n)]},a=["id parent detail",...d.map(u=>`${u.id} ${u.parent} ${u.detail}`)];return{operations:t,summary:r,raw:a.join(`
48
- `),sql:e}}class Pt extends he{async execute(e,t){if(e&&typeof e=="object"&&typeof e.execute=="function"){const n=await e.execute();return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}try{if(this.db.all){const n=this.db.all(e);return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}else{if(this.db.run)return this.db.run(e);throw new Error("SQLite database instance must have an all() or run() method")}}catch(n){throw new Error(`SQLite execution failed: ${n instanceof Error?n.message:"Unknown error"}`)}}convertNumericFields(e,t){if(!e||typeof e!="object")return e;const n={};for(const[s,i]of Object.entries(e))t&&t.includes(s)?n[s]=this.coerceToNumber(i):n[s]=i;return n}coerceToNumber(e){if(e==null||typeof e=="number")return e;if(typeof e=="string"){if(/^-?\d+(\.\d+)?$/.test(e))return e.includes(".")?parseFloat(e):parseInt(e,10);if(!isNaN(parseFloat(e))&&isFinite(parseFloat(e)))return parseFloat(e)}return e}getEngineType(){return"sqlite"}async explainQuery(e,t,n){let s=e,i=0;s=s.replace(/\?/g,()=>{const c=t[i++];return c===null?"NULL":typeof c=="number"?String(c):typeof c=="boolean"?c?"1":"0":c instanceof Date?`'${c.toISOString()}'`:`'${String(c).replace(/'/g,"''")}'`});const r=`EXPLAIN QUERY PLAN ${s}`;let a=[];if(this.db.all)a=this.db.all(o.raw(r));else throw new Error("SQLite database instance must have an all() method for EXPLAIN");const u=[];if(Array.isArray(a))for(const c of a)c&&typeof c=="object"&&u.push({id:Number(c.id)||0,parent:Number(c.parent)||0,notused:Number(c.notused)||0,detail:String(c.detail||"")});return Bt(u,{sql:e,params:t})}async getTableIndexes(e){if(!e||e.length===0)return[];if(!this.db.all)throw new Error("SQLite database instance must have an all() method");try{const t=[];for(const n of e){const s=this.db.all(o.raw(`SELECT name, "unique", origin FROM pragma_index_list('${n.toLowerCase()}')`));if(Array.isArray(s))for(const i of s){const r=i.name,a=!!i.unique,u=i.origin,c=this.db.all(o.raw(`SELECT name FROM pragma_index_info('${r}') ORDER BY seqno`)),l=[];if(Array.isArray(c))for(const m of c){const p=m.name;typeof p=="string"&&l.push(p)}t.push({table_name:n.toLowerCase(),index_name:r,columns:l,is_unique:a,is_primary:u==="pk"})}}return t}catch(t){return console.warn("Failed to get table indexes:",t),[]}}}function qe(d,e){return new Pt(d,e,"sqlite")}class Qt extends ot{getEngineType(){return"singlestore"}}function Wt(d,e){return new Qt(d,e)}function Kt(d,e){const t=[],n=[];let s=!1,i;const r=[];for(const u of d){if(/^[┌├└│─┐┤┘]+$/.test(u.trim())||/EXPLANATION|QUERY PLAN/i.test(u))continue;const c=zt(u);if(c){(c.type.includes("SEQ_SCAN")||c.type.includes("TABLE_SCAN"))&&(s=!0),c.type.includes("INDEX_SCAN")&&c.index&&n.push(c.index),t.length===0&&c.estimatedCost!==void 0&&(i=c.estimatedCost);const l=qt(u);for(;r.length>0&&r[r.length-1].indent>=l;)r.pop();if(r.length===0)t.push(c);else{const m=r[r.length-1].op;m.children||(m.children=[]),m.children.push(c)}r.push({indent:l,op:c})}}const a={database:"duckdb",planningTime:void 0,executionTime:void 0,totalCost:i,hasSequentialScans:s,usedIndexes:[...new Set(n)]};return{operations:t,summary:a,raw:d.join(`
49
- `),sql:e}}function qt(d){let e=0;for(const t of d)if(t===" "||t==="│"||t==="├"||t==="└"||t==="─")e++;else break;return e}function zt(d){const e=d.replace(/[┌├└│─┐┤┘]/g,"").replace(/^\s*/,"").trim();if(!e||e.match(/^\(cost=([\d.]+)\s+rows=(\d+)\)$/i))return null;const n=e.match(/^([A-Z_]+)(?:\s+(\S+))?(?:\s+on\s+(\S+))?(?:\s+\(cost=([\d.]+)\s+rows=(\d+)\))?/i);if(!n){const l=e.match(/^FILTER\s+(.+)$/i);return l?{type:"FILTER",filter:l[1]}:null}const s=n[1].toUpperCase();let i=n[2]||void 0,r=n[3]||void 0;const a=n[4]?parseFloat(n[4]):void 0,u=n[5]?parseInt(n[5],10):void 0;if(s==="INDEX_SCAN"){const l=i,m=r;r=l,i=m}return{type:s,table:i,index:r,estimatedRows:u,estimatedCost:a}}class Vt extends he{async execute(e,t){if(e&&typeof e=="object"&&typeof e.execute=="function")try{const n=await e.execute();return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}catch(n){const s=this.extractSqlFromQuery(e);throw console.error("[DuckDB] Query execution failed:",{error:n instanceof Error?n.message:String(n),sql:s.sql,params:s.params}),n}if(!this.db.execute)throw new Error("DuckDB database instance must have an execute method");try{const n=await this.db.execute(e);return Array.isArray(n)?n.map(s=>this.convertNumericFields(s,t)):n}catch(n){const s=this.extractSqlFromQuery(e);throw console.error("[DuckDB] Query execution failed:",{error:n instanceof Error?n.message:String(n),sql:s.sql,params:s.params}),n}}extractSqlFromQuery(e){try{if(e&&typeof e.toSQL=="function"){const{sql:t,params:n}=e.toSQL();return{sql:t,params:n}}if(e&&typeof e.getSQL=="function"){const t=e.getSQL();if(t&&typeof t.toSQL=="function"){const{sql:n,params:s}=t.toSQL();return{sql:n,params:s}}}return{sql:String(e),params:[]}}catch{return{sql:"[unable to extract SQL]",params:[]}}}convertNumericFields(e,t){if(!e||typeof e!="object")return e;const n={};for(const[s,i]of Object.entries(e))t&&t.includes(s)?n[s]=this.coerceToNumber(i):n[s]=i;return n}coerceToNumber(e){if(e==null||typeof e=="number")return e;if(typeof e=="bigint")return Number(e);if(e&&typeof e=="object"){if(typeof e.toString=="function"){const t=e.toString();if(/^-?\d+(\.\d+)?$/.test(t))return t.includes(".")?parseFloat(t):parseInt(t,10)}return e}if(typeof e=="string"){if(/^-?\d+(\.\d+)?$/.test(e))return e.includes(".")?parseFloat(e):parseInt(e,10);if(!isNaN(parseFloat(e))&&isFinite(parseFloat(e)))return parseFloat(e)}return e}getEngineType(){return"duckdb"}async explainQuery(e,t,n){const s=n?.analyze?"EXPLAIN ANALYZE":"EXPLAIN";if(!this.db.execute)throw new Error("DuckDB database instance must have an execute method");const i=await this.db.execute(o`${o.raw(s)} ${o.raw(e.replace(/\$(\d+)/g,(a,u)=>{const c=parseInt(u,10)-1,l=t[c];return l===null?"NULL":typeof l=="number"?String(l):typeof l=="boolean"?l?"TRUE":"FALSE":l instanceof Date?`'${l.toISOString()}'`:`'${String(l).replace(/'/g,"''")}'`}))}`),r=[];if(Array.isArray(i)){for(const a of i)if(a&&typeof a=="object"){const u=a.explain_value||a["QUERY PLAN"]||a.query_plan||a.Plan||Object.values(a)[0];typeof u=="string"&&r.push(u)}}return Kt(r,{sql:e,params:t})}async getTableIndexes(e){if(!e||e.length===0)return[];if(!this.db.execute)throw new Error("DuckDB database instance must have an execute method");try{const t=e.map(s=>`'${s.toLowerCase()}'`).join(","),n=await this.db.execute(o`
50
- SELECT
51
- table_name,
52
- index_name,
53
- LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY index_oid) as columns,
54
- is_unique,
55
- is_primary
56
- FROM duckdb_indexes()
57
- WHERE LOWER(table_name) IN (${o.raw(t)})
58
- GROUP BY table_name, index_name, is_unique, is_primary
59
- ORDER BY table_name, index_name
60
- `);return Array.isArray(n)?n.map(s=>({table_name:s.table_name,index_name:s.index_name,columns:typeof s.columns=="string"?s.columns.split(","):[],is_unique:!!s.is_unique,is_primary:!!s.is_primary})):[]}catch(t){return console.warn("Failed to get table indexes:",t),[]}}}function Jt(d,e){return new Vt(d,e,"duckdb")}function ze(d,e,t){if(t)switch(t){case"postgres":return Ke(d,e);case"mysql":return xt(d,e);case"sqlite":return qe(d,e);case"singlestore":return Wt(d,e);case"duckdb":return Jt(d,e)}if(d.all&&d.run)return qe(d,e);if(d.execute)return Ke(d,e);throw new Error("Unable to determine database engine type. Please specify engineType parameter.")}function K(d){return typeof d=="function"?d():d}function _e(d,e){if(e)return e;switch(d){case"belongsTo":return"inner";case"hasOne":return"left";case"hasMany":return"left";case"belongsToMany":return"left";default:return"left"}}function ve(d){return d&&typeof d=="object"?o`${o`${d}`}`:d}function R(d,e){const t=typeof d=="function"?d(e):d;return ve(t)}function Gt(d,e){if(d.relationship!=="belongsToMany"||!d.through)throw new Error("expandBelongsToManyJoin can only be called on belongsToMany relationships with through configuration");const{table:t,sourceKey:n,targetKey:s,securitySql:i}=d.through,r=[];for(const l of n){const m=l.as||W;r.push(m(l.source,l.target))}const a=[];for(const l of s){const m=l.as||W;a.push(m(l.source,l.target))}let u;if(i){const l=i(e);u=Array.isArray(l)?l:[l]}const c=_e("belongsToMany",d.sqlJoinType);return{junctionJoins:[{joinType:c,table:t,condition:A(...r)},{joinType:c,table:t,condition:A(...a)}],junctionSecurityConditions:u}}function me(d){if("and"in d)return`and:[${d.and.map(me).sort().join(",")}]`;if("or"in d)return`or:[${d.or.map(me).sort().join(",")}]`;const e=d,t=JSON.stringify(Array.isArray(e.values)?[...e.values].sort():e.values),n=e.dateRange?`:dr:${JSON.stringify(e.dateRange)}`:"";return`${e.member}:${e.operator}:${t}${n}`}function at(d,e){return`timeDim:${d}:${JSON.stringify(e)}`}class Ve{cache=new Map;stats={hits:0,misses:0};getOrBuild(e,t){const n=this.cache.get(e);if(n!==void 0)return this.stats.hits++,n;const s=t();return s&&this.cache.set(e,s),this.stats.misses++,s}has(e){return this.cache.has(e)}get(e){const t=this.cache.get(e);return t!==void 0&&this.stats.hits++,t}preload(e){for(const{key:t,sql:n}of e)this.cache.has(t)||this.cache.set(t,n)}set(e,t){this.cache.set(e,t)}getStats(){return{...this.stats,cacheSize:this.cache.size}}clear(){this.cache.clear(),this.stats={hits:0,misses:0}}}function Re(d){const e=[];for(const t of d)"and"in t&&t.and?e.push(...Re(t.and)):"or"in t&&t.or?e.push(...Re(t.or)):"member"in t&&e.push(t);return e}function Ht(d,e,t={}){const n=t.keyPrefix??"drizzle-cube:",s=Yt(d),i=Je(JSON.stringify(s));let r=`${n}query:${i}`;if(t.includeSecurityContext!==!1){const a=t.securityContextSerializer?t.securityContextSerializer(e):JSON.stringify(fe(e)),u=Je(a);r+=`:ctx:${u}`}return r}function Yt(d){return{measures:d.measures?[...d.measures].sort():void 0,dimensions:d.dimensions?[...d.dimensions].sort():void 0,filters:d.filters?q(d.filters):void 0,timeDimensions:d.timeDimensions?tn(d.timeDimensions):void 0,limit:d.limit,offset:d.offset,order:d.order?fe(d.order):void 0,fillMissingDatesValue:d.fillMissingDatesValue,funnel:d.funnel?Xt(d.funnel):void 0,flow:d.flow?Zt(d.flow):void 0,retention:d.retention?en(d.retention):void 0}}function Xt(d){return{bindingKey:d.bindingKey,timeDimension:d.timeDimension,steps:d.steps.map(e=>{const t={name:e.name,filter:e.filter?Array.isArray(e.filter)?q(e.filter):q([e.filter])[0]:void 0,timeToConvert:e.timeToConvert};return"cube"in e&&e.cube&&(t.cube=e.cube),t}),includeTimeMetrics:d.includeTimeMetrics,globalTimeWindow:d.globalTimeWindow}}function Zt(d){return{bindingKey:d.bindingKey,timeDimension:d.timeDimension,eventDimension:d.eventDimension,startingStep:{name:d.startingStep.name,filter:d.startingStep.filter?Array.isArray(d.startingStep.filter)?q(d.startingStep.filter):q([d.startingStep.filter])[0]:void 0},stepsBefore:d.stepsBefore,stepsAfter:d.stepsAfter,entityLimit:d.entityLimit,outputMode:d.outputMode,joinStrategy:d.joinStrategy}}function en(d){return{timeDimension:d.timeDimension,bindingKey:d.bindingKey,dateRange:d.dateRange,granularity:d.granularity,periods:d.periods,retentionType:d.retentionType,cohortFilters:d.cohortFilters?Array.isArray(d.cohortFilters)?q(d.cohortFilters):q([d.cohortFilters])[0]:void 0,activityFilters:d.activityFilters?Array.isArray(d.activityFilters)?q(d.activityFilters):q([d.activityFilters])[0]:void 0,breakdownDimensions:d.breakdownDimensions}}function q(d){return[...d].map(e=>{if("and"in e&&e.and)return{and:q(e.and)};if("or"in e&&e.or)return{or:q(e.or)};const t=e;return{...t,values:t.values?[...t.values].sort():t.values}}).sort((e,t)=>JSON.stringify(e).localeCompare(JSON.stringify(t)))}function tn(d){return[...d].map(e=>({dimension:e.dimension,granularity:e.granularity,dateRange:e.dateRange,fillMissingDates:e.fillMissingDates,compareDateRange:e.compareDateRange?[...e.compareDateRange].sort((t,n)=>{const s=Array.isArray(t)?t.join("-"):t,i=Array.isArray(n)?n.join("-"):n;return s.localeCompare(i)}):void 0})).sort((e,t)=>e.dimension.localeCompare(t.dimension))}function fe(d){return d===null||typeof d!="object"?d:Array.isArray(d)?d.map(fe):Object.keys(d).sort().reduce((e,t)=>(e[t]=fe(d[t]),e),{})}function Je(d){let e=2166136261;for(let t=0;t<d.length;t++)e^=d.charCodeAt(t),e=e*16777619>>>0;return e.toString(16).padStart(8,"0")}class ie{constructor(e){this.databaseAdapter=e}buildTimeDimensionExpression(e,t,n){const s=R(e,n);return t?this.databaseAdapter.buildTimeDimension(t,s):s instanceof O?s:o`${s}`}buildDateRangeCondition(e,t){if(!t)return null;if(Array.isArray(t)&&t.length>=2){const n=this.normalizeDate(t[0]);let s=this.normalizeDate(t[1]);if(!n||!s)return null;if(typeof t[1]=="string"&&/^\d{4}-\d{2}-\d{2}$/.test(t[1].trim())){const i=typeof s=="number"?new Date(s*(this.databaseAdapter.getEngineType()==="sqlite"?1e3:1)):new Date(s),r=new Date(i);r.setUTCHours(23,59,59,999),this.databaseAdapter.isTimestampInteger()?s=this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(r.getTime()/1e3):r.getTime():s=r.toISOString()}return A(ee(e,n),te(e,s))}if(typeof t=="string"){const n=this.parseRelativeDateRange(t);if(n){let l,m;return this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?(l=Math.floor(n.start.getTime()/1e3),m=Math.floor(n.end.getTime()/1e3)):(l=n.start.getTime(),m=n.end.getTime()):(l=n.start.toISOString(),m=n.end.toISOString()),A(ee(e,l),te(e,m))}const s=this.normalizeDate(t);if(!s)return null;const i=typeof s=="number"?new Date(s*(this.databaseAdapter.getEngineType()==="sqlite"?1e3:1)):new Date(s),r=new Date(i);r.setUTCHours(0,0,0,0);const a=new Date(i);a.setUTCHours(23,59,59,999);let u,c;return this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?(u=Math.floor(r.getTime()/1e3),c=Math.floor(a.getTime()/1e3)):(u=r.getTime(),c=a.getTime()):(u=r.toISOString(),c=a.toISOString()),A(ee(e,u),te(e,c))}return null}parseRelativeDateRange(e){const t=new Date,n=e.toLowerCase().trim(),s=t.getUTCFullYear(),i=t.getUTCMonth(),r=t.getUTCDate(),a=t.getUTCDay();if(n==="today"){const p=new Date(t);p.setUTCHours(0,0,0,0);const f=new Date(t);return f.setUTCHours(23,59,59,999),{start:p,end:f}}if(n==="yesterday"){const p=new Date(t);p.setUTCDate(r-1),p.setUTCHours(0,0,0,0);const f=new Date(t);return f.setUTCDate(r-1),f.setUTCHours(23,59,59,999),{start:p,end:f}}if(n==="this week"){const p=a===0?-6:1-a,f=new Date(t);f.setUTCDate(r+p),f.setUTCHours(0,0,0,0);const h=new Date(f);return h.setUTCDate(f.getUTCDate()+6),h.setUTCHours(23,59,59,999),{start:f,end:h}}if(n==="this month"){const p=new Date(Date.UTC(s,i,1,0,0,0,0)),f=new Date(Date.UTC(s,i+1,0,23,59,59,999));return{start:p,end:f}}if(n==="this quarter"){const p=Math.floor(i/3),f=new Date(Date.UTC(s,p*3,1,0,0,0,0)),h=new Date(Date.UTC(s,p*3+3,0,23,59,59,999));return{start:f,end:h}}if(n==="this year"){const p=new Date(Date.UTC(s,0,1,0,0,0,0)),f=new Date(Date.UTC(s,11,31,23,59,59,999));return{start:p,end:f}}const u=n.match(/^last\s+(\d+)\s+days?$/);if(u){const p=parseInt(u[1],10),f=new Date(t);f.setUTCDate(r-p+1),f.setUTCHours(0,0,0,0);const h=new Date(t);return h.setUTCHours(23,59,59,999),{start:f,end:h}}const c=n.match(/^last\s+(\d+)\s+weeks?$/);if(c){const f=parseInt(c[1],10)*7,h=new Date(t);h.setUTCDate(r-f+1),h.setUTCHours(0,0,0,0);const g=new Date(t);return g.setUTCHours(23,59,59,999),{start:h,end:g}}if(n==="last week"){const p=a===0?-13:-6-a,f=new Date(t);f.setUTCDate(r+p),f.setUTCHours(0,0,0,0);const h=new Date(f);return h.setUTCDate(f.getUTCDate()+6),h.setUTCHours(23,59,59,999),{start:f,end:h}}if(n==="last month"){const p=new Date(Date.UTC(s,i-1,1,0,0,0,0)),f=new Date(Date.UTC(s,i,0,23,59,59,999));return{start:p,end:f}}if(n==="last quarter"){const p=Math.floor(i/3),f=p===0?3:p-1,h=p===0?s-1:s,g=new Date(Date.UTC(h,f*3,1,0,0,0,0)),y=new Date(Date.UTC(h,f*3+3,0,23,59,59,999));return{start:g,end:y}}if(n==="last year"){const p=new Date(Date.UTC(s-1,0,1,0,0,0,0)),f=new Date(Date.UTC(s-1,11,31,23,59,59,999));return{start:p,end:f}}if(n==="last 12 months"){const p=new Date(Date.UTC(s,i-11,1,0,0,0,0)),f=new Date(t);return f.setUTCHours(23,59,59,999),{start:p,end:f}}const l=n.match(/^last\s+(\d+)\s+months?$/);if(l){const p=parseInt(l[1],10),f=new Date(Date.UTC(s,i-p+1,1,0,0,0,0)),h=new Date(t);return h.setUTCHours(23,59,59,999),{start:f,end:h}}const m=n.match(/^last\s+(\d+)\s+years?$/);if(m){const p=parseInt(m[1],10),f=new Date(Date.UTC(s-p,0,1,0,0,0,0)),h=new Date(t);return h.setUTCHours(23,59,59,999),{start:f,end:h}}return null}normalizeDate(e){if(!e)return null;if(e instanceof Date)return isNaN(e.getTime())?null:this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(e.getTime()/1e3):e.getTime():e.toISOString();if(typeof e=="number"){const n=e<1e10?e*1e3:e,s=new Date(n);return isNaN(s.getTime())?null:this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(n/1e3):n:s.toISOString()}if(typeof e=="string"){if(/^\d{4}-\d{2}-\d{2}$/.test(e.trim())){const s=new Date(e+"T00:00:00Z");return isNaN(s.getTime())?null:this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(s.getTime()/1e3):s.getTime():s.toISOString()}const n=new Date(e);return isNaN(n.getTime())?null:this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(n.getTime()/1e3):n.getTime():n.toISOString()}const t=new Date(e);return isNaN(t.getTime())?null:this.databaseAdapter.isTimestampInteger()?this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(t.getTime()/1e3):t.getTime():t.toISOString()}}class ge{constructor(e,t){this.databaseAdapter=e,this.dateTimeBuilder=t}buildFilterCondition(e,t,n,s,i){if(i!==void 0){if(t!=="inDateRange")throw new Error(`dateRange can only be used with 'inDateRange' operator, but got '${t}'. Use explicit date values in the 'values' array for other date operators.`);if(s&&s.type!=="time")throw new Error(`dateRange can only be used on time dimensions, but field '${s.name||"unknown"}' has type '${s.type}'`);return this.dateTimeBuilder.buildDateRangeCondition(e,i)}if(!n||n.length===0)return t==="equals"?this.databaseAdapter.buildBooleanLiteral(!1):null;const r=n.filter(u=>!(u==null||u===""||typeof u=="string"&&u.includes("\0"))).map(this.databaseAdapter.convertFilterValue);if(r.length===0&&!["set","notSet"].includes(t))return t==="equals"?this.databaseAdapter.buildBooleanLiteral(!1):null;const a=r[0];switch(t){case"equals":if(r.length>1){if(s?.type==="time"){const u=r.map(c=>this.dateTimeBuilder.normalizeDate(c)||c);return Te(e,u)}return Te(e,r)}else if(r.length===1){const u=s?.type==="time"&&this.dateTimeBuilder.normalizeDate(a)||a;return W(e,u)}return this.databaseAdapter.buildBooleanLiteral(!1);case"notEquals":return r.length>1?Be(e,r):r.length===1?ke(e,a):null;case"contains":return this.databaseAdapter.buildStringCondition(e,"contains",a);case"notContains":return this.databaseAdapter.buildStringCondition(e,"notContains",a);case"startsWith":return this.databaseAdapter.buildStringCondition(e,"startsWith",a);case"endsWith":return this.databaseAdapter.buildStringCondition(e,"endsWith",a);case"gt":return $e(e,a);case"gte":return ee(e,a);case"lt":return Ee(e,a);case"lte":return te(e,a);case"set":return Qe(e);case"notSet":return Pe(e);case"inDateRange":if(r.length>=2){const u=this.dateTimeBuilder.normalizeDate(r[0]);let c=this.dateTimeBuilder.normalizeDate(r[1]);if(u&&c){const l=n[1];if(typeof l=="string"&&/^\d{4}-\d{2}-\d{2}$/.test(l.trim())){const m=typeof c=="number"?new Date(c*(this.databaseAdapter.getEngineType()==="sqlite"?1e3:1)):new Date(c),p=new Date(m);p.setUTCHours(23,59,59,999),this.databaseAdapter.isTimestampInteger()?c=this.databaseAdapter.getEngineType()==="sqlite"?Math.floor(p.getTime()/1e3):p.getTime():c=p.toISOString()}return A(ee(e,u),te(e,c))}}return null;case"beforeDate":{const u=this.dateTimeBuilder.normalizeDate(a);return u?Ee(e,u):null}case"afterDate":{const u=this.dateTimeBuilder.normalizeDate(a);return u?$e(e,u):null}case"between":return r.length>=2?A(ee(e,r[0]),te(e,r[1])):null;case"notBetween":return r.length>=2?re(Ee(e,r[0]),$e(e,r[1])):null;case"in":return r.length>0?Te(e,r):null;case"notIn":return r.length>0?Be(e,r):null;case"like":return this.databaseAdapter.buildStringCondition(e,"like",a);case"notLike":return this.databaseAdapter.buildStringCondition(e,"notLike",a);case"ilike":return this.databaseAdapter.buildStringCondition(e,"ilike",a);case"regex":return this.databaseAdapter.buildStringCondition(e,"regex",a);case"notRegex":return this.databaseAdapter.buildStringCondition(e,"notRegex",a);case"isEmpty":return re(Pe(e),W(e,""));case"isNotEmpty":return A(Qe(e),ke(e,""));case"arrayContains":return this.databaseAdapter.getEngineType()==="postgres"?Tt(e,r):null;case"arrayOverlaps":return this.databaseAdapter.getEngineType()==="postgres"?Nt(e,r):null;case"arrayContained":return this.databaseAdapter.getEngineType()==="postgres"?St(e,r):null;default:return null}}buildLogicalFilter(e,t,n){if("and"in e&&e.and){const s=e.and.map(i=>this.buildSingleFilter(i,t,n)).filter(i=>i!==null);return s.length>0?s.length===1?s[0]:A(...s):null}if("or"in e&&e.or){const s=e.or.map(i=>this.buildSingleFilter(i,t,n)).filter(i=>i!==null);return s.length>0?s.length===1?s[0]:re(...s):null}return null}buildSingleFilter(e,t,n){if("and"in e||"or"in e)return this.buildLogicalFilter(e,t,n);const s=e,[i,r]=s.member.split("."),a=t.get(i);if(!a)return null;const u=a.dimensions?.[r];if(!u)return null;const c=R(u.sql,n);return this.buildFilterCondition(c,s.operator,s.values,u,s.dateRange)}}class Z{dependencyGraph;cubes;constructor(e){this.cubes=e instanceof Map?e:new Map([[e.name,e]]),this.dependencyGraph=new Map}extractDependencies(e){const t=/\{([^}]+)\}/g,n=e.matchAll(t),s=[];for(const i of n){const r=i[1].trim();if(r.includes(".")){const[a,u]=r.split(".");s.push({measureName:r,cubeName:a.trim(),fieldName:u.trim()})}else s.push({measureName:r,cubeName:null,fieldName:r})}return s}buildGraph(e){for(const[t,n]of Object.entries(e.measures))if(n.type==="calculated"&&n.calculatedSql){const s=`${e.name}.${t}`,i=this.extractDependencies(n.calculatedSql),r=new Set;for(const a of i){const c=`${a.cubeName||e.name}.${a.fieldName}`;r.add(c)}this.dependencyGraph.set(s,{id:s,dependencies:r,inDegree:0})}this.calculateInDegrees()}buildGraphForMultipleCubes(e){for(const t of e.values())this.buildGraph(t)}calculateInDegrees(){for(const e of this.dependencyGraph.values())e.inDegree=0;for(const e of this.dependencyGraph.values())for(const t of e.dependencies){const n=this.dependencyGraph.get(t);n&&n.inDegree++}}topologicalSort(e){const t=new Map,n=[],s=[];for(const i of e){const r=this.dependencyGraph.get(i);r&&t.set(i,{id:r.id,dependencies:new Set(r.dependencies),inDegree:0})}for(const i of t.values()){let r=0;for(const a of i.dependencies)t.has(a)&&r++;i.inDegree=r}for(const[i,r]of t)r.inDegree===0&&n.push(i);for(;n.length>0;){const i=n.shift();s.push(i);for(const[r,a]of t)a.dependencies.has(i)&&(a.inDegree--,a.inDegree===0&&n.push(r))}if(s.length<t.size){const i=this.detectCycle();throw new Error(`Circular dependency detected in calculated measures: ${i?i.join(" -> "):"unknown cycle"}`)}return s}detectCycle(){const e=new Set,t=new Set,n=[];for(const s of this.dependencyGraph.keys())if(!e.has(s)){const i=this.dfs(s,e,t,n);if(i)return i}return null}dfs(e,t,n,s){t.add(e),n.add(e),s.push(e);const i=this.dependencyGraph.get(e);if(!i)return s.pop(),n.delete(e),null;for(const r of i.dependencies)if(t.has(r)){if(n.has(r)){const a=s.indexOf(r);return[...s.slice(a),r]}}else{const a=this.dfs(r,t,n,s);if(a)return a}return s.pop(),n.delete(e),null}getAllDependencies(e){const t=new Set,n=new Set,s=i=>{if(n.has(i))return;n.add(i);const r=this.dependencyGraph.get(i);if(r)for(const a of r.dependencies)t.add(a),s(a)};return s(e),t}validateDependencies(e){for(const[t,n]of Object.entries(e.measures))if(n.type==="calculated"&&n.calculatedSql){const s=this.extractDependencies(n.calculatedSql);for(const i of s){const r=i.cubeName||e.name,a=this.cubes.get(r);if(!a)throw new Error(`Calculated measure '${e.name}.${t}' references unknown cube '${r}'`);if(!a.measures[i.fieldName])throw new Error(`Calculated measure '${e.name}.${t}' references unknown measure '${i.measureName}'`);if(r===e.name&&i.fieldName===t)throw new Error(`Calculated measure '${e.name}.${t}' cannot reference itself`)}}}populateDependencies(e){for(const[,t]of Object.entries(e.measures))if(t.type==="calculated"&&t.calculatedSql&&!t.dependencies){const n=this.extractDependencies(t.calculatedSql);t.dependencies=n.map(s=>s.measureName)}}static isCalculatedMeasure(e){return e.type==="calculated"&&!!e.calculatedSql}}function nn(d,e){const{cube:t,allCubes:n,resolvedMeasures:s}=e,i=Fe(d),r=new Map;for(const m of i){const{originalRef:p,cubeName:f,fieldName:h}=m,g=f||t.name;if(!n.get(g))throw new Error(`Cannot substitute {${p}}: cube '${g}' not found`);const b=`${g}.${h}`,$=s.get(b);if(!$)throw new Error(`Cannot substitute {${p}}: measure '${b}' not resolved yet. Ensure measures are resolved in dependency order.`);const D=$(),w=o`${D}`;r.set(p,w)}const a=[],u=[];let c=0;for(const m of i){const p=`{${m.originalRef}}`,f=d.indexOf(p,c);if(f>=0){a.push(d.substring(c,f));const h=r.get(m.originalRef);h&&u.push(h),c=f+p.length}}if(a.push(d.substring(c)),u.length===0)return o.raw(d);const l=[];for(let m=0;m<a.length;m++)a[m]&&l.push(new M(a[m])),m<u.length&&l.push(u[m]);return o.join(l,o.raw(""))}function Fe(d){const e=/\{([^}]+)\}/g,t=d.matchAll(e),n=[];for(const s of t){const i=s[1].trim();if(i.includes(".")){const[r,a]=i.split(".").map(u=>u.trim());n.push({originalRef:i,cubeName:r,fieldName:a})}else n.push({originalRef:i,cubeName:null,fieldName:i})}return n}function sn(d){const e=[];let t=0;for(let r=0;r<d.length;r++)if(d[r]==="{")t++;else if(d[r]==="}"&&(t--,t<0)){e.push(`Unmatched closing brace at position ${r}`);break}t>0&&e.push("Unmatched opening brace in template"),/\{\s*\}/.test(d)&&e.push("Empty member reference {} found in template"),/\{[^}]*\{/.test(d)&&e.push("Nested braces are not allowed in member references");const i=Fe(d);for(const r of i){const a=r.cubeName?`${r.cubeName}.${r.fieldName}`:r.fieldName;/^[a-zA-Z_][a-zA-Z0-9_.]*$/.test(a)||e.push(`Invalid member reference {${r.originalRef}}: must start with letter or underscore, and contain only letters, numbers, underscores, and dots`),a.split(".").length>2&&e.push(`Invalid member reference {${r.originalRef}}: only one dot allowed (Cube.measure format)`)}return{isValid:e.length===0,errors:e}}function Se(d,e){const t=Fe(d),n=new Set;for(const s of t){const r=`${s.cubeName||e}.${s.fieldName}`;n.add(r)}return Array.from(n)}class I{constructor(e){this.databaseAdapter=e}buildResolvedMeasures(e,t,n,s){const i=new Map,r=[],a=[],u=new Set(e),c=new Z(t);for(const l of t.values())c.buildGraph(l);for(const l of e){const[m,p]=l.split("."),f=t.get(m);if(f&&f.measures&&f.measures[p]){const h=f.measures[p];if(I.isPostAggregationWindow(h)){const g=I.getWindowBaseMeasure(h,m);g&&u.add(g);continue}Z.isCalculatedMeasure(h)?(a.push(l),Se(h.calculatedSql,m).forEach(b=>u.add(b)),c.getAllDependencies(l).forEach(b=>{const[$,D]=b.split("."),w=t.get($);if(w&&w.measures[D]){const C=w.measures[D];Z.isCalculatedMeasure(C)&&Se(C.calculatedSql,$).forEach(T=>u.add(T))}})):r.push(l)}}for(const l of u){const[m,p]=l.split("."),f=t.get(m);if(f&&f.measures&&f.measures[p]){const h=f.measures[p];if(I.isPostAggregationWindow(h))continue;Z.isCalculatedMeasure(h)?a.includes(l)||a.push(l):r.includes(l)||r.push(l)}}for(const l of r){const[m,p]=l.split("."),f=t.get(m),h=f.measures[p];if(s){const g=s(l,h,f);i.set(l,()=>g)}else i.set(l,()=>this.buildMeasureExpression(h,n,f))}if(a.length>0){const l=c.topologicalSort(a);for(const m of l){const[p,f]=m.split("."),h=t.get(p),g=h.measures[f];i.set(m,()=>this.buildCalculatedMeasure(g,h,t,i,n))}}return i}buildCalculatedMeasure(e,t,n,s,i){if(!e.calculatedSql)throw new Error(`Calculated measure '${t.name}.${e.name}' missing calculatedSql property`);const r=this.databaseAdapter.preprocessCalculatedTemplate(e.calculatedSql);return nn(r,{cube:t,allCubes:n,resolvedMeasures:s})}buildCTECalculatedMeasure(e,t,n,s,i){if(!e.calculatedSql)throw new Error(`Calculated measure '${t.name}.${e.name||"unknown"}' missing calculatedSql property`);const r=new Map,a=Se(e.calculatedSql,t.name);for(const u of a){const[c,l]=u.split("."),m=s.get(c);if(m&&m.measures[l]){const p=m.measures[l];if(n.measures.includes(u)){const f=o`${o.identifier(n.cteAlias)}.${o.identifier(l)}`;let h;switch(p.type){case"count":case"countDistinct":case"sum":h=V(f);break;case"avg":h=this.databaseAdapter.buildAvg(f);break;case"min":h=oe(f);break;case"max":h=H(f);break;case"number":h=V(f);break;default:h=V(f)}r.set(u,()=>h)}}}return this.buildCalculatedMeasure(e,t,s,r,i)}buildHavingMeasureExpression(e,t,n,s,i){if(i&&i.preAggregationCTEs){const r=i.preAggregationCTEs.find(a=>a.cube.name===e);if(r&&r.measures.includes(`${e}.${t}`))if(n.type==="calculated"&&n.calculatedSql){const a=i.primaryCube.name===e?i.primaryCube:i.joinCubes?.find(c=>c.cube.name===e)?.cube;if(!a)throw new Error(`Cube ${e} not found in query plan`);const u=new Map([[i.primaryCube.name,i.primaryCube]]);if(i.joinCubes)for(const c of i.joinCubes)u.set(c.cube.name,c.cube);return this.buildCTECalculatedMeasure(n,a,r,u,s)}else{const a=o`${o.identifier(r.cteAlias)}.${o.identifier(t)}`;switch(n.type){case"count":case"countDistinct":case"sum":return V(a);case"avg":return this.databaseAdapter.buildAvg(a);case"min":return oe(a);case"max":return H(a);case"number":return V(a);default:return V(a)}}}return this.buildMeasureExpression(n,s)}buildMeasureExpression(e,t,n){if(e.type==="calculated")throw new Error(`Cannot build calculated measure '${e.name}' directly. Use buildCalculatedMeasure instead.`);if(I.isPostAggregationWindow(e))throw new Error(`Post-aggregation window measure '${e.name}' should be built via buildPostAggregationWindowExpression, not buildMeasureExpression.`);if(!e.sql)throw new Error(`Measure '${e.name}' of type '${e.type}' is missing required 'sql' property. Only calculated measures and post-aggregation window functions can omit 'sql'.`);let s=R(e.sql,t);if(e.filters&&e.filters.length>0){const i=e.filters.map(r=>{const a=r(t);return a?o`(${a})`:void 0}).filter(Boolean);if(i.length>0){const r=i.length===1?i[0]:A(...i);s=this.databaseAdapter.buildCaseWhen([{when:r,then:s}])}}switch(e.type){case"count":return Ae(s);case"countDistinct":return At(s);case"sum":return V(s);case"avg":return this.databaseAdapter.buildAvg(s);case"min":return oe(s);case"max":return H(s);case"number":return s;case"stddev":case"stddevSamp":{const i=e.type==="stddevSamp"||e.statisticalConfig?.useSample,r=this.databaseAdapter.buildStddev(s,i);return r===null?(console.warn(`[drizzle-cube] ${e.type} not supported on ${this.databaseAdapter.getEngineType()}, returning NULL`),o`MAX(NULL)`):r}case"variance":case"varianceSamp":{const i=e.type==="varianceSamp"||e.statisticalConfig?.useSample,r=this.databaseAdapter.buildVariance(s,i);return r===null?(console.warn(`[drizzle-cube] ${e.type} not supported on ${this.databaseAdapter.getEngineType()}, returning NULL`),o`MAX(NULL)`):r}case"percentile":case"median":case"p95":case"p99":{let i;switch(e.type){case"median":i=50;break;case"p95":i=95;break;case"p99":i=99;break;default:i=e.statisticalConfig?.percentile??50}const r=this.databaseAdapter.buildPercentile(s,i);return r===null?(console.warn(`[drizzle-cube] ${e.type} not supported on ${this.databaseAdapter.getEngineType()}, returning NULL`),o`MAX(NULL)`):r}case"lag":case"lead":case"rank":case"denseRank":case"rowNumber":case"ntile":case"firstValue":case"lastValue":case"movingAvg":case"movingSum":{const i=e.windowConfig||{};let r;if(i.partitionBy&&i.partitionBy.length>0&&n){const c=i.partitionBy.map(l=>{const m=l.includes(".")?l.split(".")[1]:l,p=n.dimensions?.[m];return p?R(p.sql,t):(console.warn(`[drizzle-cube] Window function partition dimension '${l}' not found in cube '${n.name}'`),null)}).filter(l=>l!==null);c.length>0&&(r=c)}let a;if(i.orderBy&&i.orderBy.length>0&&n){const c=i.orderBy.map(l=>{const m=l.field.includes(".")?l.field.split(".")[1]:l.field,p=n.dimensions?.[m];if(p)return{field:R(p.sql,t),direction:l.direction};const f=n.measures?.[m];return f&&f.sql?{field:R(f.sql,t),direction:l.direction}:(console.warn(`[drizzle-cube] Window function order field '${l.field}' not found in cube '${n.name}'`),null)}).filter(l=>l!==null);c.length>0&&(a=c)}const u=this.databaseAdapter.buildWindowFunction(e.type,["rank","denseRank","rowNumber"].includes(e.type)?null:s,r,a,{offset:i.offset,defaultValue:i.defaultValue,nTile:i.nTile,frame:i.frame});return u===null?(console.warn(`[drizzle-cube] ${e.type} not supported on ${this.databaseAdapter.getEngineType()}, returning NULL`),o`NULL`):u}default:return Ae(s)}}static WINDOW_FUNCTION_TYPES=["lag","lead","rank","denseRank","rowNumber","ntile","firstValue","lastValue","movingAvg","movingSum"];static isWindowFunction(e){return I.WINDOW_FUNCTION_TYPES.includes(e)}static categorizeMeasures(e,t){const n=[],s=[];for(const i of e){const[r,a]=i.split("."),u=t.get(r);if(u?.measures?.[a]){const c=u.measures[a];I.isWindowFunction(c.type)?n.push(i):s.push(i)}}return{windowMeasures:n,aggregateMeasures:s}}static hasWindowFunctions(e,t){const{windowMeasures:n}=I.categorizeMeasures(e,t);return n.length>0}static isPostAggregationWindow(e){return I.isWindowFunction(e.type)&&e.windowConfig?.measure!==void 0}static getWindowBaseMeasure(e,t){if(!e.windowConfig?.measure)return null;const n=e.windowConfig.measure;return n.includes(".")?n:`${t}.${n}`}static getDefaultWindowOperation(e){switch(e){case"lag":case"lead":return"difference";default:return"raw"}}static categorizeForPostAggregation(e,t){const n=[],s=[],i=new Set;for(const r of e){const[a,u]=r.split("."),c=t.get(a);if(c?.measures?.[u]){const l=c.measures[u];if(I.isPostAggregationWindow(l)){s.push(r);const m=I.getWindowBaseMeasure(l,a);m&&i.add(m)}else I.isWindowFunction(l.type)||n.push(r)}}return{aggregateMeasures:n,postAggWindowMeasures:s,requiredBaseMeasures:i}}static hasPostAggregationWindows(e,t){const{postAggWindowMeasures:n}=I.categorizeForPostAggregation(e,t);return n.length>0}}class rn{constructor(e){this.dateTimeBuilder=e}isWindowFunctionType(e){return["lag","lead","rank","denseRank","rowNumber","ntile","firstValue","lastValue","movingAvg","movingSum"].includes(e)}isAggregateFunctionType(e){return["count","countDistinct","sum","avg","min","max","stddev","stddevSamp","variance","varianceSamp","median","p95","p99","percentile","number"].includes(e)}buildGroupByFields(e,t,n,s){const i=[],r=e instanceof Map?e:new Map([[e.name,e]]),a=t.dimensions&&t.dimensions.length>0||t.timeDimensions&&t.timeDimensions.length>0,u=t.measures&&t.measures.length>0,c=a&&!u;let l=!1;for(const m of t.measures||[]){const[p,f]=m.split("."),h=r.get(p);if(h&&h.measures&&h.measures[f]){const g=h.measures[f];if(this.isAggregateFunctionType(g.type)||g.type==="calculated"){l=!0;break}if(I.isPostAggregationWindow(g)){const y=I.getWindowBaseMeasure(g,p);if(y){const[b,$]=y.split("."),w=r.get(b)?.measures?.[$];if(w&&this.isAggregateFunctionType(w.type)){l=!0;break}}}}}if(!l&&!c)return[];if(t.dimensions)for(const m of t.dimensions){const[p,f]=m.split("."),h=r.get(p);if(h&&h.dimensions&&h.dimensions[f])if(s?.preAggregationCTEs?.some(y=>y.cube.name===p)){const y=s.preAggregationCTEs.find($=>$.cube.name===p),b=y.joinKeys.find($=>$.targetColumn===f);if(b&&b.sourceColumnObj)i.push(b.sourceColumnObj);else{const $=o`${o.identifier(y.cteAlias)}.${o.identifier(f)}`;i.push($)}}else{const y=h.dimensions[f],b=R(y.sql,n);i.push(b)}}if(t.timeDimensions)for(const m of t.timeDimensions){const[p,f]=m.dimension.split("."),h=r.get(p);if(h&&h.dimensions&&h.dimensions[f])if(s?.preAggregationCTEs?.some(y=>y.cube.name===p)){const y=s.preAggregationCTEs.find($=>$.cube.name===p),b=y.joinKeys.find($=>$.targetColumn===f);if(b&&b.sourceColumnObj){const $=this.dateTimeBuilder.buildTimeDimensionExpression(b.sourceColumnObj,m.granularity,n);i.push($)}else{const $=o`${o.identifier(y.cteAlias)}.${o.identifier(f)}`;i.push($)}}else{const y=h.dimensions[f],b=this.dateTimeBuilder.buildTimeDimensionExpression(y.sql,m.granularity,n);i.push(b)}}return i}}class on{dateTimeBuilder;filterBuilder;groupByBuilder;measureBuilder;constructor(e){this.dateTimeBuilder=new ie(e),this.filterBuilder=new ge(e,this.dateTimeBuilder),this.groupByBuilder=new rn(this.dateTimeBuilder),this.measureBuilder=new I(e)}buildResolvedMeasures(e,t,n,s){return this.measureBuilder.buildResolvedMeasures(e,t,n,s)}buildSelections(e,t,n){const s={},i=e instanceof Map?e:new Map([[e.name,e]]);if(t.dimensions)for(const r of t.dimensions){const[a,u]=r.split("."),c=i.get(a);if(c&&c.dimensions&&c.dimensions[u]){const l=c.dimensions[u],m=R(l.sql,n);s[r]=o`${m}`.as(r)}}if(t.measures){const r=this.buildResolvedMeasures(t.measures,i,n);for(const a of t.measures){const u=r.get(a);if(u){const c=u();s[a]=o`${c}`.as(a)}}}if(t.timeDimensions)for(const r of t.timeDimensions){const[a,u]=r.dimension.split("."),c=i.get(a);if(c&&c.dimensions&&c.dimensions[u]){const l=c.dimensions[u],m=this.buildTimeDimensionExpression(l.sql,r.granularity,n);s[r.dimension]=o`${m}`.as(r.dimension)}}return Object.keys(s).length===0&&(s.count=Ae()),s}buildCalculatedMeasure(e,t,n,s,i){return this.measureBuilder.buildCalculatedMeasure(e,t,n,s,i)}buildCTECalculatedMeasure(e,t,n,s,i){return this.measureBuilder.buildCTECalculatedMeasure(e,t,n,s,i)}buildHavingMeasureExpression(e,t,n,s,i){return this.measureBuilder.buildHavingMeasureExpression(e,t,n,s,i)}buildMeasureExpression(e,t,n){return this.measureBuilder.buildMeasureExpression(e,t,n)}buildTimeDimensionExpression(e,t,n){return this.dateTimeBuilder.buildTimeDimensionExpression(e,t,n)}buildWhereConditions(e,t,n,s,i){const r=[],a=e instanceof Map?e:new Map([[e.name,e]]),u=new Set;if(t.filters&&t.filters.length>0)for(const c of t.filters){if(i&&"member"in c){const[m]=c.member.split(".");if(a.has(m)&&i.has(m)&&!u.has(m)){const f=i.get(m);r.push(...f),u.add(m);continue}else if(u.has(m))continue}const l=this.processFilter(c,a,n,"where",s);l&&r.push(l)}if(t.timeDimensions)for(const c of t.timeDimensions){const[l,m]=c.dimension.split("."),p=a.get(l);if(p&&p.dimensions[m]&&c.dateRange){if(s?.preAggregationCTEs&&s.preAggregationCTEs.some(b=>b.cube.name===l))continue;if(n.filterCache){const y=at(c.dimension,c.dateRange),b=n.filterCache.get(y);if(b){r.push(b);continue}}const f=p.dimensions[m],h=R(f.sql,n),g=this.buildDateRangeCondition(h,c.dateRange);g&&r.push(g)}}return r}buildHavingConditions(e,t,n,s){const i=[],r=e instanceof Map?e:new Map([[e.name,e]]);if(t.filters&&t.filters.length>0)for(const a of t.filters){const u=this.processFilter(a,r,n,"having",s);u&&i.push(u)}return i}processFilter(e,t,n,s,i){if("and"in e||"or"in e){const f=e;if(f.and){const h=f.and.map(g=>this.processFilter(g,t,n,s,i)).filter(g=>g!==null);return h.length>0?A(...h):null}if(f.or){const h=f.or.map(g=>this.processFilter(g,t,n,s,i)).filter(g=>g!==null);return h.length>0?re(...h):null}}const r=e,[a,u]=r.member.split("."),c=t.get(a);if(!c)return null;const l=c.dimensions[u],m=c.measures[u],p=l||m;if(!p)return null;if(s==="where"&&l){if(i?.preAggregationCTEs&&i.preAggregationCTEs.some(y=>y.cube.name===a))return null;const f=["arrayContains","arrayOverlaps","arrayContained"].includes(r.operator);if(!f&&n.filterCache){const g=me(e),y=n.filterCache.get(g);if(y)return y}const h=f?typeof l.sql=="function"?l.sql(n):l.sql:R(l.sql,n);return this.buildFilterCondition(h,r.operator,r.values,p,r.dateRange)}else{if(s==="where"&&m)return null;if(s==="having"&&m){const f=this.buildHavingMeasureExpression(a,u,m,n,i);return this.buildFilterCondition(f,r.operator,r.values,p,r.dateRange)}}return null}buildFilterCondition(e,t,n,s,i){return this.filterBuilder.buildFilterCondition(e,t,n,s,i)}buildDateRangeCondition(e,t){return this.dateTimeBuilder.buildDateRangeCondition(e,t)}buildGroupByFields(e,t,n,s){return this.groupByBuilder.buildGroupByFields(e,t,n,s)}buildOrderBy(e,t){const n=[],s=t||[...e.measures||[],...e.dimensions||[],...e.timeDimensions?.map(i=>i.dimension)||[]];if(e.order&&Object.keys(e.order).length>0)for(const[i,r]of Object.entries(e.order)){if(!s.includes(i))throw new Error(`Cannot order by '${i}': field is not selected in the query`);const a=r==="desc"?Dt(o.identifier(i)):We(o.identifier(i));n.push(a)}if(e.timeDimensions&&e.timeDimensions.length>0){const i=new Set(Object.keys(e.order||{})),r=[...e.timeDimensions].sort((a,u)=>a.dimension.localeCompare(u.dimension));for(const a of r)i.has(a.dimension)||n.push(We(o.identifier(a.dimension)))}return n}collectNumericFields(e,t){const n=[],s=e instanceof Map?e:new Map([[e.name,e]]);if(t.measures&&n.push(...t.measures),t.dimensions)for(const i of t.dimensions){const[r,a]=i.split("."),u=s.get(r);if(u){const c=u.dimensions[a];c&&c.type==="number"&&n.push(i)}}return n}applyLimitAndOffset(e,t){let n=t.limit;t.offset!==void 0&&t.offset>0&&n===void 0&&(n=50);let s=e;if(n!==void 0){if(n<0)throw new Error("Limit must be non-negative");s=s.limit(n)}if(t.offset!==void 0){if(t.offset<0)throw new Error("Offset must be non-negative");s=s.offset(t.offset)}return s}buildFilterConditionPublic(e,t,n,s,i){return this.buildFilterCondition(e,t,n,s,i)}buildLogicalFilter(e,t,n){return this.filterBuilder.buildLogicalFilter(e,t,n)}}class ae{cubes;connectivityCache=new Map;constructor(e){this.cubes=e}findPath(e,t,n=new Set){if(e===t)return[];const s=this.getCacheKey(e,t,n),i=this.getFromCache(s);if(i!==void 0)return i;const r=[{cube:e,path:[]}],a=new Set([e,...n]);for(;r.length>0;){const{cube:u,path:c}=r.shift(),l=this.cubes.get(u);if(l?.joins)for(const[,m]of Object.entries(l.joins)){const f=K(m.targetCube).name;if(a.has(f))continue;const h=[...c,{fromCube:u,toCube:f,joinDef:m}];if(f===t)return this.setInCache(s,h),h;a.add(f),r.push({cube:f,path:h})}}return this.setInCache(s,null),null}findPathPreferring(e,t,n,s=new Set){const i=this.findAllPaths(e,t,new Set);if(i.length===0)return this.findPath(e,t,s);const r=i.map(a=>{let u=0;a.some((m,p)=>m.joinDef.preferredFor?.includes(t)&&p===0)&&(u+=10);const l=a.filter(m=>n.has(m.toCube)).length;return u+=l,u-=a.length-1,{path:a,score:u,usesProcessed:a.some(m=>s.has(m.toCube))}});return r.sort((a,u)=>u.score!==a.score?u.score-a.score:a.usesProcessed!==u.usesProcessed?a.usesProcessed?-1:1:a.path.length-u.path.length),r[0].path}findAllPaths(e,t,n,s=4){if(e===t)return[[]];const i=[],r=[{cube:e,path:[],visited:new Set([e,...n])}];for(;r.length>0;){const{cube:a,path:u,visited:c}=r.shift();if(u.length>=s)continue;const l=this.cubes.get(a);if(l?.joins)for(const[,m]of Object.entries(l.joins)){const f=K(m.targetCube).name;if(c.has(f))continue;const h=[...u,{fromCube:a,toCube:f,joinDef:m}];if(f===t)i.push(h);else{const g=new Set(c);g.add(f),r.push({cube:f,path:h,visited:g})}}}return i}canReachAll(e,t){const n=t.filter(s=>s!==e);for(const s of n){const i=this.findPath(e,s,new Set);if(!i||i.length===0)return!1}return!0}buildJoinCondition(e,t,n){const s=[];for(const i of e.on){const r=t?o`${o.identifier(t)}.${o.identifier(i.source.name)}`:ve(i.source),a=n?o`${o.identifier(n)}.${o.identifier(i.target.name)}`:ve(i.target),u=i.as||W;s.push(u(r,a))}return A(...s)}getReachableCubes(e){const t=new Set([e]),n=[e];for(;n.length>0;){const s=n.shift(),i=this.cubes.get(s);if(i?.joins)for(const[,r]of Object.entries(i.joins)){const u=K(r.targetCube).name;t.has(u)||(t.add(u),n.push(u))}}return t}getCacheKey(e,t,n){const s=Array.from(n).sort().join(",");return`${e}:${t}:${s}`}getFromCache(e){const t=this.connectivityCache.get(e);if(t)return t.path}setInCache(e,t){this.connectivityCache.set(e,{path:t})}}class ut{resolverCache=new WeakMap;getResolver(e){let t=this.resolverCache.get(e);return t||(t=new ae(e),this.resolverCache.set(e,t)),t}analyzeCubeUsage(e){const t=new Set;if(e.measures)for(const n of e.measures){const[s]=n.split(".");t.add(s)}if(e.dimensions)for(const n of e.dimensions){const[s]=n.split(".");t.add(s)}if(e.timeDimensions)for(const n of e.timeDimensions){const[s]=n.dimension.split(".");t.add(s)}if(e.filters)for(const n of e.filters)this.extractCubeNamesFromFilter(n,t);return t}extractCubeNamesFromFilter(e,t){if("and"in e||"or"in e){const n=e.and||e.or||[];for(const s of n)this.extractCubeNamesFromFilter(s,t);return}if("member"in e){const[n]=e.member.split(".");n&&t.add(n)}}extractMeasuresFromFilters(e,t){const n=[];if(!e.filters)return n;for(const s of e.filters)this.extractMeasuresFromFilter(s,t,n);return n}extractMeasuresFromFilter(e,t,n){if("and"in e||"or"in e){const s=e.and||e.or||[];for(const i of s)this.extractMeasuresFromFilter(i,t,n);return}if("member"in e){const s=e.member,[i,r]=s.split(".");i===t.name&&t.measures&&t.measures[r]&&n.push(s)}}createQueryPlan(e,t,n){const s=this.analyzeCubeUsage(t),i=Array.from(s);if(i.length===0)throw new Error("No cubes found in query");const r=this.choosePrimaryCube(i,t,e),a=e.get(r);if(!a)throw new Error(`Primary cube '${r}' not found`);if(i.length===1)return{primaryCube:a,joinCubes:[],selections:{},whereConditions:[],groupByFields:[]};const u=this.buildJoinPlan(e,a,i,n,t),c=this.planPreAggregationCTEs(e,a,u,t,n),l=this.generateWarnings(t,c);return{primaryCube:a,joinCubes:u,selections:{},whereConditions:[],groupByFields:[],preAggregationCTEs:c,warnings:l.length>0?l:void 0}}choosePrimaryCube(e,t,n){return e.length===1?e[0]:n?this.analyzePrimaryCubeSelection(e,t,n).selectedCube:[...e].sort()[0]}buildJoinPlan(e,t,n,s,i){const r=this.getResolver(e),a=[],u=new Set([t.name]),c=new Set;if(i.measures)for(const p of i.measures){const[f]=p.split(".");c.add(f)}const l=new Set;for(const p of c){if(p===t.name)continue;this.findHasManyJoinDef(t,p)&&l.add(p)}const m=n.filter(p=>p!==t.name);for(const p of m){if(u.has(p))continue;const f=new Set([...u].filter(g=>!l.has(g))),h=r.findPathPreferring(t.name,p,c,f);if(!h||h.length===0)throw new Error(`No join path found from '${t.name}' to '${p}'`);for(const{fromCube:g,toCube:y,joinDef:b}of h){if(u.has(y))continue;const $=e.get(y);if(!$)throw new Error(`Cube '${y}' not found`);if(b.relationship==="belongsToMany"&&b.through){const D=Gt(b,s.securityContext);a.push({cube:$,alias:`${y.toLowerCase()}_cube`,joinType:D.junctionJoins[1].joinType,joinCondition:D.junctionJoins[1].condition,junctionTable:{table:b.through.table,alias:`junction_${y.toLowerCase()}`,joinType:D.junctionJoins[0].joinType,joinCondition:D.junctionJoins[0].condition,securitySql:b.through.securitySql,sourceCubeName:g}})}else{const D=r.buildJoinCondition(b,null,null),w=_e(b.relationship,b.sqlJoinType);a.push({cube:$,alias:`${y.toLowerCase()}_cube`,joinType:w,joinCondition:D})}u.add(y)}}return a}planPreAggregationCTEs(e,t,n,s,i){const r=[];if(!s.measures||s.measures.length===0)return r;const a=this.detectHasManyInQuery(e,t,n);if(a.length===0)return r;const u=new Set;for(const l of s.measures){const[m]=l.split(".");u.add(m)}for(const l of e.values())this.extractMeasuresFromFilters(s,l).length>0&&u.add(l.name);const c=[];for(const l of n)c.push({cube:l.cube,alias:l.alias,isPrimary:!1});for(const{cube:l,alias:m,isPrimary:p}of c){const f=this.getCTEReason(l,t,a,s);if(!f)continue;const h=s.measures.filter(S=>S.startsWith(l.name+".")),g=this.extractMeasuresFromFilters(s,l),y=[...new Set([...h,...g])];if(y.length===0)continue;const b=this.analyzeJoinPathToPrimary(e,t,l.name,i);let $,D;if(b?.hasIntermediateHasMany&&b.intermediateJoins.length>0)$=b.correctJoinKeys,D=b.intermediateJoins;else{const S=p?this.findJoinInfoToCube(e,t.name):this.findJoinInfoForCube(e,t,l.name);if(!S)continue;S.joinDef.relationship==="belongsToMany"&&S.joinDef.through?$=S.joinDef.through.sourceKey.map(_=>({sourceColumn:_.target.name,targetColumn:_.source.name,sourceColumnObj:_.target,targetColumnObj:_.source})):$=p||"reversed"in S&&S.reversed?S.joinDef.on.map(v=>({sourceColumn:v.target.name,targetColumn:v.source.name,sourceColumnObj:v.target,targetColumnObj:v.source})):S.joinDef.on.map(v=>({sourceColumn:v.source.name,targetColumn:v.target.name,sourceColumnObj:v.source,targetColumnObj:v.target})),D=void 0}const w=this.findPropagatingFilters(s,l,e),C=new Map([[l.name,l]]),{aggregateMeasures:E,requiredBaseMeasures:T}=I.categorizeForPostAggregation(y,C),N=[...new Set([...E,...Array.from(T).filter(S=>S.startsWith(l.name+"."))])];if(N.length>0){const S=this.expandCalculatedMeasureDependencies(l,N),_=this.findDownstreamJoinKeys(l,s,e);r.push({cube:l,alias:m,cteAlias:`${l.name.toLowerCase()}_agg`,joinKeys:$,measures:S,propagatingFilters:w.length>0?w:void 0,downstreamJoinKeys:_.length>0?_:void 0,intermediateJoins:D&&D.length>0?D:void 0,cteType:"aggregate",cteReason:f})}}return r}findJoinInfoToCube(e,t){for(const[,n]of e)if(n.name!==t&&n.joins){for(const[,s]of Object.entries(n.joins))if(K(s.targetCube).name===t)return{sourceCube:n,joinDef:s}}return null}analyzeJoinPathToPrimary(e,t,n,s){const r=this.getResolver(e).findPath(t.name,n);if(!r||r.length===0)return null;const a=r.map(f=>({fromCube:f.fromCube,toCube:f.toCube,joinDef:f.joinDef}));if(!a.slice(0,-1).some(f=>f.joinDef.relationship==="hasMany"))return{path:a,hasIntermediateHasMany:!1,intermediateJoins:[],correctJoinKeys:[]};const l=[];for(let f=0;f<a.length-1;f++){const h=a[f],g=a[f+1],y=e.get(h.toCube);if(!y)continue;const $=y.sql(s).where,D=g.joinDef.on[0]?.source,w=h.joinDef.on[0]?.target;l.push({cube:y,joinDef:g.joinDef,securityFilter:$,primaryJoinColumn:w,cteJoinColumn:D})}const p=a[0].joinDef.on.map(f=>({sourceColumn:f.source.name,targetColumn:f.target.name,sourceColumnObj:f.source,targetColumnObj:f.target}));return{path:a,hasIntermediateHasMany:!0,intermediateJoins:l,correctJoinKeys:p}}detectHasManyInQuery(e,t,n){const s=[],i=new Set,r=new Set;r.add(t.name);for(const a of n)r.add(a.cube.name);for(const[a,u]of e)if(!i.has(a)&&(i.add(a),!!u.joins)){for(const[,c]of Object.entries(u.joins))if(c.relationship==="hasMany"){const l=K(c.targetCube);(r.has(a)||r.has(l.name))&&s.push({fromCube:a,toCube:l.name,joinDef:c})}}return s}getCTEReason(e,t,n,s){if(n.some(a=>a.toCube===e.name)&&s.measures?.some(u=>u.startsWith(e.name+".")))return"hasMany";if(!s.measures?.some(a=>a.startsWith(e.name+".")))return null;for(const a of n)if(a.fromCube!==e.name&&a.toCube!==e.name)return"fanOutPrevention";return null}findJoinInfoForCube(e,t,n){if(t.joins){for(const[,i]of Object.entries(t.joins))if(K(i.targetCube).name===n)return{sourceCube:t,joinDef:i}}const s=e.get(n);if(s?.joins){for(const[,i]of Object.entries(s.joins))if(K(i.targetCube).name===t.name)return{sourceCube:s,joinDef:i,reversed:!0}}for(const[,i]of e)if(!(i.name===t.name||i.name===n)&&i.joins){for(const[,r]of Object.entries(i.joins))if(K(r.targetCube).name===n)return{sourceCube:i,joinDef:r}}return null}findDownstreamJoinKeys(e,t,n){const s=[],i=new Set;if(t.dimensions)for(const r of t.dimensions){const[a]=r.split(".");a!==e.name&&i.add(a)}if(t.timeDimensions)for(const r of t.timeDimensions){const[a]=r.dimension.split(".");a!==e.name&&i.add(a)}if(e.joins)for(const[,r]of Object.entries(e.joins)){const u=K(r.targetCube).name;if(i.has(u)){let c;r.relationship==="belongsToMany"&&r.through?c=r.through.sourceKey.map(l=>({sourceColumn:l.source.name,targetColumn:l.target.name,sourceColumnObj:l.source,targetColumnObj:l.target})):c=r.on.map(l=>({sourceColumn:l.source.name,targetColumn:l.target.name,sourceColumnObj:l.source,targetColumnObj:l.target})),s.push({targetCubeName:u,joinKeys:c})}}return s}expandCalculatedMeasureDependencies(e,t){const n=new Set,s=[...t];for(;s.length>0;){const i=s.pop();if(n.has(i))continue;n.add(i);const[,r]=i.split(".");if(!e.measures||!e.measures[r])continue;const a=e.measures[r];if(a.type==="calculated"&&a.calculatedSql){const u=this.extractDependenciesFromTemplate(a.calculatedSql,e.name);for(const c of u)n.has(c)||s.push(c)}}return Array.from(n)}extractDependenciesFromTemplate(e,t){const n=/\{([^}]+)\}/g,s=e.matchAll(n),i=[];for(const r of s){const a=r[1].trim();a.includes(".")?i.push(a):i.push(`${t}.${a}`)}return i}findHasManyJoinDef(e,t){if(!e.joins)return null;for(const[,n]of Object.entries(e.joins))if(K(n.targetCube).name===t&&n.relationship==="hasMany")return n;return null}findPropagatingFilters(e,t,n){const s=[];if(!e.filters)return s;const i=new Set;if(this.extractFilterCubeNamesToSet(e.filters,i),e.timeDimensions){for(const r of e.timeDimensions)if(r.dateRange){const[a]=r.dimension.split(".");a&&i.add(a)}}for(const r of i){if(r===t.name)continue;const a=n.get(r);if(a?.joins){for(const[,u]of Object.entries(a.joins))if(K(u.targetCube).name===t.name&&u.relationship==="hasMany"){const l=this.extractFiltersForCube(e.filters,r),m=this.extractTimeDimensionFiltersForCube(e,r),p=[...l,...m];p.length>0&&u.on.length>0&&s.push({sourceCube:a,filters:p,joinConditions:u.on.map(f=>({source:f.source,target:f.target}))})}}}return s}extractFilterCubeNamesToSet(e,t){for(const n of e){if("and"in n||"or"in n){const s=n.and||n.or||[];this.extractFilterCubeNamesToSet(s,t);continue}if("member"in n){const[s]=n.member.split(".");s&&t.add(s)}}}extractFiltersForCube(e,t){const n=[];for(const s of e){if("and"in s){const i=this.extractFiltersForCube(s.and||[],t);i.length>0&&n.push({and:i});continue}if("or"in s){const i=s.or||[];if(this.allFiltersFromCube(i,t)){const a=this.extractFiltersForCube(i,t);a.length>0&&n.push({or:a})}continue}if("member"in s){const[i]=s.member.split(".");i===t&&n.push(s)}}return n}allFiltersFromCube(e,t){for(const n of e){if("and"in n){if(!this.allFiltersFromCube(n.and||[],t))return!1;continue}if("or"in n){if(!this.allFiltersFromCube(n.or||[],t))return!1;continue}if("member"in n){const[s]=n.member.split(".");if(s!==t)return!1}}return!0}extractTimeDimensionFiltersForCube(e,t){const n=[];if(!e.timeDimensions)return n;for(const s of e.timeDimensions){const[i]=s.dimension.split(".");i===t&&s.dateRange&&n.push({member:s.dimension,operator:"inDateRange",values:Array.isArray(s.dateRange)?s.dateRange:[s.dateRange]})}return n}analyzeQueryPlan(e,t,n){const s=this.analyzeCubeUsage(t),i=Array.from(s);if(i.length===0)return{timestamp:new Date().toISOString(),cubeCount:0,cubesInvolved:[],primaryCube:{selectedCube:"",reason:"single_cube",explanation:"No cubes found in query"},joinPaths:[],preAggregations:[],querySummary:{queryType:"single_cube",joinCount:0,cteCount:0,hasPreAggregation:!1},warnings:["No cubes found in query - add measures or dimensions"]};const r=this.analyzePrimaryCubeSelection(i,t,e),a=r.selectedCube,u={timestamp:new Date().toISOString(),cubeCount:i.length,cubesInvolved:i.sort(),primaryCube:r,joinPaths:[],preAggregations:[],querySummary:{queryType:"single_cube",joinCount:0,cteCount:0,hasPreAggregation:!1},warnings:[]};if(i.length>1){const c=i.filter(g=>g!==a);for(const g of c)u.joinPaths.push(this.analyzeJoinPath(e,a,g));const l=e.get(a);l&&(u.preAggregations=this.analyzePreAggregations(e,l,c,t));const m=u.joinPaths.filter(g=>g.pathFound),p=u.joinPaths.filter(g=>!g.pathFound);u.querySummary.joinCount=m.length,u.querySummary.cteCount=u.preAggregations.length,u.querySummary.hasPreAggregation=u.preAggregations.length>0;const f=new Map;for(const g of i){const y=e.get(g);y&&f.set(g,y)}const h=I.hasPostAggregationWindows(t.measures||[],f);u.querySummary.hasWindowFunctions=h,u.preAggregations.length>0?u.querySummary.queryType="multi_cube_cte":u.querySummary.queryType="multi_cube_join";for(const g of p)u.warnings.push(`No join path found to cube '${g.targetCube}'. Check that joins are defined correctly.`);h&&u.warnings.push("Query contains post-aggregation window functions which will be applied to aggregated results.")}return u}analyzePrimaryCubeSelection(e,t,n){if(e.length===1)return{selectedCube:e[0],reason:"single_cube",explanation:"Only one cube is used in this query"};const s=[],i=(t.dimensions||[]).map(l=>l.split(".")[0]),r=new Map;for(const l of i)r.set(l,(r.get(l)||0)+1);const a=this.getResolver(n);for(const l of e){const m=n.get(l),p=r.get(l)||0,f=m?.joins?Object.keys(m.joins).length:0,h=a.canReachAll(l,e);s.push({cubeName:l,dimensionCount:p,joinCount:f,canReachAll:h})}if(t.dimensions&&t.dimensions.length>0){const l=Math.max(...s.map(m=>m.dimensionCount));if(l>0){const m=s.filter(p=>p.dimensionCount===l).sort((p,f)=>p.cubeName.localeCompare(f.cubeName));for(const p of m)if(p.canReachAll)return{selectedCube:p.cubeName,reason:"most_dimensions",explanation:`Selected because it has ${p.dimensionCount} dimension${p.dimensionCount!==1?"s":""} in the query (defines the analytical grain)`,candidates:s}}}const u=s.filter(l=>l.canReachAll);if(u.length>0){const l=Math.max(...u.map(p=>p.joinCount)),m=u.filter(p=>p.joinCount===l).sort((p,f)=>p.cubeName.localeCompare(f.cubeName))[0];return{selectedCube:m.cubeName,reason:"most_connected",explanation:`Selected because it has ${m.joinCount} join relationship${m.joinCount!==1?"s":""} and can reach all other cubes`,candidates:s}}return{selectedCube:[...e].sort()[0],reason:"alphabetical_fallback",explanation:"Selected alphabetically as fallback (no cube could reach all others)",candidates:s}}analyzeJoinPath(e,t,n){const i=this.getResolver(e).findPath(t,n),r=[t];if(i)for(const u of i)r.push(u.toCube);if(!i||i.length===0)return{targetCube:n,pathFound:!1,error:`No join path found from '${t}' to '${n}'. Ensure the target cube has a relationship defined (belongsTo, hasOne, hasMany, or belongsToMany).`,visitedCubes:r};const a=i.map(u=>{const c=_e(u.joinDef.relationship,u.joinDef.sqlJoinType),l=u.joinDef.on.map(p=>({sourceColumn:p.source.name,targetColumn:p.target.name})),m={fromCube:u.fromCube,toCube:u.toCube,relationship:u.joinDef.relationship,joinType:c,joinColumns:l};if(u.joinDef.relationship==="belongsToMany"&&u.joinDef.through){const p=u.joinDef.through;m.junctionTable={tableName:p.table[Symbol.for("drizzle:Name")]||"junction_table",sourceColumns:p.sourceKey.map(f=>f.target.name),targetColumns:p.targetKey.map(f=>f.source.name)}}return m});return{targetCube:n,pathFound:!0,path:a,pathLength:a.length,visitedCubes:r}}analyzePreAggregations(e,t,n,s){const i=[];if(!s.measures||s.measures.length===0)return i;for(const r of n){const a=this.findHasManyJoinDef(t,r);if(!a)continue;const u=e.get(r);if(!u)continue;const c=s.measures.filter($=>$.startsWith(r+".")),l=this.extractMeasuresFromFilters(s,u),m=[...new Set([...c,...l])];if(m.length===0)continue;const p=a.on.map($=>({sourceColumn:$.source.name,targetColumn:$.target.name})),f=new Map([[r,u]]),{aggregateMeasures:h,postAggWindowMeasures:g,requiredBaseMeasures:y}=I.categorizeForPostAggregation(m,f),b=[...new Set([...h,...Array.from(y).filter($=>$.startsWith(r+"."))])];if(b.length>0){const $=g.length>0;i.push({cubeName:r,cteAlias:`${r.toLowerCase()}_agg`,reason:$?`hasMany relationship from ${t.name} - requires pre-aggregation; includes base measures for post-aggregation window functions`:`hasMany relationship from ${t.name} - requires pre-aggregation to prevent row duplication (fan-out)`,reasonType:"hasMany",measures:b,joinKeys:p,cteType:"aggregate"})}}return i}generateWarnings(e,t){const n=[],s=this.checkFanOutNoDimensions(e,t);return s&&n.push(s),n}checkFanOutNoDimensions(e,t){if(!t||t.length===0||!e.measures||e.measures.length===0)return null;const n=new Set;for(const r of e.measures){const[a]=r.split(".");n.add(a)}if(n.size<2)return null;const s=e.dimensions&&e.dimensions.length>0,i=e.timeDimensions?.some(r=>r.granularity);return s||i?null:{code:"FAN_OUT_NO_DIMENSIONS",message:"Query combines measures from multiple cubes with hasMany relationships but has no dimensions. Results are aggregated at the join key level, which may produce unexpected totals.",severity:"warning",cubes:[...n].sort(),measures:e.measures,suggestion:"Add a dimension to see per-group breakdowns, or add a time dimension with granularity."}}}class an{constructor(e){this.queryBuilder=e}buildPreAggregationCTE(e,t,n,s,i){const r=e.cube,a=r.sql(n),u=e.intermediateJoins&&e.intermediateJoins.length>0,c={};if(u&&e.intermediateJoins){const E=e.intermediateJoins[0].primaryJoinColumn;if(E){const T=E.name;c[T]=E}}else for(const C of e.joinKeys)if(C.targetColumnObj){c[C.targetColumn]=C.targetColumnObj;for(const[E,T]of Object.entries(r.dimensions||{}))T.sql===C.targetColumnObj&&E!==C.targetColumn&&(c[E]=o`${C.targetColumnObj}`.as(E))}if(e.downstreamJoinKeys)for(const C of e.downstreamJoinKeys)for(const E of C.joinKeys)E.sourceColumnObj&&(c[E.sourceColumn]=E.sourceColumnObj);const l=r.name,m=new Map([[l,r]]),p=this.queryBuilder.buildResolvedMeasures(e.measures,m,n);for(const C of e.measures){const[,E]=C.split("."),T=p.get(C);if(T){const N=T();c[E]=o`${N}`.as(E)}}if(t.dimensions)for(const C of t.dimensions){const[E,T]=C.split(".");if(E===l&&r.dimensions&&r.dimensions[T]){const N=r.dimensions[T],S=this.queryBuilder.buildMeasureExpression({sql:N.sql,type:"number"},n);c[T]=o`${S}`.as(T)}}if(t.timeDimensions)for(const C of t.timeDimensions){const[E,T]=C.dimension.split(".");if(E===l&&r.dimensions&&r.dimensions[T]){const N=r.dimensions[T],S=this.queryBuilder.buildTimeDimensionExpression(N.sql,C.granularity,n);c[T]=o`${S}`.as(T)}}if(Object.keys(c).length===0)return null;let f=n.db.select(c).from(a.from);if(u&&e.intermediateJoins)for(const C of e.intermediateJoins){const E=C.cube.sql(n),N=[W(C.cteJoinColumn,C.joinDef.on[0]?.target)];C.securityFilter&&N.push(C.securityFilter),f=f.leftJoin(E.from,A(...N))}const h=s?{...s,preAggregationCTEs:s.preAggregationCTEs?.filter(C=>C.cube.name!==r.name)}:void 0,g=this.queryBuilder.buildWhereConditions(r,t,n,h,i),y=[];if(t.timeDimensions)for(const C of t.timeDimensions){const[E,T]=C.dimension.split(".");if(E===l&&r.dimensions&&r.dimensions[T]&&C.dateRange){const N=r.dimensions[T],S=this.queryBuilder.buildMeasureExpression({sql:N.sql,type:"number"},n),_=this.queryBuilder.buildDateRangeCondition(S,C.dateRange);_&&y.push(_)}}if(t.filters){for(const C of t.filters)if(!("and"in C)&&!("or"in C)&&"member"in C&&"operator"in C){const E=C,[T,N]=E.member.split(".");if(T===l&&r.dimensions&&r.dimensions[N]){const S=r.dimensions[N];if(E.operator==="inDateRange"){const _=this.queryBuilder.buildMeasureExpression({sql:S.sql,type:"number"},n),v=this.queryBuilder.buildDateRangeCondition(_,E.values);v&&y.push(v)}}}}if(e.propagatingFilters&&e.propagatingFilters.length>0)for(const C of e.propagatingFilters){const E=this.buildPropagatingFilterSubquery(C,n);E&&y.push(E)}const b=[];if(a.where&&b.push(a.where),b.push(...g,...y),b.length>0){const C=b.length===1?b[0]:A(...b);f=f.where(C)}const $=[],D=new Set,w=C=>{const E=C?.name||(typeof C=="string"?C:null);E&&!D.has(E)?(D.add(E),$.push(C)):E||$.push(C)};if(u&&e.intermediateJoins){const C=e.intermediateJoins[0];C.primaryJoinColumn&&w(C.primaryJoinColumn)}else for(const C of e.joinKeys)C.targetColumnObj&&w(C.targetColumnObj);if(e.downstreamJoinKeys)for(const C of e.downstreamJoinKeys)for(const E of C.joinKeys)E.sourceColumnObj&&w(E.sourceColumnObj);if(t.dimensions)for(const C of t.dimensions){const[E,T]=C.split(".");if(E===l&&r.dimensions&&r.dimensions[T]){const N=r.dimensions[T],S=R(N.sql,n);$.push(S)}}if(t.timeDimensions)for(const C of t.timeDimensions){const[E,T]=C.dimension.split(".");if(E===l&&r.dimensions&&r.dimensions[T]){const N=r.dimensions[T],S=this.queryBuilder.buildTimeDimensionExpression(N.sql,C.granularity,n);$.push(S)}}return $.length>0&&(f=f.groupBy(...$)),n.db.$with(e.cteAlias).as(f)}buildCTEJoinCondition(e,t,n){const s=n.preAggregationCTEs?.find(r=>r.cube.name===e.cube.name);if(!s)throw new Error(`CTE info not found for cube ${e.cube.name}`);const i=[];if(s.intermediateJoins&&s.intermediateJoins.length>0){const r=s.intermediateJoins[0],a=s.joinKeys[0]?.sourceColumnObj,u=o`${o.identifier(t)}.${o.identifier(r.primaryJoinColumn.name)}`;i.push(W(a,u))}else for(const r of s.joinKeys){const a=r.sourceColumnObj||o.identifier(r.sourceColumn),u=o`${o.identifier(t)}.${o.identifier(r.targetColumn)}`;i.push(W(a,u))}return i.length===1?i[0]:A(...i)}buildPropagatingFilterSubquery(e,t){const n=e.sourceCube,s=n.sql(t),i=[];if(s.where&&i.push(s.where),e.preBuiltFilterSQL)i.push(e.preBuiltFilterSQL);else{const u={filters:e.filters},c=new Map([[n.name,n]]),l=this.queryBuilder.buildWhereConditions(c,u,t);i.push(...l)}if(i.length===0)return null;const r=i.length===1?i[0]:A(...i),a=e.joinConditions;if(a.length===1){const{source:u,target:c}=a[0],l=t.db.select({pk:u}).from(s.from).where(r);return o`${c} IN ${l}`}else{const u=a.map(m=>W(m.source,m.target)),c=A(...u,r),l=t.db.select({one:o`1`}).from(s.from).where(c);return o`EXISTS ${l}`}}}function un(d,e,t){const n=[];let s=Ge(new Date(d),t);const i=Ge(new Date(e),t),r=1e4;for(;s<=i&&n.length<r;)n.push(new Date(s)),s=ln(s,t);return n}function Ge(d,e){const t=new Date(d);switch(e){case"second":t.setUTCMilliseconds(0);break;case"minute":t.setUTCSeconds(0,0);break;case"hour":t.setUTCMinutes(0,0,0);break;case"day":t.setUTCHours(0,0,0,0);break;case"week":{const n=t.getUTCDay(),s=n===0?6:n-1;t.setUTCDate(t.getUTCDate()-s),t.setUTCHours(0,0,0,0);break}case"month":t.setUTCDate(1),t.setUTCHours(0,0,0,0);break;case"quarter":{const n=Math.floor(t.getUTCMonth()/3)*3;t.setUTCMonth(n,1),t.setUTCHours(0,0,0,0);break}case"year":t.setUTCMonth(0,1),t.setUTCHours(0,0,0,0);break}return t}function ln(d,e){const t=new Date(d);switch(e){case"second":t.setUTCSeconds(t.getUTCSeconds()+1);break;case"minute":t.setUTCMinutes(t.getUTCMinutes()+1);break;case"hour":t.setUTCHours(t.getUTCHours()+1);break;case"day":t.setUTCDate(t.getUTCDate()+1);break;case"week":t.setUTCDate(t.getUTCDate()+7);break;case"month":t.setUTCMonth(t.getUTCMonth()+1);break;case"quarter":t.setUTCMonth(t.getUTCMonth()+3);break;case"year":t.setUTCFullYear(t.getUTCFullYear()+1);break}return t}function cn(d){if(d instanceof Date)return d.toISOString();if(typeof d=="string"){const e=new Date(d);if(!isNaN(e.getTime()))return e.toISOString()}return String(d)}function dn(d,e){return e.length===0?"__all__":e.map(t=>String(d[t]??"")).join("|||")}function mn(d,e){const{timeDimensionKey:t,granularity:n,dateRange:s,fillValue:i,measures:r,dimensions:a}=e,u=un(s[0],s[1],n);if(u.length===0)return d;const c=new Map;for(const m of d){const p=dn(m,a),f=cn(m[t]);c.has(p)||c.set(p,new Map),c.get(p).set(f,m)}c.size===0&&a.length===0&&c.set("__all__",new Map);const l=[];for(const[m,p]of c){const f=p.size>0?p.values().next().value:null;for(const h of u){const g=h.toISOString(),y=p.get(g);if(y)l.push(y);else{const b={[t]:g};if(f)for(const $ of a)b[$]=f[$];for(const $ of r)b[$]=i;l.push(b)}}}return l}function fn(d){if(!d)return null;if(Array.isArray(d)){if(d.length<2)return null;const t=new Date(d[0]),n=new Date(d[1]);return isNaN(t.getTime())||isNaN(n.getTime())?null:[t,n]}const e=new Date(d);return isNaN(e.getTime())?null:[e,e]}function He(d,e,t){if(!e.timeDimensions||e.timeDimensions.length===0)return d;const n=e.timeDimensions.filter(u=>{const c=u.fillMissingDates!==!1,l=u.granularity&&u.dateRange;return c&&l});if(n.length===0)return d;const s=e.fillMissingDatesValue===void 0?0:e.fillMissingDatesValue,i=new Set(e.timeDimensions.map(u=>u.dimension)),r=(e.dimensions||[]).filter(u=>!i.has(u));let a=d;for(const u of n){const c=fn(u.dateRange);if(!c)continue;const l={timeDimensionKey:u.dimension,granularity:u.granularity,dateRange:c,fillValue:s,measures:t,dimensions:r};a=mn(a,l)}return a}class pn{dateTimeBuilder;constructor(e){this.dateTimeBuilder=new ie(e)}hasComparison(e){return e.timeDimensions?.some(t=>t.compareDateRange&&t.compareDateRange.length>=2)??!1}getComparisonTimeDimension(e){return e.timeDimensions?.find(t=>t.compareDateRange&&t.compareDateRange.length>=2)}normalizePeriods(e){const t=[];for(let n=0;n<e.length;n++){const s=e[n];let i,r,a;if(typeof s=="string"){const u=this.dateTimeBuilder.parseRelativeDateRange(s);if(u)i=u.start,r=u.end,a=s;else{const c=new Date(s);if(!isNaN(c.getTime()))i=new Date(c),i.setUTCHours(0,0,0,0),r=new Date(c),r.setUTCHours(23,59,59,999),a=s;else continue}}else{if(i=new Date(s[0]),r=new Date(s[1]),isNaN(i.getTime())||isNaN(r.getTime()))continue;/^\d{4}-\d{2}-\d{2}$/.test(s[1])&&r.setUTCHours(23,59,59,999),a=`${s[0]} - ${s[1]}`}t.push({start:i,end:r,label:a,index:n})}return t}createPeriodQuery(e,t){return{...e,timeDimensions:e.timeDimensions?.map(n=>n.compareDateRange?{...n,dateRange:[t.start.toISOString(),t.end.toISOString()],compareDateRange:void 0}:n)}}calculatePeriodDayIndex(e,t,n){const s=typeof e=="string"?new Date(e):e,i=t.getTime(),r=s.getTime();switch(n){case"second":return Math.floor((r-i)/1e3);case"minute":return Math.floor((r-i)/(1e3*60));case"hour":return Math.floor((r-i)/(1e3*60*60));case"day":return Math.floor((r-i)/(1e3*60*60*24));case"week":return Math.floor((r-i)/(1e3*60*60*24*7));case"month":{const a=t.getUTCFullYear(),u=t.getUTCMonth(),c=s.getUTCFullYear(),l=s.getUTCMonth();return(c-a)*12+(l-u)}case"quarter":{const a=t.getUTCFullYear(),u=Math.floor(t.getUTCMonth()/3),c=s.getUTCFullYear(),l=Math.floor(s.getUTCMonth()/3);return(c-a)*4+(l-u)}case"year":return s.getUTCFullYear()-t.getUTCFullYear();default:return Math.floor((r-i)/(1e3*60*60*24))}}addPeriodMetadata(e,t,n,s){return e.map(i=>{const r=i[n];let a=0;if(r){const u=typeof r=="string"?new Date(r):r instanceof Date?r:null;u&&!isNaN(u.getTime())&&(a=this.calculatePeriodDayIndex(u,t.start,s))}return{...i,__period:t.label,__periodIndex:t.index,__periodDayIndex:a}})}mergeComparisonResults(e,t,n){const s=[];let i={measures:{},dimensions:{},segments:{},timeDimensions:{}};const r=e.map(u=>u.period);for(const{result:u,period:c}of e){const l=this.addPeriodMetadata(u.data,c,t.dimension,n);s.push(...l),i={measures:{...i.measures,...u.annotation.measures},dimensions:{...i.dimensions,...u.annotation.dimensions},segments:{...i.segments,...u.annotation.segments},timeDimensions:{...i.timeDimensions,...u.annotation.timeDimensions}}}const a={ranges:r.map(u=>[u.start.toISOString().split("T")[0],u.end.toISOString().split("T")[0]]),labels:r.map(u=>u.label),timeDimension:t.dimension,granularity:n};return{data:s,annotation:{...i,periods:a}}}sortComparisonResults(e,t){return[...e].sort((n,s)=>{const i=n.__periodIndex-s.__periodIndex;if(i!==0)return i;const r=n[t],a=s[t];return typeof r=="string"&&typeof a=="string"?new Date(r).getTime()-new Date(a).getTime():0})}}class hn{constructor(e){this.databaseAdapter=e,this.dateTimeBuilder=new ie(e),this.filterBuilder=new ge(e,this.dateTimeBuilder)}filterBuilder;dateTimeBuilder;hasFunnel(e){return e.funnel!==void 0&&e.funnel.steps.length>=2}validateConfig(e,t){const n=[];if(e.steps.length<2&&n.push("Funnel must have at least 2 steps"),typeof e.bindingKey=="string"){const[s,i]=e.bindingKey.split(".");if(!s||!i)n.push(`Invalid binding key format: ${e.bindingKey}. Expected 'CubeName.dimensionName'`);else{const r=t.get(s);r?r.dimensions?.[i]||n.push(`Binding key dimension not found: ${i} in cube ${s}`):n.push(`Binding key cube not found: ${s}`)}}else if(Array.isArray(e.bindingKey))for(const s of e.bindingKey){const i=t.get(s.cube);if(!i)n.push(`Binding key mapping cube not found: ${s.cube}`);else{const[,r]=s.dimension.split(".");i.dimensions?.[r]||n.push(`Binding key dimension not found: ${r} in cube ${s.cube}`)}}if(typeof e.timeDimension=="string"){const[s,i]=e.timeDimension.split(".");if(!s||!i)n.push(`Invalid time dimension format: ${e.timeDimension}. Expected 'CubeName.dimensionName'`);else{const r=t.get(s);r?r.dimensions?.[i]||n.push(`Time dimension not found: ${i} in cube ${s}`):n.push(`Time dimension cube not found: ${s}`)}}for(let s=0;s<e.steps.length;s++){const i=e.steps[s];if(i.name||n.push(`Step ${s} must have a name`),"cube"in i&&i.cube&&(t.get(i.cube)||n.push(`Step ${s} cube not found: ${i.cube}`)),i.filter){let r;"cube"in i&&i.cube?r=i.cube:typeof e.bindingKey=="string"&&([r]=e.bindingKey.split("."));const a=r?new ae(t):null,u=Array.isArray(i.filter)?i.filter:[i.filter];for(const c of u)if("member"in c){const[l,m]=c.member.split("."),p=t.get(l);if(!p)n.push(`Step ${s} filter cube not found: ${l}`);else if(p.dimensions?.[m]||(p.measures?.[m]?n.push(`Step ${s} filter '${l}.${m}' is a measure. Funnel step filters only support dimensions, not measures.`):n.push(`Step ${s} filter member not found: ${m} in cube ${l}`)),r&&l!==r&&a){const f=a.findPath(r,l);(!f||f.length===0)&&n.push(`Step ${s} filter '${l}.${m}' requires a join from '${r}' but no join path was found. Define a join relationship between these cubes.`)}}}i.timeToConvert&&s>0&&(/^P(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/.test(i.timeToConvert)||n.push(`Step ${s} timeToConvert must be ISO 8601 duration format: ${i.timeToConvert}`))}return{isValid:n.length===0,errors:n}}buildFunnelQuery(e,t,n){const s=this.resolveSteps(e,t,n),i=[];for(let c=0;c<s.length;c++){const l=c>0?i[c-1]:void 0;i.push(this.buildStepCTE(s[c],n,l))}const r=this.buildFunnelResultsCTE(i,s,e,n),a=this.buildAggregationCTE(r,i,s,e,n),u=[...i,r,a];return n.db.with(...u).select().from(a)}transformResult(e,t){if(!e||e.length===0)return[];const n=e[0],s=[],i=Number(n.step_0_count)||0;for(let r=0;r<t.steps.length;r++){const a=t.steps[r],u=Number(n[`step_${r}_count`])||0,c=r>0&&Number(n[`step_${r-1}_count`])||0,l={step:a.name,stepIndex:r,count:u,conversionRate:r===0?null:c>0?u/c:0,cumulativeConversionRate:i>0?u/i:0};t.includeTimeMetrics&&r>0&&(l.avgSecondsToConvert=n[`step_${r}_avg_seconds`]!==null?Number(n[`step_${r}_avg_seconds`]):null,l.minSecondsToConvert=n[`step_${r}_min_seconds`]!==null?Number(n[`step_${r}_min_seconds`]):null,l.maxSecondsToConvert=n[`step_${r}_max_seconds`]!==null?Number(n[`step_${r}_max_seconds`]):null,n[`step_${r}_median_seconds`]!==void 0&&(l.medianSecondsToConvert=n[`step_${r}_median_seconds`]!==null?Number(n[`step_${r}_median_seconds`]):null),n[`step_${r}_p90_seconds`]!==void 0&&(l.p90SecondsToConvert=n[`step_${r}_p90_seconds`]!==null?Number(n[`step_${r}_p90_seconds`]):null)),s.push(l)}return s}extractFilterCubeNames(e){const t=new Set;if(!e.filter)return t;const n=Array.isArray(e.filter)?e.filter:[e.filter],s=i=>{if("and"in i&&i.and)for(const r of i.and)s(r);else if("or"in i&&i.or)for(const r of i.or)s(r);else if("type"in i&&"filters"in i){const r=i;for(const a of r.filters||[])s(a)}else if("member"in i){const[r]=i.member.split(".");t.add(r)}};for(const i of n)s(i);return t}resolveSteps(e,t,n){const s=new ae(t);return e.steps.map((i,r)=>{const a=this.resolveCubeForStep(i,e,t),u=this.resolveBindingKey(e,a,n),c=this.resolveTimeDimension(e,a,n),l=this.buildStepFilters(i,a,t,n),m=this.extractFilterCubeNames(i),p=[];for(const f of m)if(f!==a.name){const h=t.get(f);if(h){const g=s.findPath(a.name,f);g&&g.length>0&&p.push({cube:h,joinPath:g})}}return{name:i.name,index:r,cube:a,bindingKeyExpr:u,timeExpr:c,filterConditions:l,timeToConvert:i.timeToConvert,joinedCubes:p}})}resolveCubeForStep(e,t,n){if("cube"in e&&e.cube){const s=n.get(e.cube);if(!s)throw new Error(`Cube not found for step: ${e.cube}`);return s}if(typeof t.bindingKey=="string"){const[s]=t.bindingKey.split("."),i=n.get(s);if(!i)throw new Error(`Cube not found for binding key: ${t.bindingKey}`);return i}throw new Error("Cannot resolve cube for step - multi-cube funnel requires cube specification in each step")}resolveBindingKey(e,t,n){if(typeof e.bindingKey=="string"){const[,a]=e.bindingKey.split("."),u=t.dimensions?.[a];if(!u)throw new Error(`Binding key dimension not found: ${e.bindingKey}`);return R(u.sql,n)}const s=e.bindingKey.find(a=>a.cube===t.name);if(!s)throw new Error(`No binding key mapping found for cube: ${t.name}`);const[,i]=s.dimension.split("."),r=t.dimensions?.[i];if(!r)throw new Error(`Binding key dimension not found: ${s.dimension}`);return R(r.sql,n)}resolveTimeDimension(e,t,n){if(typeof e.timeDimension=="string"){const[,a]=e.timeDimension.split("."),u=t.dimensions?.[a];if(!u)throw new Error(`Time dimension not found: ${e.timeDimension}`);return R(u.sql,n)}const s=e.timeDimension.find(a=>a.cube===t.name);if(!s)throw new Error(`No time dimension mapping found for cube: ${t.name}`);const[,i]=s.dimension.split("."),r=t.dimensions?.[i];if(!r)throw new Error(`Time dimension not found: ${s.dimension}`);return R(r.sql,n)}buildStepFilters(e,t,n,s){if(!e.filter)return[];const i=Array.isArray(e.filter)?e.filter:[e.filter],r=[];for(const a of i){const u=this.buildFilterCondition(a,t,n,s);u&&r.push(u)}return r}buildFilterCondition(e,t,n,s){const i="and"in e||"or"in e,r="type"in e&&"filters"in e&&(e.type==="and"||e.type==="or");if(i||r){const g=[];let y;if(r){const b=e;y=b.type==="and";for(const $ of b.filters||[]){const D=this.buildFilterCondition($,t,n,s);D&&g.push(D)}}else{const b=e;y="and"in b&&!!b.and;const $=b.and||b.or||[];for(const D of $){const w=this.buildFilterCondition(D,t,n,s);w&&g.push(w)}}return g.length===0?null:g.length===1?g[0]:y?A(...g):o`(${o.join(g,o` OR `)})`}const a=e,[u,c]=a.member.split("."),l=a.dateRange!==void 0;if(a.operator!=="set"&&a.operator!=="notSet"&&!l&&(!a.values||a.values.length===0||a.values[0]===void 0||a.values[0]===""))return null;const p=n.get(u);if(!p)return null;if(u!==t.name){const y=new ae(n).findPath(t.name,u);if(!y||y.length===0)return console.warn(`Funnel filter: Cannot filter by '${u}.${c}' in step using '${t.name}'. No join path found. Filter will be skipped.`),null}const f=p.dimensions?.[c];if(!f)return null;const h=R(f.sql,s);return this.filterBuilder.buildFilterCondition(h,a.operator,a.values||[],f,a.dateRange)}buildStepCTE(e,t,n){return e.index===0?this.buildFirstStepCTE(e,t):this.buildSubsequentStepCTE(e,t,n)}buildFirstStepCTE(e,t){const n=`step_${e.index}`,s=e.cube.sql(t),i=[];s.where&&i.push(s.where),i.push(...e.filterConditions);let r=t.db.select({binding_key:o`${e.bindingKeyExpr}`.as("binding_key"),step_time:o`MIN(${e.timeExpr})`.as("step_time")}).from(s.from);if(r=this.addCrossJoinsToQuery(r,e,t,i),i.length>0){const a=i.length===1?i[0]:A(...i);r=r.where(a)}return r=r.groupBy(e.bindingKeyExpr),t.db.$with(n).as(r)}buildSubsequentStepCTE(e,t,n){const s=`step_${e.index}`,i=`step_${e.index-1}`,r=e.cube.sql(t),a=[];r.where&&a.push(r.where),a.push(...e.filterConditions);const u=o`${o.identifier(i)}.step_time`;let c=o`${e.timeExpr} > ${u}`;if(e.timeToConvert){const m=this.databaseAdapter.buildDateAddInterval(u,e.timeToConvert);c=o`${c} AND ${e.timeExpr} <= ${m}`}a.push(c);let l=t.db.select({binding_key:o`${e.bindingKeyExpr}`.as("binding_key"),step_time:o`MIN(${e.timeExpr})`.as("step_time")}).from(r.from).innerJoin(n,o`${e.bindingKeyExpr} = ${o.identifier(i)}.binding_key`);if(l=this.addCrossJoinsToQuery(l,e,t,a),a.length>0){const m=a.length===1?a[0]:A(...a);l=l.where(m)}return l=l.groupBy(e.bindingKeyExpr),t.db.$with(s).as(l)}addCrossJoinsToQuery(e,t,n,s){if(t.joinedCubes.length===0)return e;for(const i of t.joinedCubes)for(const r of i.joinPath){const a=r.joinDef,u=[];for(const p of a.on)p.as?u.push(p.as(p.source,p.target)):u.push(W(p.source,p.target));const c=u.length===1?u[0]:A(...u),m=i.cube.sql(n);e=e.leftJoin(m.from,c),m.where&&s.push(m.where)}return e}buildFunnelResultsCTE(e,t,n,s){const i={binding_key:o`s0.binding_key`,step_0_time:o`s0.step_time`};for(let l=1;l<t.length;l++)i[`step_${l}_time`]=o`s${o.raw(String(l))}.step_time`;let r=o`${o.identifier("step_0")} s0`;for(let l=1;l<t.length;l++)r=o`${r}
61
- LEFT JOIN ${o.identifier(`step_${l}`)} s${o.raw(String(l))} ON s0.binding_key = s${o.raw(String(l))}.binding_key`;const a=Object.entries(i).map(([l,m])=>o`${m} AS ${o.identifier(l)}`),u=o`SELECT ${o.join(a,o`, `)} FROM ${r}`,c={binding_key:o`binding_key`.as("binding_key"),step_0_time:o`step_0_time`.as("step_0_time")};for(let l=1;l<t.length;l++)c[`step_${l}_time`]=o`${o.identifier(`step_${l}_time`)}`.as(`step_${l}_time`);return s.db.$with("funnel_joined").as(s.db.select(c).from(o`(${u}) as _inner`))}buildAggregationCTE(e,t,n,s,i){const r={};r.step_0_count=o`COUNT(*)`.as("step_0_count");for(let u=1;u<n.length;u++)r[`step_${u}_count`]=o`COUNT(${o.identifier(`step_${u}_time`)})`.as(`step_${u}_count`);if(s.includeTimeMetrics)for(let u=1;u<n.length;u++){const c=o.identifier(`step_${u}_time`),l=o.identifier(`step_${u-1}_time`),m=this.databaseAdapter.buildTimeDifferenceSeconds(o`${c}`,o`${l}`),p=o`${c} IS NOT NULL`;r[`step_${u}_avg_seconds`]=this.databaseAdapter.buildConditionalAggregation("avg",m,p).as(`step_${u}_avg_seconds`),r[`step_${u}_min_seconds`]=this.databaseAdapter.buildConditionalAggregation("min",m,p).as(`step_${u}_min_seconds`),r[`step_${u}_max_seconds`]=this.databaseAdapter.buildConditionalAggregation("max",m,p).as(`step_${u}_max_seconds`);const f=this.databaseAdapter.getCapabilities(),h=this.databaseAdapter.buildPercentile(m,50);h&&f.supportsPercentileSubqueries&&(r[`step_${u}_median_seconds`]=o`(SELECT ${h} FROM ${o.identifier("funnel_joined")} WHERE ${c} IS NOT NULL)`.as(`step_${u}_median_seconds`));const g=this.databaseAdapter.buildPercentile(m,90);g&&f.supportsPercentileSubqueries&&(r[`step_${u}_p90_seconds`]=o`(SELECT ${g} FROM ${o.identifier("funnel_joined")} WHERE ${c} IS NOT NULL)`.as(`step_${u}_p90_seconds`))}const a=i.db.select(r).from(e);return i.db.$with("funnel_metrics").as(a)}}class gn{filterBuilder;dateTimeBuilder;databaseAdapter;constructor(e){this.databaseAdapter=e,this.dateTimeBuilder=new ie(e),this.filterBuilder=new ge(e,this.dateTimeBuilder)}hasFlow(e){return e.flow!==void 0&&e.flow.startingStep!==void 0&&e.flow.eventDimension!==void 0}validateConfig(e,t){const n=[],s=[],i=this.databaseAdapter.getEngineType(),r=this.databaseAdapter.supportsLateralJoins();if(i==="sqlite")return n.push("Flow queries are not supported on SQLite. Use PostgreSQL or MySQL for flow analysis."),{isValid:!1,errors:n,warnings:s};if(typeof e.bindingKey=="string"){const[a,u]=e.bindingKey.split(".");if(!a||!u)n.push(`Invalid binding key format: ${e.bindingKey}. Expected 'CubeName.dimensionName'`);else{const c=t.get(a);c?c.dimensions?.[u]||n.push(`Binding key dimension not found: ${u} in cube ${a}`):n.push(`Binding key cube not found: ${a}`)}}else if(Array.isArray(e.bindingKey))for(const a of e.bindingKey){const u=t.get(a.cube);if(!u)n.push(`Binding key mapping cube not found: ${a.cube}`);else{const[,c]=a.dimension.split(".");u.dimensions?.[c]||n.push(`Binding key dimension not found: ${c} in cube ${a.cube}`)}}if(typeof e.timeDimension=="string"){const[a,u]=e.timeDimension.split(".");if(!a||!u)n.push(`Invalid time dimension format: ${e.timeDimension}. Expected 'CubeName.dimensionName'`);else{const c=t.get(a);c?c.dimensions?.[u]||n.push(`Time dimension not found: ${u} in cube ${a}`):n.push(`Time dimension cube not found: ${a}`)}}if(e.eventDimension){const[a,u]=e.eventDimension.split(".");if(!a||!u)n.push(`Invalid event dimension format: ${e.eventDimension}. Expected 'CubeName.dimensionName'`);else{const c=t.get(a);c?c.dimensions?.[u]||n.push(`Event dimension not found: ${u} in cube ${a}`):n.push(`Event dimension cube not found: ${a}`)}}else n.push("Event dimension is required for flow analysis");return e.startingStep?(e.startingStep.filter||n.push("Starting step must have at least one filter"),e.startingStep.name||s.push("Starting step has no name - using default")):n.push("Starting step is required for flow analysis"),(e.stepsBefore<0||e.stepsBefore>5)&&n.push(`stepsBefore must be between 0 and 5, got: ${e.stepsBefore}`),(e.stepsAfter<0||e.stepsAfter>5)&&n.push(`stepsAfter must be between 0 and 5, got: ${e.stepsAfter}`),(e.stepsBefore>=4||e.stepsAfter>=4)&&s.push("High step depth (4-5) may impact query performance on large datasets"),e.joinStrategy&&!["auto","lateral","window"].includes(e.joinStrategy)?n.push(`Invalid joinStrategy: ${e.joinStrategy}`):e.joinStrategy==="lateral"&&!r&&n.push("Lateral joins are not supported on this database"),{isValid:n.length===0,errors:n,warnings:s}}buildFlowQuery(e,t,n){if(this.databaseAdapter.getEngineType()==="sqlite")throw new Error("Flow queries are not supported on SQLite. Use PostgreSQL or MySQL for flow analysis.");const i=this.databaseAdapter.supportsLateralJoins(),r=e.joinStrategy??"auto",a=r==="lateral"||r==="auto"&&i;if(r==="lateral"&&!i)throw new Error("Lateral joins are not supported on this database");const u={...e,stepsBefore:e.outputMode==="sunburst"?0:e.stepsBefore},c=this.resolveFlowConfig(u,t,n),l=[],m=this.buildStartingEntitiesCTE(u,c,n);l.push(m);const p=a?this.buildBeforeCTEsLateral(u,c,n):this.buildBeforeCTEsWindow(u,c,n);l.push(...p);const f=a?this.buildAfterCTEsLateral(u,c,n):this.buildAfterCTEsWindow(u,c,n);l.push(...f);const h=this.buildNodesAggregationCTE(u,n);l.push(h);const g=this.buildLinksAggregationCTE(u,n);l.push(g);const y=this.buildFinalResultCTE(n);return l.push(y),n.db.with(...l).select().from(y)}transformResult(e){if(!e||e.length===0)return{nodes:[],links:[]};const t=[],n=[];for(const s of e){const i=s.record_type;i==="node"?t.push({id:String(s.id),name:String(s.name),layer:Number(s.layer),value:Number(s.value)}):i==="link"&&n.push({source:String(s.source_id),target:String(s.target_id),value:Number(s.value)})}return t.sort((s,i)=>s.layer-i.layer),{nodes:t,links:n}}resolveFlowConfig(e,t,n){const s=this.resolveCube(e,t),i=s.sql(n);return{cube:s,cubeBase:i,bindingKeyExpr:this.resolveBindingKey(e,s,n),timeExpr:this.resolveTimeDimension(e,s,n),eventExpr:this.resolveEventDimension(e,s,n),startingStepFilters:this.buildStartingStepFilters(e,s,n)}}resolveCube(e,t){let n;if(typeof e.bindingKey=="string")[n]=e.bindingKey.split(".");else if(Array.isArray(e.bindingKey)&&e.bindingKey.length>0)n=e.bindingKey[0].cube;else throw new Error("Cannot resolve cube for flow query");const s=t.get(n);if(!s)throw new Error(`Cube not found: ${n}`);return s}resolveBindingKey(e,t,n){if(typeof e.bindingKey=="string"){const[,a]=e.bindingKey.split("."),u=t.dimensions?.[a];if(!u)throw new Error(`Binding key dimension not found: ${e.bindingKey}`);return R(u.sql,n)}const s=e.bindingKey.find(a=>a.cube===t.name);if(!s)throw new Error(`No binding key mapping found for cube: ${t.name}`);const[,i]=s.dimension.split("."),r=t.dimensions?.[i];if(!r)throw new Error(`Binding key dimension not found: ${s.dimension}`);return R(r.sql,n)}resolveTimeDimension(e,t,n){if(typeof e.timeDimension=="string"){const[,a]=e.timeDimension.split("."),u=t.dimensions?.[a];if(!u)throw new Error(`Time dimension not found: ${e.timeDimension}`);return R(u.sql,n)}const s=e.timeDimension.find(a=>a.cube===t.name);if(!s)throw new Error(`No time dimension mapping found for cube: ${t.name}`);const[,i]=s.dimension.split("."),r=t.dimensions?.[i];if(!r)throw new Error(`Time dimension not found: ${s.dimension}`);return R(r.sql,n)}resolveEventDimension(e,t,n){const[,s]=e.eventDimension.split("."),i=t.dimensions?.[s];if(!i)throw new Error(`Event dimension not found: ${e.eventDimension}`);return R(i.sql,n)}buildStartingStepFilters(e,t,n){if(!e.startingStep.filter)return[];const s=Array.isArray(e.startingStep.filter)?e.startingStep.filter:[e.startingStep.filter],i=[];for(const r of s){const a=this.buildFilterCondition(r,t,n);a&&i.push(a)}return i}buildFilterCondition(e,t,n){if("and"in e||"or"in e){const u=e,c=[],l=u.and||u.or||[];for(const m of l){const p=this.buildFilterCondition(m,t,n);p&&c.push(p)}return c.length===0?null:c.length===1?c[0]:"and"in e?A(...c):o`(${o.join(c,o` OR `)})`}if("type"in e&&"filters"in e){const u=e,c=[];for(const l of u.filters||[]){const m=this.buildFilterCondition(l,t,n);m&&c.push(m)}return c.length===0?null:c.length===1?c[0]:u.type==="and"?A(...c):o`(${o.join(c,o` OR `)})`}const s=e,[,i]=s.member.split("."),r=t.dimensions?.[i];if(!r)return null;const a=R(r.sql,n);return this.filterBuilder.buildFilterCondition(a,s.operator,s.values||[],r,s.dateRange)}buildStartingEntitiesCTE(e,t,n){const{cubeBase:s,bindingKeyExpr:i,timeExpr:r,eventExpr:a,startingStepFilters:u}=t,c=[];s.where&&c.push(s.where),c.push(...u);let l=n.db.select({binding_key:o`${i}`.as("binding_key"),start_time:o`MIN(${r})`.as("start_time"),event_type:o`${a}`.as("event_type"),event_path:o`${a}`.as("event_path")}).from(s.from);if(c.length>0){const m=c.length===1?c[0]:A(...c);l=l.where(m)}return l=l.groupBy(i,a),e.entityLimit&&(l=l.limit(e.entityLimit)),n.db.$with("starting_entities").as(l)}buildBeforeCTEsLateral(e,t,n){const{cubeBase:s,bindingKeyExpr:i,timeExpr:r,eventExpr:a}=t,u=[],c=e.outputMode==="sunburst";for(let l=1;l<=e.stepsBefore;l++){const m=l===1?"starting_entities":`before_step_${l-1}`,p=l===1?"start_time":"step_time",f=`before_step_${l}`,h=[];s.where&&h.push(s.where),h.push(o`${i} = ${o.identifier(m)}.binding_key`,o`${r} < ${o.identifier(m)}.${o.identifier(p)}`);const g=h.length===1?h[0]:A(...h),y=c?o`${a} || ${"→"} || ${o.identifier(m)}.event_path`:o`${a}`,b=n.db.select({binding_key:o`${i}`.as("binding_key"),step_time:o`${r}`.as("step_time"),event_type:o`${a}`.as("event_type"),event_path:y.as("event_path")}).from(s.from).where(g).orderBy(o`${r} DESC`).limit(1),$=n.db.$with(f).as(n.db.select({binding_key:o`e.binding_key`.as("binding_key"),step_time:o`e.step_time`.as("step_time"),event_type:o`e.event_type`.as("event_type"),event_path:o`e.event_path`.as("event_path")}).from(o`${o.identifier(m)}`).crossJoinLateral(b.as("e")));u.push($)}return u}buildAfterCTEsLateral(e,t,n){const{cubeBase:s,bindingKeyExpr:i,timeExpr:r,eventExpr:a}=t,u=[],c=e.outputMode==="sunburst";for(let l=1;l<=e.stepsAfter;l++){const m=l===1?"starting_entities":`after_step_${l-1}`,p=l===1?"start_time":"step_time",f=`after_step_${l}`,h=[];s.where&&h.push(s.where),h.push(o`${i} = ${o.identifier(m)}.binding_key`,o`${r} > ${o.identifier(m)}.${o.identifier(p)}`);const g=h.length===1?h[0]:A(...h),y=c?o`${o.identifier(m)}.event_path || ${"→"} || ${a}`:o`${a}`,b=n.db.select({binding_key:o`${i}`.as("binding_key"),step_time:o`${r}`.as("step_time"),event_type:o`${a}`.as("event_type"),event_path:y.as("event_path")}).from(s.from).where(g).orderBy(o`${r} ASC`).limit(1),$=n.db.$with(f).as(n.db.select({binding_key:o`e.binding_key`.as("binding_key"),step_time:o`e.step_time`.as("step_time"),event_type:o`e.event_type`.as("event_type"),event_path:o`e.event_path`.as("event_path")}).from(o`${o.identifier(m)}`).crossJoinLateral(b.as("e")));u.push($)}return u}buildBeforeCTEsWindow(e,t,n){const{cubeBase:s,bindingKeyExpr:i,timeExpr:r,eventExpr:a}=t,u=[],c=e.outputMode==="sunburst";for(let l=1;l<=e.stepsBefore;l++){const m=l===1?"starting_entities":`before_step_${l-1}`,p=l===1?"start_time":"step_time",f=`before_step_${l}`,h=[];s.where&&h.push(s.where),h.push(o`${r} < ${o.identifier(m)}.${o.identifier(p)}`);const g=h.length===1?h[0]:A(...h),y=c?o`${a} || ${"→"} || ${o.identifier(m)}.event_path`:o`${a}`,b=n.db.select({binding_key:o`${i}`.as("binding_key"),step_time:o`${r}`.as("step_time"),event_type:o`${a}`.as("event_type"),event_path:y.as("event_path"),rn:o`ROW_NUMBER() OVER (PARTITION BY ${i} ORDER BY ${r} DESC)`.as("rn")}).from(s.from).innerJoin(o`${o.identifier(m)}`,o`${i} = ${o.identifier(m)}.binding_key`).where(g),$=n.db.select({binding_key:o`binding_key`.as("binding_key"),step_time:o`step_time`.as("step_time"),event_type:o`event_type`.as("event_type"),event_path:o`event_path`.as("event_path")}).from(b.as("ranked")).where(o`rn = 1`);u.push(n.db.$with(f).as($))}return u}buildAfterCTEsWindow(e,t,n){const{cubeBase:s,bindingKeyExpr:i,timeExpr:r,eventExpr:a}=t,u=[],c=e.outputMode==="sunburst";for(let l=1;l<=e.stepsAfter;l++){const m=l===1?"starting_entities":`after_step_${l-1}`,p=l===1?"start_time":"step_time",f=`after_step_${l}`,h=[];s.where&&h.push(s.where),h.push(o`${r} > ${o.identifier(m)}.${o.identifier(p)}`);const g=h.length===1?h[0]:A(...h),y=c?o`${o.identifier(m)}.event_path || ${"→"} || ${a}`:o`${a}`,b=n.db.select({binding_key:o`${i}`.as("binding_key"),step_time:o`${r}`.as("step_time"),event_type:o`${a}`.as("event_type"),event_path:y.as("event_path"),rn:o`ROW_NUMBER() OVER (PARTITION BY ${i} ORDER BY ${r} ASC)`.as("rn")}).from(s.from).innerJoin(o`${o.identifier(m)}`,o`${i} = ${o.identifier(m)}.binding_key`).where(g),$=n.db.select({binding_key:o`binding_key`.as("binding_key"),step_time:o`step_time`.as("step_time"),event_type:o`event_type`.as("event_type"),event_path:o`event_path`.as("event_path")}).from(b.as("ranked")).where(o`rn = 1`);u.push(n.db.$with(f).as($))}return u}buildNodesAggregationCTE(e,t){const n=[],s=e.outputMode==="sunburst";for(let a=e.stepsBefore;a>=1;a--){const u=-a,c=`before_step_${a}`;s?n.push(o`
62
- SELECT
63
- ${o.raw(`'before_${a}_'`)} || event_path AS node_id,
64
- event_type AS name,
65
- ${o.raw(String(u))} AS layer,
66
- COUNT(*) AS value
67
- FROM ${o.identifier(c)}
68
- GROUP BY event_path, event_type
69
- `):n.push(o`
70
- SELECT
71
- ${o.raw(`'before_${a}_'`)} || event_type AS node_id,
72
- event_type AS name,
73
- ${o.raw(String(u))} AS layer,
74
- COUNT(*) AS value
75
- FROM ${o.identifier(c)}
76
- GROUP BY event_type
77
- `)}n.push(o`
78
- SELECT
79
- ${o.raw("'start_'")} || event_type AS node_id,
80
- event_type AS name,
81
- 0 AS layer,
82
- COUNT(*) AS value
83
- FROM starting_entities
84
- GROUP BY event_type
85
- `);for(let a=1;a<=e.stepsAfter;a++){const u=a,c=`after_step_${a}`;s?n.push(o`
86
- SELECT
87
- ${o.raw(`'after_${a}_'`)} || event_path AS node_id,
88
- event_type AS name,
89
- ${o.raw(String(u))} AS layer,
90
- COUNT(*) AS value
91
- FROM ${o.identifier(c)}
92
- GROUP BY event_path, event_type
93
- `):n.push(o`
94
- SELECT
95
- ${o.raw(`'after_${a}_'`)} || event_type AS node_id,
96
- event_type AS name,
97
- ${o.raw(String(u))} AS layer,
98
- COUNT(*) AS value
99
- FROM ${o.identifier(c)}
100
- GROUP BY event_type
101
- `)}const i=o.join(n,o` UNION ALL `),r=t.db.select({node_id:o`node_id`.as("node_id"),name:o`name`.as("name"),layer:o`layer`.as("layer"),value:o`value`.as("value")}).from(o`(${i}) AS nodes_union`);return t.db.$with("nodes_agg").as(r)}buildLinksAggregationCTE(e,t){const n=[],s=e.outputMode==="sunburst";for(let a=e.stepsBefore;a>=2;a--){const u=`before_step_${a}`,c=`before_step_${a-1}`;s?n.push(o`
102
- SELECT
103
- ${o.raw(`'before_${a}_'`)} || f.event_path AS source_id,
104
- ${o.raw(`'before_${a-1}_'`)} || t.event_path AS target_id,
105
- COUNT(*) AS value
106
- FROM ${o.identifier(u)} f
107
- INNER JOIN ${o.identifier(c)} t ON f.binding_key = t.binding_key
108
- GROUP BY f.event_path, t.event_path
109
- `):n.push(o`
110
- SELECT
111
- ${o.raw(`'before_${a}_'`)} || f.event_type AS source_id,
112
- ${o.raw(`'before_${a-1}_'`)} || t.event_type AS target_id,
113
- COUNT(*) AS value
114
- FROM ${o.identifier(u)} f
115
- INNER JOIN ${o.identifier(c)} t ON f.binding_key = t.binding_key
116
- GROUP BY f.event_type, t.event_type
117
- `)}e.stepsBefore>=1&&(s?n.push(o`
118
- SELECT
119
- ${o.raw("'before_1_'")} || b.event_path AS source_id,
120
- ${o.raw("'start_'")} || s.event_type AS target_id,
121
- COUNT(*) AS value
122
- FROM before_step_1 b
123
- INNER JOIN starting_entities s ON b.binding_key = s.binding_key
124
- GROUP BY b.event_path, s.event_type
125
- `):n.push(o`
126
- SELECT
127
- ${o.raw("'before_1_'")} || b.event_type AS source_id,
128
- ${o.raw("'start_'")} || s.event_type AS target_id,
129
- COUNT(*) AS value
130
- FROM before_step_1 b
131
- INNER JOIN starting_entities s ON b.binding_key = s.binding_key
132
- GROUP BY b.event_type, s.event_type
133
- `)),e.stepsAfter>=1&&(s?n.push(o`
134
- SELECT
135
- ${o.raw("'start_'")} || s.event_type AS source_id,
136
- ${o.raw("'after_1_'")} || a.event_path AS target_id,
137
- COUNT(*) AS value
138
- FROM starting_entities s
139
- INNER JOIN after_step_1 a ON s.binding_key = a.binding_key
140
- GROUP BY s.event_type, a.event_path
141
- `):n.push(o`
142
- SELECT
143
- ${o.raw("'start_'")} || s.event_type AS source_id,
144
- ${o.raw("'after_1_'")} || a.event_type AS target_id,
145
- COUNT(*) AS value
146
- FROM starting_entities s
147
- INNER JOIN after_step_1 a ON s.binding_key = a.binding_key
148
- GROUP BY s.event_type, a.event_type
149
- `));for(let a=1;a<e.stepsAfter;a++){const u=`after_step_${a}`,c=`after_step_${a+1}`;s?n.push(o`
150
- SELECT
151
- ${o.raw(`'after_${a}_'`)} || f.event_path AS source_id,
152
- ${o.raw(`'after_${a+1}_'`)} || t.event_path AS target_id,
153
- COUNT(*) AS value
154
- FROM ${o.identifier(u)} f
155
- INNER JOIN ${o.identifier(c)} t ON f.binding_key = t.binding_key
156
- GROUP BY f.event_path, t.event_path
157
- `):n.push(o`
158
- SELECT
159
- ${o.raw(`'after_${a}_'`)} || f.event_type AS source_id,
160
- ${o.raw(`'after_${a+1}_'`)} || t.event_type AS target_id,
161
- COUNT(*) AS value
162
- FROM ${o.identifier(u)} f
163
- INNER JOIN ${o.identifier(c)} t ON f.binding_key = t.binding_key
164
- GROUP BY f.event_type, t.event_type
165
- `)}if(n.length===0){const a=t.db.select({source_id:o`NULL`.as("source_id"),target_id:o`NULL`.as("target_id"),value:o`0`.as("value")}).from(o`(SELECT 1) AS empty`).where(o`1 = 0`);return t.db.$with("links_agg").as(a)}const i=o.join(n,o` UNION ALL `),r=t.db.select({source_id:o`source_id`.as("source_id"),target_id:o`target_id`.as("target_id"),value:o`value`.as("value")}).from(o`(${i}) AS links_union`);return t.db.$with("links_agg").as(r)}buildFinalResultCTE(e){const t=o`
166
- SELECT
167
- 'node' AS record_type,
168
- node_id AS id,
169
- name AS name,
170
- layer AS layer,
171
- value AS value,
172
- NULL AS source_id,
173
- NULL AS target_id
174
- FROM nodes_agg
175
- UNION ALL
176
- SELECT
177
- 'link' AS record_type,
178
- NULL AS id,
179
- NULL AS name,
180
- NULL AS layer,
181
- value AS value,
182
- source_id AS source_id,
183
- target_id AS target_id
184
- FROM links_agg
185
- WHERE source_id IS NOT NULL
186
- `,n=e.db.select({record_type:o`record_type`.as("record_type"),id:o`id`.as("id"),name:o`name`.as("name"),layer:o`layer`.as("layer"),value:o`value`.as("value"),source_id:o`source_id`.as("source_id"),target_id:o`target_id`.as("target_id")}).from(o`(${t}) AS final_union`);return e.db.$with("final_result").as(n)}}function Ye(d){return Array.isArray(d)}function lt(d){return typeof d=="object"&&d!==null&&"cube"in d}function Xe(d){if(lt(d))return d.cube;const e=d.indexOf(".");if(e===-1)throw new Error(`Invalid time dimension format: ${d}. Expected 'CubeName.dimensionName'`);return d.substring(0,e)}function Ze(d){if(lt(d))return d.dimension;const e=d.indexOf(".");if(e===-1)throw new Error(`Invalid time dimension format: ${d}. Expected 'CubeName.dimensionName'`);return d.substring(e+1)}class bn{constructor(e){this.databaseAdapter=e,this.dateTimeBuilder=new ie(e),this.filterBuilder=new ge(e,this.dateTimeBuilder)}filterBuilder;dateTimeBuilder;hasRetention(e){return e.retention!==void 0&&e.retention.timeDimension!=null&&e.retention.bindingKey!=null}validateConfig(e,t){const n=[];try{const r=Xe(e.timeDimension),a=Ze(e.timeDimension),u=t.get(r);u?u.dimensions?.[a]||n.push(`Time dimension not found: ${a} in cube ${r}`):n.push(`Cube not found: ${r}`)}catch{n.push(`Invalid time dimension format: ${e.timeDimension}`)}if(Ye(e.bindingKey))for(const r of e.bindingKey){const a=t.get(r.cube);if(!a)n.push(`Binding key mapping cube not found: ${r.cube}`);else{const u=this.extractDimensionName(r.dimension);a.dimensions?.[u]||n.push(`Binding key dimension not found: ${u} in cube ${r.cube}`)}}else{const[r,a]=e.bindingKey.split(".");if(!r||!a)n.push(`Invalid binding key format: ${e.bindingKey}. Expected 'CubeName.dimensionName'`);else{const u=t.get(r);u?u.dimensions?.[a]||n.push(`Binding key dimension not found: ${a} in cube ${r}`):n.push(`Binding key cube not found: ${r}`)}}if(e.breakdownDimensions&&e.breakdownDimensions.length>0)for(const r of e.breakdownDimensions){const[a,u]=r.split(".");if(!a||!u)n.push(`Invalid breakdown dimension format: ${r}. Expected 'CubeName.dimensionName'`);else{const c=t.get(a);c?c.dimensions?.[u]||n.push(`Breakdown dimension not found: ${u} in cube ${a}`):n.push(`Breakdown dimension cube not found: ${a}`)}}if(e.periods<1&&n.push("Periods must be at least 1"),e.periods>52&&n.push("Periods cannot exceed 52 (performance limit)"),["day","week","month"].includes(e.granularity)||n.push(`Invalid granularity: ${e.granularity}`),["classic","rolling"].includes(e.retentionType)||n.push(`Invalid retention type: ${e.retentionType}`),!e.dateRange)n.push("Date range is required");else{if(!e.dateRange.start)n.push("Date range start is required");else{const r=new Date(e.dateRange.start);isNaN(r.getTime())&&n.push("Invalid date range start format")}if(!e.dateRange.end)n.push("Date range end is required");else{const r=new Date(e.dateRange.end);isNaN(r.getTime())&&n.push("Invalid date range end format")}if(e.dateRange.start&&e.dateRange.end){const r=new Date(e.dateRange.start),a=new Date(e.dateRange.end);!isNaN(r.getTime())&&!isNaN(a.getTime())&&r>a&&n.push("Date range start must be before or equal to end")}}return{isValid:n.length===0,errors:n}}buildRetentionQuery(e,t,n){const s=this.resolveConfig(e,t,n),i=s.breakdowns.length,r=this.buildCohortBaseCTE(e,s,n),a=this.buildActivityPeriodsCTE(e,s,n),u=this.buildCohortSizesCTE(e,n,i),c=this.buildRetentionCountsCTE(e,n,i),l=s.breakdowns.length>0,m={period:o`rc.period_number`.as("period"),cohort_size:o`cs.cohort_size`.as("cohort_size"),retained_users:o`rc.retained_users`.as("retained_users"),retention_rate:o`CAST(rc.retained_users AS NUMERIC) / NULLIF(cs.cohort_size, 0)`.as("retention_rate")};for(let h=0;h<s.breakdowns.length;h++)m[`breakdown_${h}`]=o.raw(`rc.breakdown_${h}`).as(`breakdown_${h}`);let p=n.db.with(r,a,u,c).select(m).from(o`retention_counts rc`);if(l){const h=s.breakdowns.map((y,b)=>o`COALESCE(CAST(rc.breakdown_${o.raw(String(b))} AS TEXT), '') = COALESCE(CAST(cs.breakdown_${o.raw(String(b))} AS TEXT), '')`),g=h.length===1?h[0]:o.join(h,o` AND `);p=p.innerJoin(o`cohort_sizes cs`,g)}else p=p.innerJoin(o`cohort_sizes cs`,o`1 = 1`);const f=[];for(let h=0;h<s.breakdowns.length;h++)f.push(o.raw(`rc.breakdown_${h}`));return f.push(o`rc.period_number`),p=p.orderBy(...f),p}transformResult(e,t){const n=t.breakdownDimensions||[],s=n.length>0;return e.map(i=>{const r={period:Number(i.period),cohortSize:Number(i.cohort_size),retainedUsers:Number(i.retained_users),retentionRate:i.retention_rate!==null?Number(i.retention_rate):0};if(s){const a={};for(let u=0;u<n.length;u++){const c=n[u],l=i[`breakdown_${u}`];a[c]=l!==void 0?String(l):null}r.breakdownValues=a}return r})}resolveConfig(e,t,n){const s=Xe(e.timeDimension),i=Ze(e.timeDimension),r=t.get(s);if(!r)throw new Error(`Cube not found: ${s}`);const a=r.dimensions?.[i];if(!a)throw new Error(`Time dimension not found: ${i}`);const u=R(a.sql,n),c=this.resolveBindingKey(e.bindingKey,r,t,n),l=this.buildFilterConditions(e.cohortFilters,r,t,n),m=this.buildFilterConditions(e.activityFilters,r,t,n),p=[];if(e.breakdownDimensions&&e.breakdownDimensions.length>0)for(const f of e.breakdownDimensions){const[h,g]=f.split("."),y=t.get(h);if(y&&y.dimensions?.[g]){const b=R(y.dimensions[g].sql,n);p.push({dimension:f,expr:b})}}return{cube:r,bindingKeyExpr:c,timeExpr:u,cohortFilterConditions:l,activityFilterConditions:m,breakdowns:p}}resolveBindingKey(e,t,n,s){if(Ye(e)){const c=e.find(f=>f.cube===t.name);if(!c)throw new Error(`No binding key mapping found for cube: ${t.name}`);const l=this.extractDimensionName(c.dimension),m=n.get(c.cube);if(!m)throw new Error(`Binding key cube not found: ${c.cube}`);const p=m.dimensions?.[l];if(!p)throw new Error(`Binding key dimension not found: ${c.dimension}`);return R(p.sql,s)}const[i,r]=e.split("."),a=n.get(i);if(!a)throw new Error(`Binding key cube not found: ${i}`);const u=a.dimensions?.[r];if(!u)throw new Error(`Binding key dimension not found: ${e}`);return R(u.sql,s)}buildFilterConditions(e,t,n,s){if(!e)return[];const i=Array.isArray(e)?e:[e],r=[];for(const a of i){const u=this.buildSingleFilterCondition(a,t,n,s);u&&r.push(u)}return r}buildSingleFilterCondition(e,t,n,s){if("and"in e||"or"in e){const m=e,p=[],f="and"in m&&!!m.and,h=m.and||m.or||[];for(const g of h){const y=this.buildSingleFilterCondition(g,t,n,s);y&&p.push(y)}return p.length===0?null:p.length===1?p[0]:f?A(...p):o`(${o.join(p,o` OR `)})`}const i=e,[r,a]=i.member.split("."),u=n.get(r);if(!u)return null;const c=u.dimensions?.[a];if(!c)return null;const l=R(c.sql,s);return this.filterBuilder.buildFilterCondition(l,i.operator,i.values||[],c,i.dateRange)}buildCohortBaseCTE(e,t,n){const s=t.cube.sql(n),i=[];if(s.where&&i.push(s.where),i.push(...t.cohortFilterConditions),e.dateRange){const l=this.buildDateRangeCondition(t.timeExpr,e.dateRange);i.push(l)}const r=this.databaseAdapter.buildTimeDimension(e.granularity,t.timeExpr),a={binding_key:o`${t.bindingKeyExpr}`.as("binding_key"),cohort_entry:o`MIN(${r})`.as("cohort_entry")};for(let l=0;l<t.breakdowns.length;l++){const{expr:m}=t.breakdowns[l];a[`breakdown_${l}`]=o`MIN(${m})`.as(`breakdown_${l}`)}let u=n.db.select(a).from(s.from);if(i.length>0){const l=i.length===1?i[0]:A(...i);u=u.where(l)}const c=[t.bindingKeyExpr];for(let l=0;l<t.breakdowns.length;l++)c.push(t.breakdowns[l].expr);if(u=u.groupBy(...c),e.dateRange){const l=this.buildDateRangeHavingCondition(r,e.dateRange);u=u.having(l)}return n.db.$with("cohort_base").as(u)}buildDateRangeCondition(e,t){return o`${e} >= ${t.start}::date AND ${e} < (${t.end}::date + interval '1 day')`}buildDateRangeHavingCondition(e,t){return o`MIN(${e}) >= ${t.start}::date AND MIN(${e}) < (${t.end}::date + interval '1 day')`}buildActivityPeriodsCTE(e,t,n){const s=t.cube.sql(n),i=[];s.where&&i.push(s.where),i.push(...t.activityFilterConditions),i.push(o`${t.timeExpr} >= cohort_base.cohort_entry`);const r=this.databaseAdapter.buildTimeDimension(e.granularity,t.timeExpr),a=this.buildPeriodNumberExpression(o`cohort_base.cohort_entry`,r,e.granularity),u={binding_key:o`cohort_base.binding_key`.as("binding_key"),period_number:a.as("period_number")};for(let m=0;m<t.breakdowns.length;m++)u[`breakdown_${m}`]=o.raw(`cohort_base.breakdown_${m}`).as(`breakdown_${m}`);let c=n.db.select(u).from(s.from).innerJoin(o`cohort_base`,o`${t.bindingKeyExpr} = cohort_base.binding_key`);if(i.length>0){const m=i.length===1?i[0]:A(...i);c=c.where(m)}const l=[o`cohort_base.binding_key`,a];for(let m=0;m<t.breakdowns.length;m++)l.push(o.raw(`cohort_base.breakdown_${m}`));return c=c.groupBy(...l),n.db.$with("activity_periods").as(c)}buildCohortSizesCTE(e,t,n){if(n>0){const r={cohort_size:o`COUNT(*)`.as("cohort_size")},a=[];for(let c=0;c<n;c++)r[`breakdown_${c}`]=o.raw(`breakdown_${c}`).as(`breakdown_${c}`),a.push(o.raw(`breakdown_${c}`));const u=t.db.select(r).from(o`cohort_base`).groupBy(...a);return t.db.$with("cohort_sizes").as(u)}const i=t.db.select({cohort_size:o`COUNT(*)`.as("cohort_size")}).from(o`cohort_base`);return t.db.$with("cohort_sizes").as(i)}buildRetentionCountsCTE(e,t,n){let s;if(e.retentionType==="rolling")s=this.buildRollingRetentionCountsQuery(e,t,n);else{const i={period_number:o`period_number`.as("period_number"),retained_users:o`COUNT(DISTINCT binding_key)`.as("retained_users")},r=[o`period_number`];for(let a=0;a<n;a++)i[`breakdown_${a}`]=o.raw(`breakdown_${a}`).as(`breakdown_${a}`),r.push(o.raw(`breakdown_${a}`));s=t.db.select(i).from(o`activity_periods`).where(o`period_number >= 0 AND period_number <= ${e.periods}`).groupBy(...r)}return t.db.$with("retention_counts").as(s)}buildRollingRetentionCountsQuery(e,t,n){const s=[];for(let l=0;l<n;l++)s.push(`breakdown_${l}`);const i=s.length>0?`, ${s.join(", ")}`:"",r=o`(
187
- SELECT
188
- binding_key,
189
- ${o.raw(s.map(l=>`${l}`).join(", ")+(s.length>0?",":""))}
190
- MAX(period_number) as max_period
191
- FROM activity_periods
192
- WHERE period_number >= 0 AND period_number <= ${e.periods}
193
- GROUP BY binding_key${o.raw(i)}
194
- )`,a=this.databaseAdapter.buildPeriodSeriesSubquery(e.periods),u={period_number:o`p.period_number`.as("period_number"),retained_users:o`COUNT(DISTINCT CASE WHEN ump.max_period >= p.period_number THEN ump.binding_key END)`.as("retained_users")},c=[o`p.period_number`];for(let l=0;l<n;l++)u[`breakdown_${l}`]=o.raw(`ump.breakdown_${l}`).as(`breakdown_${l}`),c.push(o.raw(`ump.breakdown_${l}`));return t.db.select(u).from(o`${r} ump`).crossJoin(a).groupBy(...c)}buildPeriodNumberExpression(e,t,n){return this.databaseAdapter.buildDateDiffPeriods(e,t,n)}extractDimensionName(e){const t=e.split(".");return t.length>1?t[1]:t[0]}}class X{constructor(e,t){if(this.dbExecutor=e,this.databaseAdapter=e.databaseAdapter,!this.databaseAdapter)throw new Error("DatabaseExecutor must have a databaseAdapter property");this.queryBuilder=new on(this.databaseAdapter),this.queryPlanner=new ut,this.cteBuilder=new an(this.queryBuilder),this.comparisonQueryBuilder=new pn(this.databaseAdapter),this.funnelQueryBuilder=new hn(this.databaseAdapter),this.flowQueryBuilder=new gn(this.databaseAdapter),this.retentionQueryBuilder=new bn(this.databaseAdapter),this.cacheConfig=t}queryBuilder;queryPlanner;cteBuilder;databaseAdapter;comparisonQueryBuilder;funnelQueryBuilder;flowQueryBuilder;retentionQueryBuilder;cacheConfig;async execute(e,t,n,s){try{if(this.funnelQueryBuilder.hasFunnel(t)){const b=this.funnelQueryBuilder.validateConfig(t.funnel,e);if(!b.isValid)throw new Error(`Funnel validation failed: ${b.errors.join(", ")}`)}else if(this.flowQueryBuilder.hasFlow(t)){const b=this.flowQueryBuilder.validateConfig(t.flow,e);if(!b.isValid)throw new Error(`Flow validation failed: ${b.errors.join(", ")}`)}else if(this.retentionQueryBuilder.hasRetention(t)){const b=this.retentionQueryBuilder.validateConfig(t.retention,e);if(!b.isValid)throw new Error(`Retention validation failed: ${b.errors.join(", ")}`)}else{const b=ct(e,t);if(!b.isValid)throw new Error(`Query validation failed: ${b.errors.join(", ")}`)}let i;if(this.cacheConfig?.enabled!==!1&&this.cacheConfig?.provider)if(i=Ht(t,n,this.cacheConfig),s?.skipCache)this.cacheConfig.onCacheEvent?.({type:"miss",key:i,durationMs:0});else try{const b=Date.now(),$=await this.cacheConfig.provider.get(i);if($)return this.cacheConfig.onCacheEvent?.({type:"hit",key:i,durationMs:Date.now()-b}),{...$.value,cache:$.metadata?{hit:!0,cachedAt:new Date($.metadata.cachedAt).toISOString(),ttlMs:$.metadata.ttlMs,ttlRemainingMs:$.metadata.ttlRemainingMs}:{hit:!0,cachedAt:new Date().toISOString(),ttlMs:0,ttlRemainingMs:0}};this.cacheConfig.onCacheEvent?.({type:"miss",key:i,durationMs:Date.now()-b})}catch(b){this.cacheConfig.onError?.(b,"get")}if(this.comparisonQueryBuilder.hasComparison(t))return this.executeComparisonQueryWithCache(e,t,n,i);if(this.funnelQueryBuilder.hasFunnel(t))return this.executeFunnelQueryWithCache(e,t,n,i);if(this.flowQueryBuilder.hasFlow(t))return this.executeFlowQueryWithCache(e,t,n,i);if(this.retentionQueryBuilder.hasRetention(t))return this.executeRetentionQueryWithCache(e,t,n,i);const r=new Ve,a={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n,filterCache:r};this.preloadFilterCache(t,r,e,a);const u=this.queryPlanner.createQueryPlan(e,t,a);this.validateSecurityContext(u,a);const c=this.buildUnifiedQuery(u,t,a),l=this.queryBuilder.collectNumericFields(e,t),m=await this.dbExecutor.execute(c,l),p=Array.isArray(m)?m.map(b=>{const $={...b};if(t.timeDimensions){for(const D of t.timeDimensions)if(D.dimension in $){let w=$[D.dimension];if(typeof w=="string"&&w.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)){const C=w.replace(" ","T"),E=!C.endsWith("Z")&&!C.includes("+")?C+"Z":C;w=new Date(E)}w=this.databaseAdapter.convertTimeDimensionResult(w),$[D.dimension]=w}}return $}):[m],f=t.measures||[],h=He(p,t,f),g=this.generateAnnotations(u,t),y={data:h,annotation:g,warnings:u.warnings?.length?u.warnings:void 0};if(i&&this.cacheConfig?.provider)try{const b=Date.now();await this.cacheConfig.provider.set(i,y,this.cacheConfig.defaultTtlMs??3e5),this.cacheConfig.onCacheEvent?.({type:"set",key:i,durationMs:Date.now()-b})}catch(b){this.cacheConfig.onError?.(b,"set")}return y}catch(i){if(i instanceof Error){let r=i;for(;r.cause instanceof Error;)r=r.cause;let a=r.message;const u=r;throw u.code&&(a+=` [${u.code}]`),u.detail&&(a+=` Detail: ${u.detail}`),u.hint&&(a+=` Hint: ${u.hint}`),i.message=`Query execution failed: ${a}`,i}throw new Error("Query execution failed: Unknown error")}}async executeQuery(e,t,n){const s=new Map;return s.set(e.name,e),this.execute(s,t,n)}async executeComparisonQueryWithCache(e,t,n,s){const i=await this.executeComparisonQuery(e,t,n);if(s&&this.cacheConfig?.provider)try{const r=Date.now();await this.cacheConfig.provider.set(s,i,this.cacheConfig.defaultTtlMs??3e5),this.cacheConfig.onCacheEvent?.({type:"set",key:s,durationMs:Date.now()-r})}catch(r){this.cacheConfig.onError?.(r,"set")}return i}async executeComparisonQuery(e,t,n){const s=this.comparisonQueryBuilder.getComparisonTimeDimension(t);if(!s||!s.compareDateRange)throw new Error("No compareDateRange found in query");const i=this.comparisonQueryBuilder.normalizePeriods(s.compareDateRange);if(i.length<2)throw new Error("compareDateRange requires at least 2 periods");const r=s.granularity||"day",a=i.map(async l=>{const m=this.comparisonQueryBuilder.createPeriodQuery(t,l);return{result:await this.executeStandardQuery(e,m,n),period:l}}),u=await Promise.all(a),c=this.comparisonQueryBuilder.mergeComparisonResults(u,s,r);return c.data=this.comparisonQueryBuilder.sortComparisonResults(c.data,s.dimension),c}async executeFunnelQueryWithCache(e,t,n,s){const i=await this.executeFunnelQuery(e,t,n);if(s&&this.cacheConfig?.provider)try{const r=Date.now();await this.cacheConfig.provider.set(s,i,this.cacheConfig.defaultTtlMs??3e5),this.cacheConfig.onCacheEvent?.({type:"set",key:s,durationMs:Date.now()-r})}catch(r){this.cacheConfig.onError?.(r,"set")}return{...i,cache:{hit:!1}}}async executeFunnelQuery(e,t,n){const s=t.funnel,i=this.funnelQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Funnel validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=await this.funnelQueryBuilder.buildFunnelQuery(s,e,r),c=this.funnelQueryBuilder.transformResult(u,s),l={measures:{},dimensions:{},segments:{},timeDimensions:{}};return l.funnel={config:s,steps:s.steps.map((m,p)=>({name:m.name,index:p,timeToConvert:m.timeToConvert}))},{data:c,annotation:l}}async executeFlowQueryWithCache(e,t,n,s){const i=await this.executeFlowQuery(e,t,n);if(s&&this.cacheConfig?.provider)try{const r=Date.now();await this.cacheConfig.provider.set(s,i,this.cacheConfig.defaultTtlMs??3e5),this.cacheConfig.onCacheEvent?.({type:"set",key:s,durationMs:Date.now()-r})}catch(r){this.cacheConfig.onError?.(r,"set")}return{...i,cache:{hit:!1}}}async executeFlowQuery(e,t,n){const s=t.flow,i=this.flowQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Flow validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=await this.flowQueryBuilder.buildFlowQuery(s,e,r),c=this.flowQueryBuilder.transformResult(u),l={measures:{},dimensions:{},segments:{},timeDimensions:{}};return l.flow={config:s,startingStep:{name:s.startingStep.name},stepsBefore:s.stepsBefore,stepsAfter:s.stepsAfter},{data:[c],annotation:l}}async executeRetentionQueryWithCache(e,t,n,s){const i=await this.executeRetentionQuery(e,t,n);if(s&&this.cacheConfig?.provider)try{const r=Date.now();await this.cacheConfig.provider.set(s,i,this.cacheConfig.defaultTtlMs??3e5),this.cacheConfig.onCacheEvent?.({type:"set",key:s,durationMs:Date.now()-r})}catch(r){this.cacheConfig.onError?.(r,"set")}return{...i,cache:{hit:!1}}}async executeRetentionQuery(e,t,n){const s=t.retention,i=this.retentionQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Retention validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=await this.retentionQueryBuilder.buildRetentionQuery(s,e,r),c=this.retentionQueryBuilder.transformResult(u,s),l={measures:{},dimensions:{},segments:{},timeDimensions:{}};return l.retention={config:s,granularity:s.granularity,periods:s.periods,retentionType:s.retentionType,breakdownDimensions:s.breakdownDimensions},{data:c,annotation:l}}async executeStandardQuery(e,t,n){const s=new Ve,i={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n,filterCache:s};this.preloadFilterCache(t,s,e,i);const r=this.queryPlanner.createQueryPlan(e,t,i),a=this.buildUnifiedQuery(r,t,i),u=this.queryBuilder.collectNumericFields(e,t),c=await this.dbExecutor.execute(a,u),l=Array.isArray(c)?c.map(h=>{const g={...h};if(t.timeDimensions){for(const y of t.timeDimensions)if(y.dimension in g){let b=g[y.dimension];if(typeof b=="string"&&b.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)){const $=b.replace(" ","T"),D=!$.endsWith("Z")&&!$.includes("+")?$+"Z":$;b=new Date(D)}b=this.databaseAdapter.convertTimeDimensionResult(b),g[y.dimension]=b}}return g}):[c],m=t.measures||[],p=He(l,t,m),f=this.generateAnnotations(r,t);return{data:p,annotation:f}}validateSecurityContext(e,t){const n=typeof process<"u"?process.env?.NODE_ENV:void 0,s=typeof process<"u"?process.env?.DRIZZLE_CUBE_WARN_SECURITY:void 0;if(n!=="development"&&!s)return;const i=[e.primaryCube];for(const a of e.joinCubes||[])i.push(a.cube);for(const a of e.preAggregationCTEs||[])i.push(a.cube);const r=new Set;for(const a of i)if(!r.has(a.name)){r.add(a.name);try{if(a.public)continue;a.sql(t).where||console.warn(`[drizzle-cube] WARNING: Cube '${a.name}' has no security filtering. If this cube contains public data, add 'public: true' to suppress this warning. Otherwise, ensure sql() returns: { from: table, where: eq(table.orgId, ctx.securityContext.orgId) }`)}catch{}}}buildUnifiedQuery(e,t,n){const s=new Map;if(e.preAggregationCTEs&&e.preAggregationCTEs.length>0){for(const w of e.preAggregationCTEs)if(w.propagatingFilters&&w.propagatingFilters.length>0)for(const C of w.propagatingFilters){const E=C.sourceCube.name;if(!s.has(E)){const N={filters:C.filters},S=new Map([[E,C.sourceCube]]),_=this.queryBuilder.buildWhereConditions(S,N,n);s.set(E,_)}const T=s.get(E);T&&T.length>0&&(C.preBuiltFilterSQL=T.length===1?T[0]:A(...T))}}const i=[],r=new Map,a=new Map;if(e.preAggregationCTEs&&e.preAggregationCTEs.length>0)for(const w of e.preAggregationCTEs){const C=this.cteBuilder.buildPreAggregationCTE(w,t,n,e,s);if(C&&(i.push(C),r.set(w.cube.name,w.cteAlias),w.downstreamJoinKeys))for(const E of w.downstreamJoinKeys)a.set(E.targetCubeName,{cteAlias:w.cteAlias,joinKeys:E.joinKeys})}const u=e.primaryCube.sql(n),c=e.joinCubes.length>0?this.getCubesFromPlan(e):new Map([[e.primaryCube.name,e.primaryCube]]),m={...this.queryBuilder.buildSelections(e.joinCubes.length>0?c:e.primaryCube,t,n)};if(e.preAggregationCTEs)for(const w of e.preAggregationCTEs){const C=w.cube.name;for(const E of w.measures)if(m[E]){const[,T]=E.split("."),N=c.get(C);if(N&&N.measures&&N.measures[T]){const S=N.measures[T],_=o`${o.identifier(w.cteAlias)}.${o.identifier(T)}`;let v;if(S.type==="calculated"&&S.calculatedSql)v=this.queryBuilder.buildCTECalculatedMeasure(S,N,w,c,n);else{const F=w.cteReason==="fanOutPrevention",U=this.shouldUseMaxForHasManyAtJoinKeyGrain(w,t,c),j=F||U;switch(S.type){case"count":case"countDistinct":case"sum":v=j?H(_):V(_);break;case"avg":v=j?H(_):this.databaseAdapter.buildAvg(_);break;case"min":v=oe(_);break;case"max":v=H(_);break;case"number":v=j?H(_):V(_);break;default:v=j?H(_):V(_)}}m[E]=o`${v}`.as(E)}}for(const E in m){const[T,N]=E.split(".");if(T===C){const S=c.get(C),_=S&&S.dimensions?.[N],v=E.startsWith(C+".");if(_||v){let F=w.joinKeys.find(U=>U.targetColumn===N);if(!F&&S?.dimensions?.[N]){const U=S.dimensions[N].sql;F=w.joinKeys.find(j=>j.targetColumnObj===U)}F?m[E]=o`${o.identifier(w.cteAlias)}.${o.identifier(N)}`.as(E):v&&S?.dimensions?.[N]&&(m[E]=o`${o.identifier(w.cteAlias)}.${o.identifier(N)}`.as(E))}}}}if(t.measures)for(const w of t.measures){const[C,E]=w.split("."),T=c.get(C);if(T?.measures?.[E]){const N=T.measures[E];if(I.isPostAggregationWindow(N)){const S=I.getWindowBaseMeasure(N,C);if(S){const[_,v]=S.split("."),F=c.get(_);if(F?.measures?.[v]){const U=F.measures[v],j=e.preAggregationCTEs?.find(z=>z.cube?.name===_&&z.measures?.includes(S));let B;if(j){const z=o`${o.identifier(j.cteAlias)}.${o.identifier(v)}`;B=o`sum(${z})`}else B=this.queryBuilder.buildMeasureExpression(U,n,F);m[S]||(m[S]=o`${B}`.as(S));const J=this.buildPostAggregationWindowExpression(N,B,t,n,T,e);J&&(m[w]=o`${J}`.as(w))}}}}}const p=[];let f=n.db.select(m).from(u.from);if(i.length>0&&(f=n.db.with(...i).select(m).from(u.from)),u.joins)for(const w of u.joins)switch(w.type||"left"){case"left":f=f.leftJoin(w.table,w.on);break;case"inner":f=f.innerJoin(w.table,w.on);break;case"right":f=f.rightJoin(w.table,w.on);break;case"full":f=f.fullJoin(w.table,w.on);break}const h=new Set,g=new Set;if(e.preAggregationCTEs){for(const w of e.preAggregationCTEs)if(w.intermediateJoins&&w.intermediateJoins.length>0)for(const C of w.intermediateJoins)g.add(C.cube.name)}if(e.joinCubes&&e.joinCubes.length>0)for(const w of e.joinCubes){const C=w.cube.name;if(g.has(C)&&!r.has(C))continue;const E=r.get(w.cube.name);if(w.junctionTable){const F=w.junctionTable;let U=F.joinCondition;const j=F.sourceCubeName?r.get(F.sourceCubeName):void 0;if(j){const z=e.preAggregationCTEs?.find(Y=>Y.cube.name===F.sourceCubeName)?.downstreamJoinKeys?.find(Y=>Y.targetCubeName===w.cube.name);if(z&&z.joinKeys.length>0){const Y=[];for(const Ie of z.joinKeys){const gt=o`${o.identifier(j)}.${o.identifier(Ie.sourceColumn)}`,Le=Ie.targetColumnObj;Le&&Y.push(W(Le,gt))}Y.length>0&&(U=A(...Y))}}const B=[];if(F.securitySql){const J=F.securitySql(n.securityContext);Array.isArray(J)?B.push(...J):B.push(J)}try{switch(F.joinType||"left"){case"left":f=f.leftJoin(F.table,U);break;case"inner":f=f.innerJoin(F.table,U);break;case"right":f=f.rightJoin(F.table,U);break;case"full":f=f.fullJoin(F.table,U);break}B.length>0&&p.push(...B)}catch{}}let T,N,S;if(E)T=o`${o.identifier(E)}`,N=this.cteBuilder.buildCTEJoinCondition(w,E,e),S=void 0;else{const F=a.get(w.cube.name),U=w.cube.sql(n);if(T=U.from,S=U.where,F&&!w.junctionTable){const j=[];for(const B of F.joinKeys){const J=o`${o.identifier(F.cteAlias)}.${o.identifier(B.sourceColumn)}`,z=B.targetColumnObj||o.identifier(B.targetColumn);j.push(W(J,z))}N=j.length===1?j[0]:A(...j)}else N=w.joinCondition}const _=w.joinType||"left",v=_!=="inner"&&S?A(N,S):N;try{switch(_){case"left":f=f.leftJoin(T,v),S&&h.add(w.cube.name);break;case"inner":f=f.innerJoin(T,N);break;case"right":f=f.rightJoin(T,v),S&&h.add(w.cube.name);break;case"full":f=f.fullJoin(T,v),S&&h.add(w.cube.name);break}}catch{}}if(u.where&&p.push(u.where),e.joinCubes&&e.joinCubes.length>0)for(const w of e.joinCubes){const C=w.cube.name;if(r.get(C)||g.has(C)||h.has(C))continue;const T=w.cube.sql(n);T.where&&p.push(T.where)}const y=this.queryBuilder.buildWhereConditions(e.joinCubes.length>0?this.getCubesFromPlan(e):e.primaryCube,t,n,e,s);if(y.length>0&&p.push(...y),p.length>0){const w=p.length===1?p[0]:A(...p);f=f.where(w)}const b=this.queryBuilder.buildGroupByFields(e.joinCubes.length>0?this.getCubesFromPlan(e):e.primaryCube,t,n,e);b.length>0&&(f=f.groupBy(...b));const $=this.queryBuilder.buildHavingConditions(e.joinCubes.length>0?this.getCubesFromPlan(e):e.primaryCube,t,n,e);if($.length>0){const w=$.length===1?$[0]:A(...$);f=f.having(w)}const D=this.queryBuilder.buildOrderBy(t);return D.length>0&&(f=f.orderBy(...D)),f=this.queryBuilder.applyLimitAndOffset(f,t),f}getCubesFromPlan(e){const t=new Map;if(t.set(e.primaryCube.name,e.primaryCube),e.joinCubes)for(const n of e.joinCubes)t.set(n.cube.name,n.cube);return t}shouldUseMaxForHasManyAtJoinKeyGrain(e,t,n){return e.cteReason!=="hasMany"||e.downstreamJoinKeys&&e.downstreamJoinKeys.length>0||e.intermediateJoins&&e.intermediateJoins.length>0||!!!(t.dimensions&&t.dimensions.length>0||t.timeDimensions&&t.timeDimensions.length>0)||!!(t.dimensions?.some(r=>r.startsWith(`${e.cube.name}.`))||t.timeDimensions?.some(r=>r.dimension.startsWith(`${e.cube.name}.`)))?!1:e.joinKeys.length>0&&e.joinKeys.every(r=>!!r.sourceColumnObj&&this.queryGroupsByColumn(r.sourceColumnObj,t,n))}queryGroupsByColumn(e,t,n){if(t.dimensions)for(const s of t.dimensions){const[i,r]=s.split(".");if(n.get(i)?.dimensions?.[r]?.sql===e)return!0}if(t.timeDimensions)for(const s of t.timeDimensions){if(s.granularity)continue;const[i,r]=s.dimension.split(".");if(n.get(i)?.dimensions?.[r]?.sql===e)return!0}return!1}async generateSQL(e,t,n){const s=new Map;return s.set(e.name,e),this.generateUnifiedSQL(s,t,n)}async generateMultiCubeSQL(e,t,n){return this.generateUnifiedSQL(e,t,n)}async dryRunFunnel(e,t,n){if(!this.funnelQueryBuilder.hasFunnel(t))throw new Error("Query does not contain a valid funnel configuration");const s=t.funnel,i=this.funnelQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Funnel validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=this.funnelQueryBuilder.buildFunnelQuery(s,e,r).toSQL();return{sql:u.sql,params:u.params}}async dryRunFlow(e,t,n){if(!this.flowQueryBuilder.hasFlow(t))throw new Error("Query does not contain a valid flow configuration");const s=t.flow,i=this.flowQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Flow validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=this.flowQueryBuilder.buildFlowQuery(s,e,r).toSQL();return{sql:u.sql,params:u.params}}async dryRunRetention(e,t,n){if(!this.retentionQueryBuilder.hasRetention(t))throw new Error("Query does not contain a valid retention configuration");const s=t.retention,i=this.retentionQueryBuilder.validateConfig(s,e);if(!i.isValid)throw new Error(`Retention validation failed: ${i.errors.join(", ")}`);const r={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},u=this.retentionQueryBuilder.buildRetentionQuery(s,e,r).toSQL();return{sql:u.sql,params:u.params}}async explainQuery(e,t,n,s){let i;return this.funnelQueryBuilder.hasFunnel(t)?i=await this.dryRunFunnel(e,t,n):this.flowQueryBuilder.hasFlow(t)?i=await this.dryRunFlow(e,t,n):this.retentionQueryBuilder.hasRetention(t)?i=await this.dryRunRetention(e,t,n):i=await this.generateUnifiedSQL(e,t,n),this.dbExecutor.explainQuery(i.sql,i.params||[],s)}async generateUnifiedSQL(e,t,n){const s={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:n},i=this.queryPlanner.createQueryPlan(e,t,s),a=this.buildUnifiedQuery(i,t,s).toSQL();return{sql:a.sql,params:a.params}}generateAnnotations(e,t){const n={},s={},i={},r=[e.primaryCube].filter(Boolean);if(e.joinCubes&&e.joinCubes.length>0&&r.push(...e.joinCubes.map(a=>a.cube).filter(Boolean)),t.measures)for(const a of t.measures){const[u,c]=a.split("."),l=r.find(m=>m?.name===u);if(l&&l.measures[c]){const m=l.measures[c];n[a]={title:m.title||c,shortTitle:m.title||c,type:m.type}}}if(t.dimensions)for(const a of t.dimensions){const[u,c]=a.split("."),l=r.find(m=>m?.name===u);if(l&&l.dimensions?.[c]){const m=l.dimensions[c];s[a]={title:m.title||c,shortTitle:m.title||c,type:m.type}}}if(t.timeDimensions)for(const a of t.timeDimensions){const[u,c]=a.dimension.split("."),l=r.find(m=>m?.name===u);if(l&&l.dimensions?.[c]){const m=l.dimensions[c];i[a.dimension]={title:m.title||c,shortTitle:m.title||c,type:m.type,granularity:a.granularity}}}return{measures:n,dimensions:s,segments:{},timeDimensions:i}}preloadFilterCache(e,t,n,s){if(e.filters&&e.filters.length>0){const i=Re(e.filters);for(const r of i){const a=me(r);if(t.has(a))continue;const[u,c]=r.member.split("."),l=n.get(u);if(!l)continue;const m=l.dimensions?.[c];if(!m||["arrayContains","arrayOverlaps","arrayContained"].includes(r.operator))continue;const f=R(m.sql,s),h=this.queryBuilder.buildFilterConditionPublic(f,r.operator,r.values,m,r.dateRange);h&&t.set(a,h)}}if(e.timeDimensions){for(const i of e.timeDimensions)if(i.dateRange){const r=at(i.dimension,i.dateRange);if(t.has(r))continue;const[a,u]=i.dimension.split("."),c=n.get(a);if(!c)continue;const l=c.dimensions?.[u];if(!l)continue;const m=R(l.sql,s),p=this.queryBuilder.buildDateRangeCondition(m,i.dateRange);p&&t.set(r,p)}}}buildPostAggregationWindowExpression(e,t,n,s,i,r){const a=e.windowConfig||{},u=(f,h)=>{if(!r?.preAggregationCTEs)return null;const g=r.preAggregationCTEs.find(y=>y.cube?.name===f);return g&&g.cteAlias?o`${o.identifier(g.cteAlias)}.${o.identifier(h)}`:null};let c;if(a.orderBy&&a.orderBy.length>0)c=a.orderBy.map(f=>{const h=f.field.includes(".")?f.field.split(".")[1]:f.field;if(n.timeDimensions)for(const b of n.timeDimensions){const[$,D]=b.dimension.split(".");if(D===h){const w=u($,h);if(w)return{field:w,direction:f.direction};const C=i.dimensions?.[D];if(C)return{field:this.queryBuilder.buildTimeDimensionExpression(C.sql,b.granularity,s),direction:f.direction}}}const g=i.dimensions?.[h];if(g)return{field:R(g.sql,s),direction:f.direction};const y=a.measure?.includes(".")?a.measure.split(".")[1]:a.measure;return h===y||f.field===a.measure?{field:t,direction:f.direction}:null}).filter(f=>f!==null);else if(n.timeDimensions&&n.timeDimensions.length>0){const f=n.timeDimensions[0],[h,g]=f.dimension.split("."),y=u(h,g);if(y)c=[{field:y,direction:"asc"}];else{const b=i.name===h?i:void 0;if(b?.dimensions?.[g]){const $=b.dimensions[g];c=[{field:this.queryBuilder.buildTimeDimensionExpression($.sql,f.granularity,s),direction:"asc"}]}}}let l;a.partitionBy&&a.partitionBy.length>0&&(l=a.partitionBy.map(f=>{const h=f.includes(".")?f.split(".")[1]:f,g=i.dimensions?.[h];return g?R(g.sql,s):null}).filter(f=>f!==null));const m=this.databaseAdapter.buildWindowFunction(e.type,t,l,c,{offset:a.offset,defaultValue:a.defaultValue,nTile:a.nTile,frame:a.frame});if(!m)return null;switch(a.operation||I.getDefaultWindowOperation(e.type)){case"difference":return o`${t} - ${m}`;case"ratio":return o`${t} / NULLIF(${m}, 0)`;case"percentChange":return o`((${t} - ${m}) / NULLIF(${m}, 0)) * 100`;default:return m}}}const yn={name:"drizzle-cube-mcp-guide",description:"How to use drizzle-cube MCP tools to generate and run queries",messages:[{role:"user",content:{type:"text",text:["You are an analyst agent using drizzle-cube MCP.","","Workflow:","1) tools/call name=discover {topic|intent} - Find cubes and understand schema","2) Construct your query using the schema from discover (see cross-cube joins below)","3) tools/call name=validate {query} - Optional: fix schema issues","4) tools/call name=load {query} - Execute and get results","","CRITICAL - CROSS-CUBE JOINS:",'The "joins" property in discover results shows relationships between cubes.',"You can include dimensions from ANY related cube in your query!","Example: If Productivity joins to Employees, query:",'{ "measures": ["Productivity.totalPullRequests"], "dimensions": ["Employees.name"] }',"The system automatically joins the cubes for you.","","Query shapes:","- Regular: { measures, dimensions, filters[], timeDimensions[], order, limit, offset }","- Funnel: { funnel: { bindingKey, timeDimension, steps[], includeTimeMetrics? } }","- Flow: { flow: { bindingKey, eventDimension, steps?, window? } }","- Retention: { retention: { bindingKey, timeDimension, periods, granularity, retentionType, breakdownDimensions } }","","Time handling:",'- For AGGREGATED TOTALS (e.g., "last 3 months"): use filters with inDateRange, NOT timeDimensions','- For TIME SERIES (e.g., "by month"): use timeDimensions with granularity',"- You can combine both when needed","","Filters: flat arrays of { member, operator, values }. Do not nest arrays.","Do NOT hallucinate cube/field names—always use discover first."].join(`
195
- `)}}]},Cn={name:"drizzle-cube-query-rules",description:"Key generation rules aligned with Gemini single-step prompt",messages:[{role:"user",content:{type:"text",text:["Rules (keep JSON only):","- Use only measures/dimensions/timeDimensions from schema.","- timeDimensions: include granularity when grouping; use inDateRange filter for relative windows; combine when both requested.","- Funnel detection keywords: funnel, conversion, journey, drop off, step by step; use funnel format only if eventStream metadata exists.","- Funnel rules: bindingKey/timeDimension from cube metadata; include time filter on step 0 (default last 6 months) using inDateRange; steps ordered; flat filters.","- Chart selection: line/area for time trends; bar for categories; scatter for 2-measure correlations; bubble for 3-measure correlations; funnel for funnels.","- Correlation keywords (correlation/relationship/vs/compare) -> scatter/bubble, never line.","- Prefer .name fields over .id; avoid Id dimensions unless requested.","- Filters: flat array of {member, operator, values}; operators equals, notEquals, contains, notContains, gt, gte, lt, lte, inDateRange, set, notSet."].join(`
196
- `)}}]},wn={name:"drizzle-cube-query-building",description:"CRITICAL: Complete guide for building valid queries of all types with examples",messages:[{role:"user",content:{type:"text",text:["# Drizzle Cube Query Building Guide","","## CRITICAL: Cross-Cube Joins","","You can combine measures from one cube with dimensions from RELATED cubes!",'Check the "joins" property in discover results to see relationships.',"",'Example: "Top 5 employees by pull requests last 3 months"',"- Productivity cube has: measures (totalPullRequests), dimensions (date)","- Productivity joins to Employees (via employeeId)","- Employees cube has: dimensions (name, department)","","Query combining BOTH cubes:","```json","{",' "measures": ["Productivity.totalPullRequests"],',' "dimensions": ["Employees.name"],',' "filters": [',' { "member": "Productivity.date", "operator": "inDateRange", "values": ["last 3 months"] }'," ],",' "order": { "Productivity.totalPullRequests": "desc" },',' "limit": 5',"}","```","The system AUTOMATICALLY joins Productivity to Employees for you!","","---","","## Date Filtering vs Time Grouping","","### For AGGREGATED TOTALS (no time breakdown)","Use `filters` with `inDateRange` operator. Do NOT use timeDimensions.","","```json","{",' "measures": ["Productivity.totalPullRequests"],',' "dimensions": ["Employees.name"],',' "filters": [{ "member": "Productivity.date", "operator": "inDateRange", "values": ["last 3 months"] }],',' "order": { "Productivity.totalPullRequests": "desc" },',' "limit": 5',"}","```","Result: 5 rows total, one per employee, with SUMMED pull requests.","","### For TIME SERIES (grouped by period)","Use `timeDimensions` WITH `granularity`.","","```json","{",' "measures": ["Productivity.totalPullRequests"],',' "timeDimensions": [{ "dimension": "Productivity.date", "dateRange": "last 3 months", "granularity": "month" }]',"}","```","Result: 3 rows (one per month) with pull request totals.","","### WRONG: timeDimensions without granularity","```json","// DON'T DO THIS - groups by DAY, returns ~90 rows!",'{ "timeDimensions": [{ "dimension": "X.date", "dateRange": "last 3 months" }] }',"```","","---","","## Regular Query Structure","","```json","{",' "measures": ["Cube.measure1", "Cube.measure2"],',' "dimensions": ["Cube.dimension1", "RelatedCube.dimension"],',' "filters": [',' { "member": "Cube.field", "operator": "equals", "values": ["value"] },',' { "member": "Cube.date", "operator": "inDateRange", "values": ["last 30 days"] }'," ],",' "timeDimensions": [],',' "order": { "Cube.measure1": "desc" },',' "limit": 100,',' "offset": 0',"}","```","","### Filter Operators","- String: equals, notEquals, contains, notContains, startsWith, endsWith","- Numeric: gt, gte, lt, lte","- Null: set, notSet","- Date: inDateRange, beforeDate, afterDate","","### Date Range Values",'- Relative: "last 7 days", "last 3 months", "last year", "this week", "this month"','- Absolute: ["2024-01-01", "2024-03-31"]',"","---","","## Funnel Query Structure","","Use when: conversion analysis, user journeys, step-by-step analysis","Requires: Cube with eventStream metadata","","```json","{",' "funnel": {',' "bindingKey": "Events.userId",',' "timeDimension": "Events.timestamp",',' "steps": ['," {",' "name": "Step 1",',' "filter": [',' { "member": "Events.eventType", "operator": "equals", "values": ["signup"] },',' { "member": "Events.timestamp", "operator": "inDateRange", "values": ["last 6 months"] }'," ]"," },"," {",' "name": "Step 2",',' "filter": [',' { "member": "Events.eventType", "operator": "equals", "values": ["purchase"] }'," ],",' "timeToConvert": { "value": 7, "unit": "day" }'," }"," ],",' "includeTimeMetrics": true'," }","}","```","","IMPORTANT: Put time filter (inDateRange) ONLY on step 0!","","---","","## Flow Query Structure","","Use when: analyzing event sequences, path analysis","","```json","{",' "flow": {',' "bindingKey": "Events.sessionId",',' "eventDimension": "Events.eventType",',' "timeDimension": "Events.timestamp",',' "stepsBefore": 2,',' "stepsAfter": 2,',' "startingStep": "checkout"'," }","}","```","","---","","## Retention Query Structure","","Use when: cohort analysis, user retention tracking","","```json","{",' "retention": {',' "bindingKey": "Users.id",',' "timeDimension": "Events.timestamp",',' "periods": 8,',' "granularity": "week",',' "retentionType": "rolling",',' "breakdownDimensions": ["Events.country"]'," }","}","```","","---","","## Common Mistakes to Avoid","","1. Using timeDimensions when you want aggregated totals → Use filters with inDateRange instead","2. Omitting granularity in timeDimensions → Results in day-level grouping","3. Guessing field names → Always use discover first to get actual schema","4. Nested filter arrays → Filters must be flat: [{ member, operator, values }]",'5. Missing date filter for "last N" queries → Always add inDateRange filter'].join(`
197
- `)}}]},$n={name:"drizzle-cube-date-filtering",description:"CRITICAL: How to correctly filter by date vs group by time period - the #1 source of query mistakes",messages:[{role:"user",content:{type:"text",text:["# Date Filtering vs Time Grouping - CRITICAL GUIDE","","This is the most common mistake when building queries. These are TWO DIFFERENT operations.","","## Quick Decision Tree","","```","User wants data over a time period?",'├── Wants AGGREGATED TOTALS (e.g., "total sales last month")',"│ └── Use: filters with inDateRange operator","│",'└── Wants TIME SERIES breakdown (e.g., "daily sales last month")'," └── Use: timeDimensions with granularity","```","","## For Aggregated Totals (MOST COMMON)","",'When user says: "last 3 months", "over the past year", "in Q1", "since January"',"","```json","{",' "measures": ["Sales.totalRevenue"],',' "dimensions": ["Products.category"],',' "filters": [',' { "member": "Sales.date", "operator": "inDateRange", "values": ["last 3 months"] }'," ]","}","```","","Result: One row per category with TOTAL revenue over the 3-month period.","","## For Time Series (Trend Analysis)","",'When user says: "by month", "per week", "daily trend", "over time"',"","```json","{",' "measures": ["Sales.totalRevenue"],',' "timeDimensions": [',' { "dimension": "Sales.date", "dateRange": "last 3 months", "granularity": "month" }'," ]","}","```","","Result: Multiple rows, one per month.","","## WRONG: timeDimensions Without Granularity","","```json","// This returns ~90 rows (daily) instead of aggregates!","{",' "timeDimensions": [{ "dimension": "Sales.date", "dateRange": "last 3 months" }]',"}","```","","## Both: Filter AND Group","",'When user wants: "monthly breakdown for last quarter"',"","```json","{",' "measures": ["Sales.totalRevenue"],',' "filters": [',' { "member": "Sales.date", "operator": "inDateRange", "values": ["last quarter"] }'," ],",' "timeDimensions": [',' { "dimension": "Sales.date", "granularity": "month" }'," ]","}","```","","## Summary Table","","| User Request | Use | Example |","|-------------|-----|---------|",'| "total for last 3 months" | filters + inDateRange | { filters: [{ operator: "inDateRange", values: ["last 3 months"] }] } |','| "top 5 last quarter" | filters + inDateRange | Same as above + order + limit |','| "monthly trend" | timeDimensions + granularity | { timeDimensions: [{ granularity: "month" }] } |','| "daily breakdown last week" | timeDimensions | { timeDimensions: [{ dateRange: "last week", granularity: "day" }] } |'].join(`
198
- `)}}]},En=[yn,Cn,wn,$n];function Tn(){return En}class Oe{cubes=new Map;dbExecutor;metadataCache;cacheConfig;constructor(e){e?.databaseExecutor?this.dbExecutor=e.databaseExecutor:e?.drizzle&&(this.dbExecutor=ze(e.drizzle,e.schema,e.engineType)),this.cacheConfig=e?.cache}setDatabaseExecutor(e){this.dbExecutor=e}getEngineType(){return this.dbExecutor?.getEngineType()}setDrizzle(e,t,n){this.dbExecutor=ze(e,t,n)}hasExecutor(){return!!this.dbExecutor}registerCube(e){this.validateCalculatedMeasures(e),new Z(this.cubes).populateDependencies(e),this.cubes.set(e.name,e),this.invalidateMetadataCache()}validateCalculatedMeasures(e){const t=[];for(const[n,s]of Object.entries(e.measures))if(s.type==="calculated"){if(!s.calculatedSql){t.push(`Calculated measure '${e.name}.${n}' must have calculatedSql property`);continue}const i=sn(s.calculatedSql);if(!i.isValid){t.push(`Invalid calculatedSql syntax in '${e.name}.${n}': ${i.errors.join(", ")}`);continue}const r=new Map(this.cubes);r.set(e.name,e);const a=new Z(r);try{a.validateDependencies(e)}catch(u){t.push(u instanceof Error?u.message:String(u))}}if(t.length===0){const n=new Map(this.cubes);n.set(e.name,e);const s=new Z(n);s.buildGraph(e);const i=s.detectCycle();i&&t.push(`Circular dependency detected in calculated measures: ${i.join(" -> ")}`)}if(t.length>0)throw new Error(`Calculated measure validation failed for cube '${e.name}':
199
- ${t.join(`
200
- `)}`)}getCube(e){return this.cubes.get(e)}getAllCubes(){return Array.from(this.cubes.values())}getAllCubesMap(){return this.cubes}async execute(e,t,n){if(!this.dbExecutor)throw new Error("Database executor not configured");return new X(this.dbExecutor,this.cacheConfig).execute(this.cubes,e,t,n)}async executeMultiCubeQuery(e,t,n){return this.execute(e,t,n)}async executeQuery(e,t,n){if(!this.cubes.get(e))throw new Error(`Cube '${e}' not found`);return this.execute(t,n)}getMetadata(){return this.metadataCache?this.metadataCache:(this.metadataCache=Array.from(this.cubes.values()).map(e=>this.generateCubeMetadata(e)),this.metadataCache)}getColumnName(e){if(e&&e.name||e&&e.columnType&&e.name)return e.name;if(typeof e=="string")return e;if(e&&typeof e=="object"){if(e._.name)return e._.name;if(e.name)return e.name;if(e.columnName)return e.columnName}return"unknown_column"}static DEFAULT_TIME_GRANULARITIES=["year","quarter","month","week","day","hour"];generateCubeMetadata(e){const t=Object.keys(e.measures),n=Object.keys(e.dimensions),s=new Array(t.length),i=new Array(n.length);for(let c=0;c<t.length;c++){const l=t[c],m=e.measures[l];let p;m.drillMembers&&m.drillMembers.length>0&&(p=m.drillMembers.map(f=>f.includes(".")?f:`${e.name}.${f}`)),s[c]={name:`${e.name}.${l}`,title:m.title||l,shortTitle:m.title||l,type:m.type,format:void 0,description:m.description,synonyms:m.synonyms,drillMembers:p}}for(let c=0;c<n.length;c++){const l=n[c],m=e.dimensions[l];let p;m.type==="time"&&(p=m.granularities||Oe.DEFAULT_TIME_GRANULARITIES),i[c]={name:`${e.name}.${l}`,title:m.title||l,shortTitle:m.title||l,type:m.type,format:void 0,description:m.description,synonyms:m.synonyms,granularities:p}}const r=[];if(e.joins)for(const[,c]of Object.entries(e.joins)){const l=typeof c.targetCube=="function"?c.targetCube():c.targetCube;r.push({targetCube:l.name,relationship:c.relationship,joinFields:c.on.map(m=>({sourceField:this.getColumnName(m.source),targetField:this.getColumnName(m.target)}))})}const a=[];if(e.hierarchies)for(const[,c]of Object.entries(e.hierarchies))a.push({name:c.name,title:c.title||c.name,cubeName:e.name,levels:c.levels.map(l=>l.includes(".")?l:`${e.name}.${l}`)});return{name:e.name,title:e.title||e.name,description:e.description,exampleQuestions:e.exampleQuestions,measures:s,dimensions:i,segments:[],relationships:r.length>0?r:void 0,hierarchies:a.length>0?a:void 0,meta:e.meta}}async generateSQL(e,t,n){const s=this.getCube(e);if(!s)throw new Error(`Cube '${e}' not found`);if(!this.dbExecutor)throw new Error("Database executor not configured");const r=await new X(this.dbExecutor).generateSQL(s,t,n),a=this.dbExecutor.getEngineType();return{sql:Q.formatSqlString(r.sql,a),params:r.params}}async generateMultiCubeSQL(e,t){if(!this.dbExecutor)throw new Error("Database executor not configured");const s=await new X(this.dbExecutor).generateMultiCubeSQL(this.cubes,e,t),i=this.dbExecutor.getEngineType();return{sql:Q.formatSqlString(s.sql,i),params:s.params}}async dryRunFunnel(e,t){if(!this.dbExecutor)throw new Error("Database executor not configured");const s=await new X(this.dbExecutor).dryRunFunnel(this.cubes,e,t),i=this.dbExecutor.getEngineType();return{sql:Q.formatSqlString(s.sql,i),params:s.params}}async dryRunFlow(e,t){if(!this.dbExecutor)throw new Error("Database executor not configured");const s=await new X(this.dbExecutor).dryRunFlow(this.cubes,e,t),i=this.dbExecutor.getEngineType();return{sql:Q.formatSqlString(s.sql,i),params:s.params}}async dryRunRetention(e,t){if(!this.dbExecutor)throw new Error("Database executor not configured");const s=await new X(this.dbExecutor).dryRunRetention(this.cubes,e,t),i=this.dbExecutor.getEngineType();return{sql:Q.formatSqlString(s.sql,i),params:s.params}}async explainQuery(e,t,n){if(!this.dbExecutor)throw new Error("Database executor not configured");return new X(this.dbExecutor).explainQuery(this.cubes,e,t,n)}hasCube(e){return this.cubes.has(e)}removeCube(e){const t=this.cubes.delete(e);return t&&this.invalidateMetadataCache(),t}clearCubes(){this.cubes.clear(),this.invalidateMetadataCache()}invalidateMetadataCache(){this.metadataCache=void 0}getCubeNames(){return Array.from(this.cubes.keys())}validateQuery(e){return ct(this.cubes,e)}analyzeQuery(e,t){if(!this.dbExecutor)throw new Error("Database executor not configured");const n=new ut,s={db:this.dbExecutor.db,schema:this.dbExecutor.schema,securityContext:t};return n.analyzeQueryPlan(this.cubes,e,s)}}function ct(d,e){const t=[];if(e.funnel!==void 0&&e.funnel.steps?.length>=2){const s=e.funnel.bindingKey;if(typeof s=="string"){const[i]=s.split(".");i&&!d.has(i)&&t.push(`Funnel binding key cube not found: ${i}`)}else if(Array.isArray(s))for(const i of s)d.has(i.cube)||t.push(`Funnel binding key cube not found: ${i.cube}`);return{isValid:t.length===0,errors:t}}if(e.flow!==void 0&&e.flow.startingStep!==void 0&&e.flow.eventDimension!==void 0){const s=e.flow.bindingKey;if(typeof s=="string"){const[i]=s.split(".");i&&!d.has(i)&&t.push(`Flow binding key cube not found: ${i}`)}return{isValid:t.length===0,errors:t}}if(e.retention!==void 0&&e.retention.timeDimension!=null&&e.retention.bindingKey!=null){const s=Sn(e.retention.timeDimension);s&&!d.has(s)&&t.push(`Retention cube not found: ${s}`);const i=e.retention.bindingKey;if(typeof i=="string"){const[r]=i.split(".");r&&!d.has(r)&&t.push(`Retention binding key cube not found: ${r}`)}else if(Array.isArray(i))for(const r of i)d.has(r.cube)||t.push(`Retention binding key cube not found: ${r.cube}`);if(e.retention.breakdownDimensions&&Array.isArray(e.retention.breakdownDimensions))for(const r of e.retention.breakdownDimensions){const[a]=r.split(".");a&&!d.has(a)&&t.push(`Retention breakdown cube not found: ${a}`)}return{isValid:t.length===0,errors:t}}const n=new Set;if(e.measures)for(const s of e.measures){const[i,r]=s.split(".");if(!i||!r){t.push(`Invalid measure format: ${s}. Expected format: 'CubeName.fieldName'`);continue}n.add(i);const a=d.get(i);if(!a){t.push(`Cube '${i}' not found (referenced in measure '${s}')`);continue}a.measures[r]||t.push(`Measure '${r}' not found on cube '${i}'`)}if(e.dimensions)for(const s of e.dimensions){const[i,r]=s.split(".");if(!i||!r){t.push(`Invalid dimension format: ${s}. Expected format: 'CubeName.fieldName'`);continue}n.add(i);const a=d.get(i);if(!a){t.push(`Cube '${i}' not found (referenced in dimension '${s}')`);continue}a.dimensions[r]||t.push(`Dimension '${r}' not found on cube '${i}'`)}if(e.timeDimensions)for(const s of e.timeDimensions){const[i,r]=s.dimension.split(".");if(!i||!r){t.push(`Invalid timeDimension format: ${s.dimension}. Expected format: 'CubeName.fieldName'`);continue}n.add(i);const a=d.get(i);if(!a){t.push(`Cube '${i}' not found (referenced in timeDimension '${s.dimension}')`);continue}a.dimensions[r]||t.push(`TimeDimension '${r}' not found on cube '${i}' (must be a dimension with time type)`)}if(e.filters)for(const s of e.filters)dt(s,d,t,n);return n.size===0&&t.push("Query must reference at least one cube through measures, dimensions, or filters"),{isValid:t.length===0,errors:t}}function dt(d,e,t,n){if("and"in d||"or"in d){const a=d.and||d.or||[];for(const u of a)dt(u,e,t,n);return}if(!("member"in d)){t.push("Filter must have a member field");return}const[s,i]=d.member.split(".");if(!s||!i){t.push(`Invalid filter member format: ${d.member}. Expected format: 'CubeName.fieldName'`);return}n.add(s);const r=e.get(s);if(!r){t.push(`Cube '${s}' not found (referenced in filter '${d.member}')`);return}!r.dimensions[i]&&!r.measures[i]&&t.push(`Filter field '${i}' not found on cube '${s}' (must be a dimension or measure)`)}function Sn(d){if(typeof d=="string"){const[e]=d.split(".");return e||null}return d.cube}const ue=["2025-11-25","2025-06-18","2025-03-26"],mt="2025-11-25";function Nn(d){const t=Mn(d["mcp-protocol-version"])||mt;return{ok:ue.includes(t),negotiated:ue.includes(t)?t:null,supported:ue}}function Dn(d){if(!d)return!1;const e=d.split(",").map(s=>s.trim().toLowerCase()),t=e.includes("text/event-stream"),n=e.includes("application/json");return t&&!n}const An="mcp-session-id";function _n(d){if(!d)return!1;const e=d.split(",").map(s=>s.trim().toLowerCase().split(";")[0]),t=e.some(s=>s==="application/json"),n=e.some(s=>s==="text/event-stream");return t&&n}function vn(d,e={}){const{allowMissingOrigin:t=!0,allowedOrigins:n}=e;if(!d)return t?{valid:!0}:{valid:!1,reason:"Origin header is required"};if(!n||n.length===0)return{valid:!0};let s;try{s=new URL(d)}catch{return{valid:!1,reason:"Invalid Origin header format"}}return n.map(r=>{try{return new URL(r).origin}catch{return r}}).includes(s.origin)?{valid:!0}:{valid:!1,reason:"Origin not in allowed list"}}function Rn(d,e,t){const n=[];return e&&n.push(`id: ${e}`),t&&t>0&&n.push(`retry: ${t}`),n.push("event: message"),n.push(`data: ${JSON.stringify(d)}`),n.push(""),n.join(`
201
- `)}function Fn(d,e,t,n){return{jsonrpc:"2.0",id:d??null,error:{code:e,message:t,...n!==void 0?{data:n}:{}}}}function On(d,e){return{jsonrpc:"2.0",id:d??null,result:e}}function In(d){if(!d||typeof d!="object")return null;const e=d;return e.jsonrpc!=="2.0"||typeof e.method!="string"?null:{jsonrpc:"2.0",method:e.method,id:e.id,params:e.params}}async function Ln(d,e,t){const{semanticLayer:n,extractSecurityContext:s,rawRequest:i,rawResponse:r}=t,a=t.prompts??pt,u=t.resources??ht;switch(d){case"initialize":{const c=e?.protocolVersion;let l;return c&&ue.includes(c)?l=c:l=mt,{protocolVersion:l,capabilities:{tools:{listChanged:!1},resources:{listChanged:!1},prompts:{listChanged:!1},sampling:{}},sessionId:ft(),serverInfo:{name:"drizzle-cube",version:typeof process<"u"?process.env?.npm_package_version||"dev":"worker"}}}case"list_tools":case"tools/list":return{tools:Un(),nextCursor:""};case"call_tool":case"tools/call":return xn(e,t);case"resources/list":return{resources:u.map(({uri:c,name:l,description:m,mimeType:p})=>({uri:c,name:l,description:m,mimeType:p})),nextCursor:""};case"resources/templates/list":return{resourceTemplates:[],nextCursor:""};case"resources/read":{const c=e?.uri,l=u.find(m=>m.uri===c)||u[0];if(!l)throw G(-32602,"resource not found");return{contents:[{uri:l.uri,mimeType:l.mimeType,text:l.text}]}}case"prompts/list":return{prompts:a.map(({name:c,description:l})=>({name:c,description:l})),nextCursor:""};case"ping":return{};case"notifications/initialized":return{};case"prompts/get":{const c=e?.name,l=a.find(m=>m.name===c)||a[0];if(!l)throw G(-32602,"prompt not found");return{name:l.name,description:l.description,messages:l.messages}}case"discover":return Q.handleDiscover(n,e||{});case"validate":{const c=e||{};if(!c.query)throw G(-32602,"query is required");return Q.handleValidate(n,c)}case"load":{const c=e||{};if(!c.query)throw G(-32602,"query is required");const l=await s(i,r);return Q.handleLoad(n,l,c)}default:throw G(-32601,`Unknown MCP method: ${d}`)}}function G(d,e,t){const n=new Error(e);return n.code=d,n}function Mn(d){return d?Array.isArray(d)?d[0]||null:d:null}function jn(d){return d.id===void 0||d.id===null}function ft(){return`evt-${Q.generateRequestId()}`}function Un(){return[{name:"discover",description:`Find relevant cubes based on topic or intent. Call this FIRST to understand available data.
202
-
203
- Returns cubes with:
204
- - All measures and dimensions with their types
205
- - Relationship information (joins) showing how cubes connect
206
- - Metadata hints (eventStream for funnels, etc.)
207
-
208
- IMPORTANT: The 'joins' property shows relationships between cubes. You can include dimensions from ANY related cube in your query - the system auto-joins them.
209
-
210
- Example: If Productivity has a join to Employees, you can query:
211
- { "measures": ["Productivity.totalPullRequests"], "dimensions": ["Employees.name"] }`,inputSchema:{type:"object",properties:{topic:{type:"string",description:'Keyword to search (e.g., "sales", "employees")'},intent:{type:"string",description:'Natural language goal (e.g., "analyze productivity trends")'},limit:{type:"number",description:"Max results (default: 10)"},minScore:{type:"number",description:"Min relevance 0-1 (default: 0.1)"}}}},{name:"validate",description:`Validate a query and get auto-corrections for issues.
212
-
213
- Checks:
214
- - Field existence (measures, dimensions exist in schema)
215
- - Filter syntax and operators
216
- - Cross-cube join validity
217
-
218
- Returns corrected query if issues found.`,inputSchema:{type:"object",required:["query"],properties:{query:{type:"object",description:"CubeQuery to validate"}}}},{name:"load",description:`Execute a semantic query and return aggregated results.
219
-
220
- QUERY CONSTRUCTION RULES:
221
-
222
- 1. CROSS-CUBE JOINS (use dimensions from related cubes!)
223
- Check 'joins' in discover results. Include dimensions from ANY related cube.
224
- Example - get employee names with their productivity:
225
- {
226
- "measures": ["Productivity.totalPullRequests"],
227
- "dimensions": ["Employees.name"],
228
- "filters": [{ "member": "Productivity.date", "operator": "inDateRange", "values": ["last 3 months"] }]
229
- }
230
-
231
- 2. DATE FILTERING vs TIME GROUPING
232
- For AGGREGATED TOTALS: use 'filters' with 'inDateRange' (NOT timeDimensions)
233
- {
234
- "measures": ["Productivity.totalPullRequests"],
235
- "dimensions": ["Employees.name"],
236
- "filters": [{ "member": "Productivity.date", "operator": "inDateRange", "values": ["last 3 months"] }],
237
- "order": { "Productivity.totalPullRequests": "desc" },
238
- "limit": 5
239
- }
240
-
241
- For TIME SERIES: use 'timeDimensions' WITH 'granularity'
242
- {
243
- "measures": ["Productivity.totalPullRequests"],
244
- "timeDimensions": [{ "dimension": "Productivity.date", "dateRange": "last 3 months", "granularity": "month" }]
245
- }
246
-
247
- WARNING: timeDimensions WITHOUT granularity groups by day, returning many rows!
248
-
249
- 3. TOP N PATTERN: filters + order + limit`,inputSchema:{type:"object",required:["query"],properties:{query:{type:"object",description:`CubeQuery object with:
250
- - measures: string[] - Aggregations (from any cube)
251
- - dimensions: string[] - Grouping fields (can be from RELATED cubes via joins)
252
- - filters: [{ member, operator, values }] - Use 'inDateRange' for date filtering
253
- - timeDimensions: [{ dimension, granularity, dateRange }] - ONLY for time series
254
- - order: { "Cube.field": "asc"|"desc" }
255
- - limit: number`}}}}]}async function xn(d,e){const{semanticLayer:t,extractSecurityContext:n,rawRequest:s,rawResponse:i}=e,r=d||{};if(!r.name)throw G(-32602,"name is required for tools/call");const a=r.arguments;switch(r.name){case"discover":return Ne(await Q.handleDiscover(t,a||{}));case"validate":{const u=a||{};if(!u.query)throw G(-32602,"query is required");return Ne(await Q.handleValidate(t,u))}case"load":{const u=a||{};if(!u.query)throw G(-32602,"query is required");const c=await n(s,i);return Ne(await Q.handleLoad(t,c,u))}default:throw G(-32601,`Unknown tool: ${r.name}`)}}function Ne(d){return{content:[{type:"text",text:typeof d=="string"?d:JSON.stringify(d)}],isError:!1}}const pt=Tn(),ht=[{uri:"drizzle-cube://quickstart",name:"Drizzle Cube MCP Quickstart",description:"Minimal guide for using discover/suggest/validate/load",mimeType:"text/markdown",text:["# Drizzle Cube MCP Quickstart","","Tools:","- discover: { topic?, intent?, limit?, minScore? } → cubes list","- suggest: { naturalLanguage, cube? } → draft query","- validate: { query } → corrected query + issues","- load: { query } → data + annotation","","Recommended flow:","1) tools/list",'2) tools/call name=discover intent="<goal>"','3) tools/call name=suggest naturalLanguage="<goal>"',"4) tools/call name=validate query=<from suggest>","5) tools/call name=load query=<validated>","","Query shapes supported:","- regular Cube.js-style query { measures, dimensions, filters, timeDimensions, order, limit, offset }","- funnel { bindingKey, timeDimension, steps[], includeTimeMetrics? }","- flow { bindingKey, eventDimension, steps?, window? }","- retention { bindingKey, timeDimension, periods, granularity, retentionType, breakdownDimensions }","","Filter rules: flat arrays of { member, operator, values }; do not nest arrays."].join(`
256
- `)},{uri:"drizzle-cube://query-shapes",name:"Query Shapes Reference",description:"Detailed schema examples for regular, funnel, flow, and retention queries",mimeType:"text/markdown",text:["# Query Shapes","","## Regular query","```json","{",' "measures": ["Sales.count"],',' "dimensions": ["Sales.channel"],',' "filters": [ { "member": "Sales.status", "operator": "equals", "values": ["paid"] } ],',' "timeDimensions": [ { "dimension": "Sales.createdAt", "dateRange": "last 30 days", "granularity": "day" } ],',' "order": { "Sales.createdAt": "asc" },',' "limit": 500',"}","```","","## Funnel","```json","{",' "funnel": {',' "bindingKey": "Events.userId",',' "timeDimension": "Events.timestamp",',' "steps": [',' { "name": "Signup", "filter": [{ "member": "Events.eventType", "operator": "equals", "values": ["signup"] }] },',' { "name": "Purchase", "filter": [{ "member": "Events.eventType", "operator": "equals", "values": ["purchase"] }] }'," ],",' "includeTimeMetrics": true'," }","}","```","","## Flow","```json","{",' "flow": {',' "bindingKey": "Events.sessionId",',' "eventDimension": "Events.eventType",',' "steps": ["view", "add_to_cart", "checkout"],',' "window": { "unit": "minute", "value": 30 }'," }","}","```","","## Retention","```json","{",' "retention": {',' "bindingKey": "Users.id",',' "timeDimension": "Events.timestamp",',' "periods": 8,',' "granularity": "week",',' "retentionType": "rolling",',' "breakdownDimensions": ["Events.country"]'," }","}","```","","### Filter rules","- Always a flat array of filter objects: [{ member, operator, values }]","- For funnels, put time filter (inDateRange) only on step 0","- Operators: equals, notEquals, inDateRange, gt, gte, lt, lte, contains, notContains, set, notSet","","### Time handling",'- Relative ranges ("last 3 months") -> add ONLY an inDateRange filter on the time dimension (do NOT add timeDimensions unless grouping is requested).','- Grouping ("by month/week/day") -> add timeDimensions entry with granularity.',"- Both can be combined: inDateRange filter + timeDimensions granularity."].join(`
257
- `)}];function kn(){return ht}function Bn(){return pt}exports.MCP_SESSION_ID_HEADER=An;exports.SemanticLayerCompiler=Oe;exports.buildJsonRpcError=Fn;exports.buildJsonRpcResult=On;exports.dispatchMcpMethod=Ln;exports.getDefaultPrompts=Bn;exports.getDefaultResources=kn;exports.isNotification=jn;exports.negotiateProtocol=Nn;exports.parseJsonRpc=In;exports.primeEventId=ft;exports.serializeSseEvent=Rn;exports.validateAcceptHeader=_n;exports.validateOriginHeader=vn;exports.wantsEventStream=Dn;