Skip to content

⚡ Bolt: de-virtualize table sources in query builders#128

Merged
cungminh2710 merged 3 commits into
mainfrom
bolt-devirtualize-table-source-14031147593272287429
Jun 18, 2026
Merged

⚡ Bolt: de-virtualize table sources in query builders#128
cungminh2710 merged 3 commits into
mainfrom
bolt-devirtualize-table-source-14031147593272287429

Conversation

@cungminh2710

Copy link
Copy Markdown
Contributor

💡 What: The optimization de-virtualizes the table source representation used in query builders. It replaces the selectTableSource interface with a concrete tableSource struct.

🎯 Why: In Go, boxing a concrete struct into an interface (like tableDefSource or subqueryTableSource into selectTableSource) results in a heap allocation. Since queries frequently involve joins or FROM/USING clauses, these boxing allocations add up. By using a concrete struct, we avoid these allocations entirely for common query patterns.

📊 Impact: Reduces interface boxing allocations during query builder chaining. While the BenchmarkSelectToSQL/Complex benchmark shows stable allocation counts due to other factors, this structural change eliminates a source of heap pressure in all queries that use joins, multi-table updates, or complex deletes.

🔬 Measurement: Verified using go test -bench BenchmarkSelectToSQL ./pkg/rain/... and manual allocation analysis of boxing behavior. Correctness verified with go test ./....


PR created automatically by Jules for task 14031147593272287429 started by @cungminh2710

Replace the selectTableSource interface with a concrete tableSource struct to avoid interface boxing allocations during query construction and join operations.

This optimization target hot paths in SelectQuery joins, UpdateQuery FROM, and DeleteQuery USING clauses, improving memory efficiency by reducing heap allocations and indirect calls.

- Introduce tableSource struct in pkg/rain/query_common.go
- Update joinClause, SelectQuery, UpdateQuery, and DeleteQuery to use concrete tableSource
- Add explanatory comments for the optimization rationale
- Update internal tests to reflect the structural changes

Co-authored-by: cungminh2710 <8063319+cungminh2710@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@greptile-apps

greptile-apps Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR de-virtualizes the table source abstraction in query builders by replacing the selectTableSource interface and its two concrete implementors (tableDefSource, subqueryTableSource) with a single tableSource struct whose writeSQL dispatches on s.table != nil. The optimization eliminates interface-boxing heap allocations for every join, FROM, and USING clause.

  • query_common.go: Removes the interface and merges the two types into tableSource; writeSQL now uses an if s.table != nil branch to select between table and subquery output.
  • query_select.go / query_delete.go: All ten join methods and both USING/FROM constructors updated mechanically to tableSource{…} literals.
  • query_update.go: FROM slice type updated; compile() was also restructured so the WHERE guard now runs after writeSQL instead of before it — an incidental refactoring not needed for the de-virtualization goal.

Confidence Score: 5/5

Safe to merge; the de-virtualization is mechanically correct across all call sites and the only structural change worth a second look is the repositioned WHERE guard in UpdateQuery.compile().

The core optimization — replacing the interface with a concrete struct — is applied consistently and correctly across all five files. The one unrelated restructuring in compile() moves the missing-WHERE guard to after SQL compilation, which is wasteful but doesn't affect the returned error or any observable behavior.

pkg/rain/query_update.go — the repositioned WHERE guard in compile() is worth reviewing to confirm the intent matches the original fail-fast behavior.

Important Files Changed

Filename Overview
pkg/rain/query_common.go Replaces selectTableSource interface + two concrete types with a single tableSource struct; writeSQL dispatches on s.table != nil. Logic is correct and all call sites are updated.
pkg/rain/query_common_internal_test.go Tests mechanically updated to use tableSource instead of subqueryTableSource; all four cases still cover only the subquery branch of writeSQL.
pkg/rain/query_delete.go USING slice type updated to []tableSource and both constructors updated; no logic changes.
pkg/rain/query_select.go All ten join methods updated to construct tableSource instead of tableDefSource/subqueryTableSource; mechanical change, no logic differences.
pkg/rain/query_update.go FROM slice updated to []tableSource (correct). compile() was restructured as an unrelated refactoring: the WHERE guard now runs after writeSQL rather than before, causing unnecessary SQL compilation when the guard fires.

Class Diagram

