From 896490e44ef84a17ca35c2198cf72fa0406cfdd9 Mon Sep 17 00:00:00 2001 From: Aditya Hegde Date: Wed, 15 Jan 2025 18:29:59 +0530 Subject: [PATCH] Cleanup column_time_range --- runtime/metricsview/executor.go | 6 +-- runtime/queries/column_time_range.go | 49 ++----------------- runtime/resolvers/metricsview_time_range.go | 21 -------- .../dashboards/proto-state/proto.spec.ts | 1 - .../stores/dashboard-stores.spec.ts | 8 +-- .../dashboards/stores/test-data/data.ts | 1 - 6 files changed, 9 insertions(+), 77 deletions(-) diff --git a/runtime/metricsview/executor.go b/runtime/metricsview/executor.go index 83bf5f88476..e4797af16f6 100644 --- a/runtime/metricsview/executor.go +++ b/runtime/metricsview/executor.go @@ -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 { @@ -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 } diff --git a/runtime/queries/column_time_range.go b/runtime/queries/column_time_range.go index 687e0a078be..dbdb17947c8 100644 --- a/runtime/queries/column_time_range.go +++ b/runtime/queries/column_time_range.go @@ -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), ) @@ -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 } diff --git a/runtime/resolvers/metricsview_time_range.go b/runtime/resolvers/metricsview_time_range.go index 2e66805594f..3486108fb1c 100644 --- a/runtime/resolvers/metricsview_time_range.go +++ b/runtime/resolvers/metricsview_time_range.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "time" runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1" "github.com/rilldata/rill/runtime" @@ -13,12 +12,6 @@ import ( "github.com/rilldata/rill/runtime/pkg/mapstructureutil" ) -const ( - hourInDay = 24 -) - -var microsInDay = hourInDay * time.Hour.Microseconds() - func init() { runtime.RegisterResolverInitializer("metrics_time_range", newMetricsViewTimeRangeResolver) } @@ -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, - } -} diff --git a/web-common/src/features/dashboards/proto-state/proto.spec.ts b/web-common/src/features/dashboards/proto-state/proto.spec.ts index 6f4ed6190ea..af486d98bf6 100644 --- a/web-common/src/features/dashboards/proto-state/proto.spec.ts +++ b/web-common/src/features/dashboards/proto-state/proto.spec.ts @@ -49,7 +49,6 @@ describe("toProto/fromProto", () => { timeRangeSummary: { min: TestTimeConstants.LAST_DAY.toISOString(), max: TestTimeConstants.NOW.toISOString(), - interval: V1TimeGrain.TIME_GRAIN_MINUTE as any, }, }, ), diff --git a/web-common/src/features/dashboards/stores/dashboard-stores.spec.ts b/web-common/src/features/dashboards/stores/dashboard-stores.spec.ts index 4e987bf03b7..78fc6494c15 100644 --- a/web-common/src/features/dashboards/stores/dashboard-stores.spec.ts +++ b/web-common/src/features/dashboards/stores/dashboard-stores.spec.ts @@ -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"; @@ -237,7 +234,6 @@ describe("dashboard-stores", () => { timeRangeSummary: { min: TestTimeConstants.LAST_DAY.toISOString(), max: TestTimeConstants.NOW.toISOString(), - interval: V1TimeGrain.TIME_GRAIN_MINUTE as any, }, }, ), @@ -266,7 +262,6 @@ describe("dashboard-stores", () => { timeRangeSummary: { min: TestTimeConstants.LAST_DAY.toISOString(), max: TestTimeConstants.NOW.toISOString(), - interval: V1TimeGrain.TIME_GRAIN_MINUTE as any, }, }, ), @@ -296,7 +291,6 @@ describe("dashboard-stores", () => { timeRangeSummary: { min: TestTimeConstants.LAST_DAY.toISOString(), max: TestTimeConstants.NOW.toISOString(), - interval: V1TimeGrain.TIME_GRAIN_MINUTE as any, }, }, ), diff --git a/web-common/src/features/dashboards/stores/test-data/data.ts b/web-common/src/features/dashboards/stores/test-data/data.ts index 79a329a1d99..1b3c87f0955 100644 --- a/web-common/src/features/dashboards/stores/test-data/data.ts +++ b/web-common/src/features/dashboards/stores/test-data/data.ts @@ -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, }, };