@malloydata/malloy 0.0.311 → 0.0.312

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.
@@ -26,13 +26,18 @@ class PostgresBase extends dialect_1.Dialect {
26
26
  if (malloy_types_1.TD.isTimestamp(df.e.typeDef)) {
27
27
  const tz = (0, dialect_1.qtz)(qi);
28
28
  if (tz) {
29
+ // get a civil version of the time in the query time zone
29
30
  const civilSource = `(${truncThis}::TIMESTAMPTZ AT TIME ZONE '${tz}')`;
31
+ // do truncation in that time space
30
32
  let civilTrunc = `DATE_TRUNC('${df.units}', ${civilSource})`;
31
33
  if (week) {
32
34
  civilTrunc = `(${civilTrunc} - INTERVAL '1' DAY)`;
33
35
  }
34
- // Convert the truncated civil time back to UTC by treating it as being IN the query timezone
36
+ // make a tstz from the civil time ... "AT TIME ZONE" of
37
+ // a TIMESTAMP will produce a TIMESTAMPTZ in that zone
38
+ // where the civi appeareance is the same as the TIMESTAMP
35
39
  const truncTsTz = `${civilTrunc} AT TIME ZONE '${tz}'`;
40
+ // Now just make a system TIMESTAMP from that
36
41
  return `(${truncTsTz})::TIMESTAMP`;
37
42
  }
38
43
  }
@@ -1,4 +1,4 @@
1
- import type { Expr, Sampling, AtomicTypeDef, TimeDeltaExpr, TypecastExpr, RegexMatchExpr, MeasureTimeExpr, TimeLiteralNode, TimeExtractExpr, BasicAtomicTypeDef, RecordLiteralNode } from '../../model/malloy_types';
1
+ import type { Expr, Sampling, AtomicTypeDef, TimeDeltaExpr, TypecastExpr, RegexMatchExpr, MeasureTimeExpr, TimeLiteralNode, TimeExtractExpr, TimeTruncExpr, BasicAtomicTypeDef, RecordLiteralNode } from '../../model/malloy_types';
2
2
  import type { DialectFunctionOverloadDef } from '../functions';
3
3
  import type { DialectFieldList, OrderByClauseType, QueryInfo } from '../dialect';
4
4
  import { PostgresBase } from '../pg_impl';
@@ -72,6 +72,7 @@ export declare class TrinoDialect extends PostgresBase {
72
72
  sqlStringAggDistinct(distinctKey: string, valueSQL: string, separatorSQL: string): string;
73
73
  validateTypeName(sqlType: string): boolean;
74
74
  sqlLiteralTime(qi: QueryInfo, lit: TimeLiteralNode): string;
75
+ sqlTruncExpr(qi: QueryInfo, df: TimeTruncExpr): string;
75
76
  sqlTimeExtractExpr(qi: QueryInfo, from: TimeExtractExpr): string;
76
77
  sqlLiteralRecord(lit: RecordLiteralNode): string;
77
78
  }
@@ -498,6 +498,36 @@ ${(0, utils_1.indent)(sql)}
498
498
  }
499
499
  return `TIMESTAMP '${lit.literal}'`;
500
500
  }
