@malloydata/malloy 0.0.403 → 0.0.404

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/api/foundation/core.js +6 -6
  2. package/dist/api/foundation/result.js +1 -2
  3. package/dist/dialect/dialect.d.ts +9 -4
  4. package/dist/dialect/dialect.js +35 -11
  5. package/dist/dialect/pg_impl.d.ts +1 -0
  6. package/dist/dialect/pg_impl.js +8 -0
  7. package/dist/dialect/snowflake/snowflake.js +4 -5
  8. package/dist/dialect/trino/trino.d.ts +1 -0
  9. package/dist/dialect/trino/trino.js +27 -6
  10. package/dist/lang/ast/expressions/expr-func.js +4 -4
  11. package/dist/lang/ast/field-space/dynamic-space.js +2 -2
  12. package/dist/lang/ast/field-space/include-utils.js +4 -4
  13. package/dist/lang/ast/field-space/query-spaces.js +6 -8
  14. package/dist/lang/ast/field-space/refined-space.js +6 -6
  15. package/dist/lang/ast/field-space/rename-space-field.js +6 -9
  16. package/dist/lang/ast/field-space/static-space.js +1 -1
  17. package/dist/lang/ast/field-space/struct-space-field-base.js +1 -1
  18. package/dist/lang/ast/query-properties/drill.js +3 -6
  19. package/dist/lang/ast/source-elements/composite-source.js +1 -2
  20. package/dist/lang/ast/source-elements/sql-source.js +6 -8
  21. package/dist/lang/ast/source-elements/table-source.js +8 -10
  22. package/dist/lang/ast/source-elements/typed-source.js +2 -3
  23. package/dist/lang/ast/source-properties/join.js +3 -5
  24. package/dist/lang/ast/source-query-elements/sq-reference.js +1 -1
  25. package/dist/lang/ast/statements/define-source.js +1 -1
  26. package/dist/lang/ast/types/malloy-element.d.ts +1 -0
  27. package/dist/lang/ast/types/malloy-element.js +9 -7
  28. package/dist/lang/composite-source-utils.js +13 -17
  29. package/dist/lang/field-utils.js +2 -2
  30. package/dist/lang/test/test-translator.js +2 -4
  31. package/dist/model/field_instance.js +1 -1
  32. package/dist/model/malloy_types.d.ts +7 -2
  33. package/dist/model/malloy_types.js +11 -8
  34. package/dist/model/query_model_impl.d.ts +0 -1
  35. package/dist/model/query_model_impl.js +9 -28
  36. package/dist/model/query_node.d.ts +1 -10
  37. package/dist/model/query_node.js +8 -42
  38. package/dist/model/query_query.js +2 -2
  39. package/dist/model/utils.js +3 -3
  40. package/dist/to_stable.js +7 -9
  41. package/dist/version.d.ts +1 -1
  42. package/dist/version.js +1 -1
  43. package/package.json +4 -4
@@ -131,7 +131,7 @@ var JoinRelationship;
131
131
  // =============================================================================
