Skip to content

SQL: Order by ascending metadata counts, null first#140

Open
ReinierMaas wants to merge 3 commits into
reinier/pass_reserver_idsfrom
reinier/pass_metastate
Open

SQL: Order by ascending metadata counts, null first#140
ReinierMaas wants to merge 3 commits into
reinier/pass_reserver_idsfrom
reinier/pass_metastate

Conversation

@ReinierMaas

Copy link
Copy Markdown
Contributor

Extracted SQL migration to: #139

Based on #138 where we perform the same trick for the reserved ids.

This uses the fact that SQLite query engine lives in the same process as the calling Rust code to pass the integers directly into the query via SQL-FFI.

This PR removes JSON serialization from the hot path to build the query to select a chunk from the datastore.

…_key)` in `submissions_metadata`

This brings the SQL schema inline with the code enforced `StrategicMetadataMap`.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::PreferDistinct query generation to join per-key metadata-count lookups (opsqueue_metadata_count) and order by ascending count with NULLS FIRST.
  • Adds and registers a new SQLite function (opsqueue_metadata_count) backed by the in-process MetaState.
  • Updates metastate bookkeeping to a single vals_to_counts map and adds a migration making (submission_id, metadata_key) the PK for submissions_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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants