@malloydata/render 0.0.119-dev240123183113 → 0.0.119-dev240124170348

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.
@@ -235,8 +235,8 @@
235
235
  "../malloy/dist/model/malloy_types.js"(exports) {
236
236
  "use strict";
237
237
  Object.defineProperty(exports, "__esModule", { value: true });
238
- exports.isSamplingEnable = exports.isSamplingPercent = exports.isSamplingRows = exports.isQuerySegment = exports.isProjectSegment = exports.isPartialSegment = exports.isReduceSegment = exports.refIsStructDef = exports.isByExpression = exports.isByName = exports.ValueType = exports.isExtractUnit = exports.isTimestampUnit = exports.isDateUnit = exports.FieldIsIntrinsic = exports.isCastType = exports.isAtomicFieldType = exports.isTimeFieldType = exports.hasExpression = exports.maxOfExpressionTypes = exports.maxExpressionType = exports.isExpressionTypeLEQ = exports.expressionIsAnalytic = exports.expressionIsCalculation = exports.expressionInvolvesAggregate = exports.expressionIsUngroupedAggregate = exports.expressionIsAggregate = exports.expressionIsScalar = exports.mkExpr = exports.isApplyFragment = exports.isApplyValue = exports.isParameterFragment = exports.isSourceReferenceFragment = exports.isSqlStringFragment = exports.isFieldReferenceFragment = exports.isFieldFragment = exports.isSpreadFragment = exports.isSQLExpressionFragment = exports.isFunctionCallFragment = exports.isFunctionParameterFragment = exports.isUngroupFragment = exports.isAsymmetricFragment = exports.isAggregateFragment = exports.isDialectFragment = exports.isFilterFragment = exports.isOutputFieldFragment = exports.isFilteredAliasedName = exports.paramHasValue = exports.isConditionParameter = exports.isValueParameter = void 0;
239
- exports.isValueDate = exports.isValueTimestamp = exports.isValueBoolean = exports.isValueNumber = exports.isValueString = exports.isMeasureLike = exports.getPhysicalFields = exports.getDimensions = exports.isPhysical = exports.isDimensional = exports.isAtomicField = exports.isTurtleDef = exports.getIdentifier = exports.isFieldStructDef = exports.isFieldTimeBased = exports.isFieldTypeDef = exports.isSQLBlockStruct = exports.mergeEvalSpaces = exports.isSQLFragment = exports.isJoinOn = exports.isMatrixOperation = exports.isIndexSegment = exports.isRawSegment = void 0;
238
+ exports.isIndexSegment = exports.isRawSegment = exports.isSamplingEnable = exports.isSamplingPercent = exports.isSamplingRows = exports.isQuerySegment = exports.isProjectSegment = exports.isPartialSegment = exports.isReduceSegment = exports.refIsStructDef = exports.isByExpression = exports.isByName = exports.ValueType = exports.isExtractUnit = exports.isTimestampUnit = exports.isDateUnit = exports.FieldIsIntrinsic = exports.isCastType = exports.isAtomicFieldType = exports.isTimeFieldType = exports.hasExpression = exports.maxOfExpressionTypes = exports.maxExpressionType = exports.isExpressionTypeLEQ = exports.expressionIsAnalytic = exports.expressionIsCalculation = exports.expressionInvolvesAggregate = exports.expressionIsUngroupedAggregate = exports.expressionIsAggregate = exports.expressionIsScalar = exports.mkExpr = exports.isApplyFragment = exports.isApplyValue = exports.isParameterFragment = exports.isSourceReferenceFragment = exports.isSqlStringFragment = exports.isFieldFragment = exports.isSpreadFragment = exports.isSQLExpressionFragment = exports.isFunctionCallFragment = exports.isFunctionParameterFragment = exports.isUngroupFragment = exports.isAsymmetricFragment = exports.isAggregateFragment = exports.isDialectFragment = exports.isFilterFragment = exports.isOutputFieldFragment = exports.paramHasValue = exports.isConditionParameter = exports.isValueParameter = void 0;
239
+ exports.isValueDate = exports.isValueTimestamp = exports.isValueBoolean = exports.isValueNumber = exports.isValueString = exports.isMeasureLike = exports.getPhysicalFields = exports.getDimensions = exports.isPhysical = exports.isDimensional = exports.isAtomicField = exports.isTurtleDef = exports.getIdentifier = exports.isFieldStructDef = exports.isFieldTimeBased = exports.isFieldTypeDef = exports.isSQLBlockStruct = exports.mergeEvalSpaces = exports.isSQLFragment = exports.isJoinOn = exports.isMatrixOperation = void 0;
240
240
  function isValueParameter(p3) {
241
241
  return p3.value !== void 0;
242
242
  }
@@ -249,15 +249,6 @@
249
249
  return isValueParameter(p3) || p3.condition !== null;
250
250
  }
251
251
  exports.paramHasValue = paramHasValue;
252
- function isFilteredAliasedName(f4) {
253
- for (const prop of Object.keys(f4)) {
254
- if (!["name", "as", "filterList"].includes(prop)) {
255
- return false;
256
- }
257
- }
258
- return true;
259
- }
260
- exports.isFilteredAliasedName = isFilteredAliasedName;
261
252
  function isOutputFieldFragment(f4) {
262
253
  return (f4 === null || f4 === void 0 ? void 0 : f4.type) === "outputField";
263
254
  }
@@ -303,10 +294,6 @@
303
294
  return (f4 === null || f4 === void 0 ? void 0 : f4.type) === "field";
304
295
  }
305
296
  exports.isFieldFragment = isFieldFragment;
306
- function isFieldReferenceFragment(f4) {
307
- return (f4 === null || f4 === void 0 ? void 0 : f4.type) === "field-reference";
308
- }
309
- exports.isFieldReferenceFragment = isFieldReferenceFragment;
310
297
  function isSqlStringFragment(f4) {
311
298
  return (f4 === null || f4 === void 0 ? void 0 : f4.type) === "sql-string";
312
299
  }
@@ -4745,6 +4732,9 @@ ${hackSplitComment}
4745
4732
  var standardsql_1 = require_standardsql();
4746
4733
  var malloy_types_1 = require_malloy_types();
4747
4734
  var utils_1 = require_utils();
4735
+ function pathToCol(path3) {
4736
+ return path3.map((el) => encodeURIComponent(el)).join("/");
4737
+ }
4748
4738
  function generateSQLStringLiteral(sourceString) {
4749
4739
  return `'${sourceString}'`;
4750
4740
  }
@@ -5005,15 +4995,7 @@ ${(0, utils_1.indent)(sql)})
5005
4995
  generateSpread(_resultSet, _context, _frag, _state) {
5006
4996
  throw new Error("Unexpanded spread encountered during SQL generation");
5007
4997
  }
5008
- generateParameterFragment(resultSet, context3, expr2, state) {
5009
- const param2 = context3.parameters()[expr2.path];
5010
- if ((0, malloy_types_1.isValueParameter)(param2)) {
5011
- if (param2.value) {
5012
- return this.generateExpressionFromExpr(resultSet, context3, param2.value, state);
5013
- }
5014
- } else if (param2.condition) {
5015
- return this.generateExpressionFromExpr(resultSet, context3, param2.condition, state);
5016
- }
4998
+ generateParameterFragment(resultSet, context3, expr2, _state) {
5017
4999
  throw new Error(`Can't generate SQL, no value for ${expr2.path}`);
5018
5000
  }
5019
5001
  generateFilterFragment(resultSet, context3, expr2, state) {
@@ -5132,9 +5114,6 @@ ${(0, utils_1.indent)(sql)})
5132
5114
  generateDialect(resultSet, context3, expr2, state) {
5133
5115
  return this.generateExpressionFromExpr(resultSet, context3, context3.dialect.dialectExpr(resultSet.getQueryInfo(), expr2), state);
5134
5116
  }
5135
- generateFieldReference(resultSet, context3, expr2, state) {
5136
- return this.generateFieldFragment(resultSet, context3, { type: "field", path: expr2.path }, state);
5137
- }
5138
5117
  generateSqlString(resultSet, context3, expr2, state) {
5139
5118
  return expr2.e.map((part) => typeof part === "string" ? part : this.generateExpressionFromExpr(resultSet, context3, [part], state)).join("");
5140
5119
  }
@@ -5282,8 +5261,6 @@ ${(0, utils_1.indent)(sql)})
5282
5261
  s8 += this.generateSqlString(resultSet, context3, expr2, state);
5283
5262
  } else if (expr2.type === "source-reference") {
5284
5263
  s8 += this.generateSourceReference(resultSet, context3, expr2);
5285
- } else if (expr2.type === "field-reference") {
5286
- s8 += this.generateFieldReference(resultSet, context3, expr2, state);
5287
5264
  } else {
5288
5265
  throw new Error(`Internal Error: Unknown expression fragment ${JSON.stringify(expr2, void 0, 2)}`);
5289
5266
  }
@@ -5309,7 +5286,7 @@ ${(0, utils_1.indent)(sql)})
5309
5286
  return f4 instanceof QueryAtomicField && f4.isAggregate();
5310
5287
  }
5311
5288
  function isScalarField(f4) {
5312
- return f4 instanceof QueryAtomicField && !f4.isCalculated();
5289
+ return f4 instanceof QueryAtomicField && !f4.isCalculated() && !f4.isAggregate();
5313
5290
  }
5314
5291
  var QueryAtomicField = class extends QueryField {
5315
5292
  includeInWildcard() {
@@ -5355,10 +5332,10 @@ ${(0, utils_1.indent)(sql)})
5355
5332
  onExpression: [
5356
5333
  {
5357
5334
  type: "field",
5358
- path: this.primaryKey
5335
+ path: [this.primaryKey]
5359
5336
  },
5360
5337
  "=",
5361
- { type: "field", path: foreignKeyName }
5338
+ { type: "field", path: [foreignKeyName] }
5362
5339
  ]
5363
5340
  }
5364
5341
  };
@@ -5940,99 +5917,12 @@ ${(0, utils_1.indent)(sql)})
5940
5917
  inNestedPipeline() {
5941
5918
  return this.parent.fieldDef.structSource.type === "sql" && this.parent.fieldDef.structSource.method === "nested";
5942
5919
  }
5943
- getFieldList() {
5944
- switch (this.firstSegment.type) {
5945
- case "reduce":
5946
- return this.firstSegment.fields;
5947
- case "project":
5948
- return this.firstSegment.fields;
5949
- default:
5950
- throw new Error(`Query contains no fields ${JSON.stringify(this.fieldDef)}`);
5951
- }
5952
- }
5953
5920
  // get a field ref and expand it.