132
132
  class Explore extends Entity {
133
133
  constructor(structDef, parentExplore, source) {
134
- super(structDef.as || structDef.name, parentExplore, source);
134
+ super((0, model_1.activeName)(structDef), parentExplore, source);
135
135
  this._structDef = structDef;
136
136
  this._parentExplore = parentExplore;
137
137
  this.sourceExplore = source;
@@ -167,14 +167,14 @@ class Explore extends Entity {
167
167
  * @return The name of the entity.
168
168
  */
169
169
  get name() {
170
- return this.structDef.as || this.structDef.name;
170
+ return (0, model_1.activeName)(this.structDef);
171
171
  }
172
172
  getQueryByName(name) {
173
173
  const structRef = this.sourceStructDef;
174
174
  if (!structRef) {
175
175
  throw new Error(`Cannot get query by name from a struct of type ${this.structDef.type}`);
176
176
  }
177
- const view = structRef.fields.find(f => { var _a; return ((_a = f.as) !== null && _a !== void 0 ? _a : f.name) === name; });
177
+ const view = structRef.fields.find(f => (0, model_1.activeName)(f) === name);
178
178
  if (view === undefined) {
179
179
  throw new Error(`No such view named \`${name}\``);
180
180
  }
@@ -204,7 +204,7 @@ class Explore extends Entity {
204
204
  if (this._fieldMap === undefined) {
205
205
  const sourceFields = ((_a = this.source) === null || _a === void 0 ? void 0 : _a.fieldMap) || new Map();
206
206
  this._fieldMap = new Map(this.structDef.fields.map(fieldDef => {
207
- const name = fieldDef.as || fieldDef.name;
207
+ const name = (0, model_1.activeName)(fieldDef);
208
208
  const sourceField = sourceFields.get(fieldDef.name);
209
209
  if ((0, model_1.isJoined)(fieldDef)) {
210
210
  return [name, new ExploreField(fieldDef, this, sourceField)];
@@ -431,7 +431,7 @@ exports.Explore = Explore;
431
431
  // =============================================================================
432
432
  class AtomicField extends Entity {
433
433
  constructor(fieldTypeDef, parent, source) {
434
- super(fieldTypeDef.as || fieldTypeDef.name, parent, source);
434
+ super((0, model_1.activeName)(fieldTypeDef), parent, source);
435
435
  this.fieldTypeDef = fieldTypeDef;
436
436
  this.parent = parent;
437
437
  }
@@ -651,7 +651,7 @@ exports.StringField = StringField;
651
651
  // =============================================================================
652
652
  class Query extends Entity {
653
653
  constructor(turtleDef, parent, source) {
654
- super(turtleDef.as || turtleDef.name, parent, source);
654
+ super((0, model_1.activeName)(turtleDef), parent, source);
655
655
  this.turtleDef = turtleDef;
656
656
  }
657
657
  get source() {
@@ -412,10 +412,9 @@ function walkQueryData(data, structDef, normalizers) {
412
412
  * Walk a QueryRecord and normalize values according to the given normalizers.
413
413
  */
414
414
  function walkQueryRecord(row, structDef, normalizers) {
415
- var _a;
416
415
  const result = {};
417
416
  for (const fieldDef of structDef.fields) {
418
- const fieldName = (_a = fieldDef.as) !== null && _a !== void 0 ? _a : fieldDef.name;
417
+ const fieldName = (0, model_1.activeName)(fieldDef);
419
418
  const value = row[fieldName];
420
419
  result[fieldName] = walkValue(value, fieldDef, normalizers);
421
420
  }
@@ -230,6 +230,13 @@ export declare abstract class Dialect {
230
230
  * - `EscapeStyle.Unset` (default): base method throws if reached.
231
231
  */
232
232
  identifierEscapeStyle: EscapeStyleValue;
233
+ /**
234
+ * Escape the body of a backslash-style quoted token (`EscapeStyle.Backslash`
235
+ * dialects) — the backslash, the closing `delim`, and newline/CR/tab. The
236
+ * newline matters: BigQuery rejects a raw one ("Unclosed string literal").
237
+ * `\0` / U+2028 / U+2029 are passed through (no evidence they break a lexer).
238
+ */
239
+ protected escapeBackslashStyle(body: string, delim: string): string;
233
240
  /**
234
241
  * Wrap an identifier in the dialect's quote character, escaping any
235
242
  * embedded quote characters per the dialect's `identifierEscapeStyle`.
@@ -446,10 +453,8 @@ export declare abstract class Dialect {
446
453
  */
447
454
  sqlLiteralString(literal: string): string;
448
455
  /**
449
- * Render a Malloy regex literal as a SQL string literal. Defaults to
450
- * `sqlLiteralString` the regex engine receives whatever bytes the
451
- * SQL parser decodes, and `sqlLiteralString` already produces a
452
- * correctly decoding literal for both escape styles.
456
+ * Render a Malloy regex literal. Defaults to `sqlLiteralString`: the regex
457
+ * engine receives whatever the SQL parser decodes, which is already correct.
453
458
  */
454
459
  sqlLiteralRegexp(literal: string): string;
455
460
  abstract sqlLiteralArray(lit: ArrayLiteralNode): string;
@@ -323,6 +323,37 @@ class Dialect {
323
323
  sqlDateToString(sqlDateExp) {
324
324
  return this.castToString(`DATE(${sqlDateExp})`);
325
325
  }
326
+ /**
327
+ * Escape the body of a backslash-style quoted token (`EscapeStyle.Backslash`
328
+ * dialects) — the backslash, the closing `delim`, and newline/CR/tab. The
329
+ * newline matters: BigQuery rejects a raw one ("Unclosed string literal").
330
+ * `\0` / U+2028 / U+2029 are passed through (no evidence they break a lexer).
331
+ */
332
+ escapeBackslashStyle(body, delim) {
333
+ let out = '';
334
+ for (const ch of body) {
335
+ switch (ch) {
336
+ case '\\':
337
+ out += '\\\\';
338
+ break;
339
+ case '\n':
340
+ out += '\\n';
341
+ break;
342
+ case '\r':
343
+ out += '\\r';
344
+ break;
345
+ case '\t':
346
+ out += '\\t';
347
+ break;
348
+ case delim:
349
+ out += '\\' + delim;
350
+ break;
351
+ default:
352
+ out += ch;
353
+ }
354
+ }
355
+ return out;
356
+ }
326
357
  /**
327
358
  * Wrap an identifier in the dialect's quote character, escaping any
328
359
  * embedded quote characters per the dialect's `identifierEscapeStyle`.
@@ -340,11 +371,7 @@ class Dialect {
340
371
  return q + identifier.split(q).join(q + q) + q;
341
372
  }
342
373
  if (this.identifierEscapeStyle === exports.EscapeStyle.Backslash) {
343
- const escaped = identifier
344
- .replace(/\\/g, '\\\\')
345
- .split(q)
346
- .join('\\' + q);
347
- return q + escaped + q;
374
+ return q + this.escapeBackslashStyle(identifier, q) + q;
348
375
  }
349
376
  throw new Error(`${this.name}: identifierEscapeStyle is not set. ` +
350
377
  'Set it to EscapeStyle.Doubled or EscapeStyle.Backslash on the dialect, ' +
@@ -458,18 +485,15 @@ class Dialect {
458
485
  return "'" + literal.split("'").join("''") + "'";
459
486
  }
460
487
  if (this.stringLiteralStyle === 'backslash') {
461
- const escaped = literal.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
462
- return "'" + escaped + "'";
488
+ return "'" + this.escapeBackslashStyle(literal, "'") + "'";
463
489
  }
464
490
  throw new Error(`${this.name}: stringLiteralStyle is not set. ` +
465
491
  'Set it to EscapeStyle.Doubled or EscapeStyle.Backslash on the dialect, ' +
466
492
  'or override sqlLiteralString.');
467
493
  }
468
494
  /**
469
- * Render a Malloy regex literal as a SQL string literal. Defaults to
470
- * `sqlLiteralString` the regex engine receives whatever bytes the
471
- * SQL parser decodes, and `sqlLiteralString` already produces a
472
- * correctly decoding literal for both escape styles.
495
+ * Render a Malloy regex literal. Defaults to `sqlLiteralString`: the regex
496
+ * engine receives whatever the SQL parser decodes, which is already correct.
473
497
  */
474
498
  sqlLiteralRegexp(literal) {
475
499
  return this.sqlLiteralString(literal);
@@ -12,6 +12,7 @@ export declare abstract class PostgresBase extends Dialect {
12
12
  stringLiteralStyle: "doubled";
13
13
  identifierEscapeStyle: "doubled";
14
14
  identifierQuoteChar: string;
15
+ sqlLiteralString(literal: string): string;
15
16
  sqlNowExpr(): string;
16
17
  sqlTimeExtractExpr(qi: QueryInfo, from: TimeExtractExpr): string;
17
18
  sqlCast(qi: QueryInfo, cast: TypecastExpr): string;
@@ -30,6 +30,14 @@ class PostgresBase extends dialect_1.Dialect {
30
30
  this.identifierEscapeStyle = dialect_1.EscapeStyle.Doubled;
31
31
  this.identifierQuoteChar = '"';
32
32
  }
33
+ // A raw newline in a doubled literal is valid SQL but gets mangled by the
34
+ // SQL indenter (indent() in model/utils.ts), so encode it. Postgres/DuckDB
35
+ // use an E'...' escape string; Trino/Presto override with U&'...'.
36
+ sqlLiteralString(literal) {
37
+ return literal.includes('\n')
38
+ ? `E'${this.escapeBackslashStyle(literal, "'")}'`
39
+ : super.sqlLiteralString(literal);
40
+ }
33
41
  sqlNowExpr() {
34
42
  return 'LOCALTIMESTAMP';
35
43
  }
@@ -439,9 +439,8 @@ ${(0, utils_1.indent)(sql)}
439
439
  }
440
440
  else if (malloyType.type === 'record' || (0, malloy_types_1.isRepeatedRecord)(malloyType)) {
441
441
  const sqlFields = malloyType.fields.reduce((ret, f) => {
442
- var _a;
443
442
  if ((0, malloy_types_1.isAtomic)(f)) {
444
- const name = (_a = f.as) !== null && _a !== void 0 ? _a : f.name;
443
+ const name = (0, malloy_types_1.activeName)(f);
445
444
  const oneSchema = `${this.sqlQuoteIdentifier(name)} ${this.malloyTypeToSQLType(f)}`;
446
445
  ret.push(oneSchema);
447
446
  }
@@ -502,11 +501,11 @@ ${(0, utils_1.indent)(sql)}
502
501
  return sqlType.match(/^[A-Za-z\s(),[\]0-9]*$/) !== null;
503
502
  }
504
503
  sqlLiteralRecord(lit) {
505
- var _a, _b, _c;
504
+ var _a, _b;
506
505
  const rowVals = [];
507
506
  for (const f of lit.typeDef.fields) {
508
- const name = (_a = f.as) !== null && _a !== void 0 ? _a : f.name;
509
- const propVal = (_c = (_b = (0, malloy_types_1.safeRecordGet)(lit.kids, name)) === null || _b === void 0 ? void 0 : _b.sql) !== null && _c !== void 0 ? _c : 'internal-error-record-literal';
507
+ const name = (0, malloy_types_1.activeName)(f);
508
+ const propVal = (_b = (_a = (0, malloy_types_1.safeRecordGet)(lit.kids, name)) === null || _a === void 0 ? void 0 : _a.sql) !== null && _b !== void 0 ? _b : 'internal-error-record-literal';
510
509
  rowVals.push(`${this.sqlLiteralString(name)},${propVal}`);
511
510
  }
512
511
  return `OBJECT_CONSTRUCT_KEEP_NULL(${rowVals.join(',')})`;
@@ -30,6 +30,7 @@ export declare class TrinoDialect extends PostgresBase {
30
30
  supportsTempTables: boolean;
31
31
  supportsCountApprox: boolean;
32
32
  supportsHyperLogLog: boolean;
33
+ sqlLiteralString(literal: string): string;
33
34
  sqlGroupSetTable(groupSetCount: number): string;
34
35
  exprToSQL(qi: QueryInfo, df: Expr): string | undefined;
35
36
  sqlAnyValue(groupSet: number, fieldName: string): string;
@@ -200,9 +200,30 @@ class TrinoDialect extends pg_impl_1.PostgresBase {
200
200
  WITH
201
201
  WITHIN`.split(/\s/);
202
202
  }
203
- // Trino bare identifier is strict ANSI (`[A-Za-z_][A-Za-z0-9_]*`),
204
- // which matches the Dialect default no override needed. Verified
205
- // against the live engine.
203
+ // Trino bare identifier is strict ANSI, matching the Dialect default.
204
+ // Trino/Presto have no E'...' escape string, so encode a newline-bearing
205
+ // literal as U&'...'. Inside it the quote doubles (''), the backslash
206
+ // escape-introducer doubles (\\), and a control char becomes \XXXX.
207
+ sqlLiteralString(literal) {
208
+ if (!literal.includes('\n')) {
209
+ return super.sqlLiteralString(literal);
210
+ }
211
+ const body = literal.replace(/['\\\n\r\t]/g, ch => {
212
+ switch (ch) {
213
+ case "'":
214
+ return "''";
215
+ case '\\':
216
+ return '\\\\';
217
+ case '\n':
218
+ return '\\000A';
219
+ case '\r':
220
+ return '\\000D';
221
+ default: // '\t'
222
+ return '\\0009';
223
+ }
224
+ });
225
+ return `U&'${body}'`;
226
+ }
206
227
  sqlGroupSetTable(groupSetCount) {
207
228
  return `CROSS JOIN (SELECT row_number() OVER() -1 group_set FROM UNNEST(SEQUENCE(0,${groupSetCount})))`;
208
229
  }
@@ -582,13 +603,13 @@ ${(0, utils_1.indent)(sql)}
582
603
  return from.units === 'day_of_week' ? `mod(${extracted}+1,7)` : extracted;
583
604
  }
584
605
  sqlLiteralRecord(lit) {
585
- var _a, _b, _c;
606
+ var _a, _b;
586
607
  const rowVals = [];
587
608
  const rowTypes = [];
588
609
  for (const f of lit.typeDef.fields) {
589
610
  if ((0, malloy_types_1.isAtomic)(f)) {
590
- const name = (_a = f.as) !== null && _a !== void 0 ? _a : f.name;
591
- rowVals.push((_c = (_b = (0, malloy_types_1.safeRecordGet)(lit.kids, name)) === null || _b === void 0 ? void 0 : _b.sql) !== null && _c !== void 0 ? _c : 'internal-error-record-literal');
611
+ const name = (0, malloy_types_1.activeName)(f);
612
+ rowVals.push((_b = (_a = (0, malloy_types_1.safeRecordGet)(lit.kids, name)) === null || _a === void 0 ? void 0 : _a.sql) !== null && _b !== void 0 ? _b : 'internal-error-record-literal');
592
613
  const elType = this.malloyTypeToSQLType(f);
593
614
  rowTypes.push(`${this.sqlQuoteIdentifier(name)} ${elType}`);
594
615
  }
@@ -532,7 +532,7 @@ function parseSQLInterpolation(template) {
532
532
  return parts;
533
533
  }
534
534
  function isDataTypeMatch(genericsAlreadySelected, genericTypes, arg, paramT) {
535
- var _a, _b, _c, _d;
535
+ var _a, _b;
536
536
  if (malloy_types_1.TD.eq(paramT, arg) ||
537
537
  paramT.type === 'any' ||
538
538
  // TODO We should consider whether `nulls` should always be allowed. It probably
@@ -569,10 +569,10 @@ function isDataTypeMatch(genericsAlreadySelected, genericTypes, arg, paramT) {
569
569
  const genericsSet = [];
570
570
  const paramFieldsByName = new Map();
571
571
  for (const field of paramT.fields) {
572
- paramFieldsByName.set((_a = field.as) !== null && _a !== void 0 ? _a : field.name, field);
572
+ paramFieldsByName.set((0, malloy_types_1.activeName)(field), field);
573
573
  }
574
574
  for (const field of arg.fields) {
575
- const match = paramFieldsByName.get((_b = field.as) !== null && _b !== void 0 ? _b : field.name);
575
+ const match = paramFieldsByName.get((0, malloy_types_1.activeName)(field));
576
576
  if (match === undefined) {
577
577
  return { dataTypeMatch: false, genericsSet: [] };
578
578
  }
@@ -591,7 +591,7 @@ function isDataTypeMatch(genericsAlreadySelected, genericTypes, arg, paramT) {
591
591
  alreadySelected.type !== 'error') {
592
592
  return isDataTypeMatch(genericsAlreadySelected, genericTypes, arg, alreadySelected);
593
593
  }
594
- const allowedTypes = (_d = (_c = genericTypes.find(t => t.name === paramT.generic)) === null || _c === void 0 ? void 0 : _c.acceptibleTypes) !== null && _d !== void 0 ? _d : [];
594
+ const allowedTypes = (_b = (_a = genericTypes.find(t => t.name === paramT.generic)) === null || _a === void 0 ? void 0 : _a.acceptibleTypes) !== null && _b !== void 0 ? _b : [];
595
595
  for (const type of allowedTypes) {
596
596
  const result = isDataTypeMatch(genericsAlreadySelected, genericTypes, arg, type);
597
597
  if (result.dataTypeMatch) {
@@ -167,7 +167,7 @@ class DynamicSpace extends static_space_1.StaticSpace {
167
167
  }
168
168
  // Add access modifiers at the end so views don't obey them
169
169
  for (const [name, access] of this.newAccessModifiers) {
170
- const index = this.sourceDef.fields.findIndex(f => { var _a; return ((_a = f.as) !== null && _a !== void 0 ? _a : f.name) === name; });
170
+ const index = this.sourceDef.fields.findIndex(f => (0, malloy_types_1.activeName)(f) === name);
171
171
  if (index === -1) {
172
172
  throw new Error(`Can't find field '${name}' to set access modifier`);
173
173
  }
@@ -184,7 +184,7 @@ class DynamicSpace extends static_space_1.StaticSpace {
184
184
  // TODO does this need to be done when the space is instantiated?
185
185
  // e.g. if a field had a compiler flag on it...
186
186
  for (const [name, note] of this.newNotes) {
187
- const index = this.sourceDef.fields.findIndex(f => { var _a; return (_a = f.as) !== null && _a !== void 0 ? _a : f.name === name; });
187
+ const index = this.sourceDef.fields.findIndex(f => (0, malloy_types_1.activeName)(f) === name);
188
188
  if (index === -1) {
189
189
  throw new Error(`Can't find field '${name}' to set access modifier`);
190
190
  }
@@ -15,7 +15,7 @@ const composite_source_utils_1 = require("../../composite-source-utils");
15
15
  function getJoinFields(from, joinPath, logTo) {
16
16
  let fields = from.fields;
17
17
  for (const joinName of joinPath) {
18
- const join = fields.find(f => { var _a; return ((_a = f.as) !== null && _a !== void 0 ? _a : f.name) === joinName; });
18
+ const join = fields.find(f => (0, malloy_types_1.activeName)(f) === joinName);
19
19
  if (join === undefined) {
20
20
  logTo.logError('field-not-found', `\`${joinName}\` not found`);
21
21
  return [];
@@ -52,11 +52,11 @@ function getOrCreateIncludeStateForJoin(joinPath, state, from, logTo) {
52
52
  return joinState.state;
53
53
  else {
54
54
  const fromFields = getJoinFields(from, joinPath, logTo);
55
- const allFields = new Set(fromFields.map(f => { var _a; return (_a = f.as) !== null && _a !== void 0 ? _a : f.name; }));
56
- const joinNames = new Set(fromFields.filter(f => (0, malloy_types_1.isJoined)(f)).map(f => { var _a; return (_a = f.as) !== null && _a !== void 0 ? _a : f.name; }));
55
+ const allFields = new Set(fromFields.map(f => (0, malloy_types_1.activeName)(f)));
56
+ const joinNames = new Set(fromFields.filter(f => (0, malloy_types_1.isJoined)(f)).map(f => (0, malloy_types_1.activeName)(f)));
57
57
  const alreadyPrivateFields = new Set(fromFields
58
58
  .filter(f => f.accessModifier === 'private')
59
- .map(f => { var _a; return (_a = f.as) !== null && _a !== void 0 ? _a : f.name; }));
59
+ .map(f => (0, malloy_types_1.activeName)(f)));
60
60
  const joinState = {
61
61
  star: undefined,
62
62
  joinNames,
@@ -212,7 +212,6 @@ exports.QueryOperationSpace = QueryOperationSpace;
212
212
  // Project and Reduce or "QuerySegments" are built from a QuerySpace
213
213
  class QuerySpace extends QueryOperationSpace {
214
214
  addRefineFromFields(refineThis) {
215
- var _a;
216
215
  if (!model.isQuerySegment(refineThis)) {
217
216
  // TODO mtoy raw,partial,index
218
217
  return;
@@ -233,7 +232,7 @@ class QuerySpace extends QueryOperationSpace {
233
232
  }
234
233
  else {
235
234
  const entry = new refine_from_space_field_1.RefineFromSpaceField(field);
236
- const name = (_a = field.as) !== null && _a !== void 0 ? _a : field.name;
235
+ const name = model.activeName(field);
237
236
  this.setEntry(name, entry);
238
237
  this.addValidatedCompositeFieldUserFromEntry(name, entry);
239
238
  }
@@ -257,7 +256,7 @@ class QuerySpace extends QueryOperationSpace {
257
256
  return fields.map(f => f.queryFieldDef);
258
257
  }
259
258
  getOutputFieldDef(queryFieldDef, typeDesc) {
260
- var _a, _b, _c;
259
+ var _a, _b;
261
260
  let location = undefined;
262
261
  let name;
263
262
  if (queryFieldDef.type === 'fieldref') {
@@ -265,14 +264,14 @@ class QuerySpace extends QueryOperationSpace {
265
264
  location = queryFieldDef.at;
266
265
  }
267
266
  else {
268
- name = (_a = queryFieldDef.as) !== null && _a !== void 0 ? _a : queryFieldDef.name;
267
+ name = model.activeName(queryFieldDef);
269
268
  location = queryFieldDef.location;
270
269
  }
271
270
  let ret;
272
271
  if (typeDesc.type === 'turtle') {
273
272
  const pipeline = typeDesc.pipeline;
274
273
  const lastSegment = pipeline[pipeline.length - 1];
275
- const outputStruct = (_b = lastSegment === null || lastSegment === void 0 ? void 0 : lastSegment.outputStruct) !== null && _b !== void 0 ? _b : this.exprSpace.emptyStructDef();
274
+ const outputStruct = (_a = lastSegment === null || lastSegment === void 0 ? void 0 : lastSegment.outputStruct) !== null && _a !== void 0 ? _a : this.exprSpace.emptyStructDef();
276
275
  const isRepeated = lastSegment
277
276
  ? model.isQuerySegment(lastSegment)
278
277
  ? lastSegment.isRepeated
@@ -308,7 +307,7 @@ class QuerySpace extends QueryOperationSpace {
308
307
  else {
309
308
  throw new Error('Invalid type for fieldref');
310
309
  }
311
- ret.location = (_c = ret.location) !== null && _c !== void 0 ? _c : this.astEl.location;
310
+ ret.location = (_b = ret.location) !== null && _b !== void 0 ? _b : this.astEl.location;
312
311
  if (queryFieldDef.annotations) {
313
312
  ret.annotations = queryFieldDef.annotations;
314
313
  }
@@ -317,7 +316,6 @@ class QuerySpace extends QueryOperationSpace {
317
316
  // Gets the primary key field for the output struct of this query;
318
317
  // If there is exactly one scalar field, that is the primary key
319
318
  getPrimaryKey(fields) {
320
- var _a;
321
319
  const dimensions = fields.filter(f => model.TD.isAtomic(f.typeDesc) &&
322
320
  model.expressionIsScalar(f.typeDesc.expressionType));
323
321
  if (dimensions.length !== 1)
@@ -327,7 +325,7 @@ class QuerySpace extends QueryOperationSpace {
327
325
  return primaryKeyField.path[primaryKeyField.path.length - 1];
328
326
  }
329
327
  else {
330
- return (_a = primaryKeyField.as) !== null && _a !== void 0 ? _a : primaryKeyField.name;
328
+ return model.activeName(primaryKeyField);
331
329
  }
332
330
  }
333
331
  // This returns the OUTPUT struct of this query space
@@ -132,13 +132,13 @@ class RefinedSpace extends dynamic_space_1.DynamicSpace {
132
132
  }
133
133
  exports.RefinedSpace = RefinedSpace;
134
134
  function editJoinsFromIncludeState(path, from, includeState) {
135
- var _a, _b, _c;
135
+ var _a;
136
136
  let fields;
137
137
  const joinedState = (0, include_utils_1.getIncludeStateForJoin)(path, includeState);
138
138
  const isJoin = path.length > 0;
139
139
  if (isJoin) {
140
140
  if (joinedState.fieldsToInclude) {
141
- fields = from.fields.filter(f => { var _a, _b; return (_a = joinedState.fieldsToInclude) === null || _a === void 0 ? void 0 : _a.has((_b = f.as) !== null && _b !== void 0 ? _b : f.name); });
141
+ fields = from.fields.filter(f => { var _a; return (_a = joinedState.fieldsToInclude) === null || _a === void 0 ? void 0 : _a.has((0, malloy_types_1.activeName)(f)); });
142
142
  }
143
143
  else {
144
144
  fields = from.fields;
@@ -150,11 +150,11 @@ function editJoinsFromIncludeState(path, from, includeState) {
150
150
  // const fields = from.fields;
151
151
  const updatedFields = [];
152
152
  for (const field of fields) {
153
- const name = (_a = field.as) !== null && _a !== void 0 ? _a : field.name;
153
+ const name = (0, malloy_types_1.activeName)(field);
154
154
  // TODO ensure you can't make it more permissive here...
155
- const accessModifier = (_b = joinedState.modifiers.get(name)) !== null && _b !== void 0 ? _b : field.accessModifier;
155
+ const accessModifier = (_a = joinedState.modifiers.get(name)) !== null && _a !== void 0 ? _a : field.accessModifier;
156
156
  const notes = joinedState.notes.get(name);
157
- const rename = joinedState.renames.find(r => { var _a; return r.name.nameString === ((_a = field.as) !== null && _a !== void 0 ? _a : field.name); });
157
+ const rename = joinedState.renames.find(r => r.name.nameString === (0, malloy_types_1.activeName)(field));
158
158
  const editedField = isJoin
159
159
  ? {
160
160
  ...field,
@@ -172,7 +172,7 @@ function editJoinsFromIncludeState(path, from, includeState) {
172
172
  if ((0, malloy_types_1.isJoined)(editedField)) {
173
173
  updatedFields.push({
174
174
  ...editedField,
175
- fields: editJoinsFromIncludeState([...path, (_c = field.as) !== null && _c !== void 0 ? _c : field.name], editedField, includeState),
175
+ fields: editJoinsFromIncludeState([...path, (0, malloy_types_1.activeName)(field)], editedField, includeState),
176
176
  });
177
177
  }
178
178
  else {
@@ -53,15 +53,12 @@ class RenameSpaceField extends space_field_1.SpaceField {
53
53
  ...returnFieldDef,
54
54
  as: this.newName,
55
55
  location: this.location,
56
- refSummary: (0, malloy_types_1.mapFieldUsage)(returnFieldDef.refSummary, u => {
57
- var _a;
58
- return ({
59
- ...u,
60
- path: u.path[0] === ((_a = returnFieldDef.as) !== null && _a !== void 0 ? _a : returnFieldDef.name)
61
- ? [this.newName, ...u.path.slice(1)]
62
- : u.path,
63
- });
64
- }),
56
+ refSummary: (0, malloy_types_1.mapFieldUsage)(returnFieldDef.refSummary, u => ({
57
+ ...u,
58
+ path: u.path[0] === (0, malloy_types_1.activeName)(returnFieldDef)
59
+ ? [this.newName, ...u.path.slice(1)]
60
+ : u.path,
61
+ })),
65
62
  };
66
63
  }
67
64
  typeDesc() {
@@ -64,7 +64,7 @@ class StaticSpace {
64
64
  if (this.memoMap === undefined) {
65
65
  this.memoMap = new Map();
66
66
  for (const f of this.fromStruct.fields) {
67
- const name = f.as || f.name;
67
+ const name = (0, malloy_types_1.activeName)(f);
68
68
  this.memoMap.set(name, this.defToSpaceField(f));
69
69
  }
70
70
  if ((0, malloy_types_1.isSourceDef)(this.fromStruct)) {
@@ -69,7 +69,7 @@ class StructSpaceFieldBase extends space_field_1.SpaceField {
69
69
  }
70
70
  get joinPathElement() {
71
71
  return {
72
- name: this.structDef.as || this.structDef.name,
72
+ name: (0, malloy_types_1.activeName)(this.structDef),
73
73
  joinType: this.structDef.join,
74
74
  joinElementType: this.structDef.type,
75
75
  };
@@ -114,7 +114,6 @@ class Drill extends filters_1.Filter {
114
114
  }
115
115
  const segmentDimensions = segment.queryFields
116
116
  .map(f => {
117
- var _a;
118
117
  let fieldDef = undefined;
119
118
  if (f.type === 'fieldref') {
120
119
  const morePathFieldNames = f.path.map(n => new field_space_1.FieldName(n));
@@ -131,7 +130,7 @@ class Drill extends filters_1.Filter {
131
130
  }
132
131
  else {
133
132
  if ((0, malloy_types_1.isAtomic)(f) && (0, malloy_types_1.expressionIsScalar)(f.expressionType))
134
- return (_a = f.as) !== null && _a !== void 0 ? _a : f.name;
133
+ return (0, malloy_types_1.activeName)(f);
135
134
  }
136
135
  })
137
136
  .filter(utils_1.isNotUndefined);
@@ -159,12 +158,11 @@ class Drill extends filters_1.Filter {
159
158
  }
160
159
  }
161
160
  const field = segment.queryFields.find(f => {
162
- var _a;
163
161
  if (f.type === 'fieldref') {
164
162
  return f.path[f.path.length - 1] === name.refString;
165
163
  }
166
164
  else {
167
- return (_a = f.as) !== null && _a !== void 0 ? _a : f.name === name.refString;
165
+ return (0, malloy_types_1.activeName)(f) === name.refString;
168
166
  }
169
167
  });
170
168
  if (field === undefined) {
@@ -360,11 +358,10 @@ function attachDrillPaths(pipeline, nestName) {
360
358
  filterView: nestName,
361
359
  })),
362
360
  queryFields: pipeline[0].queryFields.map(f => {
363
- var _a;
364
361
  if (f.type === 'turtle') {
365
362
  return updateNestedDrillPaths(f, nestName);
366
363
  }
367
- const fieldName = f.type === 'fieldref' ? f.path[f.path.length - 1] : ((_a = f.as) !== null && _a !== void 0 ? _a : f.name);
364
+ const fieldName = f.type === 'fieldref' ? f.path[f.path.length - 1] : (0, malloy_types_1.activeName)(f);
368
365
  return {
369
366
  ...f,
370
367
  drillExpression: {
@@ -40,7 +40,6 @@ function composeSources(sources, compositeCodeSource) {
40
40
  const fieldsByName = new Map();
41
41
  const joinsToCompose = new Map();
42
42
  sources.forEach(source => {
43
- var _a;
44
43
  const sourceDef = source.sourceDef;
45
44
  // Check that connections all match; don't bother checking dialect, since it will
46
45
  // match if the connection matches.
@@ -48,7 +47,7 @@ function composeSources(sources, compositeCodeSource) {
48
47
  source.logTo.logError('composite-source-connection-mismatch', `All sources in a composite source must share the same connection; connection \`${sourceDef.connection}\` differs from previous connection \`${connection}\``);
49
48
  }
50
49
  for (const field of sourceDef.fields) {
51
- const fieldName = (_a = field.as) !== null && _a !== void 0 ? _a : field.name;
50
+ const fieldName = (0, malloy_types_1.activeName)(field);
52
51
  if (field.accessModifier === 'private') {
53
52
  continue;
54
53
  }
@@ -23,6 +23,7 @@
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.SQLSource = void 0;
26
+ const malloy_types_1 = require("../../../model/malloy_types");
26
27
  const source_def_utils_1 = require("../../../model/source_def_utils");
27
28
  const sql_block_1 = require("../../../model/sql_block");
28
29
  const source_1 = require("./source");
@@ -132,14 +133,11 @@ class SQLSource extends source_1.Source {
132
133
  // Create a base struct with updated fields (adding location and refSummary)
133
134
  const baseStruct = {
134
135
  ...lookup.value,
135
- fields: lookup.value.fields.map(f => {
136
- var _a;
137
- return ({
138
- ...f,
139
- location,
140
- refSummary: { fieldUsage: [{ path: [(_a = f.as) !== null && _a !== void 0 ? _a : f.name], at: location }] },
141
- });
142
- }),
136
+ fields: lookup.value.fields.map(f => ({
137
+ ...f,
138
+ location,
139
+ refSummary: { fieldUsage: [{ path: [(0, malloy_types_1.activeName)(f)], at: location }] },
140
+ })),
143
141
  location: this.location,
144
142
  };
145
143
  // Use factory to create SQLSourceDef without propagating sourceID/extends
@@ -23,6 +23,7 @@
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.TableMethodSource = exports.TableSource = void 0;
26
+ const malloy_types_1 = require("../../../model/malloy_types");
26
27
  const find_external_references_1 = require("../../parse-tree-walkers/find-external-references");
27
28
  const source_1 = require("./source");
28
29
  const error_factory_1 = require("../error-factory");
@@ -60,16 +61,13 @@ class TableSource extends source_1.Source {
60
61
  });
61
62
  const ret = {
62
63
  ...tableDefEntry.value,
63
- fields: tableDefEntry.value.fields.map(field => {
64
- var _a;
65
- return ({
66
- ...field,
67
- location: this.location,
68
- refSummary: {
69
- fieldUsage: [{ path: [(_a = field.as) !== null && _a !== void 0 ? _a : field.name], at: this.location }],
70
- },
71
- });
72
- }),
64
+ fields: tableDefEntry.value.fields.map(field => ({
65
+ ...field,
66
+ location: this.location,
67
+ refSummary: {
68
+ fieldUsage: [{ path: [(0, malloy_types_1.activeName)(field)], at: this.location }],
69
+ },
70
+ })),
73
71
  location: this.location,
74
72
  };
75
73
  (_d = this.document()) === null || _d === void 0 ? void 0 : _d.rememberToAddModelAnnotations(ret);