|
| 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 | + |
| 18 | +"""This module provides support for unparsing datafusion plans to SQL. |
| 19 | +
|
| 20 | +For additional information about unparsing, see https://docs.rs/datafusion-sql/latest/datafusion_sql/unparser/index.html |
| 21 | +""" |
| 22 | + |
| 23 | +from ._internal import unparser as unparser_internal |
| 24 | +from .plan import LogicalPlan |
| 25 | + |
| 26 | + |
| 27 | +class Dialect: |
| 28 | + """DataFusion data catalog.""" |
| 29 | + |
| 30 | + def __init__(self, dialect: unparser_internal.Dialect) -> None: |
| 31 | + """This constructor is not typically called by the end user.""" |
| 32 | + self.dialect = dialect |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def default() -> "Dialect": |
| 36 | + """Create a new default dialect.""" |
| 37 | + return Dialect(unparser_internal.Dialect.default()) |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def mysql() -> "Dialect": |
| 41 | + """Create a new MySQL dialect.""" |
| 42 | + return Dialect(unparser_internal.Dialect.mysql()) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def postgres() -> "Dialect": |
| 46 | + """Create a new PostgreSQL dialect.""" |
| 47 | + return Dialect(unparser_internal.Dialect.postgres()) |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def sqlite() -> "Dialect": |
| 51 | + """Create a new SQLite dialect.""" |
| 52 | + return Dialect(unparser_internal.Dialect.sqlite()) |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def duckdb() -> "Dialect": |
| 56 | + """Create a new DuckDB dialect.""" |
| 57 | + return Dialect(unparser_internal.Dialect.duckdb()) |
| 58 | + |
| 59 | + |
| 60 | +class Unparser: |
| 61 | + """DataFusion unparser.""" |
| 62 | + |
| 63 | + def __init__(self, dialect: Dialect) -> None: |
| 64 | + """This constructor is not typically called by the end user.""" |
| 65 | + self.unparser = unparser_internal.Unparser(dialect.dialect) |
| 66 | + |
| 67 | + def plan_to_sql(self, plan: LogicalPlan) -> str: |
| 68 | + """Convert a logical plan to a SQL string.""" |
| 69 | + return self.unparser.plan_to_sql(plan._raw_plan) |
| 70 | + |
| 71 | + def with_pretty(self, pretty: bool) -> "Unparser": |
| 72 | + """Set the pretty flag.""" |
| 73 | + self.unparser = self.unparser.with_pretty(pretty) |
| 74 | + return self |
| 75 | + |
| 76 | + |
| 77 | +__all__ = [ |
| 78 | + "Dialect", |
| 79 | + "Unparser", |
| 80 | +] |
0 commit comments