pub struct PivotExpander;Expand description
PIVOT Expander - Transforms PIVOT syntax into standard SQL with CASE expressions
This transformer converts SQL Server-style PIVOT operations into standard SQL using CASE expressions and GROUP BY clauses.
Transformation Example:
-- Input (PIVOT syntax):
SELECT * FROM food_eaten
PIVOT (MAX(AmountEaten) FOR FoodName IN ('Sammich', 'Pickle', 'Apple'))
-- Output (Standard SQL):
SELECT Date,
MAX(CASE WHEN FoodName = 'Sammich' THEN AmountEaten ELSE NULL END) AS Sammich,
MAX(CASE WHEN FoodName = 'Pickle' THEN AmountEaten ELSE NULL END) AS Pickle,
MAX(CASE WHEN FoodName = 'Apple' THEN AmountEaten ELSE NULL END) AS Apple
FROM food_eaten
GROUP BY DateThe algorithm:
- Detect PIVOT in FROM clause or JOINs
- Extract PIVOT specification (aggregate function, pivot column, pivot values)
- Generate CASE expression for each pivot value
- Wrap each CASE in the aggregate function
- Determine GROUP BY columns (all source columns except pivot_column and aggregate_column)
- Build new SelectStatement with CASE expressions and GROUP BY
Implementations§
Source§impl PivotExpander
impl PivotExpander
Sourcepub fn expand(statement: SelectStatement) -> Result<SelectStatement>
pub fn expand(statement: SelectStatement) -> Result<SelectStatement>
Transform a SELECT statement, expanding any PIVOT operations
Sourcepub fn expand_pivot(
source: &TableSource,
aggregate: &PivotAggregate,
pivot_column: &str,
pivot_values: &[String],
alias: &Option<String>,
) -> Result<SelectStatement>
pub fn expand_pivot( source: &TableSource, aggregate: &PivotAggregate, pivot_column: &str, pivot_values: &[String], alias: &Option<String>, ) -> Result<SelectStatement>
Expand a PIVOT operation into CASE expressions + GROUP BY
Trait Implementations§
Source§impl ASTTransformer for PivotExpander
impl ASTTransformer for PivotExpander
Source§fn description(&self) -> &str
fn description(&self) -> &str
Description of what this transformer does
Source§fn transform(&mut self, stmt: SelectStatement) -> Result<SelectStatement>
fn transform(&mut self, stmt: SelectStatement) -> Result<SelectStatement>
Transform the AST, returning the modified statement Read more
Auto Trait Implementations§
impl Freeze for PivotExpander
impl RefUnwindSafe for PivotExpander
impl Send for PivotExpander
impl Sync for PivotExpander
impl Unpin for PivotExpander
impl UnsafeUnpin for PivotExpander
impl UnwindSafe for PivotExpander
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more