@malloydata/malloy 0.0.391 → 0.0.392

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 (127) hide show
  1. package/dist/api/asynchronous.js +0 -3
  2. package/dist/api/foundation/compile.d.ts +1 -1
  3. package/dist/api/foundation/config.d.ts +80 -8
  4. package/dist/api/foundation/config.js +151 -69
  5. package/dist/api/foundation/config_compile.js +27 -35
  6. package/dist/api/foundation/config_discover.js +5 -9
  7. package/dist/api/foundation/config_overlays.d.ts +6 -0
  8. package/dist/api/foundation/config_overlays.js +12 -0
  9. package/dist/api/foundation/config_resolve.d.ts +4 -1
  10. package/dist/api/foundation/config_resolve.js +64 -4
  11. package/dist/api/foundation/core.d.ts +75 -2
  12. package/dist/api/foundation/core.js +104 -6
  13. package/dist/api/foundation/index.d.ts +2 -0
  14. package/dist/api/foundation/readers.js +1 -1
  15. package/dist/api/foundation/runtime.d.ts +68 -2
  16. package/dist/api/foundation/runtime.js +212 -10
  17. package/dist/api/foundation/types.d.ts +2 -1
  18. package/dist/index.d.ts +3 -1
  19. package/dist/lang/ast/ast-utils.js +0 -1
  20. package/dist/lang/ast/expressions/expr-aggregate-function.d.ts +1 -1
  21. package/dist/lang/ast/expressions/expr-aggregate-function.js +9 -8
  22. package/dist/lang/ast/expressions/expr-coalesce.d.ts +1 -1
  23. package/dist/lang/ast/expressions/expr-coalesce.js +2 -3
  24. package/dist/lang/ast/expressions/expr-count-distinct.js +1 -1
  25. package/dist/lang/ast/expressions/expr-count.js +6 -4
  26. package/dist/lang/ast/expressions/expr-filter-expr.js +0 -1
  27. package/dist/lang/ast/expressions/expr-func.js +9 -4
  28. package/dist/lang/ast/expressions/expr-given.d.ts +18 -0
  29. package/dist/lang/ast/expressions/expr-given.js +69 -0
  30. package/dist/lang/ast/expressions/expr-granular-time.d.ts +1 -1
  31. package/dist/lang/ast/expressions/expr-id-reference.js +3 -2
  32. package/dist/lang/ast/expressions/expr-now.js +0 -1
  33. package/dist/lang/ast/expressions/expr-props.d.ts +132 -132
  34. package/dist/lang/ast/expressions/expr-props.js +2 -2
  35. package/dist/lang/ast/expressions/expr-ungroup.d.ts +1 -1
  36. package/dist/lang/ast/expressions/expr-ungroup.js +4 -4
  37. package/dist/lang/ast/expressions/for-range.d.ts +1 -1
  38. package/dist/lang/ast/expressions/function-ordering.d.ts +1 -1
  39. package/dist/lang/ast/expressions/function-ordering.js +2 -2
  40. package/dist/lang/ast/expressions/time-literal.d.ts +3 -3
  41. package/dist/lang/ast/field-space/include-utils.js +2 -2
  42. package/dist/lang/ast/field-space/index-field-space.js +18 -23
  43. package/dist/lang/ast/field-space/passthrough-space.d.ts +1 -1
  44. package/dist/lang/ast/field-space/query-spaces.d.ts +6 -2
  45. package/dist/lang/ast/field-space/query-spaces.js +29 -19
  46. package/dist/lang/ast/field-space/reference-field.js +1 -1
  47. package/dist/lang/ast/field-space/rename-space-field.d.ts +1 -1
  48. package/dist/lang/ast/field-space/rename-space-field.js +2 -2
  49. package/dist/lang/ast/field-space/struct-space-field-base.js +2 -3
  50. package/dist/lang/ast/index.d.ts +2 -0
  51. package/dist/lang/ast/index.js +2 -0
  52. package/dist/lang/ast/query-builders/index-builder.d.ts +1 -1
  53. package/dist/lang/ast/query-builders/index-builder.js +4 -3
  54. package/dist/lang/ast/query-builders/reduce-builder.d.ts +2 -2
  55. package/dist/lang/ast/query-builders/reduce-builder.js +4 -5
  56. package/dist/lang/ast/query-elements/query-arrow.js +3 -2
  57. package/dist/lang/ast/query-elements/query-base.d.ts +1 -1
  58. package/dist/lang/ast/query-elements/query-base.js +1 -1
  59. package/dist/lang/ast/query-elements/query-refine.js +3 -1
  60. package/dist/lang/ast/query-items/field-declaration.js +2 -2
  61. package/dist/lang/ast/query-properties/drill.js +6 -6
  62. package/dist/lang/ast/query-properties/filters.js +2 -2
  63. package/dist/lang/ast/query-properties/nest.js +3 -3
  64. package/dist/lang/ast/source-elements/composite-source.js +5 -3
  65. package/dist/lang/ast/source-elements/named-source.js +4 -0
  66. package/dist/lang/ast/source-elements/sql-source.js +2 -2
  67. package/dist/lang/ast/source-elements/table-source.js +3 -1
  68. package/dist/lang/ast/source-properties/join.js +4 -4
  69. package/dist/lang/ast/source-query-elements/sq-reference.js +2 -1
  70. package/dist/lang/ast/statements/define-given.d.ts +29 -0
  71. package/dist/lang/ast/statements/define-given.js +163 -0
  72. package/dist/lang/ast/statements/import-statement.js +72 -9
  73. package/dist/lang/ast/typedesc-utils.d.ts +3 -1
  74. package/dist/lang/ast/typedesc-utils.js +4 -47
  75. package/dist/lang/ast/types/expr-value.js +2 -3
  76. package/dist/lang/ast/types/expression-def.d.ts +2 -2
  77. package/dist/lang/ast/types/expression-def.js +2 -2
  78. package/dist/lang/ast/types/malloy-element.d.ts +5 -15
  79. package/dist/lang/ast/types/malloy-element.js +113 -1
  80. package/dist/lang/ast/types/space-field.js +7 -9
  81. package/dist/lang/ast/view-elements/reference-view.js +6 -5
  82. package/dist/lang/ast/view-elements/refine-utils.js +1 -1
  83. package/dist/lang/composite-source-utils.d.ts +30 -15
  84. package/dist/lang/composite-source-utils.js +234 -64
  85. package/dist/lang/lib/Malloy/MalloyLexer.d.ts +171 -169
  86. package/dist/lang/lib/Malloy/MalloyLexer.js +1194 -1178
  87. package/dist/lang/lib/Malloy/MalloyParser.d.ts +408 -334
  88. package/dist/lang/lib/Malloy/MalloyParser.js +3062 -2561
  89. package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +68 -0
  90. package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +43 -0
  91. package/dist/lang/malloy-to-ast.d.ts +13 -1
  92. package/dist/lang/malloy-to-ast.js +90 -11
  93. package/dist/lang/parse-log.d.ts +8 -0
  94. package/dist/lang/prettify/filter-type.d.ts +3 -0
  95. package/dist/lang/prettify/filter-type.js +38 -0
  96. package/dist/lang/prettify/formatter.js +6 -0
  97. package/dist/lang/prettify/inline-renderer.js +20 -0
  98. package/dist/lang/prettify/rules.d.ts +1 -1
  99. package/dist/lang/prettify/rules.js +1 -0
  100. package/dist/lang/prettify/sections.js +2 -0
  101. package/dist/lang/prettify/tokens.js +2 -0
  102. package/dist/lang/test/expr-to-str.js +2 -0
  103. package/dist/lang/test/parse-expects.d.ts +1 -0
  104. package/dist/lang/test/parse-expects.js +27 -10
  105. package/dist/model/constant_expression_compiler.js +1 -0
  106. package/dist/model/expression_compiler.d.ts +2 -1
  107. package/dist/model/expression_compiler.js +41 -1
  108. package/dist/model/given_binding.d.ts +2 -0
  109. package/dist/model/given_binding.js +204 -0
  110. package/dist/model/index.d.ts +1 -1
  111. package/dist/model/index.js +2 -1
  112. package/dist/model/malloy_types.d.ts +163 -36
  113. package/dist/model/malloy_types.js +97 -0
  114. package/dist/model/query_model_contract.d.ts +2 -1
  115. package/dist/model/query_model_impl.d.ts +2 -1
  116. package/dist/model/query_model_impl.js +7 -0
  117. package/dist/model/query_node.d.ts +2 -1
  118. package/dist/model/source_def_utils.d.ts +2 -1
  119. package/dist/model/source_def_utils.js +4 -0
  120. package/dist/model/utils.d.ts +14 -1
  121. package/dist/model/utils.js +41 -0
  122. package/dist/to_stable.js +1 -1
  123. package/dist/util/closest_match.d.ts +9 -0
  124. package/dist/util/closest_match.js +47 -0
  125. package/dist/version.d.ts +1 -1
  126. package/dist/version.js +1 -1
  127. package/package.json +4 -4
