Route count_overlaps (LEFT JOIN + GROUP BY on INTERSECTS) through IE_JOIN with zero-fill — Closes #209#212
Merged
Merged
Conversation
The idiomatic per-interval overlap count -- SELECT a.cols, COUNT(b.col) FROM a LEFT JOIN b ON a.interval INTERSECTS b.interval GROUP BY a.cols -- declined to the naive plan like every outer join, becoming a HASH_JOIN on the low-cardinality chromosome key with the position inequalities as a residual filter (quadratic; ~180x slower than achievable at scale). Detect this shape and reuse the INNER IEJoin path (which already plans COUNT + GROUP BY through IE_JOIN), then wrap its counts in a zero-fill LEFT join back onto the distinct left keys so left intervals with no overlap report 0. Supports a single COUNT(<right column>) / COUNT(DISTINCT <right column>) with a GROUP BY on the projected left columns; COUNT(*), non-COUNT aggregates, ORDER BY / LIMIT / HAVING, and other outer-join shapes still take the naive-predicate plan. Claude-Session: https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy
Add shape, decline (COUNT(*), non-COUNT aggregate, no GROUP BY, ORDER BY, self-join), a Hypothesis property test against a Python count_overlaps reference (zero-overlap rows, one-sided chromosomes, empty right table, duplicate left rows), and an EXPLAIN plan guard asserting the generated inner SQL is not planned as a BLOCKWISE_NL_JOIN. Claude-Session: https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy
Describe the LEFT-join COUNT over INTERSECTS routing through the INNER IE_JOIN count plus a zero-fill LEFT join, and the shapes that remain on the naive-predicate plan. Claude-Session: https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy
Tighten the count_overlaps detection gate for two shapes a review found producing wrong results on matched queries: a GROUP BY carrying keys beyond the projected columns (the extra key inflates the count cardinality and drops zero-count rows the zero-fill base cannot reproduce), and a projected key whose output name collides with the COUNT alias (the zero-fill USING / COALESCE binds to the wrong duplicately-named column). Require the group keys to equal the projected keys exactly and all output names to be distinct, declining to the naive plan otherwise. Claude-Session: https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy
Add decline tests for a GROUP BY exceeding the projection, an output-name collision between a key and the COUNT alias, and a top-level WHERE, plus a COUNT(DISTINCT b.col) execution test asserting equivalence with the naive plan. Claude-Session: https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy
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.
Summary
The idiomatic per-interval overlap count —
SELECT a.cols, COUNT(b.col) FROM a LEFT JOIN b ON a.interval INTERSECTS b.interval GROUP BY a.cols— declined to the naive plan like every outer join (_has_outer_join_intersects), becoming aHASH_JOINon the low-cardinality chromosome key with the position inequalities as a residual filter — quadratic, ~180× slower than achievable (81 s vs ~0.5 s at 1e6 intervals per side, single-thread).This is a follow-up to #208 (same root cause: DuckDB only selects
IE_JOINfor INNER pure-inequality joins). The key observation is that the existing INNER path already plansCOUNT(b.col) + GROUP BYthroughIE_JOIN. So the fast path detects the LEFT-count shape, reuses the INNER machinery to compute the counts, and wraps them in a zero-fillLEFT JOINback onto the distinct left keys, so left intervals with no overlap report0. Verified end-to-end:count_overlapsat 1e6 goes from 81 s to 0.56 s single-threaded (0.21 s at 14 threads), faster than bioframe's 0.67 s. Results were validated against the naive plan and a Python reference across 500+ random and edge cases (empty inputs, one-sided chromosomes, duplicate left rows) on DuckDB 1.4.3 and 1.5.4 — exact match.Scope (v1): a single
COUNT(<right column>)/COUNT(DISTINCT <right column>)with aGROUP BYon the projected left columns.COUNT(*)(its LEFT-join semantics count the null-padded row for unmatched rows), non-COUNT aggregates (bedtools-map-style SUM/MIN/MAX — a clean follow-up),ORDER BY/LIMIT/HAVING, self-joins, and other outer-join shapes still take the naive-predicate plan.Closes #209
Proposed changes
_match_count_overlaps(detect the LEFT-join COUNT shape, decline everything else) and_build_count_overlaps_sql(reusetransform_to_sqlon an INNER copy, then wrap inWITH __giql_counts …+ a zero-fillLEFT JOINagainst the distinct left keys). Wire the detection intotransform_to_sqlahead of the outer-join decline. Purely additive.TestTranspileDuckDBIEJoinCountOverlapsclass: shape, six decline cases, a Hypothesis property test against a Pythoncount_overlapsreference, and an EXPLAIN plan guard.count_overlapssubsection in the DuckDB IEJoin performance guide.Test cases
TestTranspileDuckDBIEJoinCountOverlapsCOUNT(b.col)+ GROUP BY querydialect='duckdb'SET VARIABLE+__giql_countsCTE +COALESCE(..., 0)zero-fill form and noa.chrom = b.chromnaive predicateTestTranspileDuckDBIEJoinCountOverlapsCOUNT(*)/SUM(b.score)/ no-GROUP-BY /ORDER BY/ self-join LEFT-join querydialect='duckdb'__giql_countswrapper)TestTranspileDuckDBIEJoinCountOverlapsTestTranspileDuckDBIEJoinCountOverlapsBLOCKWISE_NL_JOINFull non-integration suite (1026 passed) and the bedtools integration oracle (79 passed) stay green.
https://claude.ai/code/session_01TERWBHov76DQM3nQeT8yyy