@malloydata/malloy 0.0.24-dev230302224355 → 0.0.24-dev230305212748
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.
- package/dist/lang/ast/expressions/for-range.d.ts +1 -5
- package/dist/lang/ast/expressions/for-range.js +6 -11
- package/dist/lang/ast/expressions/range.d.ts +1 -1
- package/dist/lang/ast/expressions/range.js +1 -0
- package/dist/lang/ast/expressions/time-literal.d.ts +77 -0
- package/dist/lang/ast/expressions/time-literal.js +316 -0
- package/dist/lang/ast/index.d.ts +1 -1
- package/dist/lang/ast/index.js +1 -1
- package/dist/lang/ast/time-utils.d.ts +1 -3
- package/dist/lang/ast/time-utils.js +1 -12
- package/dist/lang/ast/types/expr-result.d.ts +3 -0
- package/dist/lang/ast/types/expr-value.d.ts +2 -2
- package/dist/lang/ast/types/granular-result.js +3 -3
- package/dist/lang/ast/types/time-result.d.ts +2 -2
- package/dist/lang/lib/Malloy/MalloyLexer.d.ts +20 -19
- package/dist/lang/lib/Malloy/MalloyLexer.js +983 -957
- package/dist/lang/lib/Malloy/MalloyParser.d.ts +27 -19
- package/dist/lang/lib/Malloy/MalloyParser.js +574 -530
- package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +13 -0
- package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +8 -0
- package/dist/lang/malloy-to-ast.d.ts +6 -1
- package/dist/lang/malloy-to-ast.js +22 -14
- package/dist/lang/test/parse.spec.js +121 -18
- package/dist/model/malloy_types.d.ts +1 -0
- package/package.json +1 -1
- package/dist/lang/ast/expressions/granular-literal.d.ts +0 -20
- package/dist/lang/ast/expressions/granular-literal.js +0 -148
|
@@ -9,6 +9,7 @@ import { TableSourceContext } from "./MalloyParser";
|
|
|
9
9
|
import { QuerySourceContext } from "./MalloyParser";
|
|
10
10
|
import { SQLSourceNameContext } from "./MalloyParser";
|
|
11
11
|
import { LiteralTimestampContext } from "./MalloyParser";
|
|
12
|
+
import { LiteralHourContext } from "./MalloyParser";
|
|
12
13
|
import { LiteralDayContext } from "./MalloyParser";
|
|
13
14
|
import { LiteralWeekContext } from "./MalloyParser";
|
|
14
15
|
import { LiteralMonthContext } from "./MalloyParser";
|
|
@@ -295,6 +296,18 @@ export interface MalloyParserListener extends ParseTreeListener {
|
|
|
295
296
|
* @param ctx the parse tree
|
|
296
297
|
*/
|
|
297
298
|
exitLiteralTimestamp?: (ctx: LiteralTimestampContext) => void;
|
|
299
|
+
/**
|
|
300
|
+
* Enter a parse tree produced by the `literalHour`
|
|
301
|
+
* labeled alternative in `MalloyParser.dateLiteral`.
|
|
302
|
+
* @param ctx the parse tree
|
|
303
|
+
*/
|
|
304
|
+
enterLiteralHour?: (ctx: LiteralHourContext) => void;
|
|
305
|
+
/**
|
|
306
|
+
* Exit a parse tree produced by the `literalHour`
|
|
307
|
+
* labeled alternative in `MalloyParser.dateLiteral`.
|
|
308
|
+
* @param ctx the parse tree
|
|
309
|
+
*/
|
|
310
|
+
exitLiteralHour?: (ctx: LiteralHourContext) => void;
|
|
298
311
|
/**
|
|
299
312
|
* Enter a parse tree produced by the `literalDay`
|
|
300
313
|
* labeled alternative in `MalloyParser.dateLiteral`.
|
|
@@ -9,6 +9,7 @@ import { TableSourceContext } from "./MalloyParser";
|
|
|
9
9
|
import { QuerySourceContext } from "./MalloyParser";
|
|
10
10
|
import { SQLSourceNameContext } from "./MalloyParser";
|
|
11
11
|
import { LiteralTimestampContext } from "./MalloyParser";
|
|
12
|
+
import { LiteralHourContext } from "./MalloyParser";
|
|
12
13
|
import { LiteralDayContext } from "./MalloyParser";
|
|
13
14
|
import { LiteralWeekContext } from "./MalloyParser";
|
|
14
15
|
import { LiteralMonthContext } from "./MalloyParser";
|
|
@@ -248,6 +249,13 @@ export interface MalloyParserVisitor<Result> extends ParseTreeVisitor<Result> {
|
|
|
248
249
|
* @return the visitor result
|
|
249
250
|
*/
|
|
250
251
|
visitLiteralTimestamp?: (ctx: LiteralTimestampContext) => Result;
|
|
252
|
+
/**
|
|
253
|
+
* Visit a parse tree produced by the `literalHour`
|
|
254
|
+
* labeled alternative in `MalloyParser.dateLiteral`.
|
|
255
|
+
* @param ctx the parse tree
|
|
256
|
+
* @return the visitor result
|
|
257
|
+
*/
|
|
258
|
+
visitLiteralHour?: (ctx: LiteralHourContext) => Result;
|
|
251
259
|
/**
|
|
252
260
|
* Visit a parse tree produced by the `literalDay`
|
|
253
261
|
* labeled alternative in `MalloyParser.dateLiteral`.
|
|
@@ -53,6 +53,11 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
|
|
|
53
53
|
protected getFilterShortcut(cx: parse.FilterShortcutContext): ast.Filter;
|
|
54
54
|
protected getExploreSource(pcx: parse.ExploreSourceContext): ast.Source;
|
|
55
55
|
protected makeSqlString(pcx: parse.SqlStringContext, sqlStr: ast.SQLString): void;
|
|
56
|
+
/**
|
|
57
|
+
* Parse a time string into an AST TimeLiteral, if the string fails
|
|
58
|
+
* the parse, make sure it highlights properly
|
|
59
|
+
*/
|
|
60
|
+
protected parseTime(pcx: ParserRuleContext, parse: (s: string) => ast.ExpressionDef | undefined): ast.ExpressionDef;
|
|
56
61
|
visitMalloyDocument(pcx: parse.MalloyDocumentContext): ast.Document;
|
|
57
62
|
visitDefineSourceStatement(pcx: parse.DefineSourceStatementContext): ast.DefineSourceList | ast.DefineSource;
|
|
58
63
|
visitsourceDefinition(pcx: parse.SourceDefinitionContext): ast.DefineSource;
|
|
@@ -141,8 +146,8 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
|
|
|
141
146
|
visitPickStatement(pcx: parse.PickStatementContext): ast.Pick;
|
|
142
147
|
visitNamedSource(pcx: parse.NamedSourceContext): ast.NamedSource;
|
|
143
148
|
visitExprFilter(pcx: parse.ExprFilterContext): ast.ExprFilter;
|
|
144
|
-
protected getLiteralTime(cx: ParserRuleContext): ast.ExpressionDef;
|
|
145
149
|
visitLiteralTimestamp(pcx: parse.LiteralTimestampContext): ast.ExpressionDef;
|
|
150
|
+
visitLiteralHour(pcx: parse.LiteralHourContext): ast.ExpressionDef;
|
|
146
151
|
visitLiteralDay(pcx: parse.LiteralDayContext): ast.ExpressionDef;
|
|
147
152
|
visitLiteralWeek(pcx: parse.LiteralWeekContext): ast.ExpressionDef;
|
|
148
153
|
visitLiteralMonth(pcx: parse.LiteralMonthContext): ast.ExpressionDef;
|
|
@@ -259,6 +259,19 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
|
|
|
259
259
|
sqlStr.push(lastChars || '');
|
|
260
260
|
this.astAt(sqlStr, pcx);
|
|
261
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Parse a time string into an AST TimeLiteral, if the string fails
|
|
264
|
+
* the parse, make sure it highlights properly
|
|
265
|
+
*/
|
|
266
|
+
parseTime(pcx, parse) {
|
|
267
|
+
let def = parse(pcx.text);
|
|
268
|
+
if (!def) {
|
|
269
|
+
this.contextError(pcx, 'Time data parse error');
|
|
270
|
+
// return a value node so the parse can continue
|
|
271
|
+
def = new ast.LiteralTimestamp({ text: pcx.text });
|
|
272
|
+
}
|
|
273
|
+
return this.astAt(def, pcx);
|
|
274
|
+
}
|
|
262
275
|
visitMalloyDocument(pcx) {
|
|
263
276
|
const stmts = this.onlyDocStatements(pcx.malloyStatement().map(scx => this.visit(scx)));
|
|
264
277
|
return new ast.Document(stmts);
|
|
@@ -912,31 +925,26 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
|
|
|
912
925
|
const filters = this.visit(pcx.filteredBy());
|
|
913
926
|
return new ast.ExprFilter(this.getFieldExpr(pcx.fieldExpr()), filters);
|
|
914
927
|
}
|
|
915
|
-
getLiteralTime(cx) {
|
|
916
|
-
const parsed = ast.GranularLiteral.parse(cx.text);
|
|
917
|
-
if (parsed === undefined) {
|
|
918
|
-
this.contextError(cx, `${cx.text} is not a legal day specification`);
|
|
919
|
-
return new ast.ExprNow();
|
|
920
|
-
}
|
|
921
|
-
return parsed;
|
|
922
|
-
}
|
|
923
928
|
visitLiteralTimestamp(pcx) {
|
|
924
|
-
return this.
|
|
929
|
+
return this.parseTime(pcx, ast.LiteralTimestamp.parse);
|
|
930
|
+
}
|
|
931
|
+
visitLiteralHour(pcx) {
|
|
932
|
+
return this.parseTime(pcx, ast.LiteralHour.parse);
|
|
925
933
|
}
|
|
926
934
|
visitLiteralDay(pcx) {
|
|
927
|
-
return this.
|
|
935
|
+
return this.parseTime(pcx, ast.LiteralDay.parse);
|
|
928
936
|
}
|
|
929
937
|
visitLiteralWeek(pcx) {
|
|
930
|
-
return this.
|
|
938
|
+
return this.parseTime(pcx, ast.LiteralWeek.parse);
|
|
931
939
|
}
|
|
932
940
|
visitLiteralMonth(pcx) {
|
|
933
|
-
return this.
|
|
941
|
+
return this.parseTime(pcx, ast.LiteralMonth.parse);
|
|
934
942
|
}
|
|
935
943
|
visitLiteralQuarter(pcx) {
|
|
936
|
-
return this.
|
|
944
|
+
return this.parseTime(pcx, ast.LiteralQuarter.parse);
|
|
937
945
|
}
|
|
938
946
|
visitLiteralYear(pcx) {
|
|
939
|
-
return this.
|
|
947
|
+
return this.parseTime(pcx, ast.LiteralYear.parse);
|
|
940
948
|
}
|
|
941
949
|
visitImportStatement(pcx) {
|
|
942
950
|
const url = this.stripQuotes(pcx.importURL().text);
|
|
@@ -33,6 +33,7 @@ const expression_def_1 = require("../ast/types/expression-def");
|
|
|
33
33
|
const test_translator_1 = require("./test-translator");
|
|
34
34
|
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
|
35
35
|
const util_1 = require("util");
|
|
36
|
+
const granular_result_1 = require("../ast/types/granular-result");
|
|
36
37
|
const inspectCompile = false;
|
|
37
38
|
/*
|
|
38
39
|
* Thinking of these tests as just "do things parse", there should maybe
|
|
@@ -306,6 +307,7 @@ class BetaExpression extends Testable {
|
|
|
306
307
|
if (aStruct.type === 'struct') {
|
|
307
308
|
const tstFS = new static_space_1.StaticSpace(aStruct);
|
|
308
309
|
const exprDef = exprAst.getExpression(tstFS);
|
|
310
|
+
this.compiled = exprDef;
|
|
309
311
|
if (inspectCompile) {
|
|
310
312
|
console.log('EXPRESSION: ', (0, test_translator_1.pretty)(exprDef));
|
|
311
313
|
}
|
|
@@ -322,6 +324,12 @@ class BetaExpression extends Testable {
|
|
|
322
324
|
throw new Error(`Not an expression: ${whatIsIt}`);
|
|
323
325
|
}
|
|
324
326
|
}
|
|
327
|
+
generated() {
|
|
328
|
+
if (!this.compiled) {
|
|
329
|
+
throw new Error('Must compile expression before fetching generated code');
|
|
330
|
+
}
|
|
331
|
+
return this.compiled;
|
|
332
|
+
}
|
|
325
333
|
}
|
|
326
334
|
function exprOK(s) {
|
|
327
335
|
return () => {
|
|
@@ -902,25 +910,120 @@ describe('qops', () => {
|
|
|
902
910
|
}
|
|
903
911
|
});
|
|
904
912
|
});
|
|
905
|
-
describe('
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
913
|
+
describe('literals', () => {
|
|
914
|
+
test('integer', exprOK('42'));
|
|
915
|
+
test('string', exprOK("'fortywo-two'"));
|
|
916
|
+
test('string with quoted quote', exprOK("'Isn" + '\\' + "'t this nice'"));
|
|
917
|
+
test('string with quoted backslash', exprOK("'Is " + '\\' + '\\' + " nice'"));
|
|
918
|
+
const literalTimes = [
|
|
919
|
+
['@1960', 'date', 'year', { literal: '1960-01-01' }],
|
|
920
|
+
['@1960-Q2', 'date', 'quarter', { literal: '1960-04-01' }],
|
|
921
|
+
['@1960-06', 'date', 'month', { literal: '1960-06-01' }],
|
|
922
|
+
['@1960-06-26-WK', 'date', 'week', { literal: '1960-06-26' }],
|
|
923
|
+
['@1960-06-30-WK', 'date', 'week', { literal: '1960-06-26' }],
|
|
924
|
+
['@1960-06-30', 'date', 'day', { literal: '1960-06-30' }],
|
|
925
|
+
['@1960-06-30 10', 'timestamp', 'hour', { literal: '1960-06-30 10:00:00' }],
|
|
926
|
+
[
|
|
927
|
+
'@1960-06-30 10:30',
|
|
928
|
+
'timestamp',
|
|
929
|
+
'minute',
|
|
930
|
+
{ literal: '1960-06-30 10:30:00' },
|
|
931
|
+
],
|
|
932
|
+
[
|
|
933
|
+
'@1960-06-30 10:30:00',
|
|
934
|
+
'timestamp',
|
|
935
|
+
undefined,
|
|
936
|
+
{ literal: '1960-06-30 10:30:00' },
|
|
937
|
+
],
|
|
938
|
+
[
|
|
939
|
+
'@1960-06-30 10:30:00.123',
|
|
940
|
+
'timestamp',
|
|
941
|
+
undefined,
|
|
942
|
+
{ literal: '1960-06-30 10:30:00.123' },
|
|
943
|
+
],
|
|
944
|
+
[
|
|
945
|
+
'@1960-06-30T10:30:00',
|
|
946
|
+
'timestamp',
|
|
947
|
+
undefined,
|
|
948
|
+
{ literal: '1960-06-30 10:30:00' },
|
|
949
|
+
],
|
|
950
|
+
[
|
|
951
|
+
'@1960-06-30 10:30:00+0',
|
|
952
|
+
'timestamp',
|
|
953
|
+
undefined,
|
|
954
|
+
{ literal: '1960-06-30 10:30:00', timezone: '+0' },
|
|
955
|
+
],
|
|
956
|
+
[
|
|
957
|
+
'@1960-06-30 10:30:00-7',
|
|
958
|
+
'timestamp',
|
|
959
|
+
undefined,
|
|
960
|
+
{ literal: '1960-06-30 10:30:00', timezone: '-7' },
|
|
961
|
+
],
|
|
962
|
+
[
|
|
963
|
+
'@1960-06-30 10:30:00-00:15',
|
|
964
|
+
'timestamp',
|
|
965
|
+
undefined,
|
|
966
|
+
{ literal: '1960-06-30 10:30:00', timezone: '-00:15' },
|
|
967
|
+
],
|
|
968
|
+
[
|
|
969
|
+
'@1960-06-30 10:30:00[America/Los_Angeles]',
|
|
970
|
+
'timestamp',
|
|
971
|
+
undefined,
|
|
972
|
+
{
|
|
973
|
+
literal: '1960-06-30 10:30:00',
|
|
974
|
+
timezone: 'America/Los_Angeles',
|
|
975
|
+
tzIsLocale: true,
|
|
976
|
+
},
|
|
977
|
+
],
|
|
978
|
+
];
|
|
979
|
+
test.each(literalTimes)('%s', (expr, timeType, timeframe, result) => {
|
|
980
|
+
const exprModel = new BetaExpression(expr);
|
|
981
|
+
expect(exprModel).modelCompiled();
|
|
982
|
+
const ir = exprModel.generated();
|
|
983
|
+
expect(ir.dataType).toEqual(timeType);
|
|
984
|
+
if (timeframe) {
|
|
985
|
+
expect((0, granular_result_1.isGranularResult)(ir)).toBeTruthy();
|
|
986
|
+
if ((0, granular_result_1.isGranularResult)(ir)) {
|
|
987
|
+
expect(ir.timeframe).toEqual(timeframe);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
else {
|
|
991
|
+
expect((0, granular_result_1.isGranularResult)(ir)).toBeFalsy();
|
|
992
|
+
}
|
|
993
|
+
expect(ir.value[0]).toEqual(expect.objectContaining(result));
|
|
994
|
+
});
|
|
995
|
+
const morphicLiterals = [
|
|
996
|
+
['@1960', '1960-01-01 00:00:00'],
|
|
997
|
+
['@1960-Q2', '1960-04-01 00:00:00'],
|
|
998
|
+
['@1960-06', '1960-06-01 00:00:00'],
|
|
999
|
+
['@1960-06-30-Wk', '1960-06-26 00:00:00'],
|
|
1000
|
+
['@1960-06-30', '1960-06-30 00:00:00'],
|
|
1001
|
+
['@1960-06-30 00:00', undefined],
|
|
1002
|
+
];
|
|
1003
|
+
test.each(morphicLiterals)('morphic value for %s is %s', (expr, morphic) => {
|
|
1004
|
+
const exprModel = new BetaExpression(expr);
|
|
1005
|
+
expect(exprModel).modelCompiled();
|
|
1006
|
+
const ir = exprModel.generated();
|
|
1007
|
+
const morphTo = ir.morphic && ir.morphic['timestamp'];
|
|
1008
|
+
if (morphic) {
|
|
1009
|
+
expect(morphTo).toBeDefined();
|
|
1010
|
+
if (morphTo) {
|
|
1011
|
+
expect(morphTo[0]).toEqual(expect.objectContaining({ literal: morphic }));
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
expect(morphTo).toBeUndefined();
|
|
1016
|
+
}
|
|
923
1017
|
});
|
|
1018
|
+
test('minute+locale', exprOK('@1960-06-30 10:30[America/Los_Angeles]'));
|
|
1019
|
+
test('second 8601', exprOK('@1960-06-30T10:30:31'));
|
|
1020
|
+
test('null', exprOK('null'));
|
|
1021
|
+
test('now', exprOK('now'));
|
|
1022
|
+
test('true', exprOK('true'));
|
|
1023
|
+
test('false', exprOK('false'));
|
|
1024
|
+
test('regex', exprOK("r'RegularExpression'"));
|
|
1025
|
+
});
|
|
1026
|
+
describe('expressions', () => {
|
|
924
1027
|
describe('timeframes', () => {
|
|
925
1028
|
const timeframes = [
|
|
926
1029
|
'second',
|
|
@@ -197,6 +197,7 @@ export interface TimeLiteralFragment extends DialectFragmentBase {
|
|
|
197
197
|
literal: string;
|
|
198
198
|
literalType: TimeFieldType;
|
|
199
199
|
timezone: string;
|
|
200
|
+
tzIsLocale?: boolean;
|
|
200
201
|
}
|
|
201
202
|
export interface StringLiteralFragment extends DialectFragmentBase {
|
|
202
203
|
function: 'stringLiteral';
|
package/package.json
CHANGED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { TimeFieldType, TimestampUnit } from '../../../model/malloy_types';
|
|
2
|
-
import { ExprValue } from '../types/expr-value';
|
|
3
|
-
import { FieldSpace } from '../types/field-space';
|
|
4
|
-
import { ExpressionDef } from '../types/expression-def';
|
|
5
|
-
/**
|
|
6
|
-
* GranularTime made from a literal. Funky because it doesn't know if it
|
|
7
|
-
* is a date or a timestamp in many cases until it is applied.
|
|
8
|
-
*/
|
|
9
|
-
export declare class GranularLiteral extends ExpressionDef {
|
|
10
|
-
readonly moment: string;
|
|
11
|
-
readonly until: string;
|
|
12
|
-
readonly units: TimestampUnit;
|
|
13
|
-
elementType: string;
|
|
14
|
-
timeType?: TimeFieldType;
|
|
15
|
-
constructor(moment: string, until: string, units: TimestampUnit);
|
|
16
|
-
granular(): boolean;
|
|
17
|
-
static parse(possibleLiteral: string): GranularLiteral | undefined;
|
|
18
|
-
apply(fs: FieldSpace, op: string, left: ExpressionDef): ExprValue;
|
|
19
|
-
getExpression(_fs: FieldSpace): ExprValue;
|
|
20
|
-
}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2023 Google LLC
|
|
4
|
-
*
|
|
5
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
-
* a copy of this software and associated documentation files
|
|
7
|
-
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
-
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
-
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
-
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
-
* subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be
|
|
14
|
-
* included in all copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.GranularLiteral = void 0;
|
|
26
|
-
const luxon_1 = require("luxon");
|
|
27
|
-
const malloy_types_1 = require("../../../model/malloy_types");
|
|
28
|
-
const range_1 = require("./range");
|
|
29
|
-
const expr_time_1 = require("./expr-time");
|
|
30
|
-
const time_utils_1 = require("../time-utils");
|
|
31
|
-
const expression_def_1 = require("../types/expression-def");
|
|
32
|
-
/**
|
|
33
|
-
* GranularTime made from a literal. Funky because it doesn't know if it
|
|
34
|
-
* is a date or a timestamp in many cases until it is applied.
|
|
35
|
-
*/
|
|
36
|
-
class GranularLiteral extends expression_def_1.ExpressionDef {
|
|
37
|
-
constructor(moment, until, units) {
|
|
38
|
-
super();
|
|
39
|
-
this.moment = moment;
|
|
40
|
-
this.until = until;
|
|
41
|
-
this.units = units;
|
|
42
|
-
this.elementType = 'timeLiteral';
|
|
43
|
-
}
|
|
44
|
-
granular() {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
static parse(possibleLiteral) {
|
|
48
|
-
const s = possibleLiteral.slice(1);
|
|
49
|
-
const fYear = 'yyyy';
|
|
50
|
-
const fMonth = `${fYear}-LL`;
|
|
51
|
-
const fDay = `${fMonth}-dd`;
|
|
52
|
-
const fMinute = `${fDay} HH:mm`;
|
|
53
|
-
const fSecond = `${fMinute}:ss`;
|
|
54
|
-
const tss = luxon_1.DateTime.fromFormat(s, fSecond);
|
|
55
|
-
if (tss.isValid) {
|
|
56
|
-
const nextSecond = tss.plus({ second: 1 }).toFormat(fSecond);
|
|
57
|
-
const tsLit = new GranularLiteral(s, nextSecond, 'second');
|
|
58
|
-
tsLit.timeType = 'timestamp';
|
|
59
|
-
return tsLit;
|
|
60
|
-
}
|
|
61
|
-
const tsm = luxon_1.DateTime.fromFormat(s, fMinute);
|
|
62
|
-
if (tsm.isValid) {
|
|
63
|
-
// working around a weird bigquery bug ...
|
|
64
|
-
const thisMin = s + ':00';
|
|
65
|
-
const nextMinute = tsm.plus({ minute: 1 }).toFormat(fMinute) + ':00';
|
|
66
|
-
const tsLit = new GranularLiteral(thisMin, nextMinute, 'minute');
|
|
67
|
-
tsLit.timeType = 'timestamp';
|
|
68
|
-
return tsLit;
|
|
69
|
-
}
|
|
70
|
-
const quarter = s.match(/(\d{4})-[qQ](\d)$/);
|
|
71
|
-
if (quarter) {
|
|
72
|
-
const qplus = Number.parseInt(quarter[2]) - 1;
|
|
73
|
-
let qstart = luxon_1.DateTime.fromFormat(quarter[1], 'yyyy');
|
|
74
|
-
if (qplus > 0) {
|
|
75
|
-
qstart = qstart.plus({ quarters: qplus });
|
|
76
|
-
}
|
|
77
|
-
const qend = qstart.plus({ quarter: 1 });
|
|
78
|
-
return new GranularLiteral(`${qstart.toFormat(fDay)}`, `${qend.toFormat(fDay)}`, 'quarter');
|
|
79
|
-
}
|
|
80
|
-
const yyyymmdd = luxon_1.DateTime.fromFormat(s, fDay);
|
|
81
|
-
if (yyyymmdd.isValid) {
|
|
82
|
-
const next = yyyymmdd.plus({ days: 1 });
|
|
83
|
-
return new GranularLiteral(`${yyyymmdd.toFormat(fDay)}`, `${next.toFormat(fDay)}`, 'day');
|
|
84
|
-
}
|
|
85
|
-
const yyyymm = luxon_1.DateTime.fromFormat(s, fMonth);
|
|
86
|
-
if (yyyymm.isValid) {
|
|
87
|
-
const next = yyyymm.plus({ months: 1 });
|
|
88
|
-
return new GranularLiteral(`${yyyymm.toFormat(fDay)}`, `${next.toFormat(fDay)}`, 'month');
|
|
89
|
-
}
|
|
90
|
-
const yyyy = luxon_1.DateTime.fromFormat(s, fYear);
|
|
91
|
-
if (yyyy.isValid) {
|
|
92
|
-
const year = yyyy.toFormat('yyyy-01-01');
|
|
93
|
-
const nextYear = yyyy.plus({ year: 1 }).toFormat('yyyy-01-01');
|
|
94
|
-
return new GranularLiteral(year, nextYear, 'year');
|
|
95
|
-
}
|
|
96
|
-
if (s.startsWith('WK')) {
|
|
97
|
-
const yyyymmdd = luxon_1.DateTime.fromFormat(s.slice(2), fDay);
|
|
98
|
-
if (yyyymmdd.isValid) {
|
|
99
|
-
// wonky because luxon uses monday weeks and bigquery uses sunday weeks
|
|
100
|
-
let sunday = yyyymmdd;
|
|
101
|
-
if (yyyymmdd.weekday !== 7) {
|
|
102
|
-
sunday = yyyymmdd.startOf('week').minus({ day: 1 });
|
|
103
|
-
}
|
|
104
|
-
const next = sunday.plus({ days: 7 });
|
|
105
|
-
return new GranularLiteral(`${sunday.toFormat(fDay)}`, `${next.toFormat(fDay)}`, 'week');
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return undefined;
|
|
109
|
-
}
|
|
110
|
-
apply(fs, op, left) {
|
|
111
|
-
const lhs = left.getExpression(fs);
|
|
112
|
-
if ((0, malloy_types_1.isTimeFieldType)(lhs.dataType)) {
|
|
113
|
-
let rangeType = 'timestamp';
|
|
114
|
-
if (lhs.dataType === 'date' && !this.timeType) {
|
|
115
|
-
rangeType = 'date';
|
|
116
|
-
}
|
|
117
|
-
const range = new range_1.Range(new expr_time_1.ExprTime(rangeType, (0, time_utils_1.timeLiteral)(this.moment, rangeType, 'UTC')), new expr_time_1.ExprTime(rangeType, (0, time_utils_1.timeLiteral)(this.until, rangeType, 'UTC')));
|
|
118
|
-
return range.apply(fs, op, left);
|
|
119
|
-
}
|
|
120
|
-
return super.apply(fs, op, left);
|
|
121
|
-
}
|
|
122
|
-
getExpression(_fs) {
|
|
123
|
-
const dataType = this.timeType || 'date';
|
|
124
|
-
const value = {
|
|
125
|
-
dataType,
|
|
126
|
-
expressionType: 'scalar',
|
|
127
|
-
value: (0, time_utils_1.timeLiteral)(this.moment, dataType, 'UTC'),
|
|
128
|
-
};
|
|
129
|
-
// Literals with date resolution can be used as timestamps or dates,
|
|
130
|
-
// this is the third attempt to make that work. It still feels like
|
|
131
|
-
// there should be a better way to make this happen, but the point
|
|
132
|
-
// at which the data is needed, the handle is gone to the ExpressionDef
|
|
133
|
-
// which would allow a method call into this class. I think the second
|
|
134
|
-
// if clause is redundant (see "parse" above, but I'm paranoid)
|
|
135
|
-
if (dataType === 'date' && (0, malloy_types_1.isDateUnit)(this.units)) {
|
|
136
|
-
value.alsoTimestamp = true;
|
|
137
|
-
}
|
|
138
|
-
if (this.units !== 'second') {
|
|
139
|
-
return {
|
|
140
|
-
...value,
|
|
141
|
-
timeframe: this.units,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
return value;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
exports.GranularLiteral = GranularLiteral;
|
|
148
|
-
//# sourceMappingURL=granular-literal.js.map
|