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 = SelectExpr::QualifiedWildcard(
48///     "mytable".into(),
49///     WildcardOptions::default()
50/// );
51///
52/// // SELECT col1
53/// let expr = SelectExpr::Expression(col("col1").into());
54/// ```
55#[derive(Clone, Debug)]
56pub enum SelectExpr {
57    /// Represents a wildcard (`*`) that selects all columns from all tables.
58    /// The `WildcardOptions` control additional behavior like exclusions.
59    Wildcard(WildcardOptions),
60
61    /// Represents a qualified wildcard (`table.*`) that selects all columns from a specific table.
62    /// The `TableReference` specifies the table and `WildcardOptions` control additional behavior.
63    QualifiedWildcard(TableReference, WildcardOptions),
64
65    /// Represents any other valid SELECT expression like column references,
66    /// function calls, literals, etc.
67    Expression(Expr),
68}
69
70impl fmt::Display for SelectExpr {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        match self {
73            SelectExpr::Wildcard(opt) => write!(f, "*{opt}"),
74            SelectExpr::QualifiedWildcard(table, opt) => write!(f, "{table}.*{opt}"),
75            SelectExpr::Expression(expr) => write!(f, "{expr}"),
76        }
77    }
78}
79
80impl From<Expr> for SelectExpr {
81    fn from(expr: Expr) -> Self {
82        SelectExpr::Expression(expr)
83    }
84}
85
86/// Create an [`SelectExpr::Expression`] from a [`Column`]
87impl From<Column> for SelectExpr {
88    fn from(value: Column) -> Self {
89        Expr::Column(value).into()
90    }
91}
92
93/// Create an [`SelectExpr::Expression`] from an optional qualifier and a [`FieldRef`]. This is
94/// useful for creating [`SelectExpr::Expression`] from a `DFSchema`.
95///
96/// See example on [`Expr`]
97impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for SelectExpr {
98    fn from(value: (Option<&'a TableReference>, &'a FieldRef)) -> Self {
99        Expr::from(Column::from(value)).into()
100    }
101}