SQL: Order by ascending metadata counts, null first#140
Open
ReinierMaas wants to merge 3 commits into
Open
Conversation
…_key)` in `submissions_metadata` This brings the SQL schema inline with the code enforced `StrategicMetadataMap`.
There was a problem hiding this comment.
Pull request overview
This PR updates the consumer chunk-selection logic to remove JSON serialization from the PreferDistinct strategy by moving metastate lookups into SQLite via an FFI-backed function, and adjusts the schema to enforce “one metadata value per key per submission”.
Changes:
- Refactors
Strategy::PreferDistinctquery generation to join per-key metadata-count lookups (opsqueue_metadata_count) and order by ascending count withNULLS FIRST. - Adds and registers a new SQLite function (
opsqueue_metadata_count) backed by the in-processMetaState. - Updates metastate bookkeeping to a single
vals_to_countsmap and adds a migration making(submission_id, metadata_key)the PK forsubmissions_metadata.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| opsqueue/src/consumer/strategy.rs | Builds PreferDistinct SQL using a metadata-count UDF and adds helpers to iterate nested meta keys. |
| opsqueue/src/consumer/dispatcher/mod.rs | Registers opsqueue_metadata_count SQLite UDF alongside the reserved-chunk lookup. |
| opsqueue/src/consumer/dispatcher/metastate.rs | Simplifies metastate storage and exposes per-value count lookup. |
| opsqueue/migrations/20260717143000_submission_metadata_pk_by_key.up.sql | Migrates submissions_metadata to PK (submission_id, metadata_key) with deterministic deduping. |
| opsqueue/migrations/20260717143000_submission_metadata_pk_by_key.down.sql | Restores the previous PK (submission_id, metadata_key, metadata_value). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+124
to
147
| for meta_key in &prefer_distinct_metakeys { | ||
| qb.push(", "); | ||
| qb.push(format_args!("{meta_key}_counts AS (")); | ||
| qb.push(" SELECT submission_id, opsqueue_metadata_count("); | ||
| qb.push_bind(meta_key.clone()); | ||
| qb.push(", metadata_value) AS count"); | ||
| qb.push(" FROM submissions_metadata WHERE metadata_key = "); | ||
| qb.push_bind(meta_key.clone()); | ||
| qb.push(")"); | ||
| } | ||
|
|
||
| qb.push(" SELECT underlying.* FROM underlying"); | ||
| for meta_key in &prefer_distinct_metakeys { | ||
| qb.push(format_args!( | ||
| " LEFT JOIN {meta_key}_counts ON underlying.submission_id = {meta_key}_counts.submission_id" | ||
| )); | ||
| } | ||
| qb.push(" ORDER BY "); | ||
| for (i, meta_key) in prefer_distinct_metakeys.iter().enumerate() { | ||
| if i > 0 { | ||
| qb.push(", "); | ||
| } | ||
| qb.push(format_args!("{meta_key}_counts.count ASC NULLS FIRST")); | ||
| } |
Comment on lines
+141
to
145
| qb.push(" ORDER BY "); | ||
| for (i, meta_key) in prefer_distinct_metakeys.iter().enumerate() { | ||
| if i > 0 { | ||
| qb.push(", "); | ||
| } |
Comment on lines
+1
to
+3
| CREATE TABLE submissions_metadata_new | ||
| ( | ||
| submission_id INTEGER NOT NULL, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extracted SQL migration to: #139
Based on #138 where we perform the same trick for the reserved ids.
This PR removes JSON serialization from the hot path to build the query to select a chunk from the datastore.