1
0
Fork 0

Merging upstream version 10.0.1.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-13 14:53:05 +01:00
parent 528822bfd4
commit b7d21c45b7
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
98 changed files with 4080 additions and 1666 deletions

View file

@ -1,5 +1,9 @@
"""## Python SQL parser, transpiler and optimizer."""
from __future__ import annotations
import typing as t
from sqlglot import expressions as exp
from sqlglot.dialects import Dialect, Dialects
from sqlglot.diff import diff
@ -20,51 +24,54 @@ from sqlglot.expressions import (
subquery,
)
from sqlglot.expressions import table_ as table
from sqlglot.expressions import union
from sqlglot.expressions import to_column, to_table, union
from sqlglot.generator import Generator
from sqlglot.parser import Parser
from sqlglot.schema import MappingSchema
from sqlglot.tokens import Tokenizer, TokenType
__version__ = "9.0.6"
__version__ = "10.0.1"
pretty = False
schema = MappingSchema()
def parse(sql, read=None, **opts):
def parse(
sql: str, read: t.Optional[str | Dialect] = None, **opts
) -> t.List[t.Optional[Expression]]:
"""
Parses the given SQL string into a collection of syntax trees, one per
parsed SQL statement.
Parses the given SQL string into a collection of syntax trees, one per parsed SQL statement.
Args:
sql (str): the SQL code string to parse.
read (str): the SQL dialect to apply during parsing
(eg. "spark", "hive", "presto", "mysql").
sql: the SQL code string to parse.
read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql").
**opts: other options.
Returns:
typing.List[Expression]: the list of parsed syntax trees.
The resulting syntax tree collection.
"""
dialect = Dialect.get_or_raise(read)()
return dialect.parse(sql, **opts)
def parse_one(sql, read=None, into=None, **opts):
def parse_one(
sql: str,
read: t.Optional[str | Dialect] = None,
into: t.Optional[Expression | str] = None,
**opts,
) -> t.Optional[Expression]:
"""
Parses the given SQL string and returns a syntax tree for the first
parsed SQL statement.
Parses the given SQL string and returns a syntax tree for the first parsed SQL statement.
Args:
sql (str): the SQL code string to parse.
read (str): the SQL dialect to apply during parsing
(eg. "spark", "hive", "presto", "mysql").
into (Expression): the SQLGlot Expression to parse into
sql: the SQL code string to parse.
read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql").
into: the SQLGlot Expression to parse into.
**opts: other options.
Returns:
Expression: the syntax tree for the first parsed statement.
The syntax tree for the first parsed statement.
"""
dialect = Dialect.get_or_raise(read)()
@ -77,25 +84,29 @@ def parse_one(sql, read=None, into=None, **opts):
return result[0] if result else None
def transpile(sql, read=None, write=None, identity=True, error_level=None, **opts):
def transpile(
sql: str,
read: t.Optional[str | Dialect] = None,
write: t.Optional[str | Dialect] = None,
identity: bool = True,
error_level: t.Optional[ErrorLevel] = None,
**opts,
) -> t.List[str]:
"""
Parses the given SQL string using the source dialect and returns a list of SQL strings
transformed to conform to the target dialect. Each string in the returned list represents
a single transformed SQL statement.
Parses the given SQL string in accordance with the source dialect and returns a list of SQL strings transformed
to conform to the target dialect. Each string in the returned list represents a single transformed SQL statement.
Args:
sql (str): the SQL code string to transpile.
read (str): the source dialect used to parse the input string
(eg. "spark", "hive", "presto", "mysql").
write (str): the target dialect into which the input should be transformed
(eg. "spark", "hive", "presto", "mysql").
identity (bool): if set to True and if the target dialect is not specified
the source dialect will be used as both: the source and the target dialect.
error_level (ErrorLevel): the desired error level of the parser.
sql: the SQL code string to transpile.
read: the source dialect used to parse the input string (eg. "spark", "hive", "presto", "mysql").
write: the target dialect into which the input should be transformed (eg. "spark", "hive", "presto", "mysql").
identity: if set to `True` and if the target dialect is not specified the source dialect will be used as both:
the source and the target dialect.
error_level: the desired error level of the parser.
**opts: other options.
Returns:
typing.List[str]: the list of transpiled SQL statements / expressions.
The list of transpiled SQL statements.
"""
write = write or read if identity else write
return [