datafusion_sql/unparser/
extension_unparser.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 crate::unparser::ast::{QueryBuilder, RelationBuilder, SelectBuilder};
19use crate::unparser::Unparser;
20use datafusion_expr::UserDefinedLogicalNode;
21use sqlparser::ast::Statement;
22
23/// This trait allows users to define custom unparser logic for their custom logical nodes.
24pub trait UserDefinedLogicalNodeUnparser {
25    /// Unparse the custom logical node to SQL within a statement.
26    ///
27    /// This method is called when the custom logical node is part of a statement.
28    /// e.g. `SELECT * FROM custom_logical_node`
29    ///
30    /// The return value should be [UnparseWithinStatementResult::Modified] if the custom logical node was successfully unparsed.
31    /// Otherwise, return [UnparseWithinStatementResult::Unmodified].
32    fn unparse(
33        &self,
34        _node: &dyn UserDefinedLogicalNode,
35        _unparser: &Unparser,
36        _query: &mut Option<&mut QueryBuilder>,
37        _select: &mut Option<&mut SelectBuilder>,
38        _relation: &mut Option<&mut RelationBuilder>,
39    ) -> datafusion_common::Result<UnparseWithinStatementResult> {
40        Ok(UnparseWithinStatementResult::Unmodified)
41    }
42
43    /// Unparse the custom logical node to a statement.
44    ///
45    /// This method is called when the custom logical node is a custom statement.
46    ///
47    /// The return value should be [UnparseToStatementResult::Modified] if the custom logical node was successfully unparsed.
48    /// Otherwise, return [UnparseToStatementResult::Unmodified].
49    fn unparse_to_statement(
50        &self,
51        _node: &dyn UserDefinedLogicalNode,
52        _unparser: &Unparser,
53    ) -> datafusion_common::Result<UnparseToStatementResult> {
54        Ok(UnparseToStatementResult::Unmodified)
55    }
56}
57
58/// The result of unparsing a custom logical node within a statement.
59pub enum UnparseWithinStatementResult {
60    /// If the custom logical node was successfully unparsed within a statement.
61    Modified,
62    /// If the custom logical node wasn't unparsed.
63    Unmodified,
64}
65
66/// The result of unparsing a custom logical node to a statement.
67#[allow(clippy::large_enum_variant)]
68pub enum UnparseToStatementResult {
69    /// If the custom logical node was successfully unparsed to a statement.
70    Modified(Statement),
71    /// If the custom logical node wasn't unparsed.
72    Unmodified,
73}