@malloydata/render 0.0.188-dev240923162529 → 0.0.188

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.
@@ -6693,9 +6693,9 @@ class Ir extends NQ {
6693
6693
  }
6694
6694
  getQueryFieldReference(e, n) {
6695
6695
  const r = this.getQueryFieldByName(e);
6696
- if (n !== void 0) {
6696
+ if (n) {
6697
6697
  const i = { ...r.fieldDef };
6698
- return i.annotation = n, this.makeQueryField(i);
6698
+ i.annotation = n, r.fieldDef = i;
6699
6699
  }
6700
6700
  return r;
6701
6701
  }
@@ -374,7 +374,7 @@ FROM ${n}
374
374
  FROM ${h}
375
375
  `),this.resultStage}}class Wfe extends Cs{generateSQL(e){const n=this.parent.fieldDef.structSource;if(n.type!=="sql"||n.method!=="subquery")throw new Error("Invalid struct for QueryQueryRaw, currently only supports SQL");const r=n.sqlBlock.selectStr;return e.addStage(r)}prepare(){}getResultStructDef(){return this.parent.fieldDef}getResultMetadata(e){}}class Vfe extends Cs{constructor(e,n,r,i){super(e,n,r,i),this.stages=[],this.fieldDef=e,this.fieldsToStages()}fieldsToStages(){const e=this.firstSegment;if(this.parent.dialect.dontUnionIndex){this.stages=[e.indexFields];return}const n={};for(const r of e.indexFields){if(r.path.length>1){const i=H$(r.path.slice(0,r.path.length-1)),s=n[i];if(s===void 0){const o=this.parent.nameMap.get(r.path[0]);if(o instanceof Nr&&(o.fieldDef.structRelationship.type==="many"||o.fieldDef.structRelationship.type==="nested")&&o.fieldDef.fields.length>1){const u=[r];n[i]=u,this.stages.push(u);continue}}else{s.push(r);continue}}this.stages[0]===void 0&&(this.stages[0]=[]),this.stages[0].push(r)}}expandFields(e){}generateSQL(e){const n=this.firstSegment,r=[];for(const i of this.stages){const s=new Gfe({...this.fieldDef,pipeline:[{...n,indexFields:i}]},this.parent,e,this.isJoinedSubquery);s.prepare(e);const o=s.generateSQL(e);r.push(o)}return r.length===1?this.resultStage=r[0]:this.resultStage=e.addStage(r.map(i=>`SELECT * FROM ${i}
376
376
  `).join(` UNION ALL
377
- `)),this.resultStage}getResultStructDef(){const e={type:"struct",name:this.resultStage||"result",dialect:this.parent.fieldDef.dialect,fields:[{type:"string",name:"fieldName"},{type:"string",name:"fieldPath"},{type:"string",name:"fieldValue"},{type:"string",name:"fieldType"},{type:"number",name:"weight",numberType:"integer"}],structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"}};return this.parent.fieldDef.modelAnnotation&&(e.modelAnnotation=this.parent.fieldDef.modelAnnotation),e}}class Nr extends Q${constructor(e,n,r){if(super(e),this.sourceArguments=n,this.nameMap=new Map,this._arguments=void 0,this.setParent(r),"model"in r)if(this.model=r.model,this.pathAliasMap=new Map,e.structRelationship.type==="basetable")this.connectionName=e.structRelationship.connectionName;else throw new Error("All root StructDefs should be a baseTable");else this.model=this.getModel(),this.pathAliasMap=this.root().pathAliasMap,this.connectionName=this.root().connectionName;this.fieldDef=e,this.dialect=(0,wfe.getDialect)(this.fieldDef.dialect),this.addFieldsFromFieldList(this.fieldDef.fields)}resolveParentParameterReferences(e){return{...e,value:e.value===null?null:(0,an.exprMap)(e.value,n=>{if(n.node==="parameter"){if(this.parent===void 0)throw new Error("No parent from which to retrieve parameter value");const r=this.parent.arguments()[n.path[0]],i=this.parent.resolveParentParameterReferences(r);if(i.value===null)throw new Error("Invalid parameter value");return i.value}return n})}}arguments(){var e;if(this._arguments!==void 0)return this._arguments;this._arguments={};const n=(e=this.fieldDef.parameters)!==null&&e!==void 0?e:{};for(const i in n)this._arguments[i]=n[i];const r={...this.fieldDef.arguments,...this.sourceArguments};for(const i in r){const s=r[i];this._arguments[i]=this.resolveParentParameterReferences(s)}return this._arguments}addFieldsFromFieldList(e){for(const n of e){const r=(0,Ze.getIdentifier)(n);switch(n.type){case"struct":{this.addFieldToNameMap(r,new Nr(n,void 0,{struct:this}));break}case"turtle":{this.addFieldToNameMap(r,Cs.makeQuery(n,this,void 0,!1));break}default:this.addFieldToNameMap(r,this.makeQueryField(n))}}this.nameMap.has("__distinct_key")||this.addFieldToNameMap("__distinct_key",new jfe({type:"string",name:"__distinct_key"},this))}getAliasIdentifier(){const e=this.getFullOutputName(),n=this.pathAliasMap.get(e);if(n===void 0){const r=Array.from(this.pathAliasMap.values()),i=Lfe((0,Ze.getIdentifier)(this.fieldDef));let s=`${i}_0`,o=1;for(;r.includes(s)&&o<1e3;)o++,s=`${i}_${o}`;if(o<1e3)return this.pathAliasMap.set(e,s),s;throw new Error("Internal Error: cannot create unique alias name")}else return n}getSQLIdentifier(){return this.unnestWithNumbers()&&this.parent!==void 0?this.parent.getSQLIdentifier()+"."+(0,Ze.getIdentifier)(this.fieldDef)+`[${this.getIdentifier()}.__row_id]`:this.getIdentifier()}getIdentifier(){return this.fieldDef.structRelationship.type==="basetable"?"base":this.fieldDef.structRelationship.type==="inline"&&this.parent?this.parent.getSQLIdentifier()+"."+super.getIdentifier():this.getAliasIdentifier()}getFullOutputName(){return this.parent?this.parent.getFullOutputName()+(0,Ze.getIdentifier)(this.fieldDef)+".":""}needsSymetricCalculation(e){const n=this.getJoinableParent().getIdentifier(),r=e.root().joins.get(n);if(r)return!r.leafiest;throw new Error(`Join ${n} not found in result set`)}unnestWithNumbers(){return this.dialect.unnestWithNumbers&&this.fieldDef.structRelationship.type==="nested"}getJoinableParent(){if(this.fieldDef.structRelationship.type==="inline"){if(this.parent)return this.parent.getJoinableParent();throw new Error("Internal Error: inline struct cannot be root")}return this}addFieldToNameMap(e,n){if(this.nameMap.has(e))throw new Error(`Redefinition of ${e}`);this.nameMap.set(e,n)}getPrimaryKeyField(e){let n;if(n=this.primaryKey())return n;throw new Error(`Missing primary key for ${e}`)}resolveQueryFields(){if(this.fieldDef.structSource.type==="query"){const e=this.model.loadQuery(this.fieldDef.structSource.query,void 0).structs.pop();if(!e)throw new Error("Internal Error, query didn't produce a struct");const n={...this.fieldDef};for(const r of e.fields){let i;this.nameMap.has(i=(0,Ze.getIdentifier)(r))||(n.fields.push(r),this.nameMap.set(i,this.makeQueryField(r)))}this.fieldDef=n,!this.fieldDef.primaryKey&&e.primaryKey&&(this.fieldDef.primaryKey=e.primaryKey)}for(const[,e]of this.nameMap)e instanceof Nr&&e.resolveQueryFields()}getModel(){if(this.model)return this.model;if(this.parent===void 0)throw new Error("Expected this query struct to have a parent, as no model was present.");return this.parent.getModel()}setParent(e){"struct"in e&&(this.parent=e.struct),"model"in e?this.model=e.model:this.model=this.getModel()}makeQueryField(e){switch(e.type){case"string":return new Bfe(e,this);case"date":return new qT(e,this);case"timestamp":return new zT(e,this);case"number":return new $fe(e,this);case"boolean":return new UT(e,this);case"json":return new kfe(e,this);case"sql native":return new Pfe(e,this);case"turtle":return new Y$(e,this);default:throw new Error(`unknown field definition ${JSON.stringify(e)}`)}}structSourceSQL(e){switch(this.fieldDef.structSource.type){case"table":{const n=this.fieldDef.structSource.tablePath;return this.dialect.quoteTablePath(n)}case"sql":if(this.fieldDef.structSource.method==="nested"||this.fieldDef.structSource.method==="lastStage")return this.fieldDef.name;if(this.fieldDef.structSource.method==="subquery")return`(${this.fieldDef.structSource.sqlBlock.selectStr})`;throw new Error("Internal Error: Unknown structSource type 'sql' method");case"nested":return"UNNEST(this.fieldDef.name)";case"inline":return"";case"query":{const n=(0,Ze.getIdentifier)(this.fieldDef);if(n.includes("cache")){const r=new U2(!0,e);return this.model.loadQuery(this.fieldDef.structSource.query,r,!1,!1),r.addPDT(n,this.dialect)}else return this.model.loadQuery(this.fieldDef.structSource.query,e,!1,!0).lastStageName}default:throw new Error(`unknown structSource ${this.fieldDef}`)}}root(){return this.parent===void 0?this:this.parent.root()}primaryKey(){if(this.fieldDef.primaryKey)return this.getDimensionByName([this.fieldDef.primaryKey])}getChildByName(e){return this.nameMap.get(e)}getFieldByName(e){return e.reduce((n,r)=>{const i=n.getChildByName(r);if(i===void 0)throw new Error(e.length===1?`'${r}' not found`:`'${r}' not found in '${e.join(".")}'`);return i},this)}getQueryFieldByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)throw new Error(`Cannot reference ${e} as a scalar'`);return n}getQueryFieldReference(e,n){const r=this.getQueryFieldByName(e);if(n!==void 0){const i={...r.fieldDef};return i.annotation=n,this.makeQueryField(i)}return r}getDimensionOrMeasureByName(e){const n=this.getFieldByName(e);if(n instanceof Dr)return n;throw new Error(`${e} is not of type a scalar'`)}getDimensionByName(e){const n=this.getFieldByName(e);if(n instanceof Dr&&hr(n))return n;throw new Error(`${e} is not of type a scalar'`)}getStructByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)return n;throw new Error(`Error: Path to structure not found '${e.join(".")}'`)}getDistinctKey(){if(this.fieldDef.structRelationship.type!=="inline")return this.getDimensionByName(["__distinct_key"]);if(this.parent)return this.parent.getDistinctKey();throw new Error("Internal Error. inline struct can not be top level")}applyStructFiltersToTurtleDef(e){let n=e.pipeline;const r=e.annotation,i=e.filterList||[];return n=structuredClone(n),n[0].filterList=i.concat(n[0].filterList||[],this.fieldDef.filterList||[]),{type:"turtle",name:e.name,pipeline:n,annotation:r,location:e.location}}}class X${constructor(e){this.dialect=new Ffe.StandardSQLDialect,this.modelDef=void 0,this.structs=new Map,this.exploreSearchSQLMap=new Map,e&&this.loadModelFromDef(e)}loadModelFromDef(e){this.modelDef=e;for(const n of Object.values(this.modelDef.contents)){let r;if(n.type==="struct")r=new Nr(n,void 0,{model:this}),this.structs.set((0,Ze.getIdentifier)(n),r),r.resolveQueryFields();else if(n.type!=="query")throw new Error("Internal Error: Unknown structure type")}}getStructByName(e){let n;if(n=this.structs.get(e))return n;throw new Error(`Struct ${e} not found in model.`)}getStructFromRef(e,n){var r;let i;if(typeof e=="string"){const s=this.getStructByName(e);return n!==void 0?new Nr(s.fieldDef,n,(r=s.parent)!==null&&r!==void 0?r:{model:this}):s}else if(e.type==="struct")i=e;else throw new Error("Broken for now");return new Nr(i,n,{model:this})}loadQuery(e,n,r=!1,i=!1){const s="";n||(n=new U2(!0,void 0));const o={type:"turtle",name:"ignoreme",pipeline:e.pipeline,filterList:e.filterList},u=Cs.makeQuery(o,this.getStructFromRef(e.structRef,e.sourceArguments),n,i),a=u.generateSQLFromPipeline(n);if(r&&u.parent.dialect.hasFinalStage){const l=(0,Ze.getPhysicalFields)(a.outputStruct).map(c=>u.parent.dialect.sqlMaybeQuoteIdentifier(c.name));a.lastStageName=n.addStage(u.parent.dialect.sqlFinalStage(a.lastStageName,l))}return{lastStageName:a.lastStageName,malloy:s,stageWriter:n,structs:[a.outputStruct],connectionName:u.parent.connectionName}}compileQuery(e,n=!0){const i=this.loadQuery(e,void 0,n,!1),s=typeof e.structRef=="string"?e.structRef:e.structRef.type==="struct"?e.structRef.as||e.structRef.name:"(need to figure this out)";return n&&this.dialect.hasFinalStage&&(i.lastStageName=i.stageWriter.addStage(this.dialect.sqlFinalStage(i.lastStageName,[]))),{lastStageName:i.lastStageName,malloy:i.malloy,sql:i.stageWriter.generateSQLStages(),structs:i.structs,sourceExplore:s,sourceFilters:e.filterList,queryName:e.name,connectionName:i.connectionName,annotation:e.annotation}}async searchIndex(e,n,r,i=1e3,s=void 0){if(!e.canPersist())return;const o=this.getStructByName(n);let u=[];for(const[y,E]of o.nameMap)E instanceof Nr||hr(E)&&E.includeInWildcard()&&u.push({type:"fieldref",path:[y]});u=u.sort((y,E)=>y.path[0].localeCompare(E.path[0]));const a={structRef:n,pipeline:[{type:"index",indexFields:u,sample:o.dialect.defaultSampling}]},l=o.dialect.sqlMaybeQuoteIdentifier("fieldName"),c=o.dialect.sqlMaybeQuoteIdentifier("fieldPath"),f=o.dialect.sqlMaybeQuoteIdentifier("fieldValue"),x=o.dialect.sqlMaybeQuoteIdentifier("fieldType"),h=o.dialect.sqlMaybeQuoteIdentifier("weight");let p=this.exploreSearchSQLMap.get(n);p===void 0&&(p=this.compileQuery(a,!1).sql,this.exploreSearchSQLMap.set(n,p));let m=`SELECT
377
+ `)),this.resultStage}getResultStructDef(){const e={type:"struct",name:this.resultStage||"result",dialect:this.parent.fieldDef.dialect,fields:[{type:"string",name:"fieldName"},{type:"string",name:"fieldPath"},{type:"string",name:"fieldValue"},{type:"string",name:"fieldType"},{type:"number",name:"weight",numberType:"integer"}],structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"}};return this.parent.fieldDef.modelAnnotation&&(e.modelAnnotation=this.parent.fieldDef.modelAnnotation),e}}class Nr extends Q${constructor(e,n,r){if(super(e),this.sourceArguments=n,this.nameMap=new Map,this._arguments=void 0,this.setParent(r),"model"in r)if(this.model=r.model,this.pathAliasMap=new Map,e.structRelationship.type==="basetable")this.connectionName=e.structRelationship.connectionName;else throw new Error("All root StructDefs should be a baseTable");else this.model=this.getModel(),this.pathAliasMap=this.root().pathAliasMap,this.connectionName=this.root().connectionName;this.fieldDef=e,this.dialect=(0,wfe.getDialect)(this.fieldDef.dialect),this.addFieldsFromFieldList(this.fieldDef.fields)}resolveParentParameterReferences(e){return{...e,value:e.value===null?null:(0,an.exprMap)(e.value,n=>{if(n.node==="parameter"){if(this.parent===void 0)throw new Error("No parent from which to retrieve parameter value");const r=this.parent.arguments()[n.path[0]],i=this.parent.resolveParentParameterReferences(r);if(i.value===null)throw new Error("Invalid parameter value");return i.value}return n})}}arguments(){var e;if(this._arguments!==void 0)return this._arguments;this._arguments={};const n=(e=this.fieldDef.parameters)!==null&&e!==void 0?e:{};for(const i in n)this._arguments[i]=n[i];const r={...this.fieldDef.arguments,...this.sourceArguments};for(const i in r){const s=r[i];this._arguments[i]=this.resolveParentParameterReferences(s)}return this._arguments}addFieldsFromFieldList(e){for(const n of e){const r=(0,Ze.getIdentifier)(n);switch(n.type){case"struct":{this.addFieldToNameMap(r,new Nr(n,void 0,{struct:this}));break}case"turtle":{this.addFieldToNameMap(r,Cs.makeQuery(n,this,void 0,!1));break}default:this.addFieldToNameMap(r,this.makeQueryField(n))}}this.nameMap.has("__distinct_key")||this.addFieldToNameMap("__distinct_key",new jfe({type:"string",name:"__distinct_key"},this))}getAliasIdentifier(){const e=this.getFullOutputName(),n=this.pathAliasMap.get(e);if(n===void 0){const r=Array.from(this.pathAliasMap.values()),i=Lfe((0,Ze.getIdentifier)(this.fieldDef));let s=`${i}_0`,o=1;for(;r.includes(s)&&o<1e3;)o++,s=`${i}_${o}`;if(o<1e3)return this.pathAliasMap.set(e,s),s;throw new Error("Internal Error: cannot create unique alias name")}else return n}getSQLIdentifier(){return this.unnestWithNumbers()&&this.parent!==void 0?this.parent.getSQLIdentifier()+"."+(0,Ze.getIdentifier)(this.fieldDef)+`[${this.getIdentifier()}.__row_id]`:this.getIdentifier()}getIdentifier(){return this.fieldDef.structRelationship.type==="basetable"?"base":this.fieldDef.structRelationship.type==="inline"&&this.parent?this.parent.getSQLIdentifier()+"."+super.getIdentifier():this.getAliasIdentifier()}getFullOutputName(){return this.parent?this.parent.getFullOutputName()+(0,Ze.getIdentifier)(this.fieldDef)+".":""}needsSymetricCalculation(e){const n=this.getJoinableParent().getIdentifier(),r=e.root().joins.get(n);if(r)return!r.leafiest;throw new Error(`Join ${n} not found in result set`)}unnestWithNumbers(){return this.dialect.unnestWithNumbers&&this.fieldDef.structRelationship.type==="nested"}getJoinableParent(){if(this.fieldDef.structRelationship.type==="inline"){if(this.parent)return this.parent.getJoinableParent();throw new Error("Internal Error: inline struct cannot be root")}return this}addFieldToNameMap(e,n){if(this.nameMap.has(e))throw new Error(`Redefinition of ${e}`);this.nameMap.set(e,n)}getPrimaryKeyField(e){let n;if(n=this.primaryKey())return n;throw new Error(`Missing primary key for ${e}`)}resolveQueryFields(){if(this.fieldDef.structSource.type==="query"){const e=this.model.loadQuery(this.fieldDef.structSource.query,void 0).structs.pop();if(!e)throw new Error("Internal Error, query didn't produce a struct");const n={...this.fieldDef};for(const r of e.fields){let i;this.nameMap.has(i=(0,Ze.getIdentifier)(r))||(n.fields.push(r),this.nameMap.set(i,this.makeQueryField(r)))}this.fieldDef=n,!this.fieldDef.primaryKey&&e.primaryKey&&(this.fieldDef.primaryKey=e.primaryKey)}for(const[,e]of this.nameMap)e instanceof Nr&&e.resolveQueryFields()}getModel(){if(this.model)return this.model;if(this.parent===void 0)throw new Error("Expected this query struct to have a parent, as no model was present.");return this.parent.getModel()}setParent(e){"struct"in e&&(this.parent=e.struct),"model"in e?this.model=e.model:this.model=this.getModel()}makeQueryField(e){switch(e.type){case"string":return new Bfe(e,this);case"date":return new qT(e,this);case"timestamp":return new zT(e,this);case"number":return new $fe(e,this);case"boolean":return new UT(e,this);case"json":return new kfe(e,this);case"sql native":return new Pfe(e,this);case"turtle":return new Y$(e,this);default:throw new Error(`unknown field definition ${JSON.stringify(e)}`)}}structSourceSQL(e){switch(this.fieldDef.structSource.type){case"table":{const n=this.fieldDef.structSource.tablePath;return this.dialect.quoteTablePath(n)}case"sql":if(this.fieldDef.structSource.method==="nested"||this.fieldDef.structSource.method==="lastStage")return this.fieldDef.name;if(this.fieldDef.structSource.method==="subquery")return`(${this.fieldDef.structSource.sqlBlock.selectStr})`;throw new Error("Internal Error: Unknown structSource type 'sql' method");case"nested":return"UNNEST(this.fieldDef.name)";case"inline":return"";case"query":{const n=(0,Ze.getIdentifier)(this.fieldDef);if(n.includes("cache")){const r=new U2(!0,e);return this.model.loadQuery(this.fieldDef.structSource.query,r,!1,!1),r.addPDT(n,this.dialect)}else return this.model.loadQuery(this.fieldDef.structSource.query,e,!1,!0).lastStageName}default:throw new Error(`unknown structSource ${this.fieldDef}`)}}root(){return this.parent===void 0?this:this.parent.root()}primaryKey(){if(this.fieldDef.primaryKey)return this.getDimensionByName([this.fieldDef.primaryKey])}getChildByName(e){return this.nameMap.get(e)}getFieldByName(e){return e.reduce((n,r)=>{const i=n.getChildByName(r);if(i===void 0)throw new Error(e.length===1?`'${r}' not found`:`'${r}' not found in '${e.join(".")}'`);return i},this)}getQueryFieldByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)throw new Error(`Cannot reference ${e} as a scalar'`);return n}getQueryFieldReference(e,n){const r=this.getQueryFieldByName(e);if(n){const i={...r.fieldDef};i.annotation=n,r.fieldDef=i}return r}getDimensionOrMeasureByName(e){const n=this.getFieldByName(e);if(n instanceof Dr)return n;throw new Error(`${e} is not of type a scalar'`)}getDimensionByName(e){const n=this.getFieldByName(e);if(n instanceof Dr&&hr(n))return n;throw new Error(`${e} is not of type a scalar'`)}getStructByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)return n;throw new Error(`Error: Path to structure not found '${e.join(".")}'`)}getDistinctKey(){if(this.fieldDef.structRelationship.type!=="inline")return this.getDimensionByName(["__distinct_key"]);if(this.parent)return this.parent.getDistinctKey();throw new Error("Internal Error. inline struct can not be top level")}applyStructFiltersToTurtleDef(e){let n=e.pipeline;const r=e.annotation,i=e.filterList||[];return n=structuredClone(n),n[0].filterList=i.concat(n[0].filterList||[],this.fieldDef.filterList||[]),{type:"turtle",name:e.name,pipeline:n,annotation:r,location:e.location}}}class X${constructor(e){this.dialect=new Ffe.StandardSQLDialect,this.modelDef=void 0,this.structs=new Map,this.exploreSearchSQLMap=new Map,e&&this.loadModelFromDef(e)}loadModelFromDef(e){this.modelDef=e;for(const n of Object.values(this.modelDef.contents)){let r;if(n.type==="struct")r=new Nr(n,void 0,{model:this}),this.structs.set((0,Ze.getIdentifier)(n),r),r.resolveQueryFields();else if(n.type!=="query")throw new Error("Internal Error: Unknown structure type")}}getStructByName(e){let n;if(n=this.structs.get(e))return n;throw new Error(`Struct ${e} not found in model.`)}getStructFromRef(e,n){var r;let i;if(typeof e=="string"){const s=this.getStructByName(e);return n!==void 0?new Nr(s.fieldDef,n,(r=s.parent)!==null&&r!==void 0?r:{model:this}):s}else if(e.type==="struct")i=e;else throw new Error("Broken for now");return new Nr(i,n,{model:this})}loadQuery(e,n,r=!1,i=!1){const s="";n||(n=new U2(!0,void 0));const o={type:"turtle",name:"ignoreme",pipeline:e.pipeline,filterList:e.filterList},u=Cs.makeQuery(o,this.getStructFromRef(e.structRef,e.sourceArguments),n,i),a=u.generateSQLFromPipeline(n);if(r&&u.parent.dialect.hasFinalStage){const l=(0,Ze.getPhysicalFields)(a.outputStruct).map(c=>u.parent.dialect.sqlMaybeQuoteIdentifier(c.name));a.lastStageName=n.addStage(u.parent.dialect.sqlFinalStage(a.lastStageName,l))}return{lastStageName:a.lastStageName,malloy:s,stageWriter:n,structs:[a.outputStruct],connectionName:u.parent.connectionName}}compileQuery(e,n=!0){const i=this.loadQuery(e,void 0,n,!1),s=typeof e.structRef=="string"?e.structRef:e.structRef.type==="struct"?e.structRef.as||e.structRef.name:"(need to figure this out)";return n&&this.dialect.hasFinalStage&&(i.lastStageName=i.stageWriter.addStage(this.dialect.sqlFinalStage(i.lastStageName,[]))),{lastStageName:i.lastStageName,malloy:i.malloy,sql:i.stageWriter.generateSQLStages(),structs:i.structs,sourceExplore:s,sourceFilters:e.filterList,queryName:e.name,connectionName:i.connectionName,annotation:e.annotation}}async searchIndex(e,n,r,i=1e3,s=void 0){if(!e.canPersist())return;const o=this.getStructByName(n);let u=[];for(const[y,E]of o.nameMap)E instanceof Nr||hr(E)&&E.includeInWildcard()&&u.push({type:"fieldref",path:[y]});u=u.sort((y,E)=>y.path[0].localeCompare(E.path[0]));const a={structRef:n,pipeline:[{type:"index",indexFields:u,sample:o.dialect.defaultSampling}]},l=o.dialect.sqlMaybeQuoteIdentifier("fieldName"),c=o.dialect.sqlMaybeQuoteIdentifier("fieldPath"),f=o.dialect.sqlMaybeQuoteIdentifier("fieldValue"),x=o.dialect.sqlMaybeQuoteIdentifier("fieldType"),h=o.dialect.sqlMaybeQuoteIdentifier("weight");let p=this.exploreSearchSQLMap.get(n);p===void 0&&(p=this.compileQuery(a,!1).sql,this.exploreSearchSQLMap.set(n,p));let m=`SELECT
378
378
  ${l},
379
379
  ${c},
380
380
  ${f},
@@ -6693,9 +6693,9 @@ class Ir extends NQ {
6693
6693
  }
6694
6694
  getQueryFieldReference(e, n) {
6695
6695
  const r = this.getQueryFieldByName(e);
6696
- if (n !== void 0) {
6696
+ if (n) {
6697
6697
  const i = { ...r.fieldDef };
6698
- return i.annotation = n, this.makeQueryField(i);
6698
+ i.annotation = n, r.fieldDef = i;
6699
6699
  }
6700
6700
  return r;
6701
6701
  }
@@ -374,7 +374,7 @@ FROM ${n}
374
374
  FROM ${h}
375
375
  `),this.resultStage}}class Wfe extends Cs{generateSQL(e){const n=this.parent.fieldDef.structSource;if(n.type!=="sql"||n.method!=="subquery")throw new Error("Invalid struct for QueryQueryRaw, currently only supports SQL");const r=n.sqlBlock.selectStr;return e.addStage(r)}prepare(){}getResultStructDef(){return this.parent.fieldDef}getResultMetadata(e){}}class Vfe extends Cs{constructor(e,n,r,i){super(e,n,r,i),this.stages=[],this.fieldDef=e,this.fieldsToStages()}fieldsToStages(){const e=this.firstSegment;if(this.parent.dialect.dontUnionIndex){this.stages=[e.indexFields];return}const n={};for(const r of e.indexFields){if(r.path.length>1){const i=z$(r.path.slice(0,r.path.length-1)),s=n[i];if(s===void 0){const o=this.parent.nameMap.get(r.path[0]);if(o instanceof Nr&&(o.fieldDef.structRelationship.type==="many"||o.fieldDef.structRelationship.type==="nested")&&o.fieldDef.fields.length>1){const u=[r];n[i]=u,this.stages.push(u);continue}}else{s.push(r);continue}}this.stages[0]===void 0&&(this.stages[0]=[]),this.stages[0].push(r)}}expandFields(e){}generateSQL(e){const n=this.firstSegment,r=[];for(const i of this.stages){const s=new Gfe({...this.fieldDef,pipeline:[{...n,indexFields:i}]},this.parent,e,this.isJoinedSubquery);s.prepare(e);const o=s.generateSQL(e);r.push(o)}return r.length===1?this.resultStage=r[0]:this.resultStage=e.addStage(r.map(i=>`SELECT * FROM ${i}
376
376
  `).join(` UNION ALL
377
- `)),this.resultStage}getResultStructDef(){const e={type:"struct",name:this.resultStage||"result",dialect:this.parent.fieldDef.dialect,fields:[{type:"string",name:"fieldName"},{type:"string",name:"fieldPath"},{type:"string",name:"fieldValue"},{type:"string",name:"fieldType"},{type:"number",name:"weight",numberType:"integer"}],structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"}};return this.parent.fieldDef.modelAnnotation&&(e.modelAnnotation=this.parent.fieldDef.modelAnnotation),e}}class Nr extends H${constructor(e,n,r){if(super(e),this.sourceArguments=n,this.nameMap=new Map,this._arguments=void 0,this.setParent(r),"model"in r)if(this.model=r.model,this.pathAliasMap=new Map,e.structRelationship.type==="basetable")this.connectionName=e.structRelationship.connectionName;else throw new Error("All root StructDefs should be a baseTable");else this.model=this.getModel(),this.pathAliasMap=this.root().pathAliasMap,this.connectionName=this.root().connectionName;this.fieldDef=e,this.dialect=(0,wfe.getDialect)(this.fieldDef.dialect),this.addFieldsFromFieldList(this.fieldDef.fields)}resolveParentParameterReferences(e){return{...e,value:e.value===null?null:(0,an.exprMap)(e.value,n=>{if(n.node==="parameter"){if(this.parent===void 0)throw new Error("No parent from which to retrieve parameter value");const r=this.parent.arguments()[n.path[0]],i=this.parent.resolveParentParameterReferences(r);if(i.value===null)throw new Error("Invalid parameter value");return i.value}return n})}}arguments(){var e;if(this._arguments!==void 0)return this._arguments;this._arguments={};const n=(e=this.fieldDef.parameters)!==null&&e!==void 0?e:{};for(const i in n)this._arguments[i]=n[i];const r={...this.fieldDef.arguments,...this.sourceArguments};for(const i in r){const s=r[i];this._arguments[i]=this.resolveParentParameterReferences(s)}return this._arguments}addFieldsFromFieldList(e){for(const n of e){const r=(0,Ze.getIdentifier)(n);switch(n.type){case"struct":{this.addFieldToNameMap(r,new Nr(n,void 0,{struct:this}));break}case"turtle":{this.addFieldToNameMap(r,Cs.makeQuery(n,this,void 0,!1));break}default:this.addFieldToNameMap(r,this.makeQueryField(n))}}this.nameMap.has("__distinct_key")||this.addFieldToNameMap("__distinct_key",new jfe({type:"string",name:"__distinct_key"},this))}getAliasIdentifier(){const e=this.getFullOutputName(),n=this.pathAliasMap.get(e);if(n===void 0){const r=Array.from(this.pathAliasMap.values()),i=Lfe((0,Ze.getIdentifier)(this.fieldDef));let s=`${i}_0`,o=1;for(;r.includes(s)&&o<1e3;)o++,s=`${i}_${o}`;if(o<1e3)return this.pathAliasMap.set(e,s),s;throw new Error("Internal Error: cannot create unique alias name")}else return n}getSQLIdentifier(){return this.unnestWithNumbers()&&this.parent!==void 0?this.parent.getSQLIdentifier()+"."+(0,Ze.getIdentifier)(this.fieldDef)+`[${this.getIdentifier()}.__row_id]`:this.getIdentifier()}getIdentifier(){return this.fieldDef.structRelationship.type==="basetable"?"base":this.fieldDef.structRelationship.type==="inline"&&this.parent?this.parent.getSQLIdentifier()+"."+super.getIdentifier():this.getAliasIdentifier()}getFullOutputName(){return this.parent?this.parent.getFullOutputName()+(0,Ze.getIdentifier)(this.fieldDef)+".":""}needsSymetricCalculation(e){const n=this.getJoinableParent().getIdentifier(),r=e.root().joins.get(n);if(r)return!r.leafiest;throw new Error(`Join ${n} not found in result set`)}unnestWithNumbers(){return this.dialect.unnestWithNumbers&&this.fieldDef.structRelationship.type==="nested"}getJoinableParent(){if(this.fieldDef.structRelationship.type==="inline"){if(this.parent)return this.parent.getJoinableParent();throw new Error("Internal Error: inline struct cannot be root")}return this}addFieldToNameMap(e,n){if(this.nameMap.has(e))throw new Error(`Redefinition of ${e}`);this.nameMap.set(e,n)}getPrimaryKeyField(e){let n;if(n=this.primaryKey())return n;throw new Error(`Missing primary key for ${e}`)}resolveQueryFields(){if(this.fieldDef.structSource.type==="query"){const e=this.model.loadQuery(this.fieldDef.structSource.query,void 0).structs.pop();if(!e)throw new Error("Internal Error, query didn't produce a struct");const n={...this.fieldDef};for(const r of e.fields){let i;this.nameMap.has(i=(0,Ze.getIdentifier)(r))||(n.fields.push(r),this.nameMap.set(i,this.makeQueryField(r)))}this.fieldDef=n,!this.fieldDef.primaryKey&&e.primaryKey&&(this.fieldDef.primaryKey=e.primaryKey)}for(const[,e]of this.nameMap)e instanceof Nr&&e.resolveQueryFields()}getModel(){if(this.model)return this.model;if(this.parent===void 0)throw new Error("Expected this query struct to have a parent, as no model was present.");return this.parent.getModel()}setParent(e){"struct"in e&&(this.parent=e.struct),"model"in e?this.model=e.model:this.model=this.getModel()}makeQueryField(e){switch(e.type){case"string":return new Bfe(e,this);case"date":return new UT(e,this);case"timestamp":return new qT(e,this);case"number":return new $fe(e,this);case"boolean":return new jT(e,this);case"json":return new kfe(e,this);case"sql native":return new Pfe(e,this);case"turtle":return new V$(e,this);default:throw new Error(`unknown field definition ${JSON.stringify(e)}`)}}structSourceSQL(e){switch(this.fieldDef.structSource.type){case"table":{const n=this.fieldDef.structSource.tablePath;return this.dialect.quoteTablePath(n)}case"sql":if(this.fieldDef.structSource.method==="nested"||this.fieldDef.structSource.method==="lastStage")return this.fieldDef.name;if(this.fieldDef.structSource.method==="subquery")return`(${this.fieldDef.structSource.sqlBlock.selectStr})`;throw new Error("Internal Error: Unknown structSource type 'sql' method");case"nested":return"UNNEST(this.fieldDef.name)";case"inline":return"";case"query":{const n=(0,Ze.getIdentifier)(this.fieldDef);if(n.includes("cache")){const r=new M2(!0,e);return this.model.loadQuery(this.fieldDef.structSource.query,r,!1,!1),r.addPDT(n,this.dialect)}else return this.model.loadQuery(this.fieldDef.structSource.query,e,!1,!0).lastStageName}default:throw new Error(`unknown structSource ${this.fieldDef}`)}}root(){return this.parent===void 0?this:this.parent.root()}primaryKey(){if(this.fieldDef.primaryKey)return this.getDimensionByName([this.fieldDef.primaryKey])}getChildByName(e){return this.nameMap.get(e)}getFieldByName(e){return e.reduce((n,r)=>{const i=n.getChildByName(r);if(i===void 0)throw new Error(e.length===1?`'${r}' not found`:`'${r}' not found in '${e.join(".")}'`);return i},this)}getQueryFieldByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)throw new Error(`Cannot reference ${e} as a scalar'`);return n}getQueryFieldReference(e,n){const r=this.getQueryFieldByName(e);if(n!==void 0){const i={...r.fieldDef};return i.annotation=n,this.makeQueryField(i)}return r}getDimensionOrMeasureByName(e){const n=this.getFieldByName(e);if(n instanceof Dr)return n;throw new Error(`${e} is not of type a scalar'`)}getDimensionByName(e){const n=this.getFieldByName(e);if(n instanceof Dr&&hr(n))return n;throw new Error(`${e} is not of type a scalar'`)}getStructByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)return n;throw new Error(`Error: Path to structure not found '${e.join(".")}'`)}getDistinctKey(){if(this.fieldDef.structRelationship.type!=="inline")return this.getDimensionByName(["__distinct_key"]);if(this.parent)return this.parent.getDistinctKey();throw new Error("Internal Error. inline struct can not be top level")}applyStructFiltersToTurtleDef(e){let n=e.pipeline;const r=e.annotation,i=e.filterList||[];return n=structuredClone(n),n[0].filterList=i.concat(n[0].filterList||[],this.fieldDef.filterList||[]),{type:"turtle",name:e.name,pipeline:n,annotation:r,location:e.location}}}class Y${constructor(e){this.dialect=new Ffe.StandardSQLDialect,this.modelDef=void 0,this.structs=new Map,this.exploreSearchSQLMap=new Map,e&&this.loadModelFromDef(e)}loadModelFromDef(e){this.modelDef=e;for(const n of Object.values(this.modelDef.contents)){let r;if(n.type==="struct")r=new Nr(n,void 0,{model:this}),this.structs.set((0,Ze.getIdentifier)(n),r),r.resolveQueryFields();else if(n.type!=="query")throw new Error("Internal Error: Unknown structure type")}}getStructByName(e){let n;if(n=this.structs.get(e))return n;throw new Error(`Struct ${e} not found in model.`)}getStructFromRef(e,n){var r;let i;if(typeof e=="string"){const s=this.getStructByName(e);return n!==void 0?new Nr(s.fieldDef,n,(r=s.parent)!==null&&r!==void 0?r:{model:this}):s}else if(e.type==="struct")i=e;else throw new Error("Broken for now");return new Nr(i,n,{model:this})}loadQuery(e,n,r=!1,i=!1){const s="";n||(n=new M2(!0,void 0));const o={type:"turtle",name:"ignoreme",pipeline:e.pipeline,filterList:e.filterList},u=Cs.makeQuery(o,this.getStructFromRef(e.structRef,e.sourceArguments),n,i),a=u.generateSQLFromPipeline(n);if(r&&u.parent.dialect.hasFinalStage){const l=(0,Ze.getPhysicalFields)(a.outputStruct).map(c=>u.parent.dialect.sqlMaybeQuoteIdentifier(c.name));a.lastStageName=n.addStage(u.parent.dialect.sqlFinalStage(a.lastStageName,l))}return{lastStageName:a.lastStageName,malloy:s,stageWriter:n,structs:[a.outputStruct],connectionName:u.parent.connectionName}}compileQuery(e,n=!0){const i=this.loadQuery(e,void 0,n,!1),s=typeof e.structRef=="string"?e.structRef:e.structRef.type==="struct"?e.structRef.as||e.structRef.name:"(need to figure this out)";return n&&this.dialect.hasFinalStage&&(i.lastStageName=i.stageWriter.addStage(this.dialect.sqlFinalStage(i.lastStageName,[]))),{lastStageName:i.lastStageName,malloy:i.malloy,sql:i.stageWriter.generateSQLStages(),structs:i.structs,sourceExplore:s,sourceFilters:e.filterList,queryName:e.name,connectionName:i.connectionName,annotation:e.annotation}}async searchIndex(e,n,r,i=1e3,s=void 0){if(!e.canPersist())return;const o=this.getStructByName(n);let u=[];for(const[y,E]of o.nameMap)E instanceof Nr||hr(E)&&E.includeInWildcard()&&u.push({type:"fieldref",path:[y]});u=u.sort((y,E)=>y.path[0].localeCompare(E.path[0]));const a={structRef:n,pipeline:[{type:"index",indexFields:u,sample:o.dialect.defaultSampling}]},l=o.dialect.sqlMaybeQuoteIdentifier("fieldName"),c=o.dialect.sqlMaybeQuoteIdentifier("fieldPath"),f=o.dialect.sqlMaybeQuoteIdentifier("fieldValue"),x=o.dialect.sqlMaybeQuoteIdentifier("fieldType"),h=o.dialect.sqlMaybeQuoteIdentifier("weight");let p=this.exploreSearchSQLMap.get(n);p===void 0&&(p=this.compileQuery(a,!1).sql,this.exploreSearchSQLMap.set(n,p));let m=`SELECT
377
+ `)),this.resultStage}getResultStructDef(){const e={type:"struct",name:this.resultStage||"result",dialect:this.parent.fieldDef.dialect,fields:[{type:"string",name:"fieldName"},{type:"string",name:"fieldPath"},{type:"string",name:"fieldValue"},{type:"string",name:"fieldType"},{type:"number",name:"weight",numberType:"integer"}],structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"}};return this.parent.fieldDef.modelAnnotation&&(e.modelAnnotation=this.parent.fieldDef.modelAnnotation),e}}class Nr extends H${constructor(e,n,r){if(super(e),this.sourceArguments=n,this.nameMap=new Map,this._arguments=void 0,this.setParent(r),"model"in r)if(this.model=r.model,this.pathAliasMap=new Map,e.structRelationship.type==="basetable")this.connectionName=e.structRelationship.connectionName;else throw new Error("All root StructDefs should be a baseTable");else this.model=this.getModel(),this.pathAliasMap=this.root().pathAliasMap,this.connectionName=this.root().connectionName;this.fieldDef=e,this.dialect=(0,wfe.getDialect)(this.fieldDef.dialect),this.addFieldsFromFieldList(this.fieldDef.fields)}resolveParentParameterReferences(e){return{...e,value:e.value===null?null:(0,an.exprMap)(e.value,n=>{if(n.node==="parameter"){if(this.parent===void 0)throw new Error("No parent from which to retrieve parameter value");const r=this.parent.arguments()[n.path[0]],i=this.parent.resolveParentParameterReferences(r);if(i.value===null)throw new Error("Invalid parameter value");return i.value}return n})}}arguments(){var e;if(this._arguments!==void 0)return this._arguments;this._arguments={};const n=(e=this.fieldDef.parameters)!==null&&e!==void 0?e:{};for(const i in n)this._arguments[i]=n[i];const r={...this.fieldDef.arguments,...this.sourceArguments};for(const i in r){const s=r[i];this._arguments[i]=this.resolveParentParameterReferences(s)}return this._arguments}addFieldsFromFieldList(e){for(const n of e){const r=(0,Ze.getIdentifier)(n);switch(n.type){case"struct":{this.addFieldToNameMap(r,new Nr(n,void 0,{struct:this}));break}case"turtle":{this.addFieldToNameMap(r,Cs.makeQuery(n,this,void 0,!1));break}default:this.addFieldToNameMap(r,this.makeQueryField(n))}}this.nameMap.has("__distinct_key")||this.addFieldToNameMap("__distinct_key",new jfe({type:"string",name:"__distinct_key"},this))}getAliasIdentifier(){const e=this.getFullOutputName(),n=this.pathAliasMap.get(e);if(n===void 0){const r=Array.from(this.pathAliasMap.values()),i=Lfe((0,Ze.getIdentifier)(this.fieldDef));let s=`${i}_0`,o=1;for(;r.includes(s)&&o<1e3;)o++,s=`${i}_${o}`;if(o<1e3)return this.pathAliasMap.set(e,s),s;throw new Error("Internal Error: cannot create unique alias name")}else return n}getSQLIdentifier(){return this.unnestWithNumbers()&&this.parent!==void 0?this.parent.getSQLIdentifier()+"."+(0,Ze.getIdentifier)(this.fieldDef)+`[${this.getIdentifier()}.__row_id]`:this.getIdentifier()}getIdentifier(){return this.fieldDef.structRelationship.type==="basetable"?"base":this.fieldDef.structRelationship.type==="inline"&&this.parent?this.parent.getSQLIdentifier()+"."+super.getIdentifier():this.getAliasIdentifier()}getFullOutputName(){return this.parent?this.parent.getFullOutputName()+(0,Ze.getIdentifier)(this.fieldDef)+".":""}needsSymetricCalculation(e){const n=this.getJoinableParent().getIdentifier(),r=e.root().joins.get(n);if(r)return!r.leafiest;throw new Error(`Join ${n} not found in result set`)}unnestWithNumbers(){return this.dialect.unnestWithNumbers&&this.fieldDef.structRelationship.type==="nested"}getJoinableParent(){if(this.fieldDef.structRelationship.type==="inline"){if(this.parent)return this.parent.getJoinableParent();throw new Error("Internal Error: inline struct cannot be root")}return this}addFieldToNameMap(e,n){if(this.nameMap.has(e))throw new Error(`Redefinition of ${e}`);this.nameMap.set(e,n)}getPrimaryKeyField(e){let n;if(n=this.primaryKey())return n;throw new Error(`Missing primary key for ${e}`)}resolveQueryFields(){if(this.fieldDef.structSource.type==="query"){const e=this.model.loadQuery(this.fieldDef.structSource.query,void 0).structs.pop();if(!e)throw new Error("Internal Error, query didn't produce a struct");const n={...this.fieldDef};for(const r of e.fields){let i;this.nameMap.has(i=(0,Ze.getIdentifier)(r))||(n.fields.push(r),this.nameMap.set(i,this.makeQueryField(r)))}this.fieldDef=n,!this.fieldDef.primaryKey&&e.primaryKey&&(this.fieldDef.primaryKey=e.primaryKey)}for(const[,e]of this.nameMap)e instanceof Nr&&e.resolveQueryFields()}getModel(){if(this.model)return this.model;if(this.parent===void 0)throw new Error("Expected this query struct to have a parent, as no model was present.");return this.parent.getModel()}setParent(e){"struct"in e&&(this.parent=e.struct),"model"in e?this.model=e.model:this.model=this.getModel()}makeQueryField(e){switch(e.type){case"string":return new Bfe(e,this);case"date":return new UT(e,this);case"timestamp":return new qT(e,this);case"number":return new $fe(e,this);case"boolean":return new jT(e,this);case"json":return new kfe(e,this);case"sql native":return new Pfe(e,this);case"turtle":return new V$(e,this);default:throw new Error(`unknown field definition ${JSON.stringify(e)}`)}}structSourceSQL(e){switch(this.fieldDef.structSource.type){case"table":{const n=this.fieldDef.structSource.tablePath;return this.dialect.quoteTablePath(n)}case"sql":if(this.fieldDef.structSource.method==="nested"||this.fieldDef.structSource.method==="lastStage")return this.fieldDef.name;if(this.fieldDef.structSource.method==="subquery")return`(${this.fieldDef.structSource.sqlBlock.selectStr})`;throw new Error("Internal Error: Unknown structSource type 'sql' method");case"nested":return"UNNEST(this.fieldDef.name)";case"inline":return"";case"query":{const n=(0,Ze.getIdentifier)(this.fieldDef);if(n.includes("cache")){const r=new M2(!0,e);return this.model.loadQuery(this.fieldDef.structSource.query,r,!1,!1),r.addPDT(n,this.dialect)}else return this.model.loadQuery(this.fieldDef.structSource.query,e,!1,!0).lastStageName}default:throw new Error(`unknown structSource ${this.fieldDef}`)}}root(){return this.parent===void 0?this:this.parent.root()}primaryKey(){if(this.fieldDef.primaryKey)return this.getDimensionByName([this.fieldDef.primaryKey])}getChildByName(e){return this.nameMap.get(e)}getFieldByName(e){return e.reduce((n,r)=>{const i=n.getChildByName(r);if(i===void 0)throw new Error(e.length===1?`'${r}' not found`:`'${r}' not found in '${e.join(".")}'`);return i},this)}getQueryFieldByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)throw new Error(`Cannot reference ${e} as a scalar'`);return n}getQueryFieldReference(e,n){const r=this.getQueryFieldByName(e);if(n){const i={...r.fieldDef};i.annotation=n,r.fieldDef=i}return r}getDimensionOrMeasureByName(e){const n=this.getFieldByName(e);if(n instanceof Dr)return n;throw new Error(`${e} is not of type a scalar'`)}getDimensionByName(e){const n=this.getFieldByName(e);if(n instanceof Dr&&hr(n))return n;throw new Error(`${e} is not of type a scalar'`)}getStructByName(e){const n=this.getFieldByName(e);if(n instanceof Nr)return n;throw new Error(`Error: Path to structure not found '${e.join(".")}'`)}getDistinctKey(){if(this.fieldDef.structRelationship.type!=="inline")return this.getDimensionByName(["__distinct_key"]);if(this.parent)return this.parent.getDistinctKey();throw new Error("Internal Error. inline struct can not be top level")}applyStructFiltersToTurtleDef(e){let n=e.pipeline;const r=e.annotation,i=e.filterList||[];return n=structuredClone(n),n[0].filterList=i.concat(n[0].filterList||[],this.fieldDef.filterList||[]),{type:"turtle",name:e.name,pipeline:n,annotation:r,location:e.location}}}class Y${constructor(e){this.dialect=new Ffe.StandardSQLDialect,this.modelDef=void 0,this.structs=new Map,this.exploreSearchSQLMap=new Map,e&&this.loadModelFromDef(e)}loadModelFromDef(e){this.modelDef=e;for(const n of Object.values(this.modelDef.contents)){let r;if(n.type==="struct")r=new Nr(n,void 0,{model:this}),this.structs.set((0,Ze.getIdentifier)(n),r),r.resolveQueryFields();else if(n.type!=="query")throw new Error("Internal Error: Unknown structure type")}}getStructByName(e){let n;if(n=this.structs.get(e))return n;throw new Error(`Struct ${e} not found in model.`)}getStructFromRef(e,n){var r;let i;if(typeof e=="string"){const s=this.getStructByName(e);return n!==void 0?new Nr(s.fieldDef,n,(r=s.parent)!==null&&r!==void 0?r:{model:this}):s}else if(e.type==="struct")i=e;else throw new Error("Broken for now");return new Nr(i,n,{model:this})}loadQuery(e,n,r=!1,i=!1){const s="";n||(n=new M2(!0,void 0));const o={type:"turtle",name:"ignoreme",pipeline:e.pipeline,filterList:e.filterList},u=Cs.makeQuery(o,this.getStructFromRef(e.structRef,e.sourceArguments),n,i),a=u.generateSQLFromPipeline(n);if(r&&u.parent.dialect.hasFinalStage){const l=(0,Ze.getPhysicalFields)(a.outputStruct).map(c=>u.parent.dialect.sqlMaybeQuoteIdentifier(c.name));a.lastStageName=n.addStage(u.parent.dialect.sqlFinalStage(a.lastStageName,l))}return{lastStageName:a.lastStageName,malloy:s,stageWriter:n,structs:[a.outputStruct],connectionName:u.parent.connectionName}}compileQuery(e,n=!0){const i=this.loadQuery(e,void 0,n,!1),s=typeof e.structRef=="string"?e.structRef:e.structRef.type==="struct"?e.structRef.as||e.structRef.name:"(need to figure this out)";return n&&this.dialect.hasFinalStage&&(i.lastStageName=i.stageWriter.addStage(this.dialect.sqlFinalStage(i.lastStageName,[]))),{lastStageName:i.lastStageName,malloy:i.malloy,sql:i.stageWriter.generateSQLStages(),structs:i.structs,sourceExplore:s,sourceFilters:e.filterList,queryName:e.name,connectionName:i.connectionName,annotation:e.annotation}}async searchIndex(e,n,r,i=1e3,s=void 0){if(!e.canPersist())return;const o=this.getStructByName(n);let u=[];for(const[y,E]of o.nameMap)E instanceof Nr||hr(E)&&E.includeInWildcard()&&u.push({type:"fieldref",path:[y]});u=u.sort((y,E)=>y.path[0].localeCompare(E.path[0]));const a={structRef:n,pipeline:[{type:"index",indexFields:u,sample:o.dialect.defaultSampling}]},l=o.dialect.sqlMaybeQuoteIdentifier("fieldName"),c=o.dialect.sqlMaybeQuoteIdentifier("fieldPath"),f=o.dialect.sqlMaybeQuoteIdentifier("fieldValue"),x=o.dialect.sqlMaybeQuoteIdentifier("fieldType"),h=o.dialect.sqlMaybeQuoteIdentifier("weight");let p=this.exploreSearchSQLMap.get(n);p===void 0&&(p=this.compileQuery(a,!1).sql,this.exploreSearchSQLMap.set(n,p));let m=`SELECT
378
378
  ${l},
379
379
  ${c},
380
380
  ${f},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/render",
3
- "version": "0.0.188-dev240923162529",
3
+ "version": "0.0.188",
4
4
  "license": "MIT",
5
5
  "main": "dist/module/index.umd.js",
6
6
  "types": "dist/index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "build-types": "tsc --build --declaration --emitDeclarationOnly"
33
33
  },
34
34
  "dependencies": {
35
- "@malloydata/malloy": "^0.0.188-dev240923162529",
35
+ "@malloydata/malloy": "^0.0.188",
36
36
  "@tanstack/solid-virtual": "^3.10.4",
37
37
  "component-register": "^0.8.6",
38
38
  "lodash": "^4.17.20",