5954
5921
  expandField(f4) {
5955
- let as;
5956
- let field3;
5957
- if (typeof f4 === "string") {
5958
- field3 = this.parent.getQueryFieldByName(f4);
5959
- } else if ("type" in f4) {
5960
- field3 = this.parent.makeQueryField(f4);
5961
- } else if ("name" in f4 && "as" in f4) {
5962
- field3 = this.parent.getQueryFieldByName(f4.name);
5963
- as = field3.fieldDef.as || f4.as;
5964
- if (field3 instanceof QueryFieldStruct) {
5965
- throw new Error("Syntax currently disallowed. Semantics up for discussion");
5966
- }
5967
- if (field3 instanceof _QueryQuery) {
5968
- const newFieldDef = structuredClone(field3.fieldDef);
5969
- newFieldDef.as = f4.name;
5970
- newFieldDef.filterList = f4.filterList;
5971
- field3 = _QueryQuery.makeQuery(newFieldDef, this.parent, void 0, this.isJoinedSubquery);
5972
- } else if (!(field3 instanceof QueryFieldTimestamp || field3 instanceof QueryFieldDate)) {
5973
- throw new Error(`No longer generate code this way.
5974
- ${JSON.stringify(f4, void 0, 2)}`);
5975
- }
5976
- } else {
5977
- throw new Error(`Unrecognized field definition ${JSON.stringify(f4, void 0, 2)}`);
5978
- }
5979
- if (!as) {
5980
- as = field3.getIdentifier();
5981
- }
5922
+ const field3 = f4.type === "fieldref" ? this.parent.getQueryFieldReference(f4.path, f4.annotation) : this.parent.makeQueryField(f4);
5923
+ const as = field3.getIdentifier();
5982
5924
  return { as, field: field3 };
5983
5925
  }
5984
- expandDependantField(resultStruct, fieldRef2) {
5985
- this.expandField(fieldRef2);
5986
- }
5987
- // find all the fieldNames in the struct (and children)
5988
- // that match the filter
5989
- expandWildCardStruct(struct, expandChildren, filter3 = void 0) {
5990
- let fieldNames2 = [];
5991
- const structs = [];
5992
- for (const [_name, f4] of struct.nameMap) {
5993
- if (f4 instanceof QueryAtomicField && isScalarField(f4) && f4.includeInWildcard() && !this.parent.dialect.ignoreInProject(f4.fieldDef.name) && (!filter3 || filter3(f4))) {
5994
- fieldNames2.push(f4.getFullOutputName());
5995
- } else if (f4 instanceof QueryStruct && expandChildren) {
5996
- structs.push(f4);
5997
- }
5998
- }
5999
- for (const s8 of structs) {
6000
- fieldNames2 = fieldNames2.concat(this.expandWildCardStruct(s8, expandChildren, filter3));
6001
- }
6002
- return fieldNames2;
6003
- }
6004
- // Do any '*' expansion.
6005
- expandWildCards(fields, filter3 = void 0) {
6006
- let ret = [];
6007
- for (const f4 of fields) {
6008
- if (typeof f4 !== "string") {
6009
- ret.push(f4);
6010
- } else {
6011
- const fieldName = f4;
6012
- const path3 = fieldName.split(".");
6013
- if (!path3[path3.length - 1].startsWith("*")) {
6014
- ret.push(f4);
6015
- } else {
6016
- const expandChildren = path3.pop() === "**";
6017
- let struct = this.parent;
6018
- let pathElementName;
6019
- while (path3.length > 0 && (pathElementName = path3.shift())) {
6020
- const structNode = struct.getChildByName(pathElementName);
6021
- if (structNode === void 0) {
6022
- throw new Error(`Nested source not found '${pathElementName}'`);
6023
- }
6024
- if (structNode instanceof QueryStruct) {
6025
- struct = structNode;
6026
- } else {
6027
- throw new Error(`'${pathElementName}' is not a source object`);
6028
- }
6029
- }
6030
- ret = ret.concat(this.expandWildCardStruct(struct, expandChildren, filter3));
6031
- }
6032
- }
6033
- }
6034
- return ret;
6035
- }
6036
5926
  addDependantPath(resultStruct, context3, path3, uniqueKeyPossibleUse, joinStack) {
6037
5927
  const node = context3.getFieldByName(path3);
6038
5928
  let struct;
@@ -6140,9 +6030,13 @@ ${(0, utils_1.indent)(sql)})
6140
6030
  this.addDependantExpr(resultStruct, field3.parent, field3.fieldDef.e, []);
6141
6031
  }
6142
6032
  }
