Convert sql [OrderByExpr] to `Vec `. `input_schema` and `additional_schema` are used to resolve column references in the order-by expressions. `input_schema` is the schema of the input logical plan, typically derived from the SELECT list. Usually order-by expressions can only reference the input plan's columns. But the `SELECT ... FROM ... ORDER BY ...` syntax is a special case. Besides the
(
&self,
order_by_exprs: Vec<OrderByExpr>,
input_schema: &DFSchema,
planner_context: &mut PlannerContext,
literal_to_column: bool,
additional_schema: Op
| 40 | /// |
| 41 | /// If false, interpret numeric literals as constant values. |
| 42 | pub(crate) fn order_by_to_sort_expr( |
| 43 | &self, |
| 44 | order_by_exprs: Vec<OrderByExpr>, |
| 45 | input_schema: &DFSchema, |
| 46 | planner_context: &mut PlannerContext, |
| 47 | literal_to_column: bool, |
| 48 | additional_schema: Option<&DFSchema>, |
| 49 | ) -> Result<Vec<SortExpr>> { |
| 50 | if order_by_exprs.is_empty() { |
| 51 | return Ok(vec![]); |
| 52 | } |
| 53 | |
| 54 | let mut combined_schema; |
| 55 | let order_by_schema = match additional_schema { |
| 56 | Some(schema) => { |
| 57 | combined_schema = input_schema.clone(); |
| 58 | combined_schema.merge(schema); |
| 59 | &combined_schema |
| 60 | } |
| 61 | None => input_schema, |
| 62 | }; |
| 63 | |
| 64 | let mut sort_expr_vec = Vec::with_capacity(order_by_exprs.len()); |
| 65 | |
| 66 | let make_sort_expr = |expr: Expr, |
| 67 | asc: Option<bool>, |
| 68 | nulls_first: Option<bool>| { |
| 69 | let asc = asc.unwrap_or(true); |
| 70 | let nulls_first = nulls_first |
| 71 | .unwrap_or_else(|| self.options.default_null_ordering.nulls_first(asc)); |
| 72 | Sort::new(expr, asc, nulls_first) |
| 73 | }; |
| 74 | |
| 75 | for order_by_expr in order_by_exprs { |
| 76 | let OrderByExpr { |
| 77 | expr, |
| 78 | options: OrderByOptions { asc, nulls_first }, |
| 79 | with_fill, |
| 80 | } = order_by_expr; |
| 81 | |
| 82 | if let Some(with_fill) = with_fill { |
| 83 | return not_impl_err!("ORDER BY WITH FILL is not supported: {with_fill}"); |
| 84 | } |
| 85 | |
| 86 | let expr = match expr { |
| 87 | SQLExpr::Value(ValueWithSpan { |
| 88 | value: Value::Number(v, _), |
| 89 | span: _, |
| 90 | }) if literal_to_column => { |
| 91 | let field_index = v |
| 92 | .parse::<usize>() |
| 93 | .map_err(|err| plan_datafusion_err!("{}", err))?; |
| 94 | |
| 95 | if field_index == 0 { |
| 96 | return plan_err!( |
| 97 | "Order by index starts at 1 for column indexes" |
| 98 | ); |
| 99 | } else if input_schema.fields().len() < field_index { |
no test coverage detected