From 20739a12c39121a9e7ad3c9a2469ec5a6876199d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 3 Jun 2023 01:59:40 +0200 Subject: Merging upstream version 15.0.0. Signed-off-by: Daniel Baumann --- docs/sqlglot/executor.html | 231 ++++++++++++++++++++++++--------------------- 1 file changed, 121 insertions(+), 110 deletions(-) (limited to 'docs/sqlglot/executor.html') diff --git a/docs/sqlglot/executor.html b/docs/sqlglot/executor.html index fa13fd5..957817c 100644 --- a/docs/sqlglot/executor.html +++ b/docs/sqlglot/executor.html @@ -345,70 +345,76 @@ 14from sqlglot.errors import ExecuteError 15from sqlglot.executor.python import PythonExecutor 16from sqlglot.executor.table import Table, ensure_tables -17from sqlglot.optimizer import optimize -18from sqlglot.planner import Plan -19from sqlglot.schema import ensure_schema -20 -21logger = logging.getLogger("sqlglot") -22 -23if t.TYPE_CHECKING: -24 from sqlglot.dialects.dialect import DialectType -25 from sqlglot.executor.table import Tables -26 from sqlglot.expressions import Expression -27 from sqlglot.schema import Schema -28 +17from sqlglot.helper import dict_depth +18from sqlglot.optimizer import optimize +19from sqlglot.planner import Plan +20from sqlglot.schema import ensure_schema, flatten_schema, nested_get, nested_set +21 +22logger = logging.getLogger("sqlglot") +23 +24if t.TYPE_CHECKING: +25 from sqlglot.dialects.dialect import DialectType +26 from sqlglot.executor.table import Tables +27 from sqlglot.expressions import Expression +28 from sqlglot.schema import Schema 29 -30def execute( -31 sql: str | Expression, -32 schema: t.Optional[t.Dict | Schema] = None, -33 read: DialectType = None, -34 tables: t.Optional[t.Dict] = None, -35) -> Table: -36 """ -37 Run a sql query against data. -38 -39 Args: -40 sql: a sql statement. -41 schema: database schema. -42 This can either be an instance of `Schema` or a mapping in one of the following forms: -43 1. {table: {col: type}} -44 2. {db: {table: {col: type}}} -45 3. {catalog: {db: {table: {col: type}}}} -46 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). -47 tables: additional tables to register. -48 -49 Returns: -50 Simple columnar data structure. -51 """ -52 tables_ = ensure_tables(tables) -53 -54 if not schema: -55 schema = { -56 name: {column: type(table[0][column]).__name__ for column in table.columns} -57 for name, table in tables_.mapping.items() -58 } -59 -60 schema = ensure_schema(schema, dialect=read) -61 -62 if tables_.supported_table_args and tables_.supported_table_args != schema.supported_table_args: -63 raise ExecuteError("Tables must support the same table args as schema") -64 -65 now = time.time() -66 expression = optimize(sql, schema, leave_tables_isolated=True, dialect=read) +30 +31def execute( +32 sql: str | Expression, +33 schema: t.Optional[t.Dict | Schema] = None, +34 read: DialectType = None, +35 tables: t.Optional[t.Dict] = None, +36) -> Table: +37 """ +38 Run a sql query against data. +39 +40 Args: +41 sql: a sql statement. +42 schema: database schema. +43 This can either be an instance of `Schema` or a mapping in one of the following forms: +44 1. {table: {col: type}} +45 2. {db: {table: {col: type}}} +46 3. {catalog: {db: {table: {col: type}}}} +47 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). +48 tables: additional tables to register. +49 +50 Returns: +51 Simple columnar data structure. +52 """ +53 tables_ = ensure_tables(tables) +54 +55 if not schema: +56 schema = {} +57 flattened_tables = flatten_schema(tables_.mapping, depth=dict_depth(tables_.mapping)) +58 +59 for keys in flattened_tables: +60 table = nested_get(tables_.mapping, *zip(keys, keys)) +61 assert table is not None +62 +63 for column in table.columns: +64 nested_set(schema, [*keys, column], type(table[0][column]).__name__) +65 +66 schema = ensure_schema(schema, dialect=read) 67 -68 logger.debug("Optimization finished: %f", time.time() - now) -69 logger.debug("Optimized SQL: %s", expression.sql(pretty=True)) +68 if tables_.supported_table_args and tables_.supported_table_args != schema.supported_table_args: +69 raise ExecuteError("Tables must support the same table args as schema") 70 -71 plan = Plan(expression) -72 -73 logger.debug("Logical Plan: %s", plan) -74 -75 now = time.time() -76 result = PythonExecutor(tables=tables_).execute(plan) -77 -78 logger.debug("Query finished: %f", time.time() - now) -79 -80 return result +71 now = time.time() +72 expression = optimize(sql, schema, leave_tables_isolated=True, dialect=read) +73 +74 logger.debug("Optimization finished: %f", time.time() - now) +75 logger.debug("Optimized SQL: %s", expression.sql(pretty=True)) +76 +77 plan = Plan(expression) +78 +79 logger.debug("Logical Plan: %s", plan) +80 +81 now = time.time() +82 result = PythonExecutor(tables=tables_).execute(plan) +83 +84 logger.debug("Query finished: %f", time.time() - now) +85 +86 return result @@ -424,57 +430,62 @@ -
31def execute(
-32    sql: str | Expression,
-33    schema: t.Optional[t.Dict | Schema] = None,
-34    read: DialectType = None,
-35    tables: t.Optional[t.Dict] = None,
-36) -> Table:
-37    """
-38    Run a sql query against data.
-39
-40    Args:
-41        sql: a sql statement.
-42        schema: database schema.
-43            This can either be an instance of `Schema` or a mapping in one of the following forms:
-44            1. {table: {col: type}}
-45            2. {db: {table: {col: type}}}
-46            3. {catalog: {db: {table: {col: type}}}}
-47        read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql").
-48        tables: additional tables to register.
-49
-50    Returns:
-51        Simple columnar data structure.
-52    """
-53    tables_ = ensure_tables(tables)
-54
-55    if not schema:
-56        schema = {
-57            name: {column: type(table[0][column]).__name__ for column in table.columns}
-58            for name, table in tables_.mapping.items()
-59        }
-60
-61    schema = ensure_schema(schema, dialect=read)
-62
-63    if tables_.supported_table_args and tables_.supported_table_args != schema.supported_table_args:
-64        raise ExecuteError("Tables must support the same table args as schema")
-65
-66    now = time.time()
-67    expression = optimize(sql, schema, leave_tables_isolated=True, dialect=read)
+            
32def execute(
+33    sql: str | Expression,
+34    schema: t.Optional[t.Dict | Schema] = None,
+35    read: DialectType = None,
+36    tables: t.Optional[t.Dict] = None,
+37) -> Table:
+38    """
+39    Run a sql query against data.
+40
+41    Args:
+42        sql: a sql statement.
+43        schema: database schema.
+44            This can either be an instance of `Schema` or a mapping in one of the following forms:
+45            1. {table: {col: type}}
+46            2. {db: {table: {col: type}}}
+47            3. {catalog: {db: {table: {col: type}}}}
+48        read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql").
+49        tables: additional tables to register.
+50
+51    Returns:
+52        Simple columnar data structure.
+53    """
+54    tables_ = ensure_tables(tables)
+55
+56    if not schema:
+57        schema = {}
+58        flattened_tables = flatten_schema(tables_.mapping, depth=dict_depth(tables_.mapping))
+59
+60        for keys in flattened_tables:
+61            table = nested_get(tables_.mapping, *zip(keys, keys))
+62            assert table is not None
+63
+64            for column in table.columns:
+65                nested_set(schema, [*keys, column], type(table[0][column]).__name__)
+66
+67    schema = ensure_schema(schema, dialect=read)
 68
-69    logger.debug("Optimization finished: %f", time.time() - now)
-70    logger.debug("Optimized SQL: %s", expression.sql(pretty=True))
+69    if tables_.supported_table_args and tables_.supported_table_args != schema.supported_table_args:
+70        raise ExecuteError("Tables must support the same table args as schema")
 71
-72    plan = Plan(expression)
-73
-74    logger.debug("Logical Plan: %s", plan)
-75
-76    now = time.time()
-77    result = PythonExecutor(tables=tables_).execute(plan)
-78
-79    logger.debug("Query finished: %f", time.time() - now)
-80
-81    return result
+72    now = time.time()
+73    expression = optimize(sql, schema, leave_tables_isolated=True, dialect=read)
+74
+75    logger.debug("Optimization finished: %f", time.time() - now)
+76    logger.debug("Optimized SQL: %s", expression.sql(pretty=True))
+77
+78    plan = Plan(expression)
+79
+80    logger.debug("Logical Plan: %s", plan)
+81
+82    now = time.time()
+83    result = PythonExecutor(tables=tables_).execute(plan)
+84
+85    logger.debug("Query finished: %f", time.time() - now)
+86
+87    return result
 
-- cgit v1.2.3