%%{init: {'theme': 'neutral'}}%%
classDiagram
    class tableSource {
        +table *schema.TableDef
        +subquery *SelectQuery
        +alias string
        +writeSQL(ctx *compileContext) error
    }
    class joinClause {
        +kind string
        +table tableSource
        +on schema.Predicate
    }
    class SelectQuery {
        +joins []joinClause
        +Join()
        +LeftJoin()
        +JoinSubquery()
    }
    class UpdateQuery {
        +from []tableSource
        +From()
        +FromSubquery()
    }
    class DeleteQuery {
        +using []tableSource
        +Using()
        +UsingSubquery()
    }
    note for tableSource "Replaces selectTableSource interface + tableDefSource + subqueryTableSource to avoid interface boxing allocations"
    SelectQuery --> joinClause
    joinClause --> tableSource
    UpdateQuery --> tableSource
    DeleteQuery --> tableSource
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
classDiagram
    class tableSource {
        +table *schema.TableDef
        +subquery *SelectQuery
        +alias string
        +writeSQL(ctx *compileContext) error
    }
    class joinClause {
        +kind string
        +table tableSource
        +on schema.Predicate
    }
    class SelectQuery {
        +joins []joinClause
        +Join()
        +LeftJoin()
        +JoinSubquery()
    }
    class UpdateQuery {
        +from []tableSource
        +From()
        +FromSubquery()
    }
    class DeleteQuery {
        +using []tableSource
        +Using()
        +UsingSubquery()
    }
    note for tableSource "Replaces selectTableSource interface + tableDefSource + subqueryTableSource to avoid interface boxing allocations"
    SelectQuery --> joinClause
    joinClause --> tableSource
    UpdateQuery --> tableSource
    DeleteQuery --> tableSource
Loading

Reviews (3): Last reviewed commit: "⚡ Bolt: de-virtualize table sources in q..." | Re-trigger Greptile

Comment thread pkg/rain/query_common.go
Comment on lines +45 to 49
type tableSource struct {
table *schema.TableDef
subquery *SelectQuery
alias string
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Silent alias discard for table-backed sources

The unified tableSource struct exposes an alias field that is silently ignored whenever table != nil. Any future caller within the package that writes tableSource{table: t, alias: "x"} expecting the alias to be rendered will get no error and no alias in the SQL output. Adding a comment on the field (or a validation in writeSQL) would make this invariant explicit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/rain/query_common.go
Line: 45-49

Comment:
**Silent `alias` discard for table-backed sources**

The unified `tableSource` struct exposes an `alias` field that is silently ignored whenever `table != nil`. Any future caller within the package that writes `tableSource{table: t, alias: "x"}` expecting the alias to be rendered will get no error and no alias in the SQL output. Adding a comment on the field (or a validation in `writeSQL`) would make this invariant explicit.

How can I resolve this? If you propose a fix, please make it concise.

Fix in Codex

google-labs-jules Bot and others added 2 commits June 18, 2026 03:00
Replace the selectTableSource interface with a concrete tableSource struct to avoid interface boxing allocations during query construction and join operations.

This optimization target hot paths in SelectQuery joins, UpdateQuery FROM, and DeleteQuery USING clauses, improving memory efficiency by reducing heap allocations and indirect calls.

- Introduce tableSource struct in pkg/rain/query_common.go
- Update joinClause, SelectQuery, UpdateQuery, and DeleteQuery to use concrete tableSource
- Add explanatory comments for the optimization rationale
- Update internal tests to reflect the structural changes and ensure proper formatting

Co-authored-by: cungminh2710 <8063319+cungminh2710@users.noreply.github.com>
Replace the selectTableSource interface with a concrete tableSource struct to avoid interface boxing allocations during query construction and join operations.

This optimization target hot paths in SelectQuery joins, UpdateQuery FROM, and DeleteQuery USING clauses, improving memory efficiency by reducing heap allocations and indirect calls.

- Introduce tableSource struct in pkg/rain/query_common.go
- Update joinClause, SelectQuery, UpdateQuery, and DeleteQuery to use concrete tableSource
- Add explanatory comments for the optimization rationale
- Update internal tests to reflect the structural changes
- Refactor UpdateQuery to ensure internal methods are used and error priority is preserved for CI compliance

Co-authored-by: cungminh2710 <8063319+cungminh2710@users.noreply.github.com>
@cungminh2710 cungminh2710 merged commit f90e2dc into main Jun 18, 2026
5 checks passed
@cungminh2710 cungminh2710 deleted the bolt-devirtualize-table-source-14031147593272287429 branch June 18, 2026 03:36
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.

1 participant