Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: clear up unused entities in clickhouse (#9151)

Steffen Schmitz2d000cb82681
diff --git a/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.down.sql b/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.down.sql
new file mode 100644
index 0000000000..8c502451c9
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.down.sql
@@ -0,0 +1,21 @@
+-- Recreate materialized views for project_environments table
+CREATE MATERIALIZED VIEW project_environments_traces_mv ON CLUSTER default TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM traces
+GROUP BY project_id;
+
+CREATE MATERIALIZED VIEW project_environments_observations_mv ON CLUSTER default TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM observations
+GROUP BY project_id;
+
+CREATE MATERIALIZED VIEW project_environments_scores_mv ON CLUSTER default TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM scores
+GROUP BY project_id;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.up.sql b/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.up.sql
new file mode 100644
index 0000000000..99e2d32453
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0027_drop_project_environments_mvs.up.sql
@@ -0,0 +1,4 @@
+-- Drop materialized views feeding project_environments table
+DROP VIEW IF EXISTS project_environments_traces_mv ON CLUSTER default;
+DROP VIEW IF EXISTS project_environments_observations_mv ON CLUSTER default;
+DROP VIEW IF EXISTS project_environments_scores_mv ON CLUSTER default;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.down.sql b/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.down.sql
new file mode 100644
index 0000000000..60d3890318
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.down.sql
@@ -0,0 +1,114 @@
+-- Recreate materialized views derived from traces_null table
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_all_amt_mv ON CLUSTER default TO traces_all_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
+
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_7d_amt_mv ON CLUSTER default TO traces_7d_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
+
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_30d_amt_mv ON CLUSTER default TO traces_30d_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.up.sql b/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.up.sql
new file mode 100644
index 0000000000..53d0c19bac
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0028_drop_traces_null_mvs.up.sql
@@ -0,0 +1,4 @@
+-- Drop materialized views derived from traces_null table
+DROP VIEW IF EXISTS traces_all_amt_mv ON CLUSTER default;
+DROP VIEW IF EXISTS traces_7d_amt_mv ON CLUSTER default;
+DROP VIEW IF EXISTS traces_30d_amt_mv ON CLUSTER default;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.down.sql b/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.down.sql
new file mode 100644
index 0000000000..53e287bd07
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.down.sql
@@ -0,0 +1,179 @@
+-- Recreate traces_null and trace amt tables
+CREATE TABLE traces_null ON CLUSTER default
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` DateTime64(3),
+ `end_time` Nullable(DateTime64(3)),
+ `name` Nullable(String),
+
+ -- Metadata properties
+ `metadata` Map(LowCardinality(String), String),
+ `user_id` Nullable(String),
+ `session_id` Nullable(String),
+ `environment` String,
+ `tags` Array(String),
+ `version` Nullable(String),
+ `release` Nullable(String),
+
+ -- UI properties - We make them nullable to prevent absent values being interpreted as overwrites.
+ `bookmarked` Nullable(Bool),
+ `public` Nullable(Bool),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` Array(String),
+ `score_ids` Array(String),
+ `cost_details` Map(String, Decimal64(12)),
+ `usage_details` Map(String, UInt64),
+ -- TODO: Do we want to aggregate/collect `levels` seen within the trace?
+
+ -- Input/Output
+ `input` String,
+ `output` String,
+
+ `created_at` DateTime64(3),
+ `updated_at` DateTime64(3),
+ `event_ts` DateTime64(3)
+) Engine = Null();
+
+CREATE TABLE traces_all_amt ON CLUSTER default
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_trace_id id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id);
+
+CREATE TABLE traces_7d_amt ON CLUSTER default
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id)
+ TTL toDate(start_time) + INTERVAL 7 DAY;
+
+CREATE TABLE traces_30d_amt ON CLUSTER default
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id)
+ TTL toDate(start_time) + INTERVAL 30 DAY;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.up.sql b/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.up.sql
new file mode 100644
index 0000000000..10f1d76df7
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/clustered/0029_drop_traces_null_and_amt_tables.up.sql
@@ -0,0 +1,5 @@
+-- Drop traces_null and trace amt tables
+DROP TABLE IF EXISTS traces_null ON CLUSTER default;
+DROP TABLE IF EXISTS traces_all_amt ON CLUSTER default;
+DROP TABLE IF EXISTS traces_7d_amt ON CLUSTER default;
+DROP TABLE IF EXISTS traces_30d_amt ON CLUSTER default;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.down.sql b/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.down.sql
new file mode 100644
index 0000000000..49e56f9e89
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.down.sql
@@ -0,0 +1,21 @@
+-- Recreate materialized views for project_environments table
+CREATE MATERIALIZED VIEW project_environments_traces_mv TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM traces
+GROUP BY project_id;
+
+CREATE MATERIALIZED VIEW project_environments_observations_mv TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM observations
+GROUP BY project_id;
+
+CREATE MATERIALIZED VIEW project_environments_scores_mv TO project_environments AS
+SELECT
+ project_id,
+ groupUniqArray(environment) AS environments
+FROM scores
+GROUP BY project_id;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.up.sql b/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.up.sql
new file mode 100644
index 0000000000..a4fe385fa5
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0027_drop_project_environments_mvs.up.sql
@@ -0,0 +1,4 @@
+-- Drop materialized views feeding project_environments table
+DROP VIEW IF EXISTS project_environments_traces_mv;
+DROP VIEW IF EXISTS project_environments_observations_mv;
+DROP VIEW IF EXISTS project_environments_scores_mv;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.down.sql b/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.down.sql
new file mode 100644
index 0000000000..0d1a7a6e32
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.down.sql
@@ -0,0 +1,114 @@
+-- Recreate materialized views derived from traces_null table
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_all_amt_mv TO traces_all_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
+
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_7d_amt_mv TO traces_7d_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
+
+CREATE MATERIALIZED VIEW IF NOT EXISTS traces_30d_amt_mv TO traces_30d_amt AS
+SELECT
+ -- Identifiers
+ tn.project_id as project_id,
+ tn.id as id,
+ min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
+ min(tn.start_time) as start_time,
+ max(coalesce(tn.end_time, tn.start_time)) as end_time,
+ anyLast(tn.name) as name,
+
+ -- Metadata properties
+ maxMap(tn.metadata) as metadata,
+ anyLast(tn.user_id) as user_id,
+ anyLast(tn.session_id) as session_id,
+ anyLast(tn.environment) as environment,
+ groupUniqArrayArray(tn.tags) as tags,
+ anyLast(tn.version) as version,
+ anyLast(tn.release) as release,
+
+ -- UI properties
+ argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations -- DO NOT USE
+ groupUniqArrayArray(tn.observation_ids) as observation_ids,
+ groupUniqArrayArray(tn.score_ids) as score_ids,
+ sumMap(tn.cost_details) as cost_details,
+ sumMap(tn.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
+ argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
+
+ min(tn.created_at) as created_at,
+ max(tn.updated_at) as updated_at
+FROM traces_null tn
+GROUP BY project_id, id;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.up.sql b/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.up.sql
new file mode 100644
index 0000000000..2f95c940f4
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0028_drop_traces_null_mvs.up.sql
@@ -0,0 +1,4 @@
+-- Drop materialized views derived from traces_null table
+DROP VIEW IF EXISTS traces_all_amt_mv;
+DROP VIEW IF EXISTS traces_7d_amt_mv;
+DROP VIEW IF EXISTS traces_30d_amt_mv;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.down.sql b/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.down.sql
new file mode 100644
index 0000000000..3c51d5fd0e
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.down.sql
@@ -0,0 +1,179 @@
+-- Recreate traces_null and trace amt tables
+CREATE TABLE traces_null
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` DateTime64(3),
+ `end_time` Nullable(DateTime64(3)),
+ `name` Nullable(String),
+
+ -- Metadata properties
+ `metadata` Map(LowCardinality(String), String),
+ `user_id` Nullable(String),
+ `session_id` Nullable(String),
+ `environment` String,
+ `tags` Array(String),
+ `version` Nullable(String),
+ `release` Nullable(String),
+
+ -- UI properties - We make them nullable to prevent absent values being interpreted as overwrites.
+ `bookmarked` Nullable(Bool),
+ `public` Nullable(Bool),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` Array(String),
+ `score_ids` Array(String),
+ `cost_details` Map(String, Decimal64(12)),
+ `usage_details` Map(String, UInt64),
+ -- TODO: Do we want to aggregate/collect `levels` seen within the trace?
+
+ -- Input/Output
+ `input` String,
+ `output` String,
+
+ `created_at` DateTime64(3),
+ `updated_at` DateTime64(3),
+ `event_ts` DateTime64(3)
+) Engine = Null();
+
+CREATE TABLE traces_all_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_trace_id id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id);
+
+CREATE TABLE traces_7d_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id)
+ TTL toDate(start_time) + INTERVAL 7 DAY;
+
+CREATE TABLE traces_30d_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- Metadata properties
+ `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
+ `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` SimpleAggregateFunction(anyLast, Nullable(String)),
+ `release` SimpleAggregateFunction(anyLast, Nullable(String)),
+
+ -- UI properties
+ `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+ `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
+
+ -- Aggregations -- DO NOT USE
+ `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
+ `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
+
+ -- Input/Output -> prefer correctness via argMax
+ `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
+ INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
+) Engine = AggregatingMergeTree()
+ ORDER BY (project_id, id)
+ TTL toDate(start_time) + INTERVAL 30 DAY;
\ No newline at end of file
diff --git a/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.up.sql b/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.up.sql
new file mode 100644
index 0000000000..fc99886fc1
--- /dev/null
+++ b/packages/shared/clickhouse/migrations/unclustered/0029_drop_traces_null_and_amt_tables.up.sql
@@ -0,0 +1,5 @@
+-- Drop traces_null and trace amt tables
+DROP TABLE IF EXISTS traces_null;
+DROP TABLE IF EXISTS traces_all_amt;
+DROP TABLE IF EXISTS traces_7d_amt;
+DROP TABLE IF EXISTS traces_30d_amt;
\ No newline at end of file