Take a query-history definition draft's tables and columns from the query log

Open
#1,755 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
clickhouse, python
Domain
backend, databases

Research direction

Start with group_sql in server/knowledge/store/query_history_remote.py, then trace how query-history data reaches the prompt and rests_on through server/memory/app/definition_drafts.py and server/memory/domain/definition_drafts.py. Add the acceptance fixture in tests/integration/test_query_history_drafts.py and check the query-log arrays, capped lists, gap drafts, and one model call per draft.

Written by the indexing model from the issue text.

Description

enhancement
Company

ClickHouse

Component

Knowledge (documents)

Use case

A definition drafted from query history records the tables and columns it rests on in rests_on. Today the model writes both lists. The draft prompt asks for tables and columns, and the reply is stored after a size check only (server/memory/app/definition_drafts.py, server/memory/domain/definition_drafts.py). The source already knows the answer: system.query_log holds the tables and columns each query read, and the query-history read does not select them (group_sql in server/knowledge/store/query_history_remote.py).

An admin reviewing the draft, or a later check of the definition against its source, reads a list that can name a table the query never touched or omit one it did. rests_on holds the source's normalized SQL beside the model's two lists and does not mark which is which; only the documentation says so (docs/architecture/definition-drafts.md).

Describe the solution you'd like

Read the lists from the source and stop asking the model for them. This is a proposal; nothing is implemented or measured.

  • group_sql also selects the tables and columns arrays of the query whose text it keeps, each cut to the draft's existing list limits. A cut list is reported as capped, not passed off as complete. The registration probe runs the same statement, so it also verifies that the credential can read the two arrays.
  • For a query-history draft, rests_on.tables and rests_on.columns come from that read. rests_on records where the lists came from, so a reader can tell a source-read list from one written before this change.
  • The prompt and DefinitionDraft drop tables and columns. The model still returns the term, aliases and body.
  • A draft from a knowledge gap has no SQL and no source to read, so it carries no table or column list.
  • The read stays inside the registration's database allowlist, which the existing filter on the log's databases array already applies. The lists keep the visibility rests_on has today.
  • The change adds no model call, no table and no statement on the source beyond the existing bounded query-log read.

Acceptance fixture, added to tests/integration/test_query_history_drafts.py:

  1. A query group over one table yields a draft whose rests_on lists equal the log's arrays, whatever the model returns.
  2. The model input and the expected reply contain no table or column list.
  3. A group whose arrays exceed the limits yields cut lists marked capped.
  4. A query through a view records what the log records for it.
  5. A gap draft has no lists.
  6. The model-call count per draft stays at one.

Evidence still needed before this is called a gain:

  • A read-only count, on a deployment that has query-history drafts, of the share whose model-written lists differ from the log's arrays for the same query.
  • Whether the drafted term, aliases and body change when the prompt stops asking for the two lists. This needs a paired run on fixed query groups, and a paid run only on request.
  • What the log's arrays contain for views, SELECT * and joins on the supported server versions, confirmed in the fixture.
Describe alternatives you've considered
  • Parse the normalized SQL in the server. The server imports no SQL parser; sqlglot is in the extractor's dependency group only. A parser also cannot resolve a view or SELECT * the way the source does.
  • Keep the model's lists and verify them against system.columns. That adds a metadata read on the data tables, and registration verifies the query-log read only, not that the credential can see those tables' columns.
  • Leave it as documented. The lists then stay unusable as evidence for any later check.
Where the need comes from

An AI tool suggested this feature

Additional context

Found by source review; no draft was generated and no source was queried.

Related: #1176 checks maintained guidance against a registered tracker source, with objects a maintainer declares. This change is separate and smaller. It gives query-history definitions a source-read list that a later extension of that check could use. Part of the roadmap in #1232.

Dominant language
Python
Stars
263
Forks
47
Avg merge
14h 21m
Merged PRs (30d)
873

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/loom

All issues in ClickHouse/loom

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.