## Widget input query
A widget input of kind "query" is one saved ClickHouse SELECT. Opik runs it again every time the widget loads, over the dashboard's date range, and passes the rows to the view under the input name. Rows that did not come from a saved query cannot go on a dashboard.
- Result: return exactly one column named result holding one JSON object per row, e.g. SELECT toJSONString(map('model', model, 'cost', toString(round(sum(toFloat64(total_estimated_cost)), 6)))) AS result. Each object becomes one row of the input. Values built with map(...) arrive as strings, so the view converts numbers itself. One row is one data point: never fold the rows into one with groupArray or an array, or the widget gets a single row.
- Time: bound the dashboard's range with {{window_start}} and {{window_end}}, e.g. WHERE start_time >= {{window_start}} AND start_time < {{window_end}}. Opik replaces them with DateTime64(9, 'UTC') values of the range; with no range they are the epoch and now. Never write literal dates for the dashboard's range. A period the user names, such as last week, is still the dashboard's range: use the placeholders. Write a period into the SQL, e.g. start_time >= now() - INTERVAL 7 DAY, only when the widget must keep it whatever range the dashboard shows.
- Experiments and datasets: never use the placeholders in a query over experiments, experiment_items, dataset_items or dataset_item_versions. Experiments dashboards have no date range, so such a query is not allowed.
- Previous period: return it from the same query. The previous window has the same length and ends where the current one starts: read from subtractSeconds({{window_start}}, dateDiff('second', {{window_start}}, {{window_end}})) to {{window_end}} and label each row with if(start_time >= {{window_start}}, 'current', 'previous') AS period.
- Rows: aggregate in SQL to what the view shows, at most a few hundred rows; the view receives every row. A result over 100,000 rows is refused.
- Names: return a project or a dataset under the key 'project_id' or 'dataset_id', exactly. Opik adds 'project_name' or 'dataset_name' to the row when the id exists in the workspace; label by the name and fall back to the id.
- Scope: a saved query is for one project or for the whole workspace. traces, spans, feedback scores and threads are limited to its project, or to every project in the workspace when it has none; experiments and datasets are workspace-wide. Opik applies the workspace and the project: never filter on workspace_id or project_id. A query over experiments or datasets goes only on an experiments dashboard, and one over traces, spans, feedback scores or threads only on a project or multi-project dashboard.
- Allowed: a single SELECT, with CTEs and IN or EXISTS subqueries, over the tables in the schema reference. Other tables, SETTINGS and SET are refused. Join or use IN instead of a scalar subquery that reads a table.
- Cost: queries are read-only and stop at 180 seconds, 100 million rows or 20 GB read. Filter on start_time early and never select input, output or metadata whole.