501
+ sqlTruncExpr(qi, df) {
502
+ // adjusting for monday/sunday weeks
503
+ const week = df.units === 'week';
504
+ const truncThis = week ? `${df.e.sql} + INTERVAL '1' DAY` : df.e.sql;
505
+ // Only do timezone conversion for timestamps, not dates
506
+ if (malloy_types_1.TD.isTimestamp(df.e.typeDef)) {
507
+ const tz = qtz(qi);
508
+ if (tz) {
509
+ // get a civil version of the time in the query time zone
510
+ const civilSource = `(CAST(${truncThis} AS TIMESTAMP WITH TIME ZONE) AT TIME ZONE '${tz}')`;
511
+ // do truncation in that time space
512
+ let civilTrunc = `DATE_TRUNC('${df.units}', ${civilSource})`;
513
+ if (week) {
514
+ civilTrunc = `(${civilTrunc} - INTERVAL '1' DAY)`;
515
+ }
516
+ // make a tstz from the civil time ... "AT TIME ZONE" of
517
+ // a TIMESTAMP will produce a TIMESTAMP WITH TIME ZONE in that zone
518
+ // where the civil appearance is the same as the TIMESTAMP
519
+ const truncTsTz = `${civilTrunc} AT TIME ZONE '${tz}'`;
520
+ // Now just make a system TIMESTAMP from that
521
+ return `CAST(${truncTsTz} AS TIMESTAMP)`;
522
+ }
523
+ }
524
+ // For dates (civil time) or timestamps without query timezone
525
+ let result = `DATE_TRUNC('${df.units}', ${truncThis})`;
526
+ if (week) {
527
+ result = `(${result} - INTERVAL '1' DAY)`;
528
+ }
529
+ return result;
530
+ }
501
531
  sqlTimeExtractExpr(qi, from) {
502
532
  const pgUnits = pg_impl_1.timeExtractMap[from.units] || from.units;
503
533
  let extractFrom = from.e.sql || '';
package/dist/malloy.d.ts CHANGED
@@ -757,6 +757,7 @@ export declare class ExploreField extends Explore {
757
757
  isAtomicField(): this is AtomicField;
758
758
  get parentExplore(): Explore;
759
759
  get sourceClasses(): string[];
760
+ get queryTimezone(): string | undefined;
760
761
  }
761
762
  type Connectionable = {
762
763
  connection: Connection;
package/dist/malloy.js CHANGED
@@ -1715,6 +1715,14 @@ class ExploreField extends Explore {
1715
1715
  const sourceField = this.structDef.name || this.structDef.as;
1716
1716
  return sourceField ? [sourceField] : [];
1717
1717
  }
1718
+ get queryTimezone() {
1719
+ // For ExploreField, check the structDef directly first
1720
+ if ((0, model_1.isRecordOrRepeatedRecord)(this._structDef)) {
1721
+ return this._structDef.queryTimezone;
1722
+ }
1723
+ // Fall back to the parent implementation
1724
+ return super.queryTimezone;
1725
+ }
1718
1726
  }
1719
1727
  exports.ExploreField = ExploreField;
1720
1728
  /**
@@ -55,6 +55,9 @@ export declare class FieldInstanceResult implements FieldInstance {
55
55
  * Information about the query containing this result set. Invented
56
56
  * to pass on timezone information, but maybe more things will
57
57
  * eventually go in here.
58
+ *
59
+ * For nested queries, this walks up the FieldInstanceResult parent chain
60
+ * to find the most specific (innermost) query timezone that applies.
58
61
  * @returns QueryInfo
59
62
  */
60
63
  getQueryInfo(): QueryInfo;
@@ -117,6 +117,9 @@ class FieldInstanceResult {
117
117
  * Information about the query containing this result set. Invented
118
118
  * to pass on timezone information, but maybe more things will
119
119
  * eventually go in here.
120
+ *
121
+ * For nested queries, this walks up the FieldInstanceResult parent chain
122
+ * to find the most specific (innermost) query timezone that applies.
120
123
  * @returns QueryInfo
121
124
  */
122
125
  getQueryInfo() {
@@ -127,6 +130,9 @@ class FieldInstanceResult {
127
130
  return { queryTimezone };
128
131
  }
129
132
  }
133
+ if (this.parent) {
134
+ return this.parent.getQueryInfo();
135
+ }
130
136
  return {};
131
137
  }
132
138
  addField(as, field, usage, drillExpression) {
@@ -441,6 +441,7 @@ export interface RecordTypeDef {
441
441
  export interface RecordDef extends RecordTypeDef, StructDefBase, JoinBase, FieldBase {
442
442
  type: 'record';
443
443
  join: 'one';
444
+ queryTimezone?: string;
444
445
  }
445
446
  export interface RecordElementTypeDef {
446
447
  type: 'record_element';
@@ -453,11 +454,13 @@ export interface RepeatedRecordTypeDef {
453
454
  export interface RepeatedRecordDef extends RepeatedRecordTypeDef, StructDefBase, JoinBase, FieldBase {
454
455
  type: 'array';
455
456
  join: 'many';
457
+ queryTimezone?: string;
456
458
  }
457
459
  export type ArrayTypeDef = BasicArrayTypeDef | RepeatedRecordTypeDef;
458
460
  export type ArrayDef = BasicArrayDef | RepeatedRecordDef;
459
461
  export declare function isRepeatedRecordFunctionParam(paramT: FunctionParameterTypeDef): paramT is RepeatedRecordFunctionParameterTypeDef;
460
462
  export declare function isRepeatedRecord(fd: FieldDef | QueryFieldDef | StructDef | AtomicTypeDef): fd is RepeatedRecordTypeDef;
463
+ export declare function isRecordOrRepeatedRecord(fd: FieldDef | StructDef): fd is RecordDef | RepeatedRecordDef;
461
464
  export declare function isBasicArray(td: AtomicTypeDef | FieldDef | QueryFieldDef | StructDef): td is BasicArrayTypeDef;
462
465
  export interface ErrorTypeDef {
463
466
  type: 'error';
@@ -52,6 +52,7 @@ exports.mkFieldDef = mkFieldDef;
52
52
  exports.mkArrayDef = mkArrayDef;
53
53
  exports.isRepeatedRecordFunctionParam = isRepeatedRecordFunctionParam;
54
54
  exports.isRepeatedRecord = isRepeatedRecord;
55
+ exports.isRecordOrRepeatedRecord = isRecordOrRepeatedRecord;
55
56
  exports.isBasicArray = isBasicArray;
56
57
  exports.isMatrixOperation = isMatrixOperation;
57
58
  exports.isJoinable = isJoinable;
@@ -303,6 +304,12 @@ function isRepeatedRecordFunctionParam(paramT) {
303
304
  function isRepeatedRecord(fd) {
304
305
  return fd.type === 'array' && fd.elementTypeDef.type === 'record_element';
305
306
  }
307
+ function isRecordOrRepeatedRecord(fd) {
308
+ return (fd.type === 'record' ||
309
+ (fd.type === 'array' &&
310
+ 'elementTypeDef' in fd &&
311
+ fd.elementTypeDef.type === 'record_element'));
312
+ }
306
313
  function isBasicArray(td) {
307
314
  return td.type === 'array' && td.elementTypeDef.type !== 'record_element';
308
315
  }
@@ -388,24 +388,37 @@ class QueryQuery extends query_node_1.QueryField {
388
388
  const resultMetadata = this.getResultMetadata(fi);
389
389
  if (fi instanceof field_instance_1.FieldInstanceResult) {
390
390
  const { structDef, repeatedResultType } = this.generateTurtlePipelineSQL(fi, new stage_writer_1.StageWriter(true, undefined), '<nosource>');
391
+ // Get the timezone from the nested query
392
+ const nestedQueryInfo = fi.getQueryInfo();
393
+ const queryTimezone = nestedQueryInfo.queryTimezone;
391
394
  if (repeatedResultType === 'nested') {
392
395
  const multiLineNest = {
393
- ...structDef,
394
396
  type: 'array',
395
397
  elementTypeDef: { type: 'record_element' },
396
398
  join: 'many',
397
399
  name,
400
+ fields: structDef.fields,
401
+ ...(structDef.annotation && { annotation: structDef.annotation }),
402
+ ...(structDef.modelAnnotation && {
403
+ modelAnnotation: structDef.modelAnnotation,
404
+ }),
398
405
  resultMetadata,
406
+ ...(queryTimezone && { queryTimezone }),
399
407
  };
400
408
  fields.push(multiLineNest);
401
409
  }
402
410
  else {
403
411
  const oneLineNest = {
404
- ...structDef,
405
412
  type: 'record',
406
413
  join: 'one',
407
414
  name,
415
+ fields: structDef.fields,
416
+ ...(structDef.annotation && { annotation: structDef.annotation }),
417
+ ...(structDef.modelAnnotation && {
418
+ modelAnnotation: structDef.modelAnnotation,
419
+ }),
408
420
  resultMetadata,
421
+ ...(queryTimezone && { queryTimezone }),
409
422
  };
410
423
  fields.push(oneLineNest);
411
424
  }
package/dist/to_stable.js CHANGED
@@ -213,9 +213,17 @@ function convertFieldInfos(source, fields) {
213
213
  const resultMetadataAnnotation = field.resultMetadata
214
214
  ? getResultMetadataAnnotation(field, field.resultMetadata)
215
215
  : undefined;
216
+ // Check if this field has queryTimezone information (for RecordDef/RepeatedRecordDef)
217
+ let timezoneAnnotation;
218
+ if ((0, model_1.isRecordOrRepeatedRecord)(field) && field.queryTimezone) {
219
+ const timezoneTag = malloy_tag_1.Tag.withPrefix('#(malloy) ');
220
+ timezoneTag.set(['query_timezone'], field.queryTimezone);
221
+ timezoneAnnotation = { value: timezoneTag.toString() };
222
+ }
216
223
  const fieldAnnotations = [
217
224
  ...(annotations !== null && annotations !== void 0 ? annotations : []),
218
225
  ...(resultMetadataAnnotation ? [resultMetadataAnnotation] : []),
226
+ ...(timezoneAnnotation ? [timezoneAnnotation] : []),
219
227
  ];
220
228
  const fieldInfo = {
221
229
  kind: aggregate ? 'measure' : 'dimension',
@@ -343,6 +351,11 @@ function getResultStructMetadataAnnotation(field, resultMetadata) {
343
351
  }
344
352
  hasAny = true;
345
353
  }
354
+ // Include queryTimezone if present on the field
355
+ if (field.queryTimezone) {
356
+ tag.set(['query_timezone'], field.queryTimezone);
357
+ hasAny = true;
358
+ }
346
359
  return hasAny
347
360
  ? {
348
361
  value: tag.toString(),
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const MALLOY_VERSION = "0.0.311";
1
+ export declare const MALLOY_VERSION = "0.0.312";
package/dist/version.js CHANGED
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MALLOY_VERSION = void 0;
4
4
  // generated with 'generate-version-file' script; do not edit manually
5
- exports.MALLOY_VERSION = '0.0.311';
5
+ exports.MALLOY_VERSION = '0.0.312';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.311",
3
+ "version": "0.0.312",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -41,9 +41,9 @@
41
41
  "generate-version-file": "VERSION=$(npm pkg get version --workspaces=false | tr -d \\\")\necho \"// generated with 'generate-version-file' script; do not edit manually\\nexport const MALLOY_VERSION = '$VERSION';\" > src/version.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@malloydata/malloy-filter": "0.0.311",
45
- "@malloydata/malloy-interfaces": "0.0.311",
46
- "@malloydata/malloy-tag": "0.0.311",
44
+ "@malloydata/malloy-filter": "0.0.312",
45
+ "@malloydata/malloy-interfaces": "0.0.312",
46
+ "@malloydata/malloy-tag": "0.0.312",
47
47
  "antlr4ts": "^0.5.0-alpha.4",
48
48
  "assert": "^2.0.0",
49
49
  "jaro-winkler": "^0.2.8",