Skip to main content

datafusion_expr/
select_expr.rs

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