@@ -36,7 +36,7 @@ export type AnyExpr = ExprE | ExprOptionalE | ExprWithKids | ExprLeaf;
36
36
  export declare function exprHasKids(e: AnyExpr): e is ExprWithKids;
37
37
  export declare function exprHasE(e: AnyExpr): e is ExprE;
38
38
  export declare function exprIsLeaf(e: AnyExpr): boolean;
39
- export type Expr = BinaryExpr | UnaryExpr | FunctionCallNode | OutputFieldNode | FilterCondition | FilteredExpr | AggregateExpr | EmptyExpr | UngroupNode | FunctionParameterNode | SpreadExpr | AggregateOrderByNode | AggregateLimitNode | FieldnameNode | SourceReferenceNode | ParameterNode | NowNode | MeasureTimeExpr | TimeExtractExpr | TimeDeltaExpr | TimeTruncExpr | DateLiteralNode | TimestampLiteralNode | TimestamptzLiteralNode | TypecastExpr | RegexMatchExpr | RegexLiteralNode | FilterMatchExpr | FilterLiteralExpr | StringLiteralNode | NumberLiteralNode | BooleanLiteralNode | RecordLiteralNode | ArrayLiteralNode | FunctionOrderBy | GenericSQLExpr | NullNode | CaseExpr | InCompareExpr | CompositeFieldExpr | ErrorNode;
39
+ export type Expr = BinaryExpr | UnaryExpr | FunctionCallNode | OutputFieldNode | FilterCondition | FilteredExpr | AggregateExpr | EmptyExpr | UngroupNode | FunctionParameterNode | SpreadExpr | AggregateOrderByNode | AggregateLimitNode | FieldnameNode | SourceReferenceNode | ParameterNode | GivenRefNode | NowNode | MeasureTimeExpr | TimeExtractExpr | TimeDeltaExpr | TimeTruncExpr | DateLiteralNode | TimestampLiteralNode | TimestamptzLiteralNode | TypecastExpr | RegexMatchExpr | RegexLiteralNode | FilterMatchExpr | FilterLiteralExpr | StringLiteralNode | NumberLiteralNode | BooleanLiteralNode | RecordLiteralNode | ArrayLiteralNode | FunctionOrderBy | GenericSQLExpr | NullNode | CaseExpr | InCompareExpr | CompositeFieldExpr | ErrorNode;
40
40
  export type BinaryOperator = '+' | '-' | '*' | '%' | '/' | 'and' | 'or' | '=' | '!=' | '>' | '<' | '>=' | '<=' | 'coalesce' | 'like' | '!like';
