| 753 | } |
| 754 | |
| 755 | fn ambiguous_distinct_check( |
| 756 | missing_exprs: &[Expr], |
| 757 | missing_cols: &IndexSet<Column>, |
| 758 | projection_exprs: &[Expr], |
| 759 | ) -> Result<()> { |
| 760 | if missing_exprs.is_empty() { |
| 761 | return Ok(()); |
| 762 | } |
| 763 | |
| 764 | // if the missing columns are all only aliases for things in |
| 765 | // the existing select list, it is ok |
| 766 | // |
| 767 | // This handles the special case for |
| 768 | // SELECT col as <alias> ORDER BY <alias> |
| 769 | // |
| 770 | // As described in https://github.com/apache/datafusion/issues/5293 |
| 771 | let all_aliases = missing_exprs.iter().all(|e| { |
| 772 | projection_exprs.iter().any(|proj_expr| { |
| 773 | if let Expr::Alias(Alias { expr, .. }) = proj_expr { |
| 774 | e == expr.as_ref() |
| 775 | } else { |
| 776 | false |
| 777 | } |
| 778 | }) |
| 779 | }); |
| 780 | if all_aliases { |
| 781 | return Ok(()); |
| 782 | } |
| 783 | |
| 784 | let missing_col_names = missing_cols |
| 785 | .iter() |
| 786 | .map(|col| col.flat_name()) |
| 787 | .collect::<String>(); |
| 788 | |
| 789 | plan_err!( |
| 790 | "For SELECT DISTINCT, ORDER BY expressions {missing_col_names} must appear in select list" |
| 791 | ) |
| 792 | } |
| 793 | |
| 794 | /// Apply a sort by provided expressions with default direction |
| 795 | pub fn sort_by( |