datafusion_expr/select_expr.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt;
19
20use arrow::datatypes::FieldRef;
21use datafusion_common::{Column, TableReference};
22
23use crate::{expr::WildcardOptions, Expr};
24
25/// Represents a SELECT expression in a SQL query.
26///
27/// `SelectExpr` supports three types of expressions commonly found in the SELECT clause:
28///
29/// * Wildcard (`*`) - Selects all columns
30/// * Qualified wildcard (`table.*`) - Selects all columns from a specific table
31/// * Regular expression - Any other expression like columns, functions, literals etc.
32///
33/// This enum is typically used when you need to handle wildcards. After expanding `*` in the query,
34/// you can use `Expr` for all other expressions.
35///
36/// # Examples
37///
38/// ```
39/// use datafusion_expr::col;
40/// use datafusion_expr::expr::WildcardOptions;
41/// use datafusion_expr::select_expr::SelectExpr;
42///
43/// // SELECT *
44/// let wildcard = SelectExpr::Wildcard(WildcardOptions::default());
45///
46/// // SELECT mytable.*
47/// let qualified =
48/// SelectExpr::QualifiedWildcard("mytable".into(), WildcardOptions::default());
49///
50/// // SELECT col1
51/// let expr = SelectExpr::Expression(col("col1").into());
52/// ```
53#[derive(Clone, Debug)]
54pub enum SelectExpr {
55 /// Represents a wildcard (`*`) that selects all columns from all tables.
56 /// The `WildcardOptions` control additional behavior like exclusions.
57 Wildcard(WildcardOptions),
58
59 /// Represents a qualified wildcard (`table.*`) that selects all columns from a specific table.
60 /// The `TableReference` specifies the table and `WildcardOptions` control additional behavior.
61 QualifiedWildcard(TableReference, WildcardOptions),
62
63 /// Represents any other valid SELECT expression like column references,
64 /// function calls, literals, etc.
65 Expression(Expr),
66}
67
68impl fmt::Display for SelectExpr {
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 match self {
71 SelectExpr::Wildcard(opt) => write!(f, "*{opt}"),
72 SelectExpr::QualifiedWildcard(table, opt) => write!(f, "{table}.*{opt}"),
73 SelectExpr::Expression(expr) => write!(f, "{expr}"),
74 }
75 }
76}
77
78impl From<Expr> for SelectExpr {
79 fn from(expr: Expr) -> Self {
80 SelectExpr::Expression(expr)
81 }
82}
83
84/// Create an [`SelectExpr::Expression`] from a [`Column`]
85impl From<Column> for SelectExpr {
86 fn from(value: Column) -> Self {
87 Expr::Column(value).into()
88 }
89}
90
91/// Create an [`SelectExpr::Expression`] from an optional qualifier and a [`FieldRef`]. This is
92/// useful for creating [`SelectExpr::Expression`] from a `DFSchema`.
93///
94/// See example on [`Expr`]
95impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for SelectExpr {
96 fn from(value: (Option<&'a TableReference>, &'a FieldRef)) -> Self {
97 Expr::from(Column::from(value)).into()
98 }
99}