Skip to content

Commit

Permalink
Cleanup column_time_range
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Jan 15, 2025
1 parent 691a9c5 commit 896490e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 77 deletions.
6 changes: 3 additions & 3 deletions runtime/metricsview/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Executor struct {
olapRelease func()
instanceCfg drivers.InstanceConfig

min, max, watermark time.Time
timestamps TimestampsResult
watermark time.Time
timestamps TimestampsResult
}

type TimestampsResult struct {
Expand Down Expand Up @@ -136,7 +136,7 @@ func (e *Executor) ValidateQuery(qry *Query) error {

// Timestamps queries min, max and watermark for the metrics view
func (e *Executor) Timestamps(ctx context.Context, executionTime *time.Time) (TimestampsResult, error) {
if !e.min.IsZero() {
if !e.timestamps.Min.IsZero() {
return e.timestamps, nil
}

Expand Down
49 changes: 5 additions & 44 deletions runtime/queries/column_time_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,20 @@ func (q *ColumnTimeRange) Resolve(ctx context.Context, rt *runtime.Runtime, inst
}
defer release()

// TODO: Try and merge this with metrics_time_range. Both use same queries but metrics_time_range uses a specific timestamp column from metrics_view
switch olap.Dialect() {
case drivers.DialectDuckDB:
return q.resolveDuckDB(ctx, olap, priority)
case drivers.DialectDuckDB, drivers.DialectClickHouse:
return q.resolveDuckDBAndClickhouse(ctx, olap, priority)
case drivers.DialectDruid:
return q.resolveDruid(ctx, olap, priority)
case drivers.DialectClickHouse:
return q.resolveClickHouse(ctx, olap, priority)
default:
return fmt.Errorf("not available for dialect '%s'", olap.Dialect())
}
}

func (q *ColumnTimeRange) resolveDuckDB(ctx context.Context, olap drivers.OLAPStore, priority int) error {
func (q *ColumnTimeRange) resolveDuckDBAndClickhouse(ctx context.Context, olap drivers.OLAPStore, priority int) error {
rangeSQL := fmt.Sprintf(
"SELECT min(%[1]s) as \"min\", max(%[1]s) as \"max\", max(%[1]s) - min(%[1]s) as \"interval\" FROM %[2]s",
"SELECT min(%[1]s) as \"min\", max(%[1]s) as \"max\" FROM %[2]s",
safeName(q.ColumnName),
drivers.DialectDuckDB.EscapeTable(q.Database, q.DatabaseSchema, q.TableName),
)
Expand Down Expand Up @@ -202,44 +201,6 @@ func (q *ColumnTimeRange) resolveDruid(ctx context.Context, olap drivers.OLAPSto
return nil
}

func (q *ColumnTimeRange) resolveClickHouse(ctx context.Context, olap drivers.OLAPStore, priority int) error {
sql := fmt.Sprintf(
"SELECT min(%[1]s) as \"min\", max(%[1]s) as \"max\" FROM %[2]s",
safeName(q.ColumnName),
drivers.DialectClickHouse.EscapeTable(q.Database, q.DatabaseSchema, q.TableName),
)

rows, err := olap.Execute(ctx, &drivers.Statement{
Query: sql,
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return err
}
defer rows.Close()

var minTime, maxTime *time.Time
for rows.Next() {
err = rows.Scan(&minTime, &maxTime)
if err != nil {
return err
}
}

summary := &runtimev1.TimeRangeSummary{}
if minTime != nil {
summary.Min = timestamppb.New(*minTime)
}
if maxTime != nil {
summary.Max = timestamppb.New(*maxTime)
}

q.Result = summary

return nil
}

func (q *ColumnTimeRange) Export(ctx context.Context, rt *runtime.Runtime, instanceID string, w io.Writer, opts *runtime.ExportOptions) error {
return ErrExportNotSupported
}
21 changes: 0 additions & 21 deletions runtime/resolvers/metricsview_time_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ import (
"errors"
"fmt"
"io"
"time"

runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/metricsview"
"github.com/rilldata/rill/runtime/pkg/mapstructureutil"
)

const (
hourInDay = 24
)

var microsInDay = hourInDay * time.Hour.Microseconds()

func init() {
runtime.RegisterResolverInitializer("metrics_time_range", newMetricsViewTimeRangeResolver)
}
Expand Down Expand Up @@ -133,17 +126,3 @@ func (r *metricsViewTimeRangeResolver) ResolveInteractive(ctx context.Context) (
func (r *metricsViewTimeRangeResolver) ResolveExport(ctx context.Context, w io.Writer, opts *runtime.ResolverExportOptions) error {
return errors.New("not implemented")
}

func durationToInterval(duration time.Duration) map[string]any {
hours := duration.Hours()
days := int32(0)
if hours >= hourInDay {
days = int32(hours / hourInDay)
}
micros := duration.Microseconds() - microsInDay*int64(days)
return map[string]any{
"days": days,
"months": 0,
"micros": micros,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe("toProto/fromProto", () => {
timeRangeSummary: {
min: TestTimeConstants.LAST_DAY.toISOString(),
max: TestTimeConstants.NOW.toISOString(),
interval: V1TimeGrain.TIME_GRAIN_MINUTE as any,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ import {
} from "@rilldata/web-common/features/dashboards/stores/test-data/helpers";
import { createValidSpecQueryMock } from "@rilldata/web-common/features/dashboards/stores/test-data/query-mocks";
import { initLocalUserPreferenceStore } from "@rilldata/web-common/features/dashboards/user-preferences";
import {
V1ExploreComparisonMode,
V1TimeGrain,
} from "@rilldata/web-common/runtime-client";
import { V1ExploreComparisonMode } from "@rilldata/web-common/runtime-client";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store";
import { get } from "svelte/store";
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
Expand Down Expand Up @@ -237,7 +234,6 @@ describe("dashboard-stores", () => {
timeRangeSummary: {
min: TestTimeConstants.LAST_DAY.toISOString(),
max: TestTimeConstants.NOW.toISOString(),
interval: V1TimeGrain.TIME_GRAIN_MINUTE as any,
},
},
),
Expand Down Expand Up @@ -266,7 +262,6 @@ describe("dashboard-stores", () => {
timeRangeSummary: {
min: TestTimeConstants.LAST_DAY.toISOString(),
max: TestTimeConstants.NOW.toISOString(),
interval: V1TimeGrain.TIME_GRAIN_MINUTE as any,
},
},
),
Expand Down Expand Up @@ -296,7 +291,6 @@ describe("dashboard-stores", () => {
timeRangeSummary: {
min: TestTimeConstants.LAST_DAY.toISOString(),
max: TestTimeConstants.NOW.toISOString(),
interval: V1TimeGrain.TIME_GRAIN_MINUTE as any,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export const AD_BIDS_TIME_RANGE_SUMMARY: V1MetricsViewTimeRangeResponse = {
timeRangeSummary: {
min: TestTimeConstants.LAST_DAY.toISOString(),
max: TestTimeConstants.NOW.toISOString(),
interval: V1TimeGrain.TIME_GRAIN_MINUTE as any,
},
};

Expand Down

0 comments on commit 896490e

Please sign in to comment.