Skip to content

Commit

Permalink
accept bounds from timeRange, timeRangeExpression and TimeBucketExpre…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
mcbrewster committed Sep 19, 2023
1 parent 24c122c commit 883c11f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/datatypes/timeRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ export class TimeRange extends Range<Date> implements Instance<TimeRangeValue, T
return dateToIntervalPart(date) + '/' + dateToIntervalPart(new Date(date.valueOf() + 1));
}

static timeBucket(date: Date, duration: Duration, timezone: Timezone): TimeRange {
static timeBucket(
date: Date,
duration: Duration,
timezone: Timezone,
bounds?: string,
): TimeRange {
if (!date) return null;
const start = duration.floor(date, timezone);
return new TimeRange({
start: start,
end: duration.shift(start, timezone, 1),
bounds: Range.DEFAULT_BOUNDS,
bounds: bounds ?? Range.DEFAULT_BOUNDS,
});
}

Expand Down Expand Up @@ -183,6 +188,12 @@ export class TimeRange extends Range<Date> implements Instance<TimeRangeValue, T
});
}

public changeBounds(bounds: string): TimeRange {
const value = this.toJS();
value.bounds = bounds;
return TimeRange.fromJS(value);
}

public shift(duration: Duration, timezone: Timezone, step?: number): TimeRange {
const { start, end, bounds } = this;
if (!start) return this;
Expand Down
16 changes: 14 additions & 2 deletions src/expressions/timeBucketExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ export class TimeBucketExpression extends ChainableExpression {
const value = ChainableExpression.jsToValue(parameters);
value.duration = Duration.fromJS(parameters.duration);
if (parameters.timezone) value.timezone = Timezone.fromJS(parameters.timezone);
if (parameters.bounds) value.bounds = parameters.bounds;
return new TimeBucketExpression(value);
}

public duration: Duration;
public timezone: Timezone;

public bounds: string;
constructor(parameters: ExpressionValue) {
super(parameters, dummyObject);
const duration = parameters.duration;
this.duration = duration;
this.timezone = parameters.timezone;
this.bounds = parameters.bounds;
this._ensureOp('timeBucket');
this._checkOperandTypes('TIME');
if (!(duration instanceof Duration)) {
Expand All @@ -55,13 +57,15 @@ export class TimeBucketExpression extends ChainableExpression {
public valueOf(): ExpressionValue {
const value = super.valueOf();
value.duration = this.duration;
value.bounds = this.bounds;
if (this.timezone) value.timezone = this.timezone;
return value;
}

public toJS(): ExpressionJS {
const js = super.toJS();
js.duration = this.duration.toJS();
if (this.bounds) js.bounds = this.bounds;
if (this.timezone) js.timezone = this.timezone.toJS();
return js;
}
Expand All @@ -70,26 +74,34 @@ export class TimeBucketExpression extends ChainableExpression {
return (
super.equals(other) &&
this.duration.equals(other.duration) &&
this.bounds === other.bounds &&
immutableEqual(this.timezone, other.timezone)
);
}

protected _toStringParameters(_indent?: int): string[] {
const ret = [this.duration.toString()];
if (this.timezone) ret.push(Expression.safeString(this.timezone.toString()));
if (this.bounds) ret.push(this.bounds);
return ret;
}

protected _calcChainableHelper(operandValue: any): PlywoodValue {
return operandValue
? TimeRange.timeBucket(operandValue, this.duration, this.getTimezone())
? TimeRange.timeBucket(operandValue, this.duration, this.getTimezone(), this.bounds)
: null;
}

protected _getSQLChainableHelper(dialect: SQLDialect, operandSQL: string): string {
return dialect.timeBucketExpression(operandSQL, this.duration, this.getTimezone());
}

public changeBounds(bounds: string): Expression {
const value = this.valueOf();
value.bounds = bounds;
return Expression.fromValue(value);
}

// HasTimezone mixin:
public getTimezone: () => Timezone;
public changeTimezone: (timezone: Timezone) => this;
Expand Down
9 changes: 7 additions & 2 deletions src/expressions/timeRangeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class TimeRangeExpression extends ChainableExpression implements HasTimez
const value = ChainableExpression.jsToValue(parameters);
value.duration = Duration.fromJS(parameters.duration);
value.step = parameters.step;
value.bounds = parameters.bounds;
if (parameters.timezone) value.timezone = Timezone.fromJS(parameters.timezone);
return new TimeRangeExpression(value);
}
Expand Down Expand Up @@ -114,11 +115,15 @@ export class TimeRangeExpression extends ChainableExpression implements HasTimez
throw new Error('implement me');
}

public changeBounds(bounds: string): Expression {
const value = this.valueOf();
value.bounds = bounds;
return Expression.fromValue(value);
}

// HasTimezone mixin:
public getTimezone: () => Timezone;
public changeTimezone: (timezone: Timezone) => TimeRangeExpression;
public getBounds: () => String;
public changeBounds: (bounds: String) => TimeRangeExpression;
}

Expression.applyMixins(TimeRangeExpression, [HasTimezone]);
Expand Down

0 comments on commit 883c11f

Please sign in to comment.