Selecto.AutoPivot (Selecto v0.3.11)
Automatic pivot detection and application for Selecto queries.
When querying with fields from joined tables only (no source table fields), this module can automatically pivot the query to use the joined table as the source, which can be more efficient.
Auto-pivot is typically used with prevent_denormalization mode in detail views.
Aggregate views handle joins naturally through GROUP BY and don't need pivoting.
Link to this section Summary
Functions
Find the best table to pivot to based on selected columns.
Check if a selecto query should be automatically pivoted based on selected columns.
Determine if a query should be pivoted based on selected columns.
Link to this section Functions
find_pivot_target(selecto, selected_columns)
@spec find_pivot_target(Selecto.Types.t(), [String.t() | atom()]) :: atom() | nil
Find the best table to pivot to based on selected columns.
Returns the table name (as atom) that should become the new source table.
maybe_apply(selecto, opts \\ [])
@spec maybe_apply( Selecto.Types.t(), keyword() ) :: Selecto.Types.t()
Check if a selecto query should be automatically pivoted based on selected columns.
Returns the pivoted selecto if pivot is needed, otherwise returns the original.
options
Options
:view_mode- View type ("detail", "aggregate", "graph"). Only "detail" allows pivoting.
examples
Examples
# Will pivot if only category fields are selected
selecto = Selecto.configure(domain, conn)
pivoted = Selecto.AutoPivot.maybe_apply(selecto, selected: ["category.name"], view_mode: "detail")
should_pivot?(selecto, selected_columns)
@spec should_pivot?(Selecto.Types.t(), [String.t() | atom()]) :: boolean()
Determine if a query should be pivoted based on selected columns.
Pivoting is beneficial when:
- There are qualified columns from other tables
- NO source table columns are selected (they wouldn't be available after pivot)
- All qualified columns are from tables accessible from a single pivot target