Skip to content

feat: add SelectExprTrait for ergonomic alias and window chaining#1040

Merged
tyt2y3 merged 2 commits intoSeaQL:masterfrom
yuly3:issue-344
Apr 8, 2026
Merged

feat: add SelectExprTrait for ergonomic alias and window chaining#1040
tyt2y3 merged 2 commits intoSeaQL:masterfrom
yuly3:issue-344

Conversation

@yuly3
Copy link
Copy Markdown
Contributor

@yuly3 yuly3 commented Jan 11, 2026

PR Info

Changes

SelectExprTrait

Add SelectExprTrait providing alias and over methods. Implemented for both SelectExpr and T: Into<Expr>, enabling.

Expr::col(...).alias(...)
Expr::col(...).over(...)

over accepts impl Into<WindowSelectType>, with impls for

impl From<WindowStatement> for WindowSelectType;
impl<T: IntoIden> From<T> for WindowSelectType;

Compatibility and refactor

Existing helpers are kept, and their implementation now routes through SelectExprTrait.

  • SelectStatement::expr_as
  • SelectStatement::expr_window
  • SelectStatement::expr_window_as
  • SelectStatement::expr_window_name
  • SelectStatement::expr_window_name_as

Doctest fixes

Fix window-related doctests to generate executable SQL (apply the window clause to a window function, e.g. MAX(col) OVER (...), instead of a bare column).

Examples

Query::select()
    .from(Char::Table)
    .expr(Expr::col(Char::Character).alias("C"));

Query::select()
    .from(Char::Table)
    .expr(
        Expr::col(Char::Character)
            .max()
            .over(WindowStatement::partition_by(Char::FontSize))
            .alias("C"),
    );

Query::select()
    .from(Char::Table)
    .expr(Expr::col(Char::Character).max().over("w"))
    .window("w", WindowStatement::partition_by(Char::FontSize));

Design notes & feedback requested

I'd appreciate maintainer input on whether this design aligns with the project's long-term direction.

  1. Expr → SelectExpr transition

    • Option A: Explicit method like Expr::into_select() — more explicit, avoids method-name collisions
    • Option B: Extension trait allowing Expr::col(...).alias(...) directly — more ergonomic (this PR)
  2. SelectExpr construction responsibility

    • Option A: Chaining methods on SelectExpr (this PR) — simple, but moves SelectExpr toward a builder-like role
    • Option B: Separate SelectExprBuilder — keeps SelectExpr as pure data, but adds a new public type

@yuly3 yuly3 marked this pull request as ready for review January 11, 2026 10:30
@Huliiiiii Huliiiiii requested review from Expurple and tyt2y3 January 11, 2026 10:43
Comment thread src/query/select.rs Outdated
Comment on lines +250 to +261
fn window(mut self, window: WindowStatement) -> SelectExpr {
self.window = Some(WindowSelectType::Query(window));
self
}

fn window_name<W>(mut self, window: W) -> SelectExpr
where
W: IntoIden,
{
self.window = Some(WindowSelectType::Name(window.into_iden()));
self
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add impls for WindowSelectType

impl From<WindowStatement> for WindowSelectType;
impl<T: IntoIden> From<T> for WindowSelectType;

Replace window and window_name with over

fn over(mut self, over_expr: impl Into<WindowSelectType>) -> SelectExpr;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback!
I've implemented the changes you suggested.

@yuly3 yuly3 requested a review from Huliiiiii January 11, 2026 15:39
yuly3 added 2 commits April 8, 2026 20:08
- Introduce `SelectExprTrait` providing `alias`, `window`, and
  `window_name` methods for both `SelectExpr` and `T: Into<Expr>`
- Allow fluent API patterns like `.expr(Expr::col(...).alias("C"))`
- Refactor existing `expr_as`, `expr_window*` methods to use the new trait
- Update window-related doc examples to use proper window functions
- Add From<WindowStatement> and From<T: IntoIden> impls for WindowSelectType
- Replace SelectExprTrait::window() and window_name() with over(impl Into<WindowSelectType>)
@tyt2y3 tyt2y3 merged commit 7915b03 into SeaQL:master Apr 8, 2026
27 checks passed
@yuly3 yuly3 deleted the issue-344 branch April 8, 2026 23:54
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

🎉 Released In 1.0.0-rc.33 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

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.

Improve DX for alias and window

3 participants