SortOrder¶
SortOrder is an Expression that represents the following operators in a structured query:
-
AstBuilderis requested to parse ORDER BY or SORT BY sort specifications -
Column.asc, Column.asc_nulls_first, Column.asc_nulls_last, Column.desc, Column.desc_nulls_first, and Column.desc_nulls_last high-level operators
SortOrder is also used to specify the output data ordering requirements and the required child ordering of physical operators.
Creating Instance¶
SortOrder takes the following to be created:
- Child Expression
- SortDirection
- NullOrdering
- "Same Order" Expressions
SortDirection¶
| SortDirection | Default Null Ordering | SQL |
|---|---|---|
Ascending | NullsFirst | ASC |
Descending | NullsLast | DESC |
NullOrdering¶
| NullOrdering | SQL |
|---|---|
NullsFirst | NULLS FIRST |
NullsLast | NULLS LAST |
Creating SortOrder¶
apply(
child: Expression,
direction: SortDirection,
sameOrderExpressions: Seq[Expression] = Seq.empty): SortOrder
apply creates a SortOrder (with the defaultNullOrdering of the given SortDirection).
apply is created when:
- ResolveCoalesceHints logical analysis rule is executed
- many many others
Catalyst DSL¶
Catalyst DSL defines the following operators to create SortOrders:
- asc (with
nulls first) - asc_nullsLast
- desc (with
nulls last) - desc_nullsFirst
import org.apache.spark.sql.catalyst.dsl.expressions._
val sortNullsLast = 'id.asc_nullsLast
scala> println(sortNullsLast.sql)
`id` ASC NULLS LAST
Unevaluable¶
SortOrder is an Unevaluable expression.