@malloydata/render 0.0.122-dev240202181129 → 0.0.122-dev240203152115
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.
|
@@ -5887,11 +5887,11 @@ ${(0, utils_1.indent)(sql)})
|
|
|
5887
5887
|
join3.leafiest = name === leafiest;
|
|
5888
5888
|
}
|
|
5889
5889
|
for (const [_name, join3] of this.joins) {
|
|
5890
|
-
if (
|
|
5891
|
-
|
|
5892
|
-
|
|
5893
|
-
|
|
5894
|
-
|
|
5890
|
+
if (
|
|
5891
|
+
// we have a leafiest count() joined subtree
|
|
5892
|
+
join3.leafiest && join3.parent !== void 0 && join3.uniqueKeyPossibleUses.has("count") || // or not leafiest and we use an asymetric function
|
|
5893
|
+
!join3.leafiest && join3.uniqueKeyPossibleUses.hasAsymetricFunctions()
|
|
5894
|
+
) {
|
|
5895
5895
|
let j2 = join3;
|
|
5896
5896
|
while (j2) {
|
|
5897
5897
|
if (!j2.queryStruct.primaryKey()) {
|
|
@@ -185,7 +185,7 @@ ${(0,ss.indent)(o)})
|
|
|
185
185
|
))
|
|
186
186
|
-
|
|
187
187
|
SUM(DISTINCT ${i})
|
|
188
|
-
)`}/(${s}*1.0))`;return a=`CAST(${a} as ${t.defaultNumberType})`,a}var os=class{constructor(e,r,n){this.f=e,this.fieldUsage=r,this.parent=n,this.type="field",this.additionalGroupSets=[]}root(){return this.parent.root()}getSQL(){let e=this.f.generateExpression(this.parent);return Ns(this.f)&&(e=this.f.caseGroup(this.parent.groupSet>0?this.parent.childGroups.concat(this.additionalGroupSets):[],e)),e}getAnalyticalSQL(e){return this.analyticalSQL===void 0?this.getSQL():e&&this.partitionSQL?this.partitionSQL:this.analyticalSQL}},ac=class t{constructor(e,r){this.turtleDef=e,this.parent=r,this.type="query",this.allFields=new Map,this.groupSet=0,this.depth=0,this.childGroups=[],this.hasHaving=!1,this.ungroupedSets=new Map,this.resultUsesUngrouped=!1,this.firstSegment=e.pipeline[0]}getQueryInfo(){if(!(0,qe.isIndexSegment)(this.firstSegment)&&!(0,qe.isRawSegment)(this.firstSegment)){let{queryTimezone:e}=this.firstSegment;if(e)return{queryTimezone:e}}return{}}addField(e,r,n){let i=this.allFields.get(e);if(i){if(i.type==="query")throw new Error(`Redefinition of field ${r.fieldDef.name} as struct`);if(i.fieldUsage.type==="result"){if(n.type!=="result")return;throw new Error(`Ambiguous output field name '${r.fieldDef.name}'.`)}}this.add(e,new os(r,n,this))}parentGroupSet(){return this.parent?this.parent.groupSet:0}add(e,r){this.allFields.set(e,r)}hasField(e){let r=this.allFields.get(e);return r!==void 0&&r instanceof os}getField(e){let r=this.allFields.get(e);if(r===void 0)throw new Error(`Internal Error, field Not defined ${e}`);if(r instanceof os)return r;throw new Error(`can't use a query here ${e}`)}getFieldByNumber(e){for(let[r,n]of this.allFields)if(n instanceof os&&n.fieldUsage.type==="result"&&n.fieldUsage.resultIndex===e)return{name:r,fif:n};throw new Error(`Invalid Order By index '${e}`)}computeGroups(e,r){e===0&&this.resultUsesUngrouped&&this.root().computeOnlyGroups.push(e++);for(let[o,a]of this.ungroupedSets){let u=e++;a.groupSet=u,this.root().computeOnlyGroups.push(u)}this.groupSet=e++,this.depth=r;let n=r,i=!1,s=[this.groupSet];for(let[o,a]of this.allFields)if(a.type==="query"){let u=a;if(i=!0,u.firstSegment.type==="reduce"){let l=u.computeGroups(e,r+1);s=s.concat(l.children),e=l.nextGroupSetNumber,l.maxDepth>n&&(n=l.maxDepth)}}return this.childGroups=s,{nextGroupSetNumber:e,maxDepth:n,children:s,isComplex:i}}fields(e=void 0){let r=[];for(let n of this.allFields.values())n instanceof os&&(e===void 0||e(n))&&r.push(n);return r}fieldNames(e){let r=[];for(let[n,i]of this.allFields)i instanceof os&&(e===void 0||e(i))&&r.push(n);return r}getRepeatedResultType(){let e="inline_all_numbers";for(let r of this.fields())if(r.fieldUsage.type==="result"){if(Ns(r.f))return"nested";r.f instanceof Pu&&(e="inline")}return e}structs(){let e=[];for(let r of this.allFields.values())r instanceof t&&e.push(r);return e}selectStructs(e,r){r(this)&&e.push(this);for(let n of this.structs())n.selectStructs(e,r);return e}calculateDefaultOrderBy(){let e;for(let[r,n]of this.allFields)if(n instanceof os&&n.fieldUsage.type==="result"){if(e||(e=n.fieldUsage.resultIndex),["date","timestamp"].indexOf(n.f.fieldDef.type)>-1)return[{dir:"desc",field:n.fieldUsage.resultIndex}];if(Kxe(n.f))return[{dir:"desc",field:n.fieldUsage.resultIndex}]}return e?[{dir:"asc",field:e}]:[]}addStructToJoin(e,r,n,i){var s;let o=e.getIdentifier();if(i.indexOf(o)!==-1)return;let a;if(a=this.root().joins.get(o)){a.uniqueKeyPossibleUses.add_use(n);return}let u,l=(s=e.parent)===null||s===void 0?void 0:s.getJoinableParent();l&&(this.addStructToJoin(l,r,void 0,i),u=this.root().joins.get(l.getIdentifier()));let c=e.fieldDef.structRelationship;(0,qe.isJoinOn)(c)&&e.parent&&c.onExpression!==void 0&&i.indexOf(o)===-1&&r.addDependantExpr(this,e.parent,c.onExpression,[...i,o]),(a=this.root().joins.get(o))||(a=new IG(e,o,u),this.root().joins.set(o,a)),a.uniqueKeyPossibleUses.add_use(n)}findJoins(e){for(let r of this.fields())this.addStructToJoin(r.f.getJoinableParent(),e,r.f.uniqueKeyPossibleUse(),[]);for(let r of this.structs())r.findJoins(e)}root(){if(this.parent)return this.parent.root();throw new Error("Internal Error, Null parent FieldInstanceResult")}getUngroupPartitions(e){let r=[],n=this,i=[],s=[];if(e===void 0||e.type==="all"){let a=e?.fields||[];i=this.fields(u=>Ns(u.f)&&u.fieldUsage.type==="result"&&a.indexOf(u.f.getIdentifier())===-1).map(u=>u.f.getIdentifier())}else i=e.fields;let o=!0;for(;n!==void 0;)(o||e?.type==="exclude")&&(s=s.concat(n.fields(a=>Ns(a.f)&&a.fieldUsage.type==="result").map(a=>a.f.getIdentifier()))),r=r.concat(n.fields(a=>Ns(a.f)&&a.fieldUsage.type==="result"&&i.indexOf(a.f.getIdentifier())===-1)),n=n.parent,o=!1;for(let a of e?.fields||[])if(s.indexOf(a)===-1)throw new Error(`${e?.type}(): unknown field name "${a}" or name not in scope.`);return r}assignFieldsToGroups(){for(let[e,r]of this.ungroupedSets)for(let n of this.getUngroupPartitions(r))n.additionalGroupSets.push(r.groupSet);for(let e of this.structs())e.assignFieldsToGroups()}},LG=class extends ac{constructor(e){super(e,void 0),this.joins=new Map,this.havings=new ss.AndChain,this.isComplexQuery=!1,this.queryUsesPartitioning=!1,this.computeOnlyGroups=[],this.elimatedComputeGroups=!1}root(){return this}eliminateComputeGroupsSQL(){return this.elimatedComputeGroups||this.computeOnlyGroups.length===0?"":(this.elimatedComputeGroups=!0,`group_set NOT IN (${this.computeOnlyGroups.join(",")})`)}calculateSymmetricAggregates(){let e;for(let[r,n]of this.joins)if(n.parentRelationship()==="many_to_many"||n.forceAllSymmetricCalculations())e="0never";else if(e===void 0)e=r;else if(n.parentRelationship()==="one_to_many"){let s=n.queryStruct;s.parent&&s.parent.getIdentifier()===e?e=r:e="0never"}for(let[r,n]of this.joins)n.leafiest=r===e;for(let[r,n]of this.joins)if(n.leafiest)n.parent!==void 0&&n.uniqueKeyPossibleUses.has("count")&&!n.queryStruct.primaryKey()&&(n.makeUniqueKey=!0);else if(!n.leafiest&&n.uniqueKeyPossibleUses.hasAsymetricFunctions()){let i=n;for(;i;)i.queryStruct.primaryKey()||(i.makeUniqueKey=!0),i.queryStruct.fieldDef.structRelationship.type==="nested"?i=i.parent:i=void 0}}},IG=class{constructor(e,r,n){if(this.queryStruct=e,this.alias=r,this.parent=n,this.uniqueKeyPossibleUses=new _G,this.makeUniqueKey=!1,this.leafiest=!1,this.children=[],n&&n.children.push(this),this.queryStruct.fieldDef.filterList){this.joinFilterConditions=[];for(let i of this.queryStruct.fieldDef.filterList){let s=new Ab({type:"boolean",name:"ignoreme",e:i.expression},this.queryStruct);this.joinFilterConditions.push(s)}}}parentRelationship(){if(this.queryStruct.parent===void 0)return"root";switch(this.queryStruct.fieldDef.structRelationship.type){case"one":return"many_to_one";case"cross":return"many_to_many";case"many":return"one_to_many";case"nested":return"one_to_many";case"inline":return"one_to_one";default:throw new Error(`Internal error unknown relationship type to parent for ${this.queryStruct.fieldDef.name}`)}}forceAllSymmetricCalculations(){let e=this.queryStruct.fieldDef.structRelationship;return this.queryStruct.parent===void 0||!(0,qe.isJoinOn)(e)?!1:e.matrixOperation==="right"||e.matrixOperation==="full"}getDialectFieldList(){let e=[];for(let r of this.queryStruct.fieldDef.fields.filter(qe.isPhysical))e.push({type:r.type,sqlExpression:(0,qe.getIdentifier)(r),sqlOutputName:(0,qe.getIdentifier)(r)});return e}},TF=class extends Ey{},MG=class{static nextStructDef(e,r){let n=new Pu(e,{model:new CF(void 0)}),i={type:"turtle",name:"ignoreme",pipeline:[r]};return uc.makeQuery(i,n,new j1(!0,void 0),!1).getResultStructDef()}};Sy.Segment=MG;var uc=class t extends Ey{constructor(e,r,n,i){super(e,r),this.prepared=!1,this.maxDepth=0,this.maxGroupSet=0,this.fieldDef=e,this.rootResult=new LG(e),this.stageWriter=n,this.firstSegment=e.pipeline[0],this.isJoinedSubquery=i}static makeQuery(e,r,n=void 0,i){let s=r,o=r.applyStructFiltersToTurtleDef(e),a=o.pipeline[0],u=r.fieldDef;switch(n!==void 0&&(0,qe.isQuerySegment)(a)&&a.extendSource!==void 0&&(s=new Pu({...u,fields:[...u.fields,...a.extendSource]},s.parent?{struct:s}:{model:s.model}),o={...o,pipeline:[{...a,extendSource:void 0},...o.pipeline.slice(1)]}),u.queryTimezone&&(0,qe.isQuerySegment)(a)&&a.queryTimezone===void 0&&(a.queryTimezone=u.queryTimezone),a.type){case"reduce":return new PG(o,s,n,i);case"project":return new kG(o,s,n,i);case"index":return new UG(o,s,n,i);case"raw":return new qG(o,s,n,i);case"partial":throw new Error("Attempt to make query out of partial stage")}}inNestedPipeline(){return this.parent.fieldDef.structSource.type==="sql"&&this.parent.fieldDef.structSource.method==="nested"}expandField(e){let r=e.type==="fieldref"?this.parent.getQueryFieldReference(e.path,e.annotation):this.parent.makeQueryField(e);return{as:r.getIdentifier(),field:r}}addDependantPath(e,r,n,i,s){let o=r.getFieldByName(n),a;if(o instanceof Ey)a=o.parent;else if(o instanceof Pu)a=o;else throw new Error("Internal Error: Unknown object type");e.root().addStructToJoin(a.getJoinableParent(),this,i,s)}addDependantExpr(e,r,n,i){for(let s of n)if((0,qe.isFunctionCallFragment)(s)&&(0,qe.expressionIsAnalytic)(s.overload.returnType.expressionType)&&this.parent.dialect.cantPartitionWindowFunctionsOnExpressions&&(e.root().isComplexQuery=!0,e.root().queryUsesPartitioning=!0),(0,qe.isUngroupFragment)(s)){if(e.resultUsesUngrouped=!0,e.root().isComplexQuery=!0,e.root().queryUsesPartitioning=!0,s.fields&&s.fields.length>0){let o=s.fields.sort().join("|")+s.type;e.ungroupedSets.get(o)===void 0&&e.ungroupedSets.set(o,{type:s.type,fields:s.fields,groupSet:-1})}this.addDependantExpr(e,r,s.e,i)}else if((0,qe.isFieldFragment)(s)){let o=r.getDimensionOrMeasureByName(s.path);(0,qe.hasExpression)(o.fieldDef)?this.addDependantExpr(e,o.parent,o.fieldDef.e,i):e.root().addStructToJoin(o.parent.getJoinableParent(),this,void 0,i)}else if((0,qe.isFilterFragment)(s)){for(let o of s.filterList)this.addDependantExpr(e,r,o.expression,i),this.addDependantExpr(e,r,s.e,i);this.addDependantExpr(e,r,s.e,i)}else if((0,qe.isDialectFragment)(s)){let o=[];switch(s.function){case"now":break;case"div":o.push(s.denominator),o.push(s.numerator);break;case"numberLiteral":case"timeLiteral":case"stringLiteral":case"regexpLiteral":break;case"timeDiff":o.push(s.left.value,s.right.value);break;case"delta":o.push(s.base.value,s.delta);break;case"trunc":case"extract":o.push(s.expr.value);break;case"regexpMatch":case"cast":o.push(s.expr);break;default:throw new Error("Unknown dialect Fragment type. Can't generate dependancies")}for(let a of o)this.addDependantExpr(e,r,a,i)}else if((0,qe.isAggregateFragment)(s))(0,qe.isAsymmetricFragment)(s)&&(s.structPath?this.addDependantPath(e,r,s.structPath,s.function,i):e.addStructToJoin(r,this,s.function,i)),this.addDependantExpr(e,r,s.e,i);else if((0,qe.isFunctionCallFragment)(s)){s.structPath&&this.addDependantPath(e,r,s.structPath,"generic_aggregate",i);for(let o of s.args)this.addDependantExpr(e,r,o,i);if((0,qe.expressionIsAnalytic)(s.overload.returnType.expressionType)&&(e.root().queryUsesPartitioning=!0),s.orderBy)for(let o of s.orderBy)this.addDependantExpr(e,r,o.e,i)}}addDependancies(e,r){(0,qe.hasExpression)(r.fieldDef)&&this.addDependantExpr(e,r.parent,r.fieldDef.e,[])}getSegmentFields(e){let r=e.firstSegment;return r.type==="index"?r.indexFields:(0,qe.isQuerySegment)(r)?r.queryFields:[]}expandFields(e){let r=1;for(let n of this.getSegmentFields(e)){let{as:i,field:s}=this.expandField(n);if(s instanceof TF||s instanceof t){if(this.firstSegment.type==="project")throw new Error(`Nested views cannot be used in select - '${s.fieldDef.name}'`);let o=new ac(s.fieldDef,e);this.expandFields(o),e.add(i,o)}else if(s instanceof Ti&&(e.addField(i,s,{resultIndex:r,type:"result"}),this.addDependancies(e,s),Kxe(s)&&this.firstSegment.type==="project"))throw new Error(`Aggregate Fields cannot be used in select - '${s.fieldDef.name}'`);r++}this.expandFilters(e)}expandFilters(e){if(e.firstSegment.filterList!==void 0){for(let r of e.firstSegment.filterList||[]){let n=this.parent;this.addDependantExpr(e,n,r.expression,[])}for(let r of e.root().joins.values()||[])for(let n of r.joinFilterConditions||[])n.fieldDef.type==="boolean"&&n.fieldDef.e&&this.addDependantExpr(e,n.parent,n.fieldDef.e,[])}}generateSQLFilters(e,r,n=void 0){let i=new ss.AndChain,s=n||e.firstSegment.filterList;if(s===void 0)return i;for(let o of s||[]){let a=this.parent;if(r==="having"&&(0,qe.expressionIsCalculation)(o.expressionType)||r==="where"&&(0,qe.expressionIsScalar)(o.expressionType)){let u=this.generateExpressionFromExpr(e,a,o.expression,void 0);i.add(u)}}return i}prepare(e){this.prepared||(this.expandFields(this.rootResult),this.rootResult.addStructToJoin(this.parent,this,void 0,[]),this.rootResult.findJoins(this),this.rootResult.calculateSymmetricAggregates(),this.prepared=!0)}getResultMetadata(e){if(e instanceof os){if(e.fieldUsage.type==="result"){let r=e.f.fieldDef,n,i=e.f.parent.getFullOutputName()+(r.name||r.as||"undefined"),s=r.code,o=[i];return _F(e.f)?(n=e.f.getFilterList(),{sourceField:i,sourceExpression:s,filterList:n,sourceClasses:o,fieldKind:"measure"}):Ns(e.f)?{sourceField:i,sourceExpression:s,filterList:n,sourceClasses:o,fieldKind:"dimension"}:void 0}return}else if(e instanceof ac){let r=e.turtleDef.name||e.turtleDef.as,n=r?[r]:[],i=e.firstSegment.filterList,s=e.turtleDef.pipeline[e.turtleDef.pipeline.length-1],o=(0,qe.isRawSegment)(s)?void 0:s.limit,a;if((0,qe.isQuerySegment)(s)&&(a=s.orderBy),r)return{sourceField:r,filterList:i,sourceClasses:n,fieldKind:"struct",limit:o,orderBy:a}}}getResultStructDef(e=this.rootResult,r=!0){let n=[],i;this.prepare(void 0);let s=0;for(let[a,u]of e.allFields){let l=this.getResultMetadata(u);if(u instanceof ac){let{structDef:c}=this.generateTurtlePipelineSQL(u,new j1(!0,void 0),"<nosource>"),f=u.getRepeatedResultType()==="nested"?"nested":"inline";c.name=a,c.structRelationship={fieldName:a,type:f,isArray:!1},c.structSource={type:f},c.resultMetadata=l,n.push(c)}else if(u instanceof os&&u.fieldUsage.type==="result"){u.f instanceof DG&&n.push(u.f.getAsJoinedStructDef(a)),Ns(u.f)&&(s===0&&r?i=a:i=void 0,s++);let c=u.f.fieldDef.location,f=u.f.fieldDef.annotation;switch(u.f.fieldDef.type){case"boolean":case"json":case"string":n.push({name:a,type:u.f.fieldDef.type,resultMetadata:l,location:c,annotation:f});break;case"timestamp":{let x=u.f.fieldDef.timeframe;x?n.push({name:a,type:"timestamp",timeframe:x,resultMetadata:l,location:c,annotation:f}):n.push({name:a,type:"timestamp",resultMetadata:l,location:c,annotation:f});break}case"date":{n.push({name:a,type:u.f.fieldDef.type,timeframe:u.f.fieldDef.timeframe,resultMetadata:l,location:c,annotation:f});break}case"number":n.push({name:a,numberType:u.f.fieldDef.numberType,type:"number",resultMetadata:l,location:c,annotation:f});break;case"unsupported":n.push({...u.f.fieldDef,resultMetadata:l,location:c});break;default:throw new Error(`unknown Field Type in query ${JSON.stringify(u.f.fieldDef)}`)}}}let o={fields:n,name:this.resultStage||"result",dialect:this.parent.dialect.name,primaryKey:i,structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"},resultMetadata:this.getResultMetadata(this.rootResult),type:"struct",queryTimezone:e.getQueryInfo().queryTimezone};return this.parent.fieldDef.modelAnnotation&&(o.modelAnnotation=this.parent.fieldDef.modelAnnotation),o}generateSQLJoinBlock(e,r){let n="",i=r.queryStruct,s=i.fieldDef.structRelationship,o=i.structSourceSQL(e);if((0,qe.isJoinOn)(s)){let a=s.matrixOperation.toUpperCase();if(r.makeUniqueKey){let f=this.generateSQLPassthroughKeys(i);o=`(SELECT ${i.dialect.sqlGenerateUUID()} as __distinct_key, x.* ${f} FROM ${o} as x)`}let u="";if(i.parent===void 0)throw new Error("Expected joined struct to have a parent.");s.onExpression?u=new Ab({type:"boolean",name:"ignoreme",e:s.onExpression},i.parent).generateExpression(this.rootResult):u="1=1";let l="",c;if(r.joinFilterConditions&&(c=r.joinFilterConditions.map(f=>f.generateExpression(this.rootResult))),r.children.length===0||c===void 0)c!==void 0&&c.length>=1&&(l=` AND (${c.join(" AND ")})`),n+=` ${a} JOIN ${o} AS ${r.alias}
|
|
188
|
+
)`}/(${s}*1.0))`;return a=`CAST(${a} as ${t.defaultNumberType})`,a}var os=class{constructor(e,r,n){this.f=e,this.fieldUsage=r,this.parent=n,this.type="field",this.additionalGroupSets=[]}root(){return this.parent.root()}getSQL(){let e=this.f.generateExpression(this.parent);return Ns(this.f)&&(e=this.f.caseGroup(this.parent.groupSet>0?this.parent.childGroups.concat(this.additionalGroupSets):[],e)),e}getAnalyticalSQL(e){return this.analyticalSQL===void 0?this.getSQL():e&&this.partitionSQL?this.partitionSQL:this.analyticalSQL}},ac=class t{constructor(e,r){this.turtleDef=e,this.parent=r,this.type="query",this.allFields=new Map,this.groupSet=0,this.depth=0,this.childGroups=[],this.hasHaving=!1,this.ungroupedSets=new Map,this.resultUsesUngrouped=!1,this.firstSegment=e.pipeline[0]}getQueryInfo(){if(!(0,qe.isIndexSegment)(this.firstSegment)&&!(0,qe.isRawSegment)(this.firstSegment)){let{queryTimezone:e}=this.firstSegment;if(e)return{queryTimezone:e}}return{}}addField(e,r,n){let i=this.allFields.get(e);if(i){if(i.type==="query")throw new Error(`Redefinition of field ${r.fieldDef.name} as struct`);if(i.fieldUsage.type==="result"){if(n.type!=="result")return;throw new Error(`Ambiguous output field name '${r.fieldDef.name}'.`)}}this.add(e,new os(r,n,this))}parentGroupSet(){return this.parent?this.parent.groupSet:0}add(e,r){this.allFields.set(e,r)}hasField(e){let r=this.allFields.get(e);return r!==void 0&&r instanceof os}getField(e){let r=this.allFields.get(e);if(r===void 0)throw new Error(`Internal Error, field Not defined ${e}`);if(r instanceof os)return r;throw new Error(`can't use a query here ${e}`)}getFieldByNumber(e){for(let[r,n]of this.allFields)if(n instanceof os&&n.fieldUsage.type==="result"&&n.fieldUsage.resultIndex===e)return{name:r,fif:n};throw new Error(`Invalid Order By index '${e}`)}computeGroups(e,r){e===0&&this.resultUsesUngrouped&&this.root().computeOnlyGroups.push(e++);for(let[o,a]of this.ungroupedSets){let u=e++;a.groupSet=u,this.root().computeOnlyGroups.push(u)}this.groupSet=e++,this.depth=r;let n=r,i=!1,s=[this.groupSet];for(let[o,a]of this.allFields)if(a.type==="query"){let u=a;if(i=!0,u.firstSegment.type==="reduce"){let l=u.computeGroups(e,r+1);s=s.concat(l.children),e=l.nextGroupSetNumber,l.maxDepth>n&&(n=l.maxDepth)}}return this.childGroups=s,{nextGroupSetNumber:e,maxDepth:n,children:s,isComplex:i}}fields(e=void 0){let r=[];for(let n of this.allFields.values())n instanceof os&&(e===void 0||e(n))&&r.push(n);return r}fieldNames(e){let r=[];for(let[n,i]of this.allFields)i instanceof os&&(e===void 0||e(i))&&r.push(n);return r}getRepeatedResultType(){let e="inline_all_numbers";for(let r of this.fields())if(r.fieldUsage.type==="result"){if(Ns(r.f))return"nested";r.f instanceof Pu&&(e="inline")}return e}structs(){let e=[];for(let r of this.allFields.values())r instanceof t&&e.push(r);return e}selectStructs(e,r){r(this)&&e.push(this);for(let n of this.structs())n.selectStructs(e,r);return e}calculateDefaultOrderBy(){let e;for(let[r,n]of this.allFields)if(n instanceof os&&n.fieldUsage.type==="result"){if(e||(e=n.fieldUsage.resultIndex),["date","timestamp"].indexOf(n.f.fieldDef.type)>-1)return[{dir:"desc",field:n.fieldUsage.resultIndex}];if(Kxe(n.f))return[{dir:"desc",field:n.fieldUsage.resultIndex}]}return e?[{dir:"asc",field:e}]:[]}addStructToJoin(e,r,n,i){var s;let o=e.getIdentifier();if(i.indexOf(o)!==-1)return;let a;if(a=this.root().joins.get(o)){a.uniqueKeyPossibleUses.add_use(n);return}let u,l=(s=e.parent)===null||s===void 0?void 0:s.getJoinableParent();l&&(this.addStructToJoin(l,r,void 0,i),u=this.root().joins.get(l.getIdentifier()));let c=e.fieldDef.structRelationship;(0,qe.isJoinOn)(c)&&e.parent&&c.onExpression!==void 0&&i.indexOf(o)===-1&&r.addDependantExpr(this,e.parent,c.onExpression,[...i,o]),(a=this.root().joins.get(o))||(a=new IG(e,o,u),this.root().joins.set(o,a)),a.uniqueKeyPossibleUses.add_use(n)}findJoins(e){for(let r of this.fields())this.addStructToJoin(r.f.getJoinableParent(),e,r.f.uniqueKeyPossibleUse(),[]);for(let r of this.structs())r.findJoins(e)}root(){if(this.parent)return this.parent.root();throw new Error("Internal Error, Null parent FieldInstanceResult")}getUngroupPartitions(e){let r=[],n=this,i=[],s=[];if(e===void 0||e.type==="all"){let a=e?.fields||[];i=this.fields(u=>Ns(u.f)&&u.fieldUsage.type==="result"&&a.indexOf(u.f.getIdentifier())===-1).map(u=>u.f.getIdentifier())}else i=e.fields;let o=!0;for(;n!==void 0;)(o||e?.type==="exclude")&&(s=s.concat(n.fields(a=>Ns(a.f)&&a.fieldUsage.type==="result").map(a=>a.f.getIdentifier()))),r=r.concat(n.fields(a=>Ns(a.f)&&a.fieldUsage.type==="result"&&i.indexOf(a.f.getIdentifier())===-1)),n=n.parent,o=!1;for(let a of e?.fields||[])if(s.indexOf(a)===-1)throw new Error(`${e?.type}(): unknown field name "${a}" or name not in scope.`);return r}assignFieldsToGroups(){for(let[e,r]of this.ungroupedSets)for(let n of this.getUngroupPartitions(r))n.additionalGroupSets.push(r.groupSet);for(let e of this.structs())e.assignFieldsToGroups()}},LG=class extends ac{constructor(e){super(e,void 0),this.joins=new Map,this.havings=new ss.AndChain,this.isComplexQuery=!1,this.queryUsesPartitioning=!1,this.computeOnlyGroups=[],this.elimatedComputeGroups=!1}root(){return this}eliminateComputeGroupsSQL(){return this.elimatedComputeGroups||this.computeOnlyGroups.length===0?"":(this.elimatedComputeGroups=!0,`group_set NOT IN (${this.computeOnlyGroups.join(",")})`)}calculateSymmetricAggregates(){let e;for(let[r,n]of this.joins)if(n.parentRelationship()==="many_to_many"||n.forceAllSymmetricCalculations())e="0never";else if(e===void 0)e=r;else if(n.parentRelationship()==="one_to_many"){let s=n.queryStruct;s.parent&&s.parent.getIdentifier()===e?e=r:e="0never"}for(let[r,n]of this.joins)n.leafiest=r===e;for(let[r,n]of this.joins)if(n.leafiest&&n.parent!==void 0&&n.uniqueKeyPossibleUses.has("count")||!n.leafiest&&n.uniqueKeyPossibleUses.hasAsymetricFunctions()){let i=n;for(;i;)i.queryStruct.primaryKey()||(i.makeUniqueKey=!0),i.queryStruct.fieldDef.structRelationship.type==="nested"?i=i.parent:i=void 0}}},IG=class{constructor(e,r,n){if(this.queryStruct=e,this.alias=r,this.parent=n,this.uniqueKeyPossibleUses=new _G,this.makeUniqueKey=!1,this.leafiest=!1,this.children=[],n&&n.children.push(this),this.queryStruct.fieldDef.filterList){this.joinFilterConditions=[];for(let i of this.queryStruct.fieldDef.filterList){let s=new Ab({type:"boolean",name:"ignoreme",e:i.expression},this.queryStruct);this.joinFilterConditions.push(s)}}}parentRelationship(){if(this.queryStruct.parent===void 0)return"root";switch(this.queryStruct.fieldDef.structRelationship.type){case"one":return"many_to_one";case"cross":return"many_to_many";case"many":return"one_to_many";case"nested":return"one_to_many";case"inline":return"one_to_one";default:throw new Error(`Internal error unknown relationship type to parent for ${this.queryStruct.fieldDef.name}`)}}forceAllSymmetricCalculations(){let e=this.queryStruct.fieldDef.structRelationship;return this.queryStruct.parent===void 0||!(0,qe.isJoinOn)(e)?!1:e.matrixOperation==="right"||e.matrixOperation==="full"}getDialectFieldList(){let e=[];for(let r of this.queryStruct.fieldDef.fields.filter(qe.isPhysical))e.push({type:r.type,sqlExpression:(0,qe.getIdentifier)(r),sqlOutputName:(0,qe.getIdentifier)(r)});return e}},TF=class extends Ey{},MG=class{static nextStructDef(e,r){let n=new Pu(e,{model:new CF(void 0)}),i={type:"turtle",name:"ignoreme",pipeline:[r]};return uc.makeQuery(i,n,new j1(!0,void 0),!1).getResultStructDef()}};Sy.Segment=MG;var uc=class t extends Ey{constructor(e,r,n,i){super(e,r),this.prepared=!1,this.maxDepth=0,this.maxGroupSet=0,this.fieldDef=e,this.rootResult=new LG(e),this.stageWriter=n,this.firstSegment=e.pipeline[0],this.isJoinedSubquery=i}static makeQuery(e,r,n=void 0,i){let s=r,o=r.applyStructFiltersToTurtleDef(e),a=o.pipeline[0],u=r.fieldDef;switch(n!==void 0&&(0,qe.isQuerySegment)(a)&&a.extendSource!==void 0&&(s=new Pu({...u,fields:[...u.fields,...a.extendSource]},s.parent?{struct:s}:{model:s.model}),o={...o,pipeline:[{...a,extendSource:void 0},...o.pipeline.slice(1)]}),u.queryTimezone&&(0,qe.isQuerySegment)(a)&&a.queryTimezone===void 0&&(a.queryTimezone=u.queryTimezone),a.type){case"reduce":return new PG(o,s,n,i);case"project":return new kG(o,s,n,i);case"index":return new UG(o,s,n,i);case"raw":return new qG(o,s,n,i);case"partial":throw new Error("Attempt to make query out of partial stage")}}inNestedPipeline(){return this.parent.fieldDef.structSource.type==="sql"&&this.parent.fieldDef.structSource.method==="nested"}expandField(e){let r=e.type==="fieldref"?this.parent.getQueryFieldReference(e.path,e.annotation):this.parent.makeQueryField(e);return{as:r.getIdentifier(),field:r}}addDependantPath(e,r,n,i,s){let o=r.getFieldByName(n),a;if(o instanceof Ey)a=o.parent;else if(o instanceof Pu)a=o;else throw new Error("Internal Error: Unknown object type");e.root().addStructToJoin(a.getJoinableParent(),this,i,s)}addDependantExpr(e,r,n,i){for(let s of n)if((0,qe.isFunctionCallFragment)(s)&&(0,qe.expressionIsAnalytic)(s.overload.returnType.expressionType)&&this.parent.dialect.cantPartitionWindowFunctionsOnExpressions&&(e.root().isComplexQuery=!0,e.root().queryUsesPartitioning=!0),(0,qe.isUngroupFragment)(s)){if(e.resultUsesUngrouped=!0,e.root().isComplexQuery=!0,e.root().queryUsesPartitioning=!0,s.fields&&s.fields.length>0){let o=s.fields.sort().join("|")+s.type;e.ungroupedSets.get(o)===void 0&&e.ungroupedSets.set(o,{type:s.type,fields:s.fields,groupSet:-1})}this.addDependantExpr(e,r,s.e,i)}else if((0,qe.isFieldFragment)(s)){let o=r.getDimensionOrMeasureByName(s.path);(0,qe.hasExpression)(o.fieldDef)?this.addDependantExpr(e,o.parent,o.fieldDef.e,i):e.root().addStructToJoin(o.parent.getJoinableParent(),this,void 0,i)}else if((0,qe.isFilterFragment)(s)){for(let o of s.filterList)this.addDependantExpr(e,r,o.expression,i),this.addDependantExpr(e,r,s.e,i);this.addDependantExpr(e,r,s.e,i)}else if((0,qe.isDialectFragment)(s)){let o=[];switch(s.function){case"now":break;case"div":o.push(s.denominator),o.push(s.numerator);break;case"numberLiteral":case"timeLiteral":case"stringLiteral":case"regexpLiteral":break;case"timeDiff":o.push(s.left.value,s.right.value);break;case"delta":o.push(s.base.value,s.delta);break;case"trunc":case"extract":o.push(s.expr.value);break;case"regexpMatch":case"cast":o.push(s.expr);break;default:throw new Error("Unknown dialect Fragment type. Can't generate dependancies")}for(let a of o)this.addDependantExpr(e,r,a,i)}else if((0,qe.isAggregateFragment)(s))(0,qe.isAsymmetricFragment)(s)&&(s.structPath?this.addDependantPath(e,r,s.structPath,s.function,i):e.addStructToJoin(r,this,s.function,i)),this.addDependantExpr(e,r,s.e,i);else if((0,qe.isFunctionCallFragment)(s)){s.structPath&&this.addDependantPath(e,r,s.structPath,"generic_aggregate",i);for(let o of s.args)this.addDependantExpr(e,r,o,i);if((0,qe.expressionIsAnalytic)(s.overload.returnType.expressionType)&&(e.root().queryUsesPartitioning=!0),s.orderBy)for(let o of s.orderBy)this.addDependantExpr(e,r,o.e,i)}}addDependancies(e,r){(0,qe.hasExpression)(r.fieldDef)&&this.addDependantExpr(e,r.parent,r.fieldDef.e,[])}getSegmentFields(e){let r=e.firstSegment;return r.type==="index"?r.indexFields:(0,qe.isQuerySegment)(r)?r.queryFields:[]}expandFields(e){let r=1;for(let n of this.getSegmentFields(e)){let{as:i,field:s}=this.expandField(n);if(s instanceof TF||s instanceof t){if(this.firstSegment.type==="project")throw new Error(`Nested views cannot be used in select - '${s.fieldDef.name}'`);let o=new ac(s.fieldDef,e);this.expandFields(o),e.add(i,o)}else if(s instanceof Ti&&(e.addField(i,s,{resultIndex:r,type:"result"}),this.addDependancies(e,s),Kxe(s)&&this.firstSegment.type==="project"))throw new Error(`Aggregate Fields cannot be used in select - '${s.fieldDef.name}'`);r++}this.expandFilters(e)}expandFilters(e){if(e.firstSegment.filterList!==void 0){for(let r of e.firstSegment.filterList||[]){let n=this.parent;this.addDependantExpr(e,n,r.expression,[])}for(let r of e.root().joins.values()||[])for(let n of r.joinFilterConditions||[])n.fieldDef.type==="boolean"&&n.fieldDef.e&&this.addDependantExpr(e,n.parent,n.fieldDef.e,[])}}generateSQLFilters(e,r,n=void 0){let i=new ss.AndChain,s=n||e.firstSegment.filterList;if(s===void 0)return i;for(let o of s||[]){let a=this.parent;if(r==="having"&&(0,qe.expressionIsCalculation)(o.expressionType)||r==="where"&&(0,qe.expressionIsScalar)(o.expressionType)){let u=this.generateExpressionFromExpr(e,a,o.expression,void 0);i.add(u)}}return i}prepare(e){this.prepared||(this.expandFields(this.rootResult),this.rootResult.addStructToJoin(this.parent,this,void 0,[]),this.rootResult.findJoins(this),this.rootResult.calculateSymmetricAggregates(),this.prepared=!0)}getResultMetadata(e){if(e instanceof os){if(e.fieldUsage.type==="result"){let r=e.f.fieldDef,n,i=e.f.parent.getFullOutputName()+(r.name||r.as||"undefined"),s=r.code,o=[i];return _F(e.f)?(n=e.f.getFilterList(),{sourceField:i,sourceExpression:s,filterList:n,sourceClasses:o,fieldKind:"measure"}):Ns(e.f)?{sourceField:i,sourceExpression:s,filterList:n,sourceClasses:o,fieldKind:"dimension"}:void 0}return}else if(e instanceof ac){let r=e.turtleDef.name||e.turtleDef.as,n=r?[r]:[],i=e.firstSegment.filterList,s=e.turtleDef.pipeline[e.turtleDef.pipeline.length-1],o=(0,qe.isRawSegment)(s)?void 0:s.limit,a;if((0,qe.isQuerySegment)(s)&&(a=s.orderBy),r)return{sourceField:r,filterList:i,sourceClasses:n,fieldKind:"struct",limit:o,orderBy:a}}}getResultStructDef(e=this.rootResult,r=!0){let n=[],i;this.prepare(void 0);let s=0;for(let[a,u]of e.allFields){let l=this.getResultMetadata(u);if(u instanceof ac){let{structDef:c}=this.generateTurtlePipelineSQL(u,new j1(!0,void 0),"<nosource>"),f=u.getRepeatedResultType()==="nested"?"nested":"inline";c.name=a,c.structRelationship={fieldName:a,type:f,isArray:!1},c.structSource={type:f},c.resultMetadata=l,n.push(c)}else if(u instanceof os&&u.fieldUsage.type==="result"){u.f instanceof DG&&n.push(u.f.getAsJoinedStructDef(a)),Ns(u.f)&&(s===0&&r?i=a:i=void 0,s++);let c=u.f.fieldDef.location,f=u.f.fieldDef.annotation;switch(u.f.fieldDef.type){case"boolean":case"json":case"string":n.push({name:a,type:u.f.fieldDef.type,resultMetadata:l,location:c,annotation:f});break;case"timestamp":{let x=u.f.fieldDef.timeframe;x?n.push({name:a,type:"timestamp",timeframe:x,resultMetadata:l,location:c,annotation:f}):n.push({name:a,type:"timestamp",resultMetadata:l,location:c,annotation:f});break}case"date":{n.push({name:a,type:u.f.fieldDef.type,timeframe:u.f.fieldDef.timeframe,resultMetadata:l,location:c,annotation:f});break}case"number":n.push({name:a,numberType:u.f.fieldDef.numberType,type:"number",resultMetadata:l,location:c,annotation:f});break;case"unsupported":n.push({...u.f.fieldDef,resultMetadata:l,location:c});break;default:throw new Error(`unknown Field Type in query ${JSON.stringify(u.f.fieldDef)}`)}}}let o={fields:n,name:this.resultStage||"result",dialect:this.parent.dialect.name,primaryKey:i,structRelationship:{type:"basetable",connectionName:this.parent.connectionName},structSource:{type:"query_result"},resultMetadata:this.getResultMetadata(this.rootResult),type:"struct",queryTimezone:e.getQueryInfo().queryTimezone};return this.parent.fieldDef.modelAnnotation&&(o.modelAnnotation=this.parent.fieldDef.modelAnnotation),o}generateSQLJoinBlock(e,r){let n="",i=r.queryStruct,s=i.fieldDef.structRelationship,o=i.structSourceSQL(e);if((0,qe.isJoinOn)(s)){let a=s.matrixOperation.toUpperCase();if(r.makeUniqueKey){let f=this.generateSQLPassthroughKeys(i);o=`(SELECT ${i.dialect.sqlGenerateUUID()} as __distinct_key, x.* ${f} FROM ${o} as x)`}let u="";if(i.parent===void 0)throw new Error("Expected joined struct to have a parent.");s.onExpression?u=new Ab({type:"boolean",name:"ignoreme",e:s.onExpression},i.parent).generateExpression(this.rootResult):u="1=1";let l="",c;if(r.joinFilterConditions&&(c=r.joinFilterConditions.map(f=>f.generateExpression(this.rootResult))),r.children.length===0||c===void 0)c!==void 0&&c.length>=1&&(l=` AND (${c.join(" AND ")})`),n+=` ${a} JOIN ${o} AS ${r.alias}
|
|
189
189
|
ON ${u}${l}
|
|
190
190
|
`;else{let f=`SELECT ${r.alias}.*`,x="";for(let d of r.children){x+=this.generateSQLJoinBlock(e,d);let h=(0,qe.getPhysicalFields)(d.queryStruct.fieldDef).map(p=>this.parent.dialect.sqlMaybeQuoteIdentifier(p.name));f+=`, ${this.parent.dialect.sqlSelectAliasAsStruct(d.alias,h)} AS ${d.alias}`}return f+=`
|
|
191
191
|
FROM ${o} AS ${r.alias}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/render",
|
|
3
|
-
"version": "0.0.122-
|
|
3
|
+
"version": "0.0.122-dev240203152115",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@lit/context": "^1.1.0",
|
|
28
|
-
"@malloydata/malloy": "^0.0.122-
|
|
28
|
+
"@malloydata/malloy": "^0.0.122-dev240203152115",
|
|
29
29
|
"@types/luxon": "^2.4.0",
|
|
30
30
|
"lit": "^3.0.2",
|
|
31
31
|
"lodash": "^4.17.20",
|