41
41
  export interface BinaryExpr extends ExprWithKids {
42
42
  node: BinaryOperator;
@@ -71,7 +71,7 @@ export interface FilterCondition extends ExprE {
71
71
  node: 'filterCondition';
72
72
  code: string;
73
73
  expressionType: ExpressionType;
74
- fieldUsage?: FieldUsage[];
74
+ refSummary?: RefSummary;
75
75
  filterView?: string;
76
76
  stableFilter?: Malloy.Filter;
77
77
  isSourceFilter?: boolean;
@@ -128,6 +128,11 @@ export interface ParameterNode extends ExprLeaf {
128
128
  node: 'parameter';
129
129
  path: string[];
130
130
  }
131
+ export interface GivenRefNode extends ExprLeaf {
132
+ node: 'given';
133
+ id: GivenID;
134
+ refName: string;
135
+ }
131
136
  export interface NowNode extends ExprLeaf {
132
137
  node: 'now';
133
138
  typeDef: {
@@ -287,12 +292,12 @@ export interface InCompareExpr extends ExprWithKids {
287
292
  export type ExpressionType = 'scalar' | 'aggregate' | 'scalar_analytic' | 'aggregate_analytic' | 'ungrouped_aggregate';
288
293
  export interface Expression {
289
294
  e?: Expr;
290
- fieldUsage?: FieldUsage[];
295
+ refSummary?: RefSummary;
291
296
  expressionType?: ExpressionType;
292
297
  code?: string;
293
298
  drillExpression?: Malloy.Expression;
294
299
  }
295
- type ConstantExpr = Expr;
300
+ export type ConstantExpr = Expr;
296
301
  interface ParameterInfo {
297
302
  name: string;
298
303
  value: ConstantExpr | null;
@@ -306,6 +311,42 @@ export declare function isParameterType(t: string): t is ParameterType;
306
311
  export type ParameterTypeDef = StringTypeDef | NumberTypeDef | BooleanTypeDef | TemporalTypeDef | FilterExpressionParamTypeDef | ErrorTypeDef;
307
312
  export type Parameter = ParameterTypeDef & ParameterInfo;
308
313
  export type Argument = Parameter;
314
+ /**
315
+ * Type of a given declaration. The grammar's `malloyType` already excludes
316
+ * `json` and `sql native`, so any value of this union is a legal given type.
317
+ *
318
+ * The fully-narrowed recursive form described in `~/ctx/mp/implementation.md`
319
+ * (a fresh union over a smaller atomic base) lands with the IR work; the
320
+ * shape here is the conservative "not yet narrowed" version.
321
+ */
322
+ export type GivenTypeDef = AtomicTypeDef | FilterExpressionParamTypeDef;
323
+ export interface Given extends HasLocation, HasAnnotation {
324
+ /** The name as written at the declaration site. Used by diagnostics
325
+ * that need a readable surface name out of an opaque GivenID. */
326
+ name: string;
327
+ type: GivenTypeDef;
328
+ /** Non-optional so the no-default case is explicit at every read site. */
329
+ default: ConstantExpr | undefined;
330
+ /** Pre-rendered source text of the default expression, captured at
331
+ * declaration time so hover (`DocumentGivenReference`) can show it
332
+ * without re-rendering an Expr back to source. Absent when there's
333
+ * no default. */
334
+ defaultText?: string;
335
+ /** Transitive closure of givens this declaration references — only
336
+ * reachable through the default's expression chain (a Given has no
337
+ * other given-referencing surface). Precomputed at declaration time.
338
+ * For `A is $B + 1` where `B is $C`: A's `givenUsage` is `[B, C]`.
339
+ * The satisfiability check expands each Query.givenUsage entry
340
+ * through this closure and verifies each id is in-namespace or has
341
+ * its own default. Empty/undefined when the default is a closed
342
+ * literal. */
343
+ givenUsage?: GivenUsage;
344
+ }
345
+ export interface GivenEntry {
346
+ type: 'given';
347
+ name: string;
348
+ id: GivenID;
349
+ }
309
350
  export declare function paramHasValue(p: Parameter): boolean;
310
351
  export interface DocumentRange {
311
352
  start: DocumentPosition;
@@ -335,10 +376,8 @@ export interface DocumentLocation {
335
376
  * the references, and in that case, this should include something like an
336
377
  * index or pointer to the full definition elsewhere in the model.
337
378
  */
338
- export interface LightweightDefinition {
379
+ export interface LightweightDefinition extends HasLocation, HasAnnotation {
339
380
  type: string;
340
- annotation?: Annotation;
341
- location?: DocumentLocation;
342
381
  }
343
382
  interface DocumentReferenceBase {
344
383
  text: string;
@@ -360,11 +399,26 @@ export interface DocumentQueryReference extends DocumentReferenceBase {
360
399
  export interface DocumentFieldReference extends DocumentReferenceBase {
361
400
  type: 'fieldReference';
362
401
  }
363
- export type DocumentReference = DocumentExploreReference | DocumentQueryReference | DocumentSQLBlockReference | DocumentFieldReference | DocumentJoinReference;
402
+ /**
403
+ * Definition info attached to a `DocumentGivenReference`. Extends the
404
+ * base with `defaultText` — the pre-rendered source of the given's
405
+ * default expression, if one was declared.
406
+ */
407
+ export interface GivenLightweightDefinition extends LightweightDefinition {
408
+ defaultText?: string;
409
+ }
410
+ export interface DocumentGivenReference extends DocumentReferenceBase {
411
+ type: 'givenReference';
412
+ definition: GivenLightweightDefinition;
413
+ }
414
+ export type DocumentReference = DocumentExploreReference | DocumentQueryReference | DocumentSQLBlockReference | DocumentFieldReference | DocumentJoinReference | DocumentGivenReference;
364
415
  /** put location into the parse tree. */
365
416
  export interface HasLocation {
366
417
  location?: DocumentLocation;
367
418
  }
419
+ export interface HasAnnotation {
420
+ annotation?: Annotation;
421
+ }
368
422
  /** All names have their source names and how they will appear in the symbol table that owns them */
369
423
  export interface AliasedName {
370
424
  name: string;
@@ -423,8 +477,7 @@ export declare function canOrderBy(s: string): boolean;
423
477
  * value could be an expression, and this is one of the objects
424
478
  * which might have an annotation.
425
479
  */
426
- export interface FieldBase extends NamedObject, Expression, ResultMetadata {
427
- annotation?: Annotation;
480
+ export interface FieldBase extends NamedObject, Expression, ResultMetadata, HasAnnotation {
428
481
  accessModifier?: NonDefaultAccessModifierLabel | undefined;
429
482
  requiresGroupBy?: RequiredGroupBy[];
430
483
  ungroupings?: AggregateUngrouping[];
@@ -514,7 +567,7 @@ export interface JoinBase {
514
567
  join: JoinType;
515
568
  matrixOperation?: MatrixOperation;
516
569
  onExpression?: Expr;
517
- fieldUsage?: FieldUsage[];
570
+ refSummary?: RefSummary;
518
571
  accessModifier?: NonDefaultAccessModifierLabel | undefined;
519
572
  }
520
573
  export type Joinable = CompositeSourceDef | TableSourceDef | SQLSourceDef | QuerySourceDef | VirtualSourceDef | RepeatedRecordDef | RecordDef | ArrayDef;
@@ -586,14 +639,14 @@ export interface TurtleSegment extends Filtered {
586
639
  export interface Pipeline {
587
640
  pipeline: PipeSegment[];
588
641
  }
589
- export interface Query extends Pipeline, Filtered, HasLocation {
642
+ export interface Query extends Pipeline, Filtered, HasLocation, HasAnnotation {
590
643
  type?: 'query';
591
644
  name?: string;
592
645
  structRef: StructRef;
593
646
  sourceArguments?: SafeRecord<Argument>;
594
- annotation?: Annotation;
595
647
  modelAnnotation?: Annotation;
596
648
  compositeResolvedSourceDef?: SourceDef;
649
+ givenUsage?: GivenUsage;
597
650
  }
598
651
  export type NamedQueryDef = Query & NamedObject;
599
652
  export type PipeSegment = QuerySegment | IndexSegment | RawSegment;
@@ -647,8 +700,9 @@ export type SegmentFieldDef = IndexFieldDef | QueryFieldDef;
647
700
  * 4) Each join path ending in an analytic funtion
648
701
  */
649
702
  export interface SegmentUsageSummary {
650
- activeJoins?: FieldUsage[];
651
- expandedFieldUsage?: FieldUsage[];
703
+ activeJoins?: FieldUsage;
704
+ expandedFieldUsage?: FieldUsage;
705
+ expandedGivenUsage?: GivenUsage;
652
706
  expandedUngroupings?: AggregateUngrouping[];
653
707
  }
654
708
  export interface IndexSegment extends Filtered, SegmentUsageSummary {
@@ -658,18 +712,82 @@ export interface IndexSegment extends Filtered, SegmentUsageSummary {
658
712
  weightMeasure?: string;
659
713
  sample?: Sampling;
660
714
  alwaysJoins?: string[];
661
- fieldUsage?: FieldUsage[];
715
+ refSummary?: RefSummary;
662
716
  referencedAt?: DocumentLocation;
663
717
  outputStruct: SourceDef;
664
718
  }
665
719
  export declare function isIndexSegment(pe: PipeSegment): pe is IndexSegment;
666
- export interface FieldUsage {
720
+ export interface FieldUsageEntry {
667
721
  path: string[];
668
722
  at?: DocumentLocation;
669
723
  uniqueKeyRequirement?: UniqueKeyRequirement;
670
724
  analyticFunctionUse?: boolean;
671
725
  }
672
- export declare function bareFieldUsage(fu: FieldUsage): boolean;
726
+ /** Plural alias most "field usage" data is a collection of entries. */
727
+ export type FieldUsage = FieldUsageEntry[];
728
+ export declare function bareFieldUsage(fu: FieldUsageEntry): boolean;
729
+ /**
730
+ * What a `givenRef` IR node references, accumulated while walking IR.
731
+ * `id` is the global GivenID; `at` carries the reference site for diagnostics.
732
+ */
733
+ export interface GivenUsageEntry {
734
+ id: GivenID;
735
+ at?: DocumentLocation;
736
+ }
737
+ /** Plural alias — most "given usage" data is a collection of entries. */
738
+ export type GivenUsage = GivenUsageEntry[];
739
+ /**
740
+ * `refSummary` is the IR-level reference-tracking field — what fields and
741
+ * givens does this IR fragment reference. `fieldUsage` carries source-rooted
742
+ * field paths (paths-rooted-in-source invariant unchanged); `givenUsage`
743
+ * carries GivenIDs reachable from this fragment, populated by the same
744
+ * walker that populates `fieldUsage`.
745
+ *
746
+ * Read sites use `fieldUsageFrom` / `givenUsageFrom` accessors so callers
747
+ * don't have to handle the optional-`refSummary` case.
748
+ */
749
+ export interface RefSummary {
750
+ fieldUsage: FieldUsage;
751
+ givenUsage?: GivenUsage;
752
+ }
753
+ export declare function fieldUsageFrom(rs: RefSummary | undefined): FieldUsage;
754
+ export declare function givenUsageFrom(rs: RefSummary | undefined): GivenUsage;
755
+ /**
756
+ * Construct a `RefSummary` from optionally-supplied component arrays. Returns
757
+ * undefined when no component is supplied (matching the IR convention that an
758
+ * absent `refSummary` means "I never set this"). An explicitly-empty array
759
+ * is preserved (e.g. `{fieldUsage: []}` mirrors the pre-rename "I checked,
760
+ * found nothing" state distinct from "I never set the field").
761
+ *
762
+ * Object-arg shape is the forward-compatible seam for `givenUsage` and any
763
+ * future RefSummary slots — they get added as additional optional keys on
764
+ * the destructured param without touching call sites that don't supply them.
765
+ */
766
+ export declare function mkRefSummary({ fieldUsage, givenUsage, }: {
767
+ fieldUsage?: FieldUsage;
768
+ givenUsage?: GivenUsage;
769
+ }): RefSummary | undefined;
770
+ /**
771
+ * Apply `fn` to each `FieldUsageEntry` in a `RefSummary`, preserving all other
772
+ * RefSummary fields. Returns undefined when `rs` is undefined. The site that
773
+ * needs to rewrite usage paths (e.g. rename, location-rebrand) goes through
774
+ * here so future RefSummary fields like `givenUsage` are carried along
775
+ * untouched without each call site having to know about them.
776
+ */
777
+ export declare function mapFieldUsage(rs: RefSummary | undefined, fn: (u: FieldUsageEntry) => FieldUsageEntry): RefSummary | undefined;
778
+ /**
779
+ * Mutating setter for a node's `fieldUsage`. When the node already has a
780
+ * `refSummary`, replaces just the `fieldUsage` slice (preserving `givenUsage`
781
+ * and any future RefSummary fields). When it doesn't, creates a fresh
782
+ * `refSummary` with the supplied usages.
783
+ *
784
+ * Use at sites that mutate an already-constructed IR node; for sites that
785
+ * build a node from a literal, write `{fieldUsage: [...]}` directly or use
786
+ * `mkRefSummary` for possibly-undefined inputs.
787
+ */
788
+ export declare function setFieldUsage(target: {
789
+ refSummary?: RefSummary;
790
+ }, usages: FieldUsage): void;
673
791
  export interface QuerySegment extends Filtered, Ordered, SegmentUsageSummary {
674
792
  type: 'reduce' | 'project' | 'partial';
675
793
  queryFields: QueryFieldDef[];
@@ -677,25 +795,23 @@ export interface QuerySegment extends Filtered, Ordered, SegmentUsageSummary {
677
795
  limit?: number;
678
796
  queryTimezone?: string;
679
797
  alwaysJoins?: string[];
680
- fieldUsage?: FieldUsage[];
798
+ refSummary?: RefSummary;
681
799
  referencedAt?: DocumentLocation;
682
800
  outputStruct: SourceDef;
683
801
  isRepeated: boolean;
684
802
  }
685
803
  export type NonDefaultAccessModifierLabel = 'private' | 'internal';
686
804
  export type AccessModifierLabel = NonDefaultAccessModifierLabel | 'public';
687
- export interface TurtleDef extends NamedObject, Pipeline {
805
+ export interface TurtleDef extends NamedObject, Pipeline, HasAnnotation {
688
806
  type: 'turtle';
689
- annotation?: Annotation;
690
807
  accessModifier?: NonDefaultAccessModifierLabel | undefined;
691
- fieldUsage?: FieldUsage[];
808
+ refSummary?: RefSummary;
692
809
  requiredGroupBys?: string[][];
693
810
  }
694
811
  export interface TurtleDefPlusFilters extends TurtleDef, Filtered {
695
812
  }
696
- interface StructDefBase extends HasLocation, NamedObject {
813
+ interface StructDefBase extends HasLocation, NamedObject, HasAnnotation {
697
814
  type: string;
698
- annotation?: Annotation;
699
815
  modelAnnotation?: ModelAnnotation;
700
816
  fields: FieldDef[];
701
817
  /** Marker for error placeholder structs created by ErrorFactory */
@@ -739,6 +855,8 @@ export declare function isSegmentSQL(f: SQLPhraseSegment): f is SQLStringSegment
739
855
  export declare function isSegmentSource(f: SQLPhraseSegment): f is PersistableSourceDef;
740
856
  /** Format: "name@modelUrl" - uniquely identifies a source for persistence */
741
857
  export type SourceID = string;
858
+ /** Created with `mkGivenID`. */
859
+ export type GivenID = string;
742
860
  /** Hash of (connectionDigest, sql) - uniquely identifies a built artifact */
743
861
  export type BuildID = string;
744
862
  /**
@@ -832,13 +950,13 @@ export type ExpressionValueType = AtomicFieldType | NonAtomicType | TurtleType;
832
950
  export type ExpressionValueTypeDef = AtomicTypeDef | NonAtomicTypeDef | TurtleTypeDef;
833
951
  export type BasicExpressionType = Exclude<ExpressionValueType, JoinElementType | 'turtle'>;
834
952
  export interface RequiredGroupBy {
835
- fieldUsage?: FieldUsage;
953
+ fieldUsage?: FieldUsageEntry;
836
954
  at?: DocumentLocation;
837
955
  path: string[];
838
956
  }
839
957
  export interface AggregateUngrouping {
840
958
  ungroupedFields: string[][] | '*';
841
- fieldUsage: FieldUsage[];
959
+ fieldUsage: FieldUsage;
842
960
  requiresGroupBy?: RequiredGroupBy[];
843
961
  exclude: boolean;
844
962
  path: string[];
@@ -847,7 +965,7 @@ export interface AggregateUngrouping {
847
965
  export type TypeInfo = {
848
966
  expressionType: ExpressionType;
849
967
  evalSpace: EvalSpace;
850
- fieldUsage: FieldUsage[];
968
+ refSummary?: RefSummary;
851
969
  requiresGroupBy?: RequiredGroupBy[];
852
970
  ungroupings?: AggregateUngrouping[];
853
971
  };
@@ -946,10 +1064,9 @@ export type AtomicFieldDef = BasicAtomicDef | BasicArrayDef | RecordDef | Repeat
946
1064
  export declare function isBasicAtomic(fd: FieldDef | QueryFieldDef | AtomicTypeDef): fd is BasicAtomicDef;
947
1065
  export type FieldDef = BasicAtomicDef | JoinFieldDef | TurtleDef;
948
1066
  export type FieldDefType = AtomicFieldType | 'turtle' | JoinElementType;
949
- export interface RefToField {
1067
+ export interface RefToField extends HasAnnotation {
950
1068
  type: 'fieldref';
951
1069
  path: string[];
952
- annotation?: Annotation;
953
1070
  at?: DocumentLocation;
954
1071
  drillExpression?: Malloy.Expression | undefined;
955
1072
  }
@@ -957,18 +1074,16 @@ export type QueryFieldDef = AtomicFieldDef | TurtleDef | RefToField;
957
1074
  export type TypedDef = AtomicTypeDef | JoinFieldDef | TurtleDef | RefToField | StructDef;
958
1075
  /** Get the output name for a NamedObject */
959
1076
  export declare function getIdentifier(n: AliasedName): string;
960
- export interface UserTypeFieldDef {
1077
+ export interface UserTypeFieldDef extends HasAnnotation {
961
1078
  name: string;
962
1079
  typeDef: AtomicTypeDef;
963
- annotation?: Annotation;
964
1080
  }
965
- export interface UserTypeDef extends NamedObject {
1081
+ export interface UserTypeDef extends NamedObject, HasAnnotation {
966
1082
  type: 'userType';
967
1083
  fields: UserTypeFieldDef[];
968
- annotation?: Annotation;
969
1084
  }
970
1085
  export declare function isUserTypeDef(sd: NamedModelObject): sd is UserTypeDef;
971
- export type NamedModelObject = SourceDef | NamedQueryDef | FunctionDef | ConnectionDef | UserTypeDef;
1086
+ export type NamedModelObject = SourceDef | NamedQueryDef | FunctionDef | ConnectionDef | UserTypeDef | GivenEntry;
972
1087
  export interface DependencyTree {
973
1088
  [url: string]: DependencyTree;
974
1089
  }
@@ -984,6 +1099,7 @@ export interface ModelDef {
984
1099
  * Each entry includes a lazily-computed persist flag.
985
1100
  */
986
1101
  sourceRegistry: Record<SourceID, SourceRegistryValue>;
1102
+ givens?: Record<GivenID, Given>;
987
1103
  annotation?: ModelAnnotation;
988
1104
  queryList: Query[];
989
1105
  dependencies: DependencyTree;
@@ -1034,7 +1150,7 @@ export interface DrillSource {
1034
1150
  sourceFilters?: FilterCondition[];
1035
1151
  sourceArguments?: SafeRecord<Argument>;
1036
1152
  }
1037
- export interface CompiledQuery extends DrillSource {
1153
+ export interface CompiledQuery extends DrillSource, HasAnnotation {
1038
1154
  structs: SourceDef[];
1039
1155
  sql: string;
1040
1156
  lastStageName: string;
@@ -1042,7 +1158,6 @@ export interface CompiledQuery extends DrillSource {
1042
1158
  queryName?: string | undefined;
1043
1159
  connectionName: string;
1044
1160
  queryTimezone?: string;
1045
- annotation?: Annotation;
1046
1161
  defaultRowLimitAdded?: number;
1047
1162
  }
1048
1163
  /** Result type for running a Malloy query. */
@@ -1089,6 +1204,10 @@ export interface SearchValueMapResult {
1089
1204
  * Uses Map instead of Record because both keys are user-provided strings.
1090
1205
  */
1091
1206
  export type VirtualMap = Map<string, Map<string, string>>;
1207
+ /** JS-side accepted shapes for caller-supplied given values. */
1208
+ export type GivenValue = string | number | bigint | boolean | Date | null | GivenValue[] | {
1209
+ [key: string]: GivenValue;
1210
+ };
1092
1211
  export interface PrepareResultOptions {
1093
1212
  defaultRowLimit?: number;
1094
1213
  isPartialQuery?: boolean;
@@ -1099,6 +1218,7 @@ export interface PrepareResultOptions {
1099
1218
  connectionDigests?: SafeRecord<string>;
1100
1219
  /** Map from connectionName → virtualName → tablePath for virtual source resolution */
1101
1220
  virtualMap?: VirtualMap;
1221
+ resolvedGivens?: Map<GivenID, Expr>;
1102
1222
  }
1103
1223
  type UTD = AtomicTypeDef | TypedDef | FunctionParameterTypeDef | FunctionReturnTypeDef | undefined;
1104
1224
  /**
@@ -1119,6 +1239,13 @@ export declare const TD: {
1119
1239
  isAnyTimestamp(td: UTD): td is ATimestampTypeDef;
1120
1240
  isTemporal(td: UTD): td is TemporalTypeDef;
1121
1241
  isError: (td: UTD) => td is ErrorTypeDef;
1242
+ /**
1243
+ * Strip non-type metadata (name, joins, expressionType, evalSpace, ...)
1244
+ * off a typed def, leaving a clean AtomicTypeDef. Used when copying a
1245
+ * field's type onto an Expr's typeDef, where the field metadata would
1246
+ * pollute the IR. Returns `{type: 'error'}` for non-atomic inputs.
1247
+ */
1248
+ atomicDef(td: UTD): AtomicTypeDef;
1122
1249
  eq(x: UTD, y: UTD): boolean;
1123
1250
  };
1124
1251
  /**
@@ -78,6 +78,11 @@ exports.isSamplingEnable = isSamplingEnable;
78
78
  exports.isRawSegment = isRawSegment;
79
79
  exports.isIndexSegment = isIndexSegment;
80
80
  exports.bareFieldUsage = bareFieldUsage;
81
+ exports.fieldUsageFrom = fieldUsageFrom;
82
+ exports.givenUsageFrom = givenUsageFrom;
83
+ exports.mkRefSummary = mkRefSummary;
84
+ exports.mapFieldUsage = mapFieldUsage;
85
+ exports.setFieldUsage = setFieldUsage;
81
86
  exports.isSegmentSQL = isSegmentSQL;
82
87
  exports.isSegmentSource = isSegmentSource;
83
88
  exports.isSourceRegistryReference = isSourceRegistryReference;
@@ -458,6 +463,61 @@ function bareFieldUsage(fu) {
458
463
  return (fu.uniqueKeyRequirement === undefined &&
459
464
  fu.analyticFunctionUse === undefined);
460
465
  }
466
+ function fieldUsageFrom(rs) {
467
+ var _a;
468
+ return (_a = rs === null || rs === void 0 ? void 0 : rs.fieldUsage) !== null && _a !== void 0 ? _a : [];
469
+ }
470
+ function givenUsageFrom(rs) {
471
+ var _a;
472
+ return (_a = rs === null || rs === void 0 ? void 0 : rs.givenUsage) !== null && _a !== void 0 ? _a : [];
473
+ }
474
+ /**
475
+ * Construct a `RefSummary` from optionally-supplied component arrays. Returns
476
+ * undefined when no component is supplied (matching the IR convention that an
477
+ * absent `refSummary` means "I never set this"). An explicitly-empty array
478
+ * is preserved (e.g. `{fieldUsage: []}` mirrors the pre-rename "I checked,
479
+ * found nothing" state distinct from "I never set the field").
480
+ *
481
+ * Object-arg shape is the forward-compatible seam for `givenUsage` and any
482
+ * future RefSummary slots — they get added as additional optional keys on
483
+ * the destructured param without touching call sites that don't supply them.
484
+ */
485
+ function mkRefSummary({ fieldUsage, givenUsage, }) {
486
+ if (fieldUsage === undefined && givenUsage === undefined)
487
+ return undefined;
488
+ return {
489
+ fieldUsage: fieldUsage !== null && fieldUsage !== void 0 ? fieldUsage : [],
490
+ ...(givenUsage && { givenUsage }),
491
+ };
492
+ }
493
+ /**
494
+ * Apply `fn` to each `FieldUsageEntry` in a `RefSummary`, preserving all other
495
+ * RefSummary fields. Returns undefined when `rs` is undefined. The site that
496
+ * needs to rewrite usage paths (e.g. rename, location-rebrand) goes through
497
+ * here so future RefSummary fields like `givenUsage` are carried along
498
+ * untouched without each call site having to know about them.
499
+ */
500
+ function mapFieldUsage(rs, fn) {
501
+ return rs && { ...rs, fieldUsage: rs.fieldUsage.map(fn) };
502
+ }
503
+ /**
504
+ * Mutating setter for a node's `fieldUsage`. When the node already has a
505
+ * `refSummary`, replaces just the `fieldUsage` slice (preserving `givenUsage`
506
+ * and any future RefSummary fields). When it doesn't, creates a fresh
507
+ * `refSummary` with the supplied usages.
508
+ *
509
+ * Use at sites that mutate an already-constructed IR node; for sites that
510
+ * build a node from a literal, write `{fieldUsage: [...]}` directly or use
511
+ * `mkRefSummary` for possibly-undefined inputs.
512
+ */
513
+ function setFieldUsage(target, usages) {
514
+ if (target.refSummary) {
515
+ target.refSummary.fieldUsage = usages;
516
+ }
517
+ else {
518
+ target.refSummary = { fieldUsage: usages };
519
+ }
520
+ }
461
521
  function isSegmentSQL(f) {
462
522
  return 'sql' in f;
463
523
  }
@@ -585,6 +645,43 @@ exports.TD = {
585
645
  return isTemporalType(typ);
586
646
  },
587
647
  isError: (td) => (td === null || td === void 0 ? void 0 : td.type) === 'error',
648
+ /**
649
+ * Strip non-type metadata (name, joins, expressionType, evalSpace, ...)
650
+ * off a typed def, leaving a clean AtomicTypeDef. Used when copying a
651
+ * field's type onto an Expr's typeDef, where the field metadata would
652
+ * pollute the IR. Returns `{type: 'error'}` for non-atomic inputs.
653
+ */
654
+ atomicDef(td) {
655
+ if (!exports.TD.isAtomic(td))
656
+ return { type: 'error' };
657
+ switch (td.type) {
658
+ case 'array':
659
+ return isRepeatedRecord(td)
660
+ ? {
661
+ type: 'array',
662
+ elementTypeDef: td.elementTypeDef,
663
+ fields: td.fields,
664
+ }
665
+ : { type: 'array', elementTypeDef: td.elementTypeDef };
666
+ case 'record':
667
+ return { type: 'record', fields: td.fields };
668
+ case 'number':
669
+ return td.numberType
670
+ ? { type: 'number', numberType: td.numberType }
671
+ : { type: 'number' };
672
+ case 'sql native':
673
+ return td.rawType
674
+ ? { type: 'sql native', rawType: td.rawType }
675
+ : { type: 'sql native' };
676
+ case 'timestamp':
677
+ case 'timestamptz':
678
+ return td.timeframe
679
+ ? { type: td.type, timeframe: td.timeframe }
680
+ : { type: td.type };
681
+ default:
682
+ return { type: td.type };
683
+ }
684
+ },
588
685
  eq(x, y) {
589
686
  if (x === undefined || y === undefined) {
590
687
  return false;
@@ -1,7 +1,7 @@
1
1
  import type { Connection } from '../connection/types';
2
2
  import type { Dialect } from '../dialect';
3
3
  import type { EventStream } from '../runtime_types';
4
- import type { SourceDef, ModelDef, StructRef, Argument, PrepareResultOptions, CompiledQuery, SearchIndexResult, Query } from './malloy_types';
4
+ import type { SourceDef, ModelDef, StructRef, Argument, Given, PrepareResultOptions, CompiledQuery, SearchIndexResult, Query } from './malloy_types';
5
5
  import type { QueryStruct } from './query_node';
6
6
  import type { StageWriter } from './stage_writer';
7
7
  export interface ParentQueryModel {
@@ -18,6 +18,7 @@ export interface QueryModel {
18
18
  dialect: Dialect;
19
19
  modelDef: ModelDef | undefined;
20
20
  structs: Map<string, QueryStruct>;
21
+ givens: Record<string, Given>;
21
22
  eventStream?: EventStream;
22
23
  loadModelFromDef(modelDef: ModelDef): void;
23
24
  getStructByName(name: string): QueryStruct;
@@ -1,4 +1,4 @@
1
- import type { ModelDef, StructRef, Argument, PrepareResultOptions, Query, SourceDef, SearchIndexResult, CompiledQuery, TurtleDef } from './malloy_types';
1
+ import type { ModelDef, StructRef, Argument, Given, PrepareResultOptions, Query, SourceDef, SearchIndexResult, CompiledQuery, TurtleDef } from './malloy_types';
2
2
  import { StageWriter } from './stage_writer';
3
3
  import { type Dialect } from '../dialect';
4
4
  import type { Connection } from '../connection/types';
@@ -10,6 +10,7 @@ export declare class QueryModelImpl implements QueryModel, ModelRootInterface {
10
10
  dialect: Dialect;
11
11
  modelDef: ModelDef | undefined;
12
12
  structs: Map<string, QueryStruct>;
13
+ get givens(): Record<string, Given>;
13
14
  constructor(modelDef: ModelDef | undefined);
14
15
  getFinalOutputStruct(query: Query, options: PrepareResultOptions | undefined): SourceDef | undefined;
15
16
  loadModelFromDef(modelDef: ModelDef): void;
@@ -18,6 +18,10 @@ function makeQueryModel(modelDef) {
18
18
  return new QueryModelImpl(modelDef);
19
19
  }
20
20
  class QueryModelImpl {
21
+ get givens() {
22
+ var _a, _b;
23
+ return (_b = (_a = this.modelDef) === null || _a === void 0 ? void 0 : _a.givens) !== null && _b !== void 0 ? _b : {};
24
+ }
21
25
  constructor(modelDef) {
22
26
  this.dialect = new dialect_1.StandardSQLDialect();
23
27
  // dialect: Dialect = new PostgresDialect();
@@ -49,6 +53,9 @@ class QueryModelImpl {
49
53
  else if (s.type === 'userType') {
50
54
  // User type definitions are metadata only, not queryable
51
55
  }
56
+ else if (s.type === 'given') {
57
+ // Givens are metadata in the namespace, not queryable structures.
58
+ }
52
59
  else {
53
60
  throw new Error('Internal Error: Unknown structure type');
54
61
  }
@@ -1,4 +1,4 @@
1
- import type { FieldDef, BooleanFieldDef, DateFieldDef, StringFieldDef, JSONFieldDef, NumberFieldDef, ATimestampFieldDef, NativeUnsupportedFieldDef, JoinFieldDef, Argument, PrepareResultOptions, AtomicFieldDef, BasicAtomicDef, FilterCondition, RefToField, StructDef, TurtleDef, TurtleDefPlusFilters, SourceDef, Query } from './malloy_types';
1
+ import type { FieldDef, BooleanFieldDef, DateFieldDef, StringFieldDef, JSONFieldDef, NumberFieldDef, ATimestampFieldDef, NativeUnsupportedFieldDef, JoinFieldDef, Argument, Given, GivenID, PrepareResultOptions, AtomicFieldDef, BasicAtomicDef, FilterCondition, RefToField, StructDef, TurtleDef, TurtleDefPlusFilters, SourceDef, Query } from './malloy_types';
2
2
  import type { EventStream } from '../runtime_types';
3
3
  import type { Tag } from '@malloydata/malloy-tag';
4
4
  import type { Dialect } from '../dialect';
@@ -62,6 +62,7 @@ export interface ParentQueryStruct {
62
62
  }
63
63
  export interface ModelRootInterface {
64
64
  structs: Map<string, QueryStruct>;
65
+ givens: Record<GivenID, Given>;
65
66
  }
66
67
  export interface ParentQueryModel {
67
68
  model: ModelRootInterface;
@@ -7,8 +7,9 @@
7
7
  *
8
8
  * The `extends` property is set by callers when processing extend blocks.
9
9
  */
10
- import type { BuildID, FieldDef, ModelDef, PersistableSourceDef, Query, QuerySourceDef, SourceDef, SourceID, SourceRegistryEntry, SourceRegistryValue, SQLPhraseSegment, SQLSourceDef, TableSourceDef } from './malloy_types';
10
+ import type { BuildID, FieldDef, GivenID, ModelDef, PersistableSourceDef, Query, QuerySourceDef, SourceDef, SourceID, SourceRegistryEntry, SourceRegistryValue, SQLPhraseSegment, SQLSourceDef, TableSourceDef } from './malloy_types';
11
11
  export declare function mkSourceID(name: string, url: string | undefined): SourceID;
12
+ export declare function mkGivenID(name: string, url: string | undefined): GivenID;
12
13
  /**
13
14
  * Create a BuildID from connection digest and SQL.
14
15
  * BuildID is a hash that uniquely identifies a build artifact.
@@ -5,6 +5,7 @@
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.mkSourceID = mkSourceID;
8
+ exports.mkGivenID = mkGivenID;
8
9
  exports.mkBuildID = mkBuildID;
9
10
  exports.mkQuerySourceDef = mkQuerySourceDef;
10
11
  exports.mkSQLSourceDef = mkSQLSourceDef;
@@ -17,6 +18,9 @@ const malloy_types_1 = require("./malloy_types");
17
18
  function mkSourceID(name, url) {
18
19
  return `${name}@${url !== null && url !== void 0 ? url : 'unknown'}`;
19
20
  }
21
+ function mkGivenID(name, url) {
22
+ return (0, utils_1.pathToKey)('given', [name, url !== null && url !== void 0 ? url : 'unknown']);
23
+ }
20
24
  /**
21
25
  * Create a BuildID from connection digest and SQL.
22
26
  * BuildID is a hash that uniquely identifies a build artifact.
@@ -1,5 +1,18 @@
1
- import type { Expr, GenericSQLExpr, ModelDef, StructDef } from './malloy_types';
1
+ import type { AtomicTypeDef, Expr, FieldDef, GenericSQLExpr, GivenTypeDef, ModelDef, StructDef } from './malloy_types';
2
2
  import type { DialectFieldList } from '../dialect';
3
+ /**
4
+ * Format a typeDef as user-readable Malloy type syntax. Used in error
5
+ * messages where we want to echo a compound type back to the user in the
6
+ * same form they wrote it.
7
+ *
8
+ * `string` → `string`
9
+ * `string[]` → `string[]`
10
+ * `{a::string, b::number}` → `{a :: string, b :: number}`
11
+ * `{a::string}[]` → `{a :: string}[]`
12
+ * `filter<string>` → `filter<string>`
13
+ * sql-native rawType → `sql native: jsonb`
14
+ */
15
+ export declare function typeDefToString(td: AtomicTypeDef | GivenTypeDef | FieldDef): string;
3
16
  /** simple indent function */
4
17
  export declare function indent(s: string): string;
5
18
  /**