Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ WITH feedback_scores_combined_raw AS (
value,
last_updated_at,
last_updated_by AS author
FROM feedback_scores FINAL
FROM feedback_scores
WHERE entity_type = 'trace'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand All @@ -182,7 +182,7 @@ WITH feedback_scores_combined_raw AS (
value,
last_updated_at,
author
FROM authored_feedback_scores FINAL
FROM authored_feedback_scores
WHERE entity_type = 'trace'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand Down Expand Up @@ -270,10 +270,7 @@ traces_filtered AS (
FROM (
SELECT
id,
if(end_time IS NOT NULL AND start_time IS NOT NULL
AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
(dateDiff('microsecond', start_time, end_time) / 1000.0),
NULL) AS duration,
duration,
Comment thread
ldaugusto marked this conversation as resolved.
error_info
<if(group_expression)>,
project_id,
Expand Down Expand Up @@ -323,7 +320,7 @@ WITH feedback_scores_combined_raw AS (
value,
last_updated_at,
last_updated_by AS author
FROM feedback_scores FINAL
FROM feedback_scores
WHERE entity_type = 'span'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand All @@ -337,7 +334,7 @@ WITH feedback_scores_combined_raw AS (
value,
last_updated_at,
author
FROM authored_feedback_scores FINAL
FROM authored_feedback_scores
WHERE entity_type = 'span'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand Down Expand Up @@ -411,22 +408,19 @@ spans_filtered AS (
FROM (
SELECT
id,
if(end_time IS NOT NULL AND start_time IS NOT NULL
AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
(dateDiff('microsecond', start_time, end_time) / 1000.0),
NULL) AS duration,
usage,
error_info,
total_estimated_cost
<if(group_expression)>,
project_id,
name,
tags,
metadata,
model,
provider,
type
<endif>
duration,
usage,
error_info,
total_estimated_cost
<if(group_expression)>,
project_id,
name,
tags,
metadata,
model,
provider,
type
<endif>
FROM spans FINAL
<if(feedback_scores_empty_filters)>
LEFT JOIN fsc ON fsc.entity_id = spans.id
Expand Down Expand Up @@ -458,14 +452,7 @@ ORDER BY (workspace_id, project_id, entity_id, name) DESC, last_updated_at DESC
""";

private static final String THREAD_FILTERED_PREFIX = """
WITH traces_final AS (
SELECT
*
FROM traces FINAL
WHERE workspace_id = :workspace_id
AND project_id = :project_id
AND thread_id \\<> ''
), trace_threads_final AS (
WITH trace_threads_final AS (
SELECT
workspace_id,
project_id,
Expand All @@ -482,6 +469,13 @@ WITH traces_final AS (
AND project_id = :project_id
<if(uuid_from_time)> AND id >= :uuid_from_time<endif>
<if(uuid_to_time)> AND id \\<= :uuid_to_time<endif>
), traces_final AS (
SELECT
*
FROM traces FINAL
WHERE workspace_id = :workspace_id
AND project_id = :project_id
AND thread_id IN (SELECT thread_id FROM trace_threads_final)
), feedback_scores_combined_raw AS (
SELECT
workspace_id,
Expand All @@ -491,7 +485,7 @@ WITH traces_final AS (
value,
last_updated_at,
last_updated_by AS author
FROM feedback_scores FINAL
FROM feedback_scores
WHERE entity_type = 'thread'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand All @@ -504,7 +498,7 @@ AND entity_id IN (SELECT thread_model_id FROM trace_threads_final)
value,
last_updated_at,
author
FROM authored_feedback_scores FINAL
FROM authored_feedback_scores
WHERE entity_type = 'thread'
AND workspace_id = :workspace_id
AND project_id = :project_id
Expand Down Expand Up @@ -709,6 +703,7 @@ TO toDateTime(UUIDv7ToDateTime(toUUID(:uuid_to_time)))
FROM spans final
WHERE project_id = :project_id
AND workspace_id = :workspace_id
AND trace_id IN (SELECT id FROM traces_filtered)
<if(uuid_from_time)> AND id >= :uuid_from_time<endif>
<if(uuid_to_time)> AND id \\<= :uuid_to_time<endif>
Comment thread
thiagohora marked this conversation as resolved.
) s ON s.trace_id = t.id
Expand Down Expand Up @@ -738,6 +733,7 @@ TO toDateTime(UUIDv7ToDateTime(toUUID(:uuid_to_time)))
FROM spans final
WHERE project_id = :project_id
AND workspace_id = :workspace_id
AND trace_id IN (SELECT id FROM traces_filtered)
<if(uuid_from_time)> AND id >= :uuid_from_time<endif>
<if(uuid_to_time)> AND id \\<= :uuid_to_time<endif>
) s ON s.trace_id = t.id
Expand All @@ -764,6 +760,7 @@ TO toDateTime(UUIDv7ToDateTime(toUUID(:uuid_to_time)))
FROM spans final
WHERE project_id = :project_id
AND workspace_id = :workspace_id
AND trace_id IN (SELECT id FROM traces_filtered)
<if(uuid_from_time)> AND id >= :uuid_from_time<endif>
<if(uuid_to_time)> AND id \\<= :uuid_to_time<endif>
) s ON s.trace_id = t.id
Expand Down Expand Up @@ -797,6 +794,7 @@ TO toDateTime(UUIDv7ToDateTime(toUUID(:uuid_to_time)))
FROM spans final
WHERE project_id = :project_id
AND workspace_id = :workspace_id
AND trace_id IN (SELECT id FROM traces_filtered)
<if(uuid_from_time)> AND id >= :uuid_from_time<endif>
<if(uuid_to_time)> AND id \\<= :uuid_to_time<endif>
) s ON s.trace_id = t.id
Expand Down Expand Up @@ -1102,12 +1100,7 @@ TO toDateTime(UUIDv7ToDateTime(toUUID(:uuid_to_time)))

private static final String GET_AVERAGE_DURATION = """
SELECT
avg(
if(end_time IS NOT NULL AND start_time IS NOT NULL
AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
(dateDiff('microsecond', start_time, end_time) / 1000.0),
NULL)
) AS avg_duration
avg(duration) AS avg_duration
FROM traces final
WHERE workspace_id = :workspace_id
<if(project_ids)> AND project_id IN :project_ids <endif>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--liquibase formatted sql
--changeset thiagoh:000078_add_minmax_index_authored_feedback_scores_created_at
--comment: Add minmax skip index on created_at to enable time-bounded queries on authored_feedback_scores

ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.authored_feedback_scores ON CLUSTER '{cluster}'
Comment thread
thiagohora marked this conversation as resolved.
ADD INDEX IF NOT EXISTS idx_authored_feedback_scores_created_at created_at TYPE minmax GRANULARITY 1;
Comment thread
thiagohora marked this conversation as resolved.

--rollback ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.authored_feedback_scores ON CLUSTER '{cluster}' DROP INDEX IF EXISTS idx_authored_feedback_scores_created_at;
Loading