Skip to content

Evaluate LHS subqueries once#7352

Open
howenyap wants to merge 5 commits into
tursodatabase:mainfrom
howenyap:fix/between-expression-evaluates-left-side-twice
Open

Evaluate LHS subqueries once#7352
howenyap wants to merge 5 commits into
tursodatabase:mainfrom
howenyap:fix/between-expression-evaluates-left-side-twice

Conversation

@howenyap

@howenyap howenyap commented Jun 4, 2026

Copy link
Copy Markdown

Description

Closes #5152

When parsing where clauses in the planner, BETWEEN statements are expanded into two comparisons. This results in LHS being cloned, and later evaluated twice.

My initial idea of a fix is to add a guard to avoid expanding BETWEEN statements if the LHS is a subquery, but this still evaluates LHS twice if the subquery is nested, so I chose to walk the LHS and check whether any subqueries exist in any level instead.

The expr_contains_subquery function was copied from here by @PThorpe92.

Testing

Before adding the fix, I've added a snapshot test that shows the query plan as:

QUERY PLAN
|--SCALAR SUBQUERY 1
|  `--SCAN products
|--SCALAR SUBQUERY 2
|  `--SCAN products
`--SCAN products

After the fix, the new query plan reflects only evaluating the subquery once:

QUERY PLAN
|--SCALAR SUBQUERY 1
|  `--SCAN products
`--SCAN products

Description of AI Usage

Codex was used to explore the codebase and write most of the code, all code was manually reviewd.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Fossier: Manual Review Requested

@howenyap is a new contributor. A maintainer should review this PR before merging.

Score Breakdown

Total Score: 51.2/100 | Confidence: 100% | Outcome: REVIEW

Signal Value Score Weight
account_age 1429 1.00 0.07
public_repos 43 1.00 0.04
contribution_history 43 0.21 0.04
follower_ratio 0.42 0.21 0.04
bot_signals False 0.50 0.03
open_prs_elsewhere 7 0.12 0.07
closed_prs_elsewhere 9 0.10 0.08
merged_prs_elsewhere 34 1.00 0.06
prior_interaction 1 0.33 0.24
activity_velocity 1 1.00 0.06
pr_content ... 1.00 0.06
commit_email no_email 0.50 0.03
pr_description ... 0.80 0.04
repo_stars 19049 0.30 0.03
org_membership 0 0.20 0.02
commit_verification ... 0.30 0.03
contributor_stars 9 0.18 0.03

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Fossier: Manual Review Requested

@howenyap is a new contributor. A maintainer should review this PR before merging.

Score Breakdown

Total Score: 51.2/100 | Confidence: 100% | Outcome: REVIEW

Signal Value Score Weight
account_age 1429 1.00 0.07
public_repos 43 1.00 0.04
contribution_history 43 0.21 0.04
follower_ratio 0.42 0.21 0.04
bot_signals False 0.50 0.03
open_prs_elsewhere 7 0.12 0.07
closed_prs_elsewhere 9 0.10 0.08
merged_prs_elsewhere 34 1.00 0.06
prior_interaction 1 0.33 0.24
activity_velocity 1 1.00 0.06
pr_content ... 1.00 0.06
commit_email no_email 0.50 0.03
pr_description ... 0.80 0.04
repo_stars 19049 0.30 0.03
org_membership 0 0.20 0.02
commit_verification ... 0.30 0.03
contributor_stars 9 0.18 0.03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BETWEEN Expression Evaluates Left Side Twice

1 participant