Repository navigation
feat(birdbench): add PostgreSQL dialect - #6063
Conversation
Grade a model that writes PostgreSQL the way the spark dialect grades Spark SQL: the model's query runs on PostgreSQL and the untouched BIRD gold runs on the original SQLite database; only the result sets are compared. No gold SQL is transpiled. setup_bird_postgres.py loads each SQLite database into its own schema (the Spark loader's column typing, text columns with the "C" collation so ordering matches SQLite) and creates a read-only grader role. The model's query runs as one prepared statement in a read-only transaction with a server-side statement_timeout and parallel query off. Connection and server failures are scored unknown_error, not as the model's mistake. The grader DSN (postgres_dsn) and the optional loader DSN (postgres_admin_dsn) come from config; no password is built in. psycopg and pandas are needed only for this dialect and are imported lazily, like pyspark for spark. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
…d check prepare renders sql_context for PostgreSQL: schema-qualified, double-quoted names and the column types the loader creates. prompts/default_postgres.yaml is the Spark prompt with the engine changed and one sentence on quoting: PostgreSQL folds unquoted names to lower case. eval.sh accepts --dialect postgres and passes BIRD_PG_DSN (and BIRD_PG_ADMIN_DSN, if set) to the server. postgres_gold_check.py mirrors spark_gold_check.py: it grades sqlglot transpiles of the dev golds through the server's own comparison, raw and with three mechanical fixes (identifier case, double-quoted strings, REAL). The two tests that used "postgres" as their unsupported dialect now use "mysql". Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Two tests run without a server: dialect routing, and an unreachable server scored unknown_error. The rest run when BIRD_PG_TEST_ADMIN_DSN points at a PostgreSQL server that can create schemas and roles: loader types, string fallback and collation, idempotent load, the read-only role, every execute_spark case mirrored, statement timeout, "COMMIT; DROP" and an end-to-end verify(). They load one small schema and drop it afterwards. Without psycopg or a server they skip. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
The postgres dialect opened a new connection for every grade: a server process forked and SCRAM authentication each time. That capped one server near 350 grades/s and let concurrent grades run the server out of max_connections. Each server process now keeps one psycopg_pool pool of max_concurrency connections, opened on the first grade. A grade holding the semaphore always finds a free connection, so grades still queue in the semaphore as before, and the connection is taken before the timeout backstop starts: waiting for one never counts against the query. Every transaction is BEGIN READ ONLY and always rolls back, so nothing a query sets (SET TimeZone, set_config(..., false)) reaches the next grade on that connection. When the backstop fires, the connection is cancelled and closed, and the pool replaces it. Connections are checked before use, so a server restart doesn't fail grades. A missing psycopg_pool fails startup instead of every grade. postgres_gold_check.py passes a pool too. On BIRD dev it gives the same 1,402 of 1,534 matches as before; two questions swap status, both known ties under ORDER BY ... LIMIT. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Grades reuse a connection; concurrent grades never use more than the pool size; a statement timeout returns its connection for reuse; a query's SET doesn't reach later grades; waiting for a connection isn't charged to the query; a backstop cancel discards the connection and leaves no query running; verify() keeps one pool per server; a missing psycopg_pool fails startup. The DSN-level tests now go through a pool. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Declared like the Spark ones on main: psycopg[binary]>=3.2 and psycopg_pool>=3.2 are plain dependencies of the bird_sql server, imported only under dialect: postgres. prepare renders the column types the loader creates, so the birdbench preparation project depends on psycopg[binary] too. The READMEs drop the manual `uv pip install`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Main masks a row whose SQLite file is missing before any query runs, so the routing and outage tests create the placeholder file. A grader outage is now a masked verifier error (failure_reason unknown_error) rather than a scored zero; the outage test checks both, and the bird_sql README now says so. ServerClient comes from nemo_gym.client. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
A new table has no planner statistics until autovacuum reaches it, about a minute after the load. A gold check started right after loading dev timed out on two correct queries (card_games 345 and 346) that run in well under a second once the tables are analyzed. With postgres_admin_dsn, the server loads at startup and grades straight away, so a correct answer could be scored as the model's timeout. The loader now runs ANALYZE on each table inside the load transaction. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
PostgreSQL folds unquoted names to lower case, and the loader kept BIRD's mixed-case column names, so an unquoted mixed-case column that SQLite accepts failed as an undefined column. On saved predictions from a model that often leaves names unquoted, lower-casing only the columns raised dev accuracy from 32.99% to 45.96%. The loader now lower-cases column names. Table names keep BIRD's case: lower-casing them too broke answers that quote names like "Player" (44.59% on the same predictions). prepare renders the lower-case names, and the gold check spells identifiers as loaded. The gold check is unchanged: 1,401 of 1,534 against 1,402 on the mixed-case load; the one difference is a LIMIT over tied rows (question 1024), the same query in both layouts. sqlglot's output before the three fixes now matches 1,051 golds instead of 537. Each loaded db_id records layout 2 in _bird_meta.loaded. Startup, and a load, stop with an error on a db_id loaded with an older layout, which the grader would otherwise score as the model's mistakes, until it is dropped and loaded again. A separate commit so it can be dropped: it changes the names a model sees. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Brings the branch up to date with main for the lock-freshness check. Main's 14 new commits change no lock this PR touches; every managed lock still passes scripts/component_locks.py after the merge. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
|
/ok to test d9bfee0 |
| except psycopg.errors.QueryCanceled: | ||
| raise TimeoutError(f"PostgreSQL query exceeded {timeout_s}s") from None | ||
| except psycopg.Error as e: | ||
| if e.sqlstate is None or e.sqlstate[:2] in _POSTGRES_INFRA_SQLSTATE_CLASSES: |
There was a problem hiding this comment.
A missing SQLSTATE doesn’t necessarily mean an infrastructure failure. For example, SELECT 'infinity'::date runs successfully in PostgreSQL, but psycopg cannot convert the result into a Python date and raises DataError with sqlstate=None.
Could we distinguish query/result-conversion errors from actual connection or server failures?
There was a problem hiding this comment.
Good catch, thanks. Fixed in a294a0c: errors psycopg raises itself (no SQLSTATE) now count as a failed query unless the connection was lost or closed, so 'infinity'::date scores as wrong instead of crashing the grade. Server errors still use the SQLSTATE class, and tests cover both.
A query can succeed on the server and still fail in psycopg: SELECT 'infinity'::date returns 'infinity', which has no Python date, so psycopg raises DataError. psycopg's own errors carry no SQLSTATE, and the grader re-raised every error without one as an infrastructure failure, so a model answer like this masked the row instead of scoring it as wrong. Errors without a SQLSTATE now raise only for a lost or closed connection (OperationalError, InterfaceError) and otherwise score as the query's fault. Server errors keep the SQLSTATE class rule, since OperationalError also covers query faults such as 54001. Tests: 'infinity'::date scores as pred_sql_error; a connection terminated mid-query still raises; and a table of client and server errors pins which side each lands on. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Abel Brown <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
4abf6cb
|
/ok to test 89b76ff |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44234950356 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44246831968 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44248958938 |
What does this PR do?
Adds
--dialect postgresto the BIRD benchmark, next tosqliteandsparkfrom #4898. It grades the way Spark does: the model's query runs on PostgreSQL, the gold SQL runs untouched on SQLite, and the two result sets are compared.Please review
prompts/default_postgres.yaml, the new config fields inbird_sql/app.py, the PostgreSQL half ofeval_utils.py, and the last commit. Skip the tests andpostgres_gold_check.py.dialect: postgres):setup_bird_postgres.pyloads each BIRD database into its own schema, typed like the Spark loader, with text in the"C"collation so sorting matches SQLite. It runsANALYZEafter each load. Each grade is one prepared statement in a read-only transaction with a server-side timeout. A connection or server failure is masked as a verifier error, not scored against the model. New config:postgres_dsn(a read-only role) and an optionalpostgres_admin_dsn(loads missing databases at startup).max_concurrencyconnections. On 8 cores the pooled grader does 642–664 grades per second, compared to 67.5 when it opens a connection per grade. SQLite and Spark are unchanged.eval.sh:--dialect postgresrenders schema-qualified, double-quoted names. The prompt is the Spark prompt with the engine changed and one sentence: write every name double-quoted, exactly as shown, because PostgreSQL folds unquoted names to lower case.psycopg[binary]andpsycopg_poolare locked dependencies ofbird_sql, likepyspark, and load only fordialect: postgres. The birdbench prepare project addspsycopg[binary].04737b78c): the loader lower-cases column names; tables keep BIRD's names. Unquoted column names then work as they do in SQLite. One model's saved dev predictions go from 32.99% to 45.96% with no change to its SQL, and the gold check is unchanged. The trade-off: BIRD's evidence spells some columns in mixed case, which can lead a model to quote a name that no longer exists (61 of 1,534 dev questions). I propose keeping it. Drop the commit to keep BIRD's column names.Validation
resources_servers/bird_sql/testsagainst a local PostgreSQL 16, Spark tests included: 115 passed (114 without the last commit). Without a server, the PostgreSQL tests skip.benchmarks/birdbench/tests: 77 passed.postgres_gold_check.pyon dev: 1,401 of 1,534 gold queries (91.3%) match after sqlglot translation and three mechanical fixes.eval.sh --dialect postgreson all of dev, with a stub model that answers with those translations: 1,401 of 1,534. It differs from the gold check only on two questions whose gold usesLIMIT 1over tied rows.pre-commiton the changed files anduv lock --checkfor both projects: clean.Rollouts
The stub-model run above covers the full
eval.shpath without model calls. Earlier full dev runs with real models used a previous version of this grader and aren't repeated here.Compatibility and benchmark impact
None for
sqliteandspark: defaults are unchanged. PostgreSQL'smax_connectionsmust exceedmax_concurrencytimes the number of server processes. Two existing tests usedpostgresas their unsupported-dialect example; they now usemysql.Checklist
pre-commit run --all-files) (so CI lint/format checks pass).git commit -s) (so the DCO check passes).🤖 Generated with Claude Code