Skip to main content

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