6033
+ getSegmentFields(resultStruct) {
6034
+ const fs = resultStruct.firstSegment;
6035
+ return fs.type === "index" ? fs.indexFields : (0, malloy_types_1.isQuerySegment)(fs) ? fs.queryFields : [];
6036
+ }
6143
6037
  expandFields(resultStruct) {
6144
6038
  let resultIndex = 1;
6145
- for (const f4 of this.expandWildCards(resultStruct.firstSegment.fields)) {
6039
+ for (const f4 of this.getSegmentFields(resultStruct)) {
6146
6040
  const { as, field: field3 } = this.expandField(f4);
6147
6041
  if (field3 instanceof QueryTurtle || field3 instanceof _QueryQuery) {
6148
6042
  if (this.firstSegment.type === "project") {
@@ -6275,7 +6169,7 @@ ${(0, utils_1.indent)(sql)})
6275
6169
  const resultType = fi.getRepeatedResultType() === "nested" ? "nested" : "inline";
6276
6170
  structDef.name = name;
6277
6171
  structDef.structRelationship = {
6278
- field: name,
6172
+ fieldName: name,
6279
6173
  type: resultType,
6280
6174
  isArray: false
6281
6175
  };
@@ -6439,7 +6333,7 @@ ${(0, utils_1.indent)(select2)}) AS ${ji.alias}
6439
6333
  if (qs.parent === void 0 || ji.parent === void 0) {
6440
6334
  throw new Error("Internal Error, nested structure with no parent.");
6441
6335
  }
6442
- const fieldExpression = this.parent.dialect.sqlFieldReference(qs.parent.getSQLIdentifier(), structRelationship.field, "struct", qs.parent.fieldDef.structRelationship.type === "nested", this.parent.fieldDef.structRelationship.type === "nested" && this.parent.fieldDef.structRelationship.isArray);
6336
+ const fieldExpression = this.parent.dialect.sqlFieldReference(qs.parent.getSQLIdentifier(), structRelationship.fieldName, "struct", qs.parent.fieldDef.structRelationship.type === "nested", this.parent.fieldDef.structRelationship.type === "nested" && this.parent.fieldDef.structRelationship.isArray);
6443
6337
  s8 += `${this.parent.dialect.sqlUnnestAlias(fieldExpression, ji.alias, ji.getDialectFieldList(), ji.makeUniqueKey, structRelationship.isArray, this.inNestedPipeline())}
6444
6338
  `;
6445
6339
  } else if (structRelationship.type === "inline") {
@@ -6985,20 +6879,21 @@ FROM ${stage0Name}
6985
6879
  var QueryQueryIndexStage = class extends QueryQuery {
6986
6880
  constructor(fieldDef, parent, stageWriter, isJoinedSubquery) {
6987
6881
  super(fieldDef, parent, stageWriter, isJoinedSubquery);
6882
+ this.indexPaths = {};
6988
6883
  this.fieldDef = fieldDef;
6989
6884
  }
6990
- // get a field ref and expand it.
6991
6885
  expandField(f4) {
6992
- const field3 = this.parent.getFieldByName(f4);
6993
- return { as: f4, field: field3 };
6886
+ const as = f4.path.join(".");
6887
+ const field3 = this.parent.getQueryFieldByName(f4.path);
6888
+ return { as, field: field3 };
6994
6889
  }
6995
6890
  expandFields(resultStruct) {
6996
6891
  let resultIndex = 1;
6997
6892
  const groupIndex = resultStruct.groupSet;
6998
6893
  this.maxGroupSet = groupIndex;
6999
- const fieldNames2 = this.firstSegment.fields || [];
7000
- for (const f4 of fieldNames2) {
6894
+ for (const f4 of this.firstSegment.indexFields) {
7001
6895
  const { as, field: field3 } = this.expandField(f4);
6896
+ this.indexPaths[as] = f4.path;
7002
6897
  resultStruct.addField(as, field3, {
7003
6898
  resultIndex,
7004
6899
  type: "result"
@@ -7010,7 +6905,7 @@ FROM ${stage0Name}
7010
6905
  }
7011
6906
  const measure2 = this.firstSegment.weightMeasure;
7012
6907
  if (measure2 !== void 0) {
7013
- const f4 = this.parent.getFieldByName(measure2);
6908
+ const f4 = this.parent.getFieldByName([measure2]);
7014
6909
  resultStruct.addField(measure2, f4, {
7015
6910
  resultIndex,
7016
6911
  type: "result"
@@ -7023,6 +6918,7 @@ FROM ${stage0Name}
7023
6918
  let measureSQL = "COUNT(*)";
7024
6919
  const dialect = this.parent.dialect;
7025
6920
  const fieldNameColumn = dialect.sqlMaybeQuoteIdentifier("fieldName");
6921
+ const fieldPathColumn = dialect.sqlMaybeQuoteIdentifier("fieldPath");
7026
6922
  const fieldValueColumn = dialect.sqlMaybeQuoteIdentifier("fieldValue");
7027
6923
  const fieldTypeColumn = dialect.sqlMaybeQuoteIdentifier("fieldType");
7028
6924
  const fieldRangeColumn = dialect.sqlMaybeQuoteIdentifier("fieldRange");
@@ -7035,7 +6931,8 @@ FROM ${stage0Name}
7035
6931
  const fi = field3;
7036
6932
  if (fi.fieldUsage.type === "result" && isScalarField(fi.f)) {
7037
6933
  const expression3 = fi.f.generateExpression(this.rootResult);
7038
- fields.push({ name, type: fi.f.fieldDef.type, expression: expression3 });
6934
+ const path3 = this.indexPaths[name] || [];
6935
+ fields.push({ name, path: path3, type: fi.f.fieldDef.type, expression: expression3 });
7039
6936
  }
7040
6937
  }
7041
6938
  let s8 = "SELECT\n group_set,\n";
@@ -7044,14 +6941,24 @@ FROM ${stage0Name}
7044
6941
  s8 += ` WHEN ${i6} THEN '${fields[i6].name}'
7045
6942
  `;
7046
6943
  }
7047
- s8 += ` END as ${fieldNameColumn},`;
6944
+ s8 += ` END as ${fieldNameColumn},
6945
+ `;
6946
+ s8 += " CASE group_set\n";
6947
+ for (let i6 = 0; i6 < fields.length; i6++) {
6948
+ const path3 = pathToCol(fields[i6].path);
6949
+ s8 += ` WHEN ${i6} THEN '${path3}'
6950
+ `;
6951
+ }
6952
+ s8 += ` END as ${fieldPathColumn},
6953
+ `;
7048
6954
  s8 += " CASE group_set\n";
7049
6955
  for (let i6 = 0; i6 < fields.length; i6++) {
7050
6956
  s8 += ` WHEN ${i6} THEN '${fields[i6].type}'
7051
6957
  `;
7052
6958
  }
7053
6959
  s8 += ` END as ${fieldTypeColumn},`;
7054
- s8 += " CASE group_set WHEN 99999 THEN NULL\n";
6960
+ s8 += ` CASE group_set WHEN 99999 THEN ${dialect.castToString("NULL")}
6961
+ `;
7055
6962
  for (let i6 = 0; i6 < fields.length; i6++) {
7056
6963
  if (fields[i6].type === "string") {
7057
6964
  s8 += ` WHEN ${i6} THEN ${fields[i6].expression}
@@ -7078,7 +6985,7 @@ FROM ${stage0Name}
7078
6985
  s8 += this.generateSQLJoins(stageWriter);
7079
6986
  s8 += dialect.sqlGroupSetTable(fields.length) + "\n";
7080
6987
  s8 += this.generateSQLFilters(this.rootResult, "where").sql("where");
7081
- s8 += "GROUP BY 1,2,3,4\n";
6988
+ s8 += "GROUP BY 1,2,3,4,5\n";
7082
6989
  if (!(0, malloy_types_1.isRawSegment)(this.firstSegment) && this.firstSegment.limit) {
7083
6990
  s8 += `LIMIT ${this.firstSegment.limit}
7084
6991
  `;
@@ -7086,6 +6993,7 @@ FROM ${stage0Name}
7086
6993
  const resultStage = stageWriter.addStage(s8);
7087
6994
  this.resultStage = stageWriter.addStage(`SELECT
7088
6995
  ${fieldNameColumn},
6996
+ ${fieldPathColumn},
7089
6997
  ${fieldTypeColumn},
7090
6998
  COALESCE(${fieldValueColumn}, ${fieldRangeColumn}) as ${fieldValueColumn},
7091
6999
  weight
@@ -7115,83 +7023,52 @@ FROM ${resultStage}
7115
7023
  var QueryQueryIndex = class extends QueryQuery {
7116
7024
  constructor(fieldDef, parent, stageWriter, isJoinedSubquery) {
7117
7025
  super(fieldDef, parent, stageWriter, isJoinedSubquery);
7118
- this.rootFields = [];
7119
- this.fanPrefixMap = {};
7026
+ this.stages = [];
7120
7027
  this.fieldDef = fieldDef;
7121
- this.findFanPrefexes(parent);
7122
- }
7123
- // we want to generate a different query for each
7124
- // nested structure so we don't do a crazy cross product.
7125
- findFanPrefexes(qs) {
7126
- for (const [_name, f4] of qs.nameMap) {
7127
- if (f4 instanceof QueryStruct && (f4.fieldDef.structRelationship.type === "many" || f4.fieldDef.structRelationship.type === "nested") && f4.fieldDef.fields.length > 1 && // leave arrays in parent.
7128
- this.parent.dialect.dontUnionIndex === false) {
7129
- const key2 = f4.getFullOutputName();
7130
- this.fanPrefixMap[key2] = [];
7131
- this.findFanPrefexes(f4);
7132
- }
7133
- }
7134
- }
7135
- expandIndexWildCards() {
7136
- let fieldNames2 = this.firstSegment.fields || [];
7137
- if (fieldNames2.length === 0) {
7138
- fieldNames2.push("**");
7139
- }
7140
- fieldNames2 = this.expandWildCards(fieldNames2, (qf) => ["string", "number", "timestamp", "date"].indexOf(qf.fieldDef.type) !== -1);
7141
- return fieldNames2;
7028
+ this.fieldsToStages();
7142
7029
  }
7143
- // return the number of stages it is going to take to generate this index.
7144
- getStageFields() {
7145
- const s8 = [];
7146
- if (this.rootFields.length > 0) {
7147
- s8.push(this.rootFields);
7148
- }
7149
- for (const fieldList of Object.values(this.fanPrefixMap)) {
7150
- if (fieldList.length > 0) {
7151
- s8.push(fieldList);
7152
- }
7030
+ fieldsToStages() {
7031
+ const indexSeg = this.firstSegment;
7032
+ if (this.parent.dialect.dontUnionIndex) {
7033
+ this.stages = [indexSeg.indexFields];
7034
+ return;
7153
7035
  }
7154
- return s8;
7155
- }
7156
- // Map fields into stages based on their level of repeated nesting
7157
- //
7158
- mapFieldsIntoStages(fieldNames2) {
7159
- const fannedPrefixes = Object.keys(this.fanPrefixMap).sort((k1, k2) => {
7160
- if (k1.length < k2.length) {
7161
- return 1;
7162
- }
7163
- if (k1.length > k2.length) {
7164
- return -1;
7165
- }
7166
- return 0;
7167
- });
7168
- for (const fn of fieldNames2) {
7169
- let found = false;
7170
- for (const prefix of fannedPrefixes) {
7171
- if (fn.startsWith(prefix)) {
7172
- this.fanPrefixMap[prefix].push(fn);
7173
- found = true;
7036
+ const stageMap = {};
7037
+ for (const fref of indexSeg.indexFields) {
7038
+ if (fref.path.length > 1) {
7039
+ const stageRoot = pathToCol(fref.path.slice(0, fref.path.length - 1));
7040
+ const stage = stageMap[stageRoot];
7041
+ if (stage === void 0) {
7042
+ const f4 = this.parent.nameMap.get(fref.path[0]);
7043
+ if (f4 instanceof QueryStruct && (f4.fieldDef.structRelationship.type === "many" || f4.fieldDef.structRelationship.type === "nested") && f4.fieldDef.fields.length > 1) {
7044
+ const toStage = [fref];
7045
+ stageMap[stageRoot] = toStage;
7046
+ this.stages.push(toStage);
7047
+ continue;
7048
+ }
7049
+ } else {
7050
+ stage.push(fref);
7051
+ continue;
7174
7052
  }
7175
7053
  }
7176
- if (!found) {
7177
- this.rootFields.push(fn);
7054
+ if (this.stages[0] === void 0) {
7055
+ this.stages[0] = [];
7178
7056
  }
7057
+ this.stages[0].push(fref);
7179
7058
  }
7180
7059
  }
7181
7060
  expandFields(_resultStruct) {
7182
- const fieldNames2 = this.expandIndexWildCards();
7183
- this.mapFieldsIntoStages(fieldNames2);
7184
7061
  }
7185
7062
  generateSQL(stageWriter) {
7186
- const stages = this.getStageFields();
7063
+ const indexSeg = this.firstSegment;
7187
7064
  const outputStageNames = [];
7188
- for (const fields of stages) {
7065
+ for (const fields of this.stages) {
7189
7066
  const q = new QueryQueryIndexStage({
7190
7067
  ...this.fieldDef,
7191
7068
  pipeline: [
7192
7069
  {
7193
- ...this.fieldDef.pipeline[0],
7194
- fields
7070
+ ...indexSeg,
7071
+ indexFields: fields
7195
7072
  }
7196
7073
  ]
7197
7074
  }, this.parent, stageWriter, this.isJoinedSubquery);
@@ -7207,7 +7084,11 @@ FROM ${resultStage}
7207
7084
  }
7208
7085
  return this.resultStage;
7209
7086
  }
7210
- /** All Indexes have the same output schema */
7087
+ /**
7088
+ * All Indexes have the same output schema.
7089
+ * fieldName is deprecated, dots in fieldName may or may not be join nodes
7090
+ * fieldPath is a URL encoded slash separated path
7091
+ */
7211
7092
  getResultStructDef() {
7212
7093
  const ret = {
7213
7094
  type: "struct",
@@ -7215,6 +7096,7 @@ FROM ${resultStage}
7215
7096
  dialect: this.parent.fieldDef.dialect,
7216
7097
  fields: [
7217
7098
  { type: "string", name: "fieldName" },
7099
+ { type: "string", name: "fieldPath" },
7218
7100
  { type: "string", name: "fieldValue" },
7219
7101
  { type: "string", name: "fieldType" },
7220
7102
  { type: "number", name: "weight", numberType: "integer" }
@@ -7304,32 +7186,6 @@ FROM ${resultStage}
7304
7186
  return ret;
7305
7187
  }
7306
7188
  }
7307
- // when structs are referenced in queries, incorporate the
7308
- // primary key of struct and add the struct as a join to the result.
7309
- getAsQueryField() {
7310
- if (this.fieldDef.primaryKey === void 0) {
7311
- throw new Error(`Joined explores can only be included in queries if a primary key is defined: '${this.getFullOutputName()}' has no primary key`);
7312
- }
7313
- const pkField = this.getPrimaryKeyField(this.fieldDef);
7314
- const pkType = pkField.fieldDef.type;
7315
- if (pkType !== "string" && pkType !== "number") {
7316
- throw new Error(`Unknown Primary key data type for ${pkField.fieldDef.name}`);
7317
- }
7318
- const aliasName = (0, malloy_types_1.getIdentifier)(this.fieldDef);
7319
- const pkName = this.fieldDef.primaryKey;
7320
- const fieldDef = {
7321
- type: pkType,
7322
- name: `${aliasName}_id`,
7323
- e: [
7324
- {
7325
- type: "field",
7326
- // path: pkField.getFullOutputName(),
7327
- path: pkField.getIdentifier()
7328
- }
7329
- ]
7330
- };
7331
- return new QueryFieldStruct(fieldDef, this, `${aliasName}.${pkName}`);
7332
- }
7333
7189
  getSQLIdentifier() {
7334
7190
  if (this.unnestWithNumbers() && this.parent !== void 0) {
7335
7191
  const x6 = this.parent.getSQLIdentifier() + "." + (0, malloy_types_1.getIdentifier)(this.fieldDef) + `[${this.getIdentifier()}.__row_id]`;
@@ -7469,25 +7325,6 @@ FROM ${resultStage}
7469
7325
  throw new Error(`unknown field definition ${JSON.stringify(field3)}`);
7470
7326
  }
7471
7327
  }
7472
- /**
7473
- * return a field if it exists, make one if we are passed a field definition.
7474
- */
7475
- getOrMakeField(fieldRef2) {
7476
- if (typeof fieldRef2 === "string") {
7477
- return this.getFieldByName(fieldRef2);
7478
- } else {
7479
- return this.makeQueryField(fieldRef2);
7480
- }
7481
- }
7482
- /** returns a dimension for the given name or make one. */
7483
- getOrMakeDimension(fieldRef2) {
7484
- const dim = this.getOrMakeField(fieldRef2);
7485
- if (dim instanceof QueryAtomicField && isScalarField(dim)) {
7486
- return dim;
7487
- } else {
7488
- throw new Error(`${fieldRef2} is not of type a scalar'`);
7489
- }
7490
- }
7491
7328
  structSourceSQL(stageWriter) {
7492
7329
  switch (this.fieldDef.structSource.type) {
7493
7330
  case "table": {
@@ -7534,36 +7371,38 @@ FROM ${resultStage}
7534
7371
  }
7535
7372
  primaryKey() {
7536
7373
  if (this.fieldDef.primaryKey) {
7537
- return this.getDimensionByName(this.fieldDef.primaryKey);
7374
+ return this.getDimensionByName([this.fieldDef.primaryKey]);
7538
7375
  } else {
7539
7376
  return void 0;
7540
7377
  }
7541
7378
  }
7542
- /** get the componennts of a field path */
7543
- static resolvePath(name) {
7544
- return name.split(".");
7545
- }
7546
7379
  getChildByName(name) {
7547
7380
  return this.nameMap.get(name);
7548
7381
  }
7549
- /** convert a name into a field reference */
7550
- getFieldByName(name) {
7551
- const path3 = _QueryStruct.resolvePath(name);
7552
- let ret = this;
7553
- for (const n7 of path3) {
7554
- const r8 = ret.getChildByName(n7);
7382
+ /** convert a path into a field reference */
7383
+ getFieldByName(path3) {
7384
+ return path3.reduce((lookIn, childName) => {
7385
+ const r8 = lookIn.getChildByName(childName);
7555
7386
  if (r8 === void 0) {
7556
- throw new Error(`Path not found ${name}`);
7387
+ throw new Error(path3.length === 1 ? `'${childName}' not found` : `'${childName}' not found in '${path3.join(".")}'`);
7557
7388
  }
7558
- ret = r8;
7559
- }
7560
- return ret;
7389
+ return r8;
7390
+ }, this);
7561
7391
  }
7562
7392
  // structs referenced in queries are converted to fields.
7563
7393
  getQueryFieldByName(name) {
7564
- let field3 = this.getFieldByName(name);
7394
+ const field3 = this.getFieldByName(name);
7565
7395
  if (field3 instanceof _QueryStruct) {
7566
- field3 = field3.getAsQueryField();
7396
+ throw new Error(`Cannot reference ${name} as a scalar'`);
7397
+ }
7398
+ return field3;
7399
+ }
7400
+ getQueryFieldReference(name, refAnnoatation) {
7401
+ const field3 = this.getQueryFieldByName(name);
7402
+ if (refAnnoatation) {
7403
+ const newDef = { ...field3.fieldDef };
7404
+ newDef.annotation = refAnnoatation;
7405
+ field3.fieldDef = newDef;
7567
7406
  }
7568
7407
  return field3;
7569
7408
  }
@@ -7590,12 +7429,12 @@ FROM ${resultStage}
7590
7429
  if (struct instanceof _QueryStruct) {
7591
7430
  return struct;
7592
7431
  } else {
7593
- throw new Error(`Error: Path to structure not found '${name}'`);
7432
+ throw new Error(`Error: Path to structure not found '${name.join(".")}'`);
7594
7433
  }
7595
7434
  }
7596
7435
  getDistinctKey() {
7597
7436
  if (this.fieldDef.structRelationship.type !== "inline") {
7598
- return this.getDimensionByName("__distinct_key");
7437
+ return this.getDimensionByName(["__distinct_key"]);
7599
7438
  } else if (this.parent) {
7600
7439
  return this.parent.getDistinctKey();
7601
7440
  } else {
@@ -7642,20 +7481,6 @@ FROM ${resultStage}
7642
7481
  }
7643
7482
  }
7644
7483
  }
7645
- parseQueryPath(name) {
7646
- const path3 = name.split(".");
7647
- let struct;
7648
- if (struct = this.structs.get(path3[0])) {
7649
- if (path3.length > 1) {
7650
- path3.shift();
7651
- } else {
7652
- throw new Error(`No query specified in Struct '${path3[0]}'`);
7653
- }
7654
- return { queryName: path3.join("."), struct };
7655
- } else {
7656
- throw new Error(`Cannot find Struct '${path3[0]}' Model`);
7657
- }
7658
- }
7659
7484
  getStructByName(name) {
7660
7485
  let s8;
7661
7486
  if (s8 = this.structs.get(name)) {
@@ -7732,17 +7557,27 @@ FROM ${resultStage}
7732
7557
  return void 0;
7733
7558
  }
7734
7559
  const struct = this.getStructByName(explore);
7560
+ let indexStar = [];
7561
+ for (const [fn, fv] of struct.nameMap) {
7562
+ if (!(fv instanceof QueryStruct)) {
7563
+ if (isScalarField(fv) && fv.includeInWildcard()) {
7564
+ indexStar.push({ type: "fieldref", path: [fn] });
7565
+ }
7566
+ }
7567
+ }
7568
+ indexStar = indexStar.sort((a5, b4) => a5.path[0].localeCompare(b4.path[0]));
7735
7569
  const indexQuery = {
7736
7570
  structRef: explore,
7737
7571
  pipeline: [
7738
7572
  {
7739
7573
  type: "index",
7740
- fields: ["*"],
7574
+ indexFields: indexStar,
7741
7575
  sample: struct.dialect.defaultSampling
7742
7576
  }
7743
7577
  ]
7744
7578
  };
7745
7579
  const fieldNameColumn = struct.dialect.sqlMaybeQuoteIdentifier("fieldName");
7580
+ const fieldPathColumn = struct.dialect.sqlMaybeQuoteIdentifier("fieldPath");
7746
7581
  const fieldValueColumn = struct.dialect.sqlMaybeQuoteIdentifier("fieldValue");
7747
7582
  const fieldTypeColumn = struct.dialect.sqlMaybeQuoteIdentifier("fieldType");
7748
7583
  let sqlPDT = this.exploreSearchSQLMap.get(explore);
@@ -7752,10 +7587,11 @@ FROM ${resultStage}
7752
7587
  }
7753
7588
  let query = `SELECT
7754
7589
  ${fieldNameColumn},
7590
+ ${fieldPathColumn},
7755
7591
  ${fieldValueColumn},
7756
7592
  ${fieldTypeColumn},
7757
7593
  weight,
7758
- CASE WHEN lower(${fieldValueColumn}) LIKE lower(${generateSQLStringLiteral(searchValue + "%")}) THEN 1 ELSE 0 END as match_first
7594
+ CASE WHEN lower(${fieldValueColumn}) LIKE lower(${generateSQLStringLiteral(searchValue + "%")}) THEN 1 ELSE 0 END as match_first
7759
7595
  FROM ${await connection.manifestTemporaryTable(sqlPDT)}
7760
7596
  WHERE lower(${fieldValueColumn}) LIKE lower(${generateSQLStringLiteral("%" + searchValue + "%")}) ${searchField !== void 0 ? ` AND ${fieldNameColumn} = '` + searchField + "' \n" : ""}
7761
7597
  ORDER BY CASE WHEN lower(${fieldValueColumn}) LIKE lower(${generateSQLStringLiteral(searchValue + "%")}) THEN 1 ELSE 0 END DESC, weight DESC
@@ -7767,6 +7603,7 @@ ${query}
7767
7603
  )
7768
7604
  ${struct.dialect.sqlFinalStage("__stage0", [
7769
7605
  fieldNameColumn,
7606
+ fieldPathColumn,
7770
7607
  fieldValueColumn,
7771
7608
  fieldTypeColumn,
7772
7609
  "weight",
@@ -49926,6 +49763,20 @@ ${spaces}}`;
49926
49763
  };
49927
49764
  this.log.push(logMsg);
49928
49765
  }
49766
+ semanticError(cx, msg) {
49767
+ const errAt = {
49768
+ line: this.atLine,
49769
+ // mtoy TODO get this right
49770
+ character: 0
49771
+ };
49772
+ const range6 = { start: errAt, end: errAt };
49773
+ const logMsg = {
49774
+ message: msg,
49775
+ at: { url: this.sourceURL, range: range6 },
49776
+ severity: "error"
49777
+ };
49778
+ this.log.push(logMsg);
49779
+ }
49929
49780
  };
49930
49781
  function getBuildOn(ctx) {
49931
49782
  const buildOn = ctx["buildOn"];
@@ -49969,19 +49820,20 @@ ${spaces}}`;
49969
49820
  const inputStream = antlr4ts_1.CharStreams.fromString(source3);
49970
49821
  const lexer = new MalloyTagLexer_1.MalloyTagLexer(inputStream);
49971
49822
  const tokenStream = new antlr4ts_1.CommonTokenStream(lexer);
49823
+ const pLog = new TagErrorListener(sourceURL, onLine, atChar);
49972
49824
  const taglineParser = new MalloyTagParser_1.MalloyTagParser(tokenStream);
49973
49825
  taglineParser.removeErrorListeners();
49974
- const pLog = new TagErrorListener(sourceURL, onLine, atChar);
49975
49826
  taglineParser.addErrorListener(pLog);
49976
49827
  const tagTree = taglineParser.tagLine();
49977
- const treeWalker = new TaglineParser(outerScope);
49828
+ const treeWalker = new TaglineParser(outerScope, pLog);
49978
49829
  const tag = treeWalker.tagLineToTag(tagTree, extending);
49979
49830
  return { tag, log: pLog.log };
49980
49831
  }
49981
49832
  var TaglineParser = class extends tree_1.AbstractParseTreeVisitor {
49982
- constructor(outerScopes = []) {
49833
+ constructor(outerScopes = [], msgLog) {
49983
49834
  super();
49984
49835
  this.scopes = [];
49836
+ this.msgLog = msgLog;
49985
49837
  this.scopes.unshift(...outerScopes);
49986
49838
  }
49987
49839
  defaultResult() {
@@ -50051,6 +49903,7 @@ ${spaces}}`;
50051
49903
  break;
50052
49904
  }
50053
49905
  }
49906
+ this.msgLog.semanticError(ctx, `Reference to undefined property ${path3.join(".")}`);
50054
49907
  return this.defaultResult();
50055
49908
  }
50056
49909
  visitTagEq(ctx) {
@@ -50889,13 +50742,13 @@ ${left} `;
50889
50742
  };
50890
50743
  }
50891
50744
  static get reduceSegment() {
50892
- return { type: "reduce", fields: [] };
50745
+ return { type: "reduce", queryFields: [] };
50893
50746
  }
50894
50747
  static get projectSegment() {
50895
- return { type: "project", fields: [] };
50748
+ return { type: "project", queryFields: [] };
50896
50749
  }
50897
50750
  static get indexSegment() {
50898
- return { type: "index", fields: [] };
50751
+ return { type: "index", indexFields: [] };
50899
50752
  }
50900
50753
  };
50901
50754
  exports.ErrorFactory = ErrorFactory;
@@ -51106,20 +50959,21 @@ ${left} `;
51106
50959
  "../malloy/dist/lang/field-utils.js"(exports) {
51107
50960
  "use strict";
51108
50961
  Object.defineProperty(exports, "__esModule", { value: true });
51109
- exports.mergeFields = exports.nameOf = void 0;
51110
- function nameOf(qfd) {
51111
- if (typeof qfd === "string") {
51112
- return qfd;
50962
+ exports.mergeFields = exports.nameFromDef = void 0;
50963
+ function nameFromDef(f1) {
50964
+ var _a;
50965
+ if (f1.type === "fieldref") {
50966
+ return f1.path[f1.path.length - 1];
51113
50967
  }
51114
- return qfd.as || qfd.name;
50968
+ return (_a = f1.as) !== null && _a !== void 0 ? _a : f1.name;
51115
50969
  }
51116
- exports.nameOf = nameOf;
50970
+ exports.nameFromDef = nameFromDef;
51117
50971
  function mergeFields(older, newer) {
51118
50972
  if (older === void 0) {
51119
50973
  return newer;
51120
50974
  }
51121
- const redefined = new Set(newer.map((f4) => nameOf(f4)));
51122
- const merged = older.filter((f4) => !redefined.has(nameOf(f4)));
50975
+ const redefined = new Set(newer.map((f4) => nameFromDef(f4)));
50976
+ const merged = older.filter((f4) => !redefined.has(nameFromDef(f4)));
51123
50977
  merged.push(...newer);
51124
50978
  return merged;
51125
50979
  }
@@ -51495,7 +51349,7 @@ ${left} `;
51495
51349
  this.setEntry(newName, entry2);
51496
51350
  }
51497
51351
  addFieldDef(fd) {
51498
- this.setEntry((0, field_utils_1.nameOf)(fd), this.defToSpaceField(fd));
51352
+ this.setEntry((0, field_utils_1.nameFromDef)(fd), this.defToSpaceField(fd));
51499
51353
  }
51500
51354
  setTimezone(tz) {
51501
51355
  this.newTimezone = tz;
@@ -51867,7 +51721,7 @@ ${left} `;
51867
51721
  onExpression: [
51868
51722
  {
51869
51723
  type: "field",
51870
- path: `${this.name}.${inStruct.primaryKey}`
51724
+ path: [this.name.refString, inStruct.primaryKey]
51871
51725
  },
51872
51726
  "=",
51873
51727
  ...exprX.value
@@ -54121,7 +53975,6 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54121
53975
  "use strict";
54122
53976
  Object.defineProperty(exports, "__esModule", { value: true });
54123
53977
  exports.ReferenceField = void 0;
54124
- var malloy_types_1 = require_malloy_types();
54125
53978
  var space_field_1 = require_space_field();
54126
53979
  var ReferenceField = class extends space_field_1.SpaceField {
54127
53980
  constructor(fieldRef2, inFS) {
@@ -54143,7 +53996,22 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54143
53996
  if (check.error) {
54144
53997
  this.fieldRef.log(check.error);
54145
53998
  }
54146
- this.queryFieldDef = this.maybeAnnotate();
53999
+ this.queryFieldDef = {
54000
+ type: "fieldref",
54001
+ path: this.fieldRef.list.map((f4) => f4.name)
54002
+ };
54003
+ const refTo = this.referenceTo;
54004
+ if (refTo instanceof space_field_1.SpaceField && refTo.haveFieldDef) {
54005
+ const origFd = refTo.haveFieldDef;
54006
+ const notes = this.fieldRef.note;
54007
+ if (origFd.annotation || notes) {
54008
+ const annotation = notes || {};
54009
+ if (origFd.annotation) {
54010
+ annotation.inherits = origFd.annotation;
54011
+ }
54012
+ this.queryFieldDef.annotation = annotation;
54013
+ }
54014
+ }
54147
54015
  }
54148
54016
  return this.queryFieldDef;
54149
54017
  }
@@ -54157,44 +54025,6 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54157
54025
  }
54158
54026
  return { dataType: "error", expressionType: "scalar", evalSpace: "input" };
54159
54027
  }
54160
- /**
54161
- * If the referenced field has any annotations, replace the reference
54162
- * with something which can hold an annotation
54163
- */
54164
- maybeAnnotate() {
54165
- const path3 = this.fieldRef.refString;
54166
- const refTo = this.referenceTo;
54167
- if (refTo instanceof space_field_1.SpaceField && refTo.haveFieldDef && typeof refTo.haveFieldDef !== "string") {
54168
- const origFd = refTo.haveFieldDef;
54169
- if ((0, malloy_types_1.isFilteredAliasedName)(origFd)) {
54170
- return origFd;
54171
- }
54172
- const notes = this.fieldRef.note;
54173
- if (origFd.annotation || notes) {
54174
- const annotation = notes || {};
54175
- if (origFd.annotation) {
54176
- annotation.inherits = origFd.annotation;
54177
- }
54178
- if (this.fieldRef.list.length > 1) {
54179
- if ((0, malloy_types_1.isAtomicFieldType)(origFd.type)) {
54180
- const newField = {
54181
- name: this.fieldRef.list[this.fieldRef.list.length - 1].refString,
54182
- type: origFd.type,
54183
- e: [{ type: "field", path: path3 }],
54184
- annotation
54185
- };
54186
- if ((0, malloy_types_1.hasExpression)(origFd)) {
54187
- newField.expressionType = origFd.expressionType;
54188
- }
54189
- return newField;
54190
- }
54191
- return path3;
54192
- }
54193
- return { ...origFd, annotation };
54194
- }
54195
- }
54196
- return path3;
54197
- }
54198
54028
  };
54199
54029
  exports.ReferenceField = ReferenceField;
54200
54030
  }
@@ -54364,8 +54194,17 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54364
54194
  fs.newEntry(refName, this, new reference_field_1.ReferenceField(this, fromFS));
54365
54195
  }
54366
54196
  }
54197
+ get refToField() {
54198
+ return {
54199
+ type: "fieldref",
54200
+ path: this.list.map((n7) => n7.refString)
54201
+ };
54202
+ }
54367
54203
  get refString() {
54368
- return this.list.map((n7) => n7.refString).join(".");
54204
+ return this.path.join(".");
54205
+ }
54206
+ get path() {
54207
+ return this.list.map((n7) => n7.refString);
54369
54208
  }
54370
54209
  get outputName() {
54371
54210
  const last = this.list[this.list.length - 1];
@@ -54422,7 +54261,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54422
54261
  }
54423
54262
  };
54424
54263
  exports.CalculateFieldReference = CalculateFieldReference;
54425
- var IndexFieldReference = class extends FieldReference {
54264
+ var IndexFieldReference = class _IndexFieldReference extends FieldReference {
54426
54265
  constructor() {
54427
54266
  super(...arguments);
54428
54267
  this.elementType = "indexFieldReference";
@@ -54430,6 +54269,13 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54430
54269
  typecheck(type3) {
54431
54270
  (0, typecheck_utils_1.typecheckIndex)(type3, this);
54432
54271
  }
54272
+ // Index entries are expected to be unique on path
54273
+ static indexOutputName(path3) {
54274
+ return path3.map((pe) => encodeURIComponent(pe)).join("/");
54275
+ }
54276
+ get outputName() {
54277
+ return _IndexFieldReference.indexOutputName(this.path);
54278
+ }
54433
54279
  };
54434
54280
  exports.IndexFieldReference = IndexFieldReference;
54435
54281
  var AggregateFieldReference = class extends FieldReference {
@@ -54560,7 +54406,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54560
54406
  value: [{ type: "outputField", name: this.refString }]
54561
54407
  };
54562
54408
  }
54563
- const value3 = [{ type: def2.found.refType, path: this.refString }];
54409
+ const value3 = [{ type: def2.found.refType, path: this.fieldReference.path }];
54564
54410
  const evalSpace = (0, malloy_types_1.expressionIsAggregate)(td.expressionType) ? "output" : td.evalSpace;
54565
54411
  return { ...td, value: value3, evalSpace };
54566
54412
  }
@@ -54582,7 +54428,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54582
54428
  {
54583
54429
  type: "apply",
54584
54430
  value: lval.value,
54585
- to: [{ type: "parameter", path: this.refString }]
54431
+ to: [{ type: "parameter", path: this.fieldReference.path }]
54586
54432
  }
54587
54433
  ]
54588
54434
  };
@@ -54631,7 +54477,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54631
54477
  const inputFS = fs.isQueryFieldSpace() ? fs.inputSpace() : fs;
54632
54478
  let expr2 = this.expr;
54633
54479
  let exprVal = (_a = this.expr) === null || _a === void 0 ? void 0 : _a.getExpression(inputFS);
54634
- let structPath = (_b = this.source) === null || _b === void 0 ? void 0 : _b.refString;
54480
+ let structPath = (_b = this.source) === null || _b === void 0 ? void 0 : _b.path;
54635
54481
  let sourceRelationship = [];
54636
54482
  if (this.source) {
54637
54483
  const result = this.source.getField(inputFS);
@@ -54650,17 +54496,16 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54650
54496
  name: this.source.refString
54651
54497
  } : {
54652
54498
  type: "field",
54653
- path: this.source.refString
54499
+ path: this.source.path
54654
54500
  }
54655
54501
  ],
54656
54502
  evalSpace: footType.evalSpace
54657
54503
  };
54658
- structPath = this.source.sourceString;
54504
+ structPath = this.source.path;
54659
54505
  const joinUsage = this.getJoinUsage(inputFS);
54660
- const allUsagePaths = joinUsage.map((x6) => x6.map((y7) => y7.name).join("."));
54661
- const allUsagesSame = allUsagePaths.length > 0 && allUsagePaths.slice(1).every((x6) => x6 === allUsagePaths[0]);
54506
+ const allUsagesSame = joinUsage.length === 1 || joinUsage.slice(1).every((p3) => joinPathEq(p3, joinUsage[0]));
54662
54507
  if (allUsagesSame) {
54663
- structPath = allUsagePaths[0];
54508
+ structPath = joinUsage[0].map((p3) => p3.name);
54664
54509
  sourceRelationship = joinUsage[0];
54665
54510
  }
54666
54511
  } else {
@@ -54708,7 +54553,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54708
54553
  function: this.func,
54709
54554
  e: exprVal.value
54710
54555
  };
54711
- if (structPath) {
54556
+ if (structPath && structPath.length > 0) {
54712
54557
  f4.structPath = structPath;
54713
54558
  }
54714
54559
  return {
@@ -54728,7 +54573,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54728
54573
  if (this.source) {
54729
54574
  const lookup6 = this.source.getField(fs);
54730
54575
  if (lookup6.found) {
54731
- const sfd = [{ type: "field", path: this.source.refString }];
54576
+ const sfd = [{ type: "field", path: this.source.path }];
54732
54577
  result.push(...getJoinUsage(fs, sfd));
54733
54578
  }
54734
54579
  }
@@ -54740,18 +54585,30 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
54740
54585
  }
54741
54586
  };
54742
54587
  exports.ExprAggregateFunction = ExprAggregateFunction;
54588
+ function joinPathEq(a1, a22) {
54589
+ let len = a1.length;
54590
+ if (len !== a22.length) {
54591
+ return false;
54592
+ }
54593
+ while (len > 0) {
54594
+ len -= 1;
54595
+ if (a1[len].name !== a22[len].name) {
54596
+ return false;
54597
+ }
54598
+ }
54599
+ return true;
54600
+ }
54743
54601
  function getJoinUsage(fs, expr2) {
54744
54602
  const result = [];
54745
54603
  const lookup6 = (fs2, path3) => {
54746
- const parts = path3.split(".");
54747
- const head = parts[0];
54748
- const rest = parts.slice(1);
54604
+ const head = path3[0];
54605
+ const rest = path3.slice(1);
54749
54606
  const def2 = fs2.entry(head);
54750
54607
  if (def2 === void 0) {
54751
54608
  throw new Error(`Invalid field lookup ${head}`);
54752
54609
  }
54753
54610
  if (def2 instanceof static_space_1.StructSpaceField && rest.length > 0) {
54754
- const restDef = lookup6(def2.fieldSpace, rest.join("."));
54611
+ const restDef = lookup6(def2.fieldSpace, rest);
54755
54612
  return {
54756
54613
  ...restDef,
54757
54614
  relationship: [
@@ -55067,7 +54924,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55067
54924
  e: []
55068
54925
  };
55069
54926
  if (this.source) {
55070
- ret.structPath = this.source.refString;
54927
+ ret.structPath = this.source.path;
55071
54928
  }
55072
54929
  return {
55073
54930
  dataType: "number",
@@ -55207,7 +55064,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55207
55064
  this.log(`Case insensitivity for function names is deprecated, use '${func.name}' instead`, "warn");
55208
55065
  }
55209
55066
  let implicitExpr = void 0;
55210
- let structPath = (_d2 = this.source) === null || _d2 === void 0 ? void 0 : _d2.refString;
55067
+ let structPath = (_d2 = this.source) === null || _d2 === void 0 ? void 0 : _d2.path;
55211
55068
  if (this.source) {
55212
55069
  const sourceFoot = this.source.getField(fs).found;
55213
55070
  if (sourceFoot) {
@@ -55216,10 +55073,10 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55216
55073
  implicitExpr = {
55217
55074
  dataType: footType.dataType,
55218
55075
  expressionType: footType.expressionType,
55219
- value: [{ type: "field", path: this.source.refString }],
55076
+ value: [{ type: "field", path: this.source.path }],
55220
55077
  evalSpace: footType.evalSpace
55221
55078
  };
55222
- structPath = this.source.sourceString;
55079
+ structPath = this.source.path.slice(0, -1);
55223
55080
  } else {
55224
55081
  if (!(sourceFoot instanceof struct_space_field_base_1.StructSpaceFieldBase)) {
55225
55082
  const message = `Aggregate source cannot be a ${footType.dataType}`;
@@ -55303,7 +55160,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55303
55160
  funcCall = [
55304
55161
  {
55305
55162
  type: "sql-string",
55306
- e: parts.map((part) => part.type === "string" ? part.value : part.name === "TABLE" ? { type: "source-reference" } : { type: "field-reference", path: part.name })
55163
+ e: parts.map((part) => part.type === "string" ? part.value : part.name === "TABLE" ? { type: "source-reference" } : { type: "field", path: [part.name] })
55307
55164
  }
55308
55165
  ];
55309
55166
  }
@@ -55915,6 +55772,33 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55915
55772
  }
55916
55773
  });
55917
55774
 
55775
+ // ../malloy/dist/lang/ast/types/field-space.js
55776
+ var require_field_space = __commonJS({
55777
+ "../malloy/dist/lang/ast/types/field-space.js"(exports) {
55778
+ "use strict";
55779
+ Object.defineProperty(exports, "__esModule", { value: true });
55780
+ exports.FieldName = void 0;
55781
+ var malloy_element_1 = require_malloy_element();
55782
+ var FieldName = class extends malloy_element_1.MalloyElement {
55783
+ constructor(name) {
55784
+ super();
55785
+ this.name = name;
55786
+ this.elementType = "fieldName";
55787
+ }
55788
+ get refString() {
55789
+ return this.name;
55790
+ }
55791
+ toString() {
55792
+ return this.refString;
55793
+ }
55794
+ getField(fs) {
55795
+ return fs.lookup([this]);
55796
+ }
55797
+ };
55798
+ exports.FieldName = FieldName;
55799
+ }
55800
+ });
55801
+
55918
55802
  // ../malloy/dist/lang/ast/field-space/query-input-space.js
55919
55803
  var require_query_input_space = __commonJS({
55920
55804
  "../malloy/dist/lang/ast/field-space/query-input-space.js"(exports) {
@@ -55996,16 +55880,17 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
55996
55880
  return result;
55997
55881
  };
55998
55882
  Object.defineProperty(exports, "__esModule", { value: true });
55999
- exports.ReduceFieldSpace = exports.QuerySpace = void 0;
55883
+ exports.ReduceFieldSpace = exports.QuerySpace = exports.QueryOperationSpace = void 0;
56000
55884
  var model = __importStar(require_malloy_types());
56001
55885
  var field_utils_1 = require_field_utils();
55886
+ var field_space_1 = require_field_space();
56002
55887
  var space_field_1 = require_space_field();
56003
55888
  var field_references_1 = require_field_references();
56004
55889
  var refined_space_1 = require_refined_space();
56005
55890
  var column_space_field_1 = require_column_space_field();
56006
55891
  var static_space_1 = require_static_space();
56007
55892
  var query_input_space_1 = require_query_input_space();
56008
- var QuerySpace = class extends refined_space_1.RefinedSpace {
55893
+ var QueryOperationSpace = class extends refined_space_1.RefinedSpace {
56009
55894
  constructor(queryInputSpace, refineThis, nestParent, astEl) {
56010
55895
  super(queryInputSpace.emptyStructDef());
56011
55896
  this.queryInputSpace = queryInputSpace;
@@ -56016,40 +55901,16 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56016
55901
  if (refineThis)
56017
55902
  this.addRefineFromFields(refineThis);
56018
55903
  }
56019
- addRefineFromFields(refineThis) {
56020
- var _a, _b;
56021
- for (const field3 of refineThis.fields) {
56022
- if (typeof field3 === "string") {
56023
- const ent = this.exprSpace.entry(field3);
56024
- if (ent) {
56025
- this.setEntry(field3, ent);
56026
- }
56027
- } else if (model.isFilteredAliasedName(field3)) {
56028
- const name = (_a = field3.as) !== null && _a !== void 0 ? _a : field3.name;
56029
- const ent = this.exprSpace.entry(name);
56030
- if (ent) {
56031
- this.setEntry(name, ent);
56032
- }
56033
- } else {
56034
- if (field3.type !== "turtle") {
56035
- this.setEntry((_b = field3.as) !== null && _b !== void 0 ? _b : field3.name, new column_space_field_1.ColumnSpaceField(field3));
56036
- }
56037
- }
56038
- }
56039
- }
56040
55904
  log(s8) {
56041
55905
  if (this.astEl) {
56042
55906
  this.astEl.log(s8);
56043
55907
  }
56044
55908
  }
56045
- pushFields(...defs) {
56046
- for (const f4 of defs) {
56047
- if (f4 instanceof field_references_1.WildcardFieldReference) {
56048
- this.addWild(f4);
56049
- } else {
56050
- super.pushFields(f4);
56051
- }
56052
- }
55909
+ inputSpace() {
55910
+ return this.exprSpace;
55911
+ }
55912
+ outputSpace() {
55913
+ return this;
56053
55914
  }
56054
55915
  addWild(wild) {
56055
55916
  var _a;
@@ -56074,6 +55935,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56074
55935
  }
56075
55936
  }
56076
55937
  const dialect = this.dialectObj();
55938
+ const expandEntries = [];
56077
55939
  for (const [name, entry2] of current2.entries()) {
56078
55940
  if (wild.except.has(name)) {
56079
55941
  continue;
@@ -56084,11 +55946,42 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56084
55946
  } else {
56085
55947
  const eType = entry2.typeDesc();
56086
55948
  if (model.isAtomicFieldType(eType.dataType) && model.expressionIsScalar(eType.expressionType) && (dialect === void 0 || !dialect.ignoreInProject(name))) {
56087
- this.setEntry(name, entry2);
55949
+ expandEntries.push({ name, entry: entry2 });
56088
55950
  this.expandedWild[name] = joinPath.concat(name);
56089
55951
  }
56090
55952
  }
56091
55953
  }
55954
+ for (const x6 of expandEntries.sort((a5, b4) => a5.name.localeCompare(b4.name))) {
55955
+ this.setEntry(x6.name, x6.entry);
55956
+ }
55957
+ }
55958
+ };
55959
+ exports.QueryOperationSpace = QueryOperationSpace;
55960
+ var QuerySpace = class extends QueryOperationSpace {
55961
+ addRefineFromFields(refineThis) {
55962
+ var _a;
55963
+ if (!model.isQuerySegment(refineThis)) {
55964
+ return;
55965
+ }
55966
+ for (const field3 of refineThis.queryFields) {
55967
+ if (field3.type === "fieldref") {
55968
+ const refTo = this.exprSpace.lookup(field3.path.map((f4) => new field_space_1.FieldName(f4)));
55969
+ if (refTo.found) {
55970
+ this.setEntry(field3.path[field3.path.length - 1], refTo.found);
55971
+ }
55972
+ } else if (field3.type !== "turtle") {
55973
+ this.setEntry((_a = field3.as) !== null && _a !== void 0 ? _a : field3.name, new column_space_field_1.ColumnSpaceField(field3));
55974
+ }
55975
+ }
55976
+ }
55977
+ pushFields(...defs) {
55978
+ for (const f4 of defs) {
55979
+ if (f4 instanceof field_references_1.WildcardFieldReference) {
55980
+ this.addWild(f4);
55981
+ } else {
55982
+ super.pushFields(f4);
55983
+ }
55984
+ }
56092
55985
  }
56093
55986
  canContain(_typeDesc) {
56094
55987
  return true;
@@ -56099,7 +55992,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56099
55992
  if (field3 instanceof space_field_1.SpaceField) {
56100
55993
  const wildPath = this.expandedWild[name];
56101
55994
  if (wildPath) {
56102
- fields.push(wildPath.join("."));
55995
+ fields.push({ type: "fieldref", path: wildPath });
56103
55996
  continue;
56104
55997
  }
56105
55998
  const fieldQueryDef = field3.getQueryFieldDef(this.exprSpace);
@@ -56123,7 +56016,8 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56123
56016
  }
56124
56017
  getPipeSegment(refineFrom) {
56125
56018
  if (this.segmentType === "index") {
56126
- throw new Error("INDEX FIELD PIPE SEGMENT MIS HANDLED");
56019
+ this.log("internal error generating index segment from non index query");
56020
+ return { type: "reduce", queryFields: [] };
56127
56021
  }
56128
56022
  if (refineFrom === null || refineFrom === void 0 ? void 0 : refineFrom.extendSource) {
56129
56023
  for (const xField of refineFrom.extendSource) {
@@ -56132,9 +56026,9 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56132
56026
  }
56133
56027
  const segment = {
56134
56028
  type: this.segmentType,
56135
- fields: this.queryFieldDefs()
56029
+ queryFields: this.queryFieldDefs()
56136
56030
  };
56137
- segment.fields = (0, field_utils_1.mergeFields)(refineFrom === null || refineFrom === void 0 ? void 0 : refineFrom.fields, segment.fields);
56031
+ segment.queryFields = (0, field_utils_1.mergeFields)(refineFrom === null || refineFrom === void 0 ? void 0 : refineFrom.queryFields, segment.queryFields);
56138
56032
  if (refineFrom === null || refineFrom === void 0 ? void 0 : refineFrom.extendSource) {
56139
56033
  segment.extendSource = refineFrom.extendSource;
56140
56034
  }
@@ -56142,7 +56036,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56142
56036
  const newExtends = [];
56143
56037
  const extendedStruct = this.exprSpace.structDef();
56144
56038
  for (const extendName of this.exprSpace.extendList) {
56145
- const extendEnt = extendedStruct.fields.find((f4) => (0, field_utils_1.nameOf)(f4) === extendName);
56039
+ const extendEnt = extendedStruct.fields.find((f4) => (0, field_utils_1.nameFromDef)(f4) === extendName);
56146
56040
  if (extendEnt) {
56147
56041
  newExtends.push(extendEnt);
56148
56042
  }
@@ -56165,12 +56059,6 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56165
56059
  isQueryFieldSpace() {
56166
56060
  return true;
56167
56061
  }
56168
- outputSpace() {
56169
- return this;
56170
- }
56171
- inputSpace() {
56172
- return this.exprSpace;
56173
- }
56174
56062
  };
56175
56063
  exports.QuerySpace = QuerySpace;
56176
56064
  var ReduceFieldSpace = class extends QuerySpace {
@@ -56181,7 +56069,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
56181
56069
  };
56182
56070
  exports.ReduceFieldSpace = ReduceFieldSpace;
56183
56071
  function isEmptyNest(fd) {
56184
- return typeof fd !== "string" && !model.isFilteredAliasedName(fd) && fd.type === "turtle" && fd.pipeline.length === 0;
56072
+ return typeof fd !== "string" && fd.type === "turtle" && fd.pipeline.length === 0;
56185
56073
  }
56186
56074
  }
56187
56075
  });
@@ -62983,6 +62871,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
62983
62871
  Object.defineProperty(exports, "__esModule", { value: true });
62984
62872
  exports.refine = void 0;
62985
62873
  var model_1 = require_model();
62874
+ var field_utils_1 = require_field_utils();
62986
62875
  function refine(logTo, refineTo, refineFrom) {
62987
62876
  var _a, _b;
62988
62877
  if (refineTo.length !== 1) {
@@ -63020,31 +62909,31 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63020
62909
  }
63021
62910
  }
63022
62911
  to.filterList = to.filterList !== void 0 || from.filterList !== void 0 ? [...(_a = to.filterList) !== null && _a !== void 0 ? _a : [], ...(_b = from.filterList) !== null && _b !== void 0 ? _b : []] : void 0;
63023
- const overlappingFields = [];
63024
- const nonOverlappingFields = [];
63025
- const existingNames = new Map(to.fields.map((f4) => [
63026
- extractName(f4),
63027
- f4
63028
- ]));
63029
- for (const field3 of from.fields) {
63030
- if (existingNames.has(extractName(field3))) {
63031
- overlappingFields.push(field3);
63032
- } else {
63033
- nonOverlappingFields.push(field3);
62912
+ if ((0, model_1.isQuerySegment)(from) && (0, model_1.isQuerySegment)(to)) {
62913
+ const overlappingFields = [];
62914
+ const nonOverlappingFields = [];
62915
+ const existingNames = new Map(to.queryFields.map((f4) => [
62916
+ (0, field_utils_1.nameFromDef)(f4),
62917
+ f4
62918
+ ]));
62919
+ for (const field3 of from.queryFields) {
62920
+ if (existingNames.has((0, field_utils_1.nameFromDef)(field3))) {
62921
+ overlappingFields.push(field3);
62922
+ } else {
62923
+ nonOverlappingFields.push(field3);
62924
+ }
63034
62925
  }
63035
- }
63036
- to.fields = [...to.fields, ...nonOverlappingFields];
63037
- if (overlappingFields.length > 0) {
63038
- logTo.log(`overlapping fields in refinement: ${overlappingFields.map(extractName)}`);
62926
+ to.queryFields = [...to.queryFields, ...nonOverlappingFields];
62927
+ if (overlappingFields.length > 0) {
62928
+ logTo.log(`overlapping fields in refinement: ${overlappingFields.map(field_utils_1.nameFromDef)}`);
62929
+ }
62930
+ } else if (from.type === "index" && to.type === "index") {
62931
+ to.indexFields = [...from.indexFields, ...to.indexFields];
63039
62932
  }
63040
62933
  }
63041
62934
  return [to];
63042
62935
  }
63043
62936
  exports.refine = refine;
63044
- function extractName(f1) {
63045
- var _a;
63046
- return typeof f1 === "string" ? f1 : (_a = f1.as) !== null && _a !== void 0 ? _a : f1.name;
63047
- }
63048
62937
  }
63049
62938
  });
63050
62939
 
@@ -63142,7 +63031,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63142
63031
  }
63143
63032
  const newSegment = {
63144
63033
  type: "reduce",
63145
- fields: [this.reference.refString]
63034
+ queryFields: [this.reference.refToField]
63146
63035
  };
63147
63036
  const { dialect, queryTimezone, structRelationship } = fs.structDef();
63148
63037
  const name = this.reference.nameString;
@@ -63287,40 +63176,102 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63287
63176
  "use strict";
63288
63177
  Object.defineProperty(exports, "__esModule", { value: true });
63289
63178
  exports.IndexFieldSpace = void 0;
63179
+ var malloy_types_1 = require_malloy_types();
63290
63180
  var field_references_1 = require_field_references();
63181
+ var space_field_1 = require_space_field();
63291
63182
  var query_spaces_1 = require_query_spaces();
63292
- var IndexFieldSpace = class extends query_spaces_1.QuerySpace {
63183
+ var reference_field_1 = require_reference_field();
63184
+ var static_space_1 = require_static_space();
63185
+ var IndexFieldSpace = class extends query_spaces_1.QueryOperationSpace {
63293
63186
  constructor() {
63294
63187
  super(...arguments);
63295
63188
  this.segmentType = "index";
63296
- this.fieldList = /* @__PURE__ */ new Set();
63297
63189
  }
63298
63190
  pushFields(...defs) {
63299
63191
  for (const indexField of defs) {
63300
63192
  if (indexField instanceof field_references_1.FieldReference) {
63301
- if (indexField.getField(this.inputSpace()).found) {
63302
- this.fieldList.add(indexField.refString);
63303
- }
63193
+ super.pushFields(indexField);
63304
63194
  } else if (indexField instanceof field_references_1.WildcardFieldReference) {
63305
- this.fieldList.add(indexField.refString);
63195
+ this.addWild(indexField);
63306
63196
  } else {
63307
63197
  indexField.log("Internal error, not expected in index query");
63308
63198
  }
63309
63199
  }
63310
63200
  }
63311
63201
  getPipeSegment(refineIndex) {
63312
- if (refineIndex && refineIndex.fields) {
63313
- for (const exists of refineIndex.fields) {
63314
- if (typeof exists === "string") {
63315
- this.fieldList.add(exists);
63202
+ if (refineIndex) {
63203
+ this.log("index query operations cannot be refined");
63204
+ return { type: "index", indexFields: [] };
63205
+ }
63206
+ const indexFields = [];
63207
+ for (const [name, field3] of this.entries()) {
63208
+ if (field3 instanceof space_field_1.SpaceField) {
63209
+ const wildPath = this.expandedWild[name];
63210
+ if (wildPath) {
63211
+ indexFields.push({ type: "fieldref", path: wildPath });
63212
+ continue;
63213
+ }
63214
+ if (field3 instanceof reference_field_1.ReferenceField) {
63215
+ const fieldRef2 = field3.fieldRef;
63216
+ const check = fieldRef2.getField(this.exprSpace);
63217
+ if (check.error) {
63218
+ fieldRef2.log(check.error);
63219
+ } else {
63220
+ indexFields.push(fieldRef2.refToField);
63221
+ }
63316
63222
  }
63317
63223
  }
63318
63224
  }
63319
- this.isComplete();
63320
- return {
63321
- type: "index",
63322
- fields: Array.from(this.fieldList.values())
63323
- };
63225
+ return { type: "index", indexFields };
63226
+ }
63227
+ addRefineFromFields(_refineThis) {
63228
+ }
63229
+ addWild(wild) {
63230
+ var _a;
63231
+ let current2 = this.exprSpace;
63232
+ const joinPath = [];
63233
+ if (wild.joinPath) {
63234
+ for (const pathPart of wild.joinPath.list) {
63235
+ const part = pathPart.refString;
63236
+ joinPath.push(part);
63237
+ const ent = current2.entry(part);
63238
+ if (ent) {
63239
+ if (ent instanceof static_space_1.StructSpaceField) {
63240
+ current2 = ent.fieldSpace;
63241
+ } else {
63242
+ pathPart.log(`Field '${part}' does not contain rows and cannot be expanded with '*'`);
63243
+ return;
63244
+ }
63245
+ } else {
63246
+ pathPart.log(`No such field as '${part}'`);
63247
+ return;
63248
+ }
63249
+ }
63250
+ }
63251
+ const dialect = this.dialectObj();
63252
+ const expandEntries = [];
63253
+ for (const [name, entry2] of current2.entries()) {
63254
+ if (wild.except.has(name)) {
63255
+ continue;
63256
+ }
63257
+ const indexName = field_references_1.IndexFieldReference.indexOutputName([
63258
+ ...joinPath,
63259
+ name
63260
+ ]);
63261
+ if (this.entry(indexName)) {
63262
+ const conflict = (_a = this.expandedWild[indexName]) === null || _a === void 0 ? void 0 : _a.join(".");
63263
+ wild.log(`Cannot expand '${name}' in '${wild.refString}' because a field with that name already exists${conflict ? ` (conflicts with ${conflict})` : ""}`);
63264
+ } else {
63265
+ const eType = entry2.typeDesc();
63266
+ if ((0, malloy_types_1.isAtomicFieldType)(eType.dataType) && (0, malloy_types_1.expressionIsScalar)(eType.expressionType) && (dialect === void 0 || !dialect.ignoreInProject(name))) {
63267
+ expandEntries.push({ name: indexName, entry: entry2 });
63268
+ this.expandedWild[indexName] = joinPath.concat(name);
63269
+ }
63270
+ }
63271
+ }
63272
+ for (const x6 of expandEntries.sort((a5, b4) => a5.name.localeCompare(b4.name))) {
63273
+ this.setEntry(x6.name, x6.entry);
63274
+ }
63324
63275
  }
63325
63276
  };
63326
63277
  exports.IndexFieldSpace = IndexFieldSpace;
@@ -63451,33 +63402,6 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63451
63402
  }
63452
63403
  });
63453
63404
 
63454
- // ../malloy/dist/lang/ast/types/field-space.js
63455
- var require_field_space = __commonJS({
63456
- "../malloy/dist/lang/ast/types/field-space.js"(exports) {
63457
- "use strict";
63458
- Object.defineProperty(exports, "__esModule", { value: true });
63459
- exports.FieldName = void 0;
63460
- var malloy_element_1 = require_malloy_element();
63461
- var FieldName = class extends malloy_element_1.MalloyElement {
63462
- constructor(name) {
63463
- super();
63464
- this.name = name;
63465
- this.elementType = "fieldName";
63466
- }
63467
- get refString() {
63468
- return this.name;
63469
- }
63470
- toString() {
63471
- return this.refString;
63472
- }
63473
- getField(fs) {
63474
- return fs.lookup([this]);
63475
- }
63476
- };
63477
- exports.FieldName = FieldName;
63478
- }
63479
- });
63480
-
63481
63405
  // ../malloy/dist/lang/ast/query-properties/ordering.js
63482
63406
  var require_ordering = __commonJS({
63483
63407
  "../malloy/dist/lang/ast/query-properties/ordering.js"(exports) {
@@ -63601,7 +63525,7 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63601
63525
  "../malloy/dist/lang/ast/query-builders/reduce-builder.js"(exports) {
63602
63526
  "use strict";
63603
63527
  Object.defineProperty(exports, "__esModule", { value: true });
63604
- exports.ReduceBuilder = void 0;
63528
+ exports.ReduceBuilder = exports.QuerySegmentBuilder = void 0;
63605
63529
  var malloy_types_1 = require_malloy_types();
63606
63530
  var error_factory_1 = require_error_factory();
63607
63531
  var filters_1 = require_filters();
@@ -63610,15 +63534,9 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63610
63534
  var top_1 = require_top();
63611
63535
  var query_spaces_1 = require_query_spaces();
63612
63536
  var definition_list_1 = require_definition_list();
63613
- var ReduceBuilder = class {
63614
- constructor(baseFS, refineThis, isNestIn, astEl) {
63537
+ var QuerySegmentBuilder = class {
63538
+ constructor() {
63615
63539
  this.filters = [];
63616
- this.resultFS = this.getResultSpace(baseFS, refineThis, isNestIn, astEl);
63617
- this.inputFS = this.resultFS.inputSpace();
63618
- this.type = "grouping";
63619
- }
63620
- getResultSpace(fs, refineThis, isNestIn, astEl) {
63621
- return new query_spaces_1.ReduceFieldSpace(fs, refineThis, isNestIn, astEl);
63622
63540
  }
63623
63541
  execute(qp) {
63624
63542
  if (qp.queryExecute) {
@@ -63689,6 +63607,15 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63689
63607
  to.filterList = [...oldFilters, ...this.filters];
63690
63608
  }
63691
63609
  }
63610
+ };
63611
+ exports.QuerySegmentBuilder = QuerySegmentBuilder;
63612
+ var ReduceBuilder = class extends QuerySegmentBuilder {
63613
+ constructor(baseFS, refineThis, isNestIn, astEl) {
63614
+ super();
63615
+ this.type = "grouping";
63616
+ this.resultFS = new query_spaces_1.ReduceFieldSpace(baseFS, refineThis, isNestIn, astEl);
63617
+ this.inputFS = this.resultFS.inputSpace();
63618
+ }
63692
63619
  finalize(fromSeg) {
63693
63620
  let from;
63694
63621
  if (fromSeg) {
@@ -63719,13 +63646,12 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63719
63646
  var group_by_1 = require_group_by();
63720
63647
  var project_field_space_1 = require_project_field_space();
63721
63648
  var reduce_builder_1 = require_reduce_builder();
63722
- var ProjectBuilder = class extends reduce_builder_1.ReduceBuilder {
63723
- constructor() {
63724
- super(...arguments);
63649
+ var ProjectBuilder = class extends reduce_builder_1.QuerySegmentBuilder {
63650
+ constructor(baseFS, refineThis, isNestIn, astEl) {
63651
+ super();
63725
63652
  this.type = "project";
63726
- }
63727
- getResultSpace(fs, refineThis, isNestIn, astEl) {
63728
- return new project_field_space_1.ProjectFieldSpace(fs, refineThis, isNestIn, astEl);
63653
+ this.resultFS = new project_field_space_1.ProjectFieldSpace(baseFS, refineThis, isNestIn, astEl);
63654
+ this.inputFS = this.resultFS.inputSpace();
63729
63655
  }
63730
63656
  execute(qp) {
63731
63657
  if (qp.elementType === "having" || qp instanceof group_by_1.GroupBy) {
@@ -63759,11 +63685,15 @@ QUERY: ${(0, util_1.inspect)(opDesc, { breakLength: 72, depth: Infinity })}`);
63759
63685
  "use strict";
63760
63686
  Object.defineProperty(exports, "__esModule", { value: true });
63761
63687
  exports.PartialBuilder = void 0;
63688
+ var model_1 = require_model();
63762
63689
  var reduce_builder_1 = require_reduce_builder();
63763
63690
  var PartialBuilder = class extends reduce_builder_1.ReduceBuilder {
63764
63691
  finalize(fromSeg) {
63765
63692
  const seg = super.finalize(fromSeg);
63766
- return { ...seg, type: "partial" };
63693
+ if ((0, model_1.isQuerySegment)(seg)) {
63694
+ return { ...seg, type: "partial" };
63695
+ }
63696
+ throw new Error(`Partial Builder cannot finalize from ${seg.type}`);
63767
63697
  }
63768
63698
  };
63769
63699
  exports.PartialBuilder = PartialBuilder;
@@ -68926,7 +68856,7 @@ ${errText}`, errors);
68926
68856
  pipeline: [
68927
68857
  {
68928
68858
  type: "reduce",
68929
- fields: [name]
68859
+ queryFields: [{ type: "fieldref", path: [name] }]
68930
68860
  }
68931
68861
  ]
68932
68862
  };
@@ -70774,8 +70704,7 @@ ${errText}`, errors);
70774
70704
  "../malloy/dist/index.js"(exports) {
70775
70705
  "use strict";
70776
70706
  Object.defineProperty(exports, "__esModule", { value: true });
70777
- exports.toAsyncGenerator = exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.MalloyTranslator = exports.HighlightType = exports.indent = exports.expressionIsCalculation = exports.mkExpr = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.isFilteredAliasedName = exports.Segment = exports.FUNCTIONS = exports.Dialect = exports.spread = exports.param = exports.sqlFragment = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
70778
- exports.Tag = void 0;
70707
+ exports.Tag = exports.toAsyncGenerator = exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.MalloyTranslator = exports.HighlightType = exports.indent = exports.expressionIsCalculation = exports.mkExpr = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.Segment = exports.FUNCTIONS = exports.Dialect = exports.spread = exports.param = exports.sqlFragment = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
70779
70708
  var dialect_1 = require_dialect2();
70780
70709
  Object.defineProperty(exports, "DuckDBDialect", { enumerable: true, get: function() {
70781
70710
  return dialect_1.DuckDBDialect;
@@ -70835,9 +70764,6 @@ ${errText}`, errors);
70835
70764
  Object.defineProperty(exports, "Segment", { enumerable: true, get: function() {
70836
70765
  return model_1.Segment;
70837
70766
  } });
70838
- Object.defineProperty(exports, "isFilteredAliasedName", { enumerable: true, get: function() {
70839
- return model_1.isFilteredAliasedName;
70840
- } });
70841
70767
  Object.defineProperty(exports, "isSamplingEnable", { enumerable: true, get: function() {
70842
70768
  return model_1.isSamplingEnable;
70843
70769
  } });