From f6f683350602f2645adfbf48d5f07dec00b91c92 Mon Sep 17 00:00:00 2001 From: Michael Toy <66150587+mtoy-googly-moogly@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:05:14 -0800 Subject: [PATCH] cleanup naming for isTemporal --- packages/malloy/src/doc/fielddef.md | 2 +- .../src/lang/ast/expressions/expr-time-extract.ts | 8 ++++---- packages/malloy/src/lang/ast/expressions/expr-time.ts | 4 ++-- .../malloy/src/lang/ast/expressions/time-literal.ts | 4 ++-- .../src/lang/ast/query-builders/reduce-builder.ts | 4 ++-- packages/malloy/src/lang/ast/types/expression-def.ts | 10 +++++----- packages/malloy/src/model/malloy_types.ts | 7 ++++--- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/malloy/src/doc/fielddef.md b/packages/malloy/src/doc/fielddef.md index d9f1d86a1..49b4c7a46 100644 --- a/packages/malloy/src/doc/fielddef.md +++ b/packages/malloy/src/doc/fielddef.md @@ -93,7 +93,7 @@ are an array of ... ## Discriminators -* `isTemporalField` -- `date` or `timestamp` type +* `isTemporalType` -- `date` or `timestamp` type * `isAtomicFieldType` -- Does type string match the type of one of the atomiv types * `isRepeatedRecord` -- In some databases this is a type, in other this is an array of record * `isScalarArray` -- Is a ".each" array diff --git a/packages/malloy/src/lang/ast/expressions/expr-time-extract.ts b/packages/malloy/src/lang/ast/expressions/expr-time-extract.ts index 0ad933293..947c3183c 100644 --- a/packages/malloy/src/lang/ast/expressions/expr-time-extract.ts +++ b/packages/malloy/src/lang/ast/expressions/expr-time-extract.ts @@ -24,7 +24,7 @@ import { ExtractUnit, isExtractUnit, - isTemporalField, + isTemporalType, isTimestampUnit, mkTemporal, TD, @@ -89,13 +89,13 @@ export class ExprTimeExtract extends ExpressionDef { from: [first, last], }); } - if (!isTemporalField(first.type)) { + if (!isTemporalType(first.type)) { return from.first.loggedErrorExpr( 'invalid-type-for-time-extraction', `Can't extract ${extractTo} from '${first.type}'` ); } - if (!isTemporalField(last.type)) { + if (!isTemporalType(last.type)) { return from.last.loggedErrorExpr( 'invalid-type-for-time-extraction', `Cannot extract ${extractTo} from '${last.type}'` @@ -151,7 +151,7 @@ export class ExprTimeExtract extends ExpressionDef { }); } else { const argV = from.getExpression(fs); - if (isTemporalField(argV.type)) { + if (isTemporalType(argV.type)) { return computedExprValue({ dataType: {type: 'number', numberType: 'integer'}, value: { diff --git a/packages/malloy/src/lang/ast/expressions/expr-time.ts b/packages/malloy/src/lang/ast/expressions/expr-time.ts index a965bcc2a..78d305c57 100644 --- a/packages/malloy/src/lang/ast/expressions/expr-time.ts +++ b/packages/malloy/src/lang/ast/expressions/expr-time.ts @@ -25,7 +25,7 @@ import { Expr, TemporalFieldType, TypecastExpr, - isTemporalField, + isTemporalType, } from '../../../model/malloy_types'; import {FieldSpace} from '../types/field-space'; @@ -58,7 +58,7 @@ export class ExprTime extends ExpressionDef { dstType: {type: timeType}, e: expr.value, }; - if (isTemporalField(expr.type)) { + if (isTemporalType(expr.type)) { toTs.srcType = {type: expr.type}; } value = toTs; diff --git a/packages/malloy/src/lang/ast/expressions/time-literal.ts b/packages/malloy/src/lang/ast/expressions/time-literal.ts index d8711ceaa..a064819dc 100644 --- a/packages/malloy/src/lang/ast/expressions/time-literal.ts +++ b/packages/malloy/src/lang/ast/expressions/time-literal.ts @@ -26,7 +26,7 @@ import {DateTime as LuxonDateTime} from 'luxon'; import { TemporalFieldType, TimestampUnit, - isTemporalField, + isTemporalType, TimeLiteralNode, } from '../../../model/malloy_types'; @@ -206,7 +206,7 @@ class GranularLiteral extends TimeLiteral { } // Compiler is unsure about rangeEnd = newEnd for some reason - if (rangeEnd && isTemporalField(testValue.type)) { + if (rangeEnd && isTemporalType(testValue.type)) { const rangeType = testValue.type; const range = new Range( new ExprTime(rangeType, rangeStart.value), diff --git a/packages/malloy/src/lang/ast/query-builders/reduce-builder.ts b/packages/malloy/src/lang/ast/query-builders/reduce-builder.ts index 6441ef8c9..4b6cb5cf9 100644 --- a/packages/malloy/src/lang/ast/query-builders/reduce-builder.ts +++ b/packages/malloy/src/lang/ast/query-builders/reduce-builder.ts @@ -36,7 +36,7 @@ import { isPartialSegment, isQuerySegment, isReduceSegment, - isTemporalField, + isTemporalType, } from '../../../model/malloy_types'; import {ErrorFactory} from '../error-factory'; @@ -215,7 +215,7 @@ export class ReduceBuilder extends QuerySegmentBuilder implements QueryBuilder { fieldAnalytic = hasExpression(field) && expressionIsAnalytic(field.expressionType); } - if (isTemporalField(fieldType) || fieldAggregate) { + if (isTemporalType(fieldType) || fieldAggregate) { reduceSegment.defaultOrderBy = true; reduceSegment.orderBy = [{field: fieldName, dir: 'desc'}]; usableDefaultOrderField = undefined; diff --git a/packages/malloy/src/lang/ast/types/expression-def.ts b/packages/malloy/src/lang/ast/types/expression-def.ts index e76f4853a..89346a553 100644 --- a/packages/malloy/src/lang/ast/types/expression-def.ts +++ b/packages/malloy/src/lang/ast/types/expression-def.ts @@ -25,7 +25,7 @@ import { Expr, TimestampUnit, isDateUnit, - isTemporalField, + isTemporalType, expressionIsAggregate, TD, LeafExpressionType, @@ -181,7 +181,7 @@ export class ExprDuration extends ExpressionDef { ): ExprValue { const lhs = left.getExpression(fs); this.typeCheck(this, lhs); - if (isTemporalField(lhs.type) && (op === '+' || op === '-')) { + if (isTemporalType(lhs.type) && (op === '+' || op === '-')) { const num = this.n.getExpression(fs); if (!TDU.typeEq(num, TDU.numberT)) { this.logError( @@ -253,8 +253,8 @@ function timeCompare( op: CompareMalloyOperator, rhs: ExprValue ): Expr | undefined { - const leftIsTime = isTemporalField(lhs.type); - const rightIsTime = isTemporalField(rhs.type); + const leftIsTime = isTemporalType(lhs.type); + const rightIsTime = isTemporalType(rhs.type); const node = getExprNode(op); if (leftIsTime && rightIsTime) { if (lhs.type !== rhs.type) { @@ -459,7 +459,7 @@ function delta( return noGo; } - const timeLHS = isTemporalField(lhs.type); + const timeLHS = isTemporalType(lhs.type); const err = errorCascade(timeLHS ? 'error' : 'number', lhs, rhs); if (err) return err; diff --git a/packages/malloy/src/model/malloy_types.ts b/packages/malloy/src/model/malloy_types.ts index 3613d5ead..5a71455c7 100644 --- a/packages/malloy/src/model/malloy_types.ts +++ b/packages/malloy/src/model/malloy_types.ts @@ -603,7 +603,7 @@ export function hasExpression( } export type TemporalFieldType = 'date' | 'timestamp'; -export function isTemporalField(s: string): s is TemporalFieldType { +export function isTemporalType(s: string): s is TemporalFieldType { return s === 'date' || s === 'timestamp'; } export type CastType = @@ -1341,7 +1341,7 @@ export function isLeafAtomic( ): fd is LeafAtomicDef { return ( fd.type === 'string' || - isTemporalField(fd.type) || + isTemporalType(fd.type) || fd.type === 'number' || fd.type === 'boolean' || fd.type === 'json' || @@ -1560,7 +1560,8 @@ export const TD = { isDate: (td: UTD): td is DateTypeDef => td?.type === 'date', isTimestamp: (td: UTD): td is TimestampTypeDef => td?.type === 'timestamp', isTemporal(td: UTD): td is TimestampTypeDef { - return td?.type === 'timestamp' || td?.type === 'date'; + const typ = td?.type ?? ''; + return isTemporalType(typ); }, isError: (td: UTD): td is ErrorTypeDef => td?.type === 'error', eq(x: UTD, y: UTD): boolean {