### Install SQLGlot from Local Checkout Source: https://sqlglot.com/ Install SQLGlot by building from a local source checkout using the make install command. Optionally use uv for installation. ```bash make install ``` -------------------------------- ### Install Development Requirements Source: https://sqlglot.com/sqlglot.html Installs additional requirements needed for SQLGlot development. Optionally uses 'uv' for installation. ```bash # Optionally prefix with UV=1 to use uv for the installation make install-dev ``` -------------------------------- ### Install Development Requirements Source: https://sqlglot.com/ Install additional requirements for development, including optional dependencies, using the make install-dev command. Optionally use uv for installation. ```bash make install-dev ``` -------------------------------- ### Install SQLGlot with C Extensions Source: https://sqlglot.com/ Install SQLGlot with C extensions for potentially better performance, using a prebuilt wheel if available or building from source. ```bash pip3 install "sqlglot[c]" ``` -------------------------------- ### Install SQLGlot (Pure Python) Source: https://sqlglot.com/ Install the pure Python version of SQLGlot using pip. ```bash pip3 install sqlglot ``` -------------------------------- ### Install Expression Source: https://sqlglot.com/sqlglot/expressions/ddl.html Represents an INSTALL statement. ```APIDOC ## Install Expression ### Description Represents an INSTALL statement. ### Arguments - **this**: (Required) The object to install. - **from_**: (Optional) The source to install from. - **force**: (Optional) If true, forces the installation. ``` -------------------------------- ### DuckDB FORCE INSTALL Parsing Source: https://sqlglot.com/sqlglot/parsers/duckdb.html Handles the FORCE keyword when followed by INSTALL, enabling forceful installation of extensions. ```python def _parse_force(self) -> exp.Install | exp.Command: # FORCE can only be followed by INSTALL or CHECKPOINT # In the case of CHECKPOINT, we fallback if not self._match(TokenType.INSTALL): return self._parse_as_command(self._prev) return self._parse_install(force=True) ``` -------------------------------- ### Generate SQL for Install Statement Source: https://sqlglot.com/sqlglot/generators/duckdb.html Converts an Install DDL expression into DuckDB's INSTALL syntax, supporting FORCE and FROM clauses. ```python 2568 def install_sql(self, expression: exp.Install) -> str: 2569 force = "FORCE " if expression.args.get("force") else "" 2570 this = self.sql(expression, "this") 2571 from_clause = expression.args.get("from_") 2572 from_clause = f" FROM {from_clause}" if from_clause else "" 2573 return f"{force}INSTALL {this}{from_clause}" ``` -------------------------------- ### Generate INSTALL SQL for DuckDB Source: https://sqlglot.com/sqlglot/generators/duckdb.html Translates an INSTALL expression into DuckDB's INSTALL syntax, supporting the FORCE option. ```python def install_sql(self, expression: exp.Install) -> str: force = "FORCE " if expression.args.get("force") else "" this = self.sql(expression, "this") from_clause = expression.args.get("from_") from_clause = f" FROM {from_clause}" if from_clause else "" return f"{force}INSTALL {this}{from_clause}" ``` -------------------------------- ### Example of using ORDER BY in SQLGlot Source: https://sqlglot.com/sqlglot/expressions/query.html Demonstrates how to build a SQL query with an ORDER BY clause using SQLGlot's Python API. The example shows sorting by a column in descending order. ```python Select().from_("tbl").select("x").order_by("x DESC").sql() ``` -------------------------------- ### Example of using OFFSET in SQLGlot Source: https://sqlglot.com/sqlglot/expressions/query.html Demonstrates how to build a SQL query with an OFFSET clause using SQLGlot's Python API. The example shows chaining methods to construct the final SQL string. ```python Select().from_("tbl").select("x").offset(10).sql() ``` -------------------------------- ### Install Expression Source: https://sqlglot.com/sqlglot/expressions/ddl.html Represents the INSTALL statement, used for installing extensions or packages. It supports specifying the source and a force option. ```python class Install(Expression): arg_types = {"this": True, "from_": False, "force": False} ``` -------------------------------- ### SQLGlot Diff Example Source: https://sqlglot.com/sqlglot/diff.html This example demonstrates how to use the `diff` function in SQLGlot to compare two SQL queries and visualize the resulting edit script, showing removals, insertions, and kept elements. ```python >>> from sqlglot import parse_one, diff >>> diff(parse_one("SELECT a + b + c, d, e"), parse_one("SELECT a - b + c, e, f")) Remove(Add) Remove(Column(d)) Remove(Identifier(d)) Insert(Sub) Insert(Column(f)) Insert(Identifier(f)) Keep(Select, Select) Keep(Add, Add) Keep(Column(a), Column(a)) Keep(Identifier(a), Identifier(a)) Keep(Column(b), Column(b)) Keep(Identifier(b), Identifier(b)) Keep(Column(c), Column(c)) Keep(Identifier(c), Identifier(c)) Keep(Column(e), Column(e)) Keep(Identifier(e), Identifier(e)) ``` -------------------------------- ### Transpile INSTALL Expression to DuckDB SQL Source: https://sqlglot.com/sqlglot/generators/duckdb.html Generates DuckDB SQL for an INSTALL expression, which is used to install extensions. ```python def install_sql(self, expression: exp.Install) -> str: this = self.sql(expression, "this") from_clause = expression.args.get("from_") from_clause = f" FROM {from_clause}" if from_clause else "" return f"INSTALL {this}{from_clause}" ``` -------------------------------- ### Pushdown Predicates Example Source: https://sqlglot.com/sqlglot/executor.html Illustrates the `pushdown_predicates` rule, which moves filters into the innermost subqueries for optimization. ```sql SELECT * FROM ( SELECT * FROM x AS x ) AS y WHERE y.a = 1; SELECT * FROM ( SELECT * FROM x AS x ) AS y WHERE TRUE ``` -------------------------------- ### DuckDB INSTALL Statement Parsing Source: https://sqlglot.com/sqlglot/parsers/duckdb.html Parses the INSTALL statement for DuckDB, including optional FROM clauses and the FORCE option. ```python def _parse_install(self, force: bool = False) -> exp.Install: return self.expression( exp.Install( this=self._parse_id_var(), from_=self._parse_var_or_string() if self._match(TokenType.FROM) else None, force=force, ) ) ``` -------------------------------- ### Oracle (+) Outer Join Syntax Examples Source: https://sqlglot.com/sqlglot/transforms.html Examples demonstrating Oracle's (+) operator for outer joins in WHERE clauses and left correlations in SELECT statements. ```sql -- example with WHERE SELECT d.department_name, sum(e.salary) as total_salary FROM departments d, employees e WHERE e.department_id(+) = d.department_id group by department_name ``` ```sql -- example of left correlation in select SELECT d.department_name, ( SELECT SUM(e.salary) FROM employees e WHERE e.department_id(+) = d.department_id) AS total_salary FROM departments d; ``` ```sql -- example of left correlation in from SELECT d.department_name, t.total_salary FROM departments d, ( SELECT SUM(e.salary) AS total_salary FROM employees e WHERE e.department_id(+) = d.department_id ) t ``` -------------------------------- ### Boolean and Math Simplification Example Source: https://sqlglot.com/sqlglot/executor.html Illustrates the `simplify` rule for simplifying boolean expressions and mathematical operations. ```sql ((NOT FALSE) AND (x = x)) AND (TRUE OR 1 <> 3); x = x; 1 + 1; 2; ``` -------------------------------- ### Example of unsupported timedelta expression without dateutil Source: https://sqlglot.com/ This example shows a timedelta expression that the optimizer will not simplify if the dateutil module is not found. Ensure dateutil is installed for full optimizer functionality. ```sql x + interval '1' month ``` -------------------------------- ### Serve API Documentation Source: https://sqlglot.com/ Build and serve the API documentation locally using pdoc. ```bash make docs-serve ``` -------------------------------- ### Get the Leaf Nodes of the DAG Source: https://sqlglot.com/sqlglot/planner.html This property returns an iterator over the leaf nodes of the DAG. Leaf nodes represent the starting points of the execution plan, typically data sources or initial scans. ```python @property def leaves(self) -> Iterator[Step]: return (node for node, deps in self.dag.items() if not deps) ``` -------------------------------- ### Parse Snowflake GET command Source: https://sqlglot.com/sqlglot/parsers/snowflake.html Parses the Snowflake GET command, distinguishing between a GET function call and a GET statement. It extracts the file path, target location, and properties. ```python def _parse_get(self) -> exp.Expr | None: start = self._prev # If we detect GET( then we need to parse a function, not a statement if self._match(TokenType.L_PAREN): self._retreat(self._index - 2) return self._parse_expression() target = self._parse_location_path() # Parse as command if unquoted file path if self._curr.token_type == TokenType.URI_START: return self._parse_as_command(start) return self.expression( exp.Get(this=self._parse_string(), target=target, properties=self._parse_properties()) ) ``` -------------------------------- ### Qualify Tables and Columns Example Source: https://sqlglot.com/sqlglot/executor.html Demonstrates how `qualify_tables` and `qualify_columns` rules add database/catalog qualifiers to tables and ensure column unambiguity. ```sql SELECT * FROM x; SELECT "db"."x" AS "x"; ``` -------------------------------- ### Example SQL Query for Execution Source: https://sqlglot.com/sqlglot/executor.html This is a sample SQL query that will be used to demonstrate the execution steps within the SQLGlot engine. ```sql SELECT bar.a, b + 1 AS b FROM bar JOIN baz ON bar.a = baz.a WHERE bar.a > 1 ``` -------------------------------- ### Registering a Custom Dialect Plugin Source: https://sqlglot.com/ This Python snippet demonstrates how to register a custom SQL dialect using setuptools entry points. This allows SQLGlot to discover and use your custom dialect. ```python from setuptools import setup setup( name="mydb-sqlglot-dialect", entry_points={ "sqlglot.dialects": [ "mydb = my_package.dialect:MyDB", ], }, ) ``` -------------------------------- ### Run Optimization Benchmark Source: https://sqlglot.com/sqlglot.html Execute the optimization benchmark for SQLGlot. ```bash make bench-optimize ``` -------------------------------- ### BigQuery Struct Star Expansion Example Source: https://sqlglot.com/sqlglot/dialects/bigquery.html Shows how BigQuery supports expanding struct fields using star notation. This allows all fields within a struct column to be selected. ```sql SELECT t.struct_col.* FROM table t ``` -------------------------------- ### Parsing GET command in Snowflake Source: https://sqlglot.com/sqlglot/parsers/snowflake.html This snippet shows the parsing logic for the GET command in Snowflake, used to download files from a stage. It differentiates between parsing GET as a function call versus a statement and extracts the file, target, and properties. ```python return self.expression( exp.Get(this=self._parse_string(), target=target, properties=self._parse_properties()) ) ``` -------------------------------- ### SingleStore Tokenizer Keywords and Byte Strings Source: https://sqlglot.com/sqlglot/dialects/singlestore.html Illustrates the comprehensive set of keywords and byte strings recognized by the SingleStore tokenizer, including standard SQL and SingleStore-specific tokens. ```python BYTE_STRINGS = [("e'", "'"), ("E'", "'")] KEYWORDS = {'': , '{%+': , '{%-': , '%}': , '+%}': , '-%}': , '{{+': , '{{-': , '+}}': , '-}}': , '/*+': , '&<': , '&>': , '==': , '::': , '?::': , '||': , '|>': , '>=': , '<=': , '<>': , '!=': , ':=': , '<=>': , '->': , '->>': , '=>': , '#>': , '#>>': , '<->': , '<<->>': , '&&': , '??': , '~~~': , '~~': , '~~*': , '~*': , '-|-': , 'ALL': , 'AND': , 'ANTI': , 'ANY': , 'ASC': , 'AS': , 'ASOF': , 'AUTOINCREMENT': , 'AUTO_INCREMENT': , 'BEGIN': , 'BETWEEN': , 'CACHE': , 'UNCACHE': , 'CASE': , 'CHARACTER SET': , 'CLUSTER BY': , 'COLLATE': , 'COLUMN': , 'COMMIT': , 'CONNECT BY': , 'CONSTRAINT': , 'COPY': , 'CREATE': , 'CROSS': , 'CUBE': , 'CURRENT_DATE': , 'CURRENT_SCHEMA': , 'CURRENT_TIME': , 'CURRENT_TIMESTAMP': , 'CURRENT_USER': , 'CURRENT_CATALOG': , 'DATABASE': , 'DEFAULT': , 'DELETE': , 'DESC': , 'DESCRIBE': , 'DISTINCT': , 'DISTRIBUTE BY': , 'DIV': , 'DROP': , 'ELSE': , 'END': , 'ENUM': , 'ESCAPE': , 'EXCEPT': , 'EXECUTE': , 'EXISTS': , 'FALSE': , 'FETCH': , 'FILTER': , 'FILE': , 'FIRST': , 'FULL': , 'FUNCTION': , 'FOR': , 'FOREIGN KEY': , 'FORMAT': , 'FROM': , 'GEOGRAPHY': , 'GEOMETRY': , 'GLOB': , 'GROUP BY': , 'GROUPING SETS': , 'HAVING': , 'ILIKE': , 'IN': , 'INDEX': , 'INET': , 'INNER': , 'INSERT': , 'INTERVAL': ``` -------------------------------- ### Generate START TRANSACTION SQL Source: https://sqlglot.com/sqlglot/generators/presto.html Generates the SQL statement for starting a transaction, optionally including specified modes. ```python modes = expression.args.get("modes") modes = f" {', '.join(modes)}" if modes else "" return f"START TRANSACTION{modes}" ``` -------------------------------- ### Handle DuckDB Table Sample Method Source: https://sqlglot.com/sqlglot/parsers/duckdb.html Ensures that table samples in DuckDB have a method specified, defaulting to RESERVOIR if a size is provided, and SYSTEM otherwise. This aligns with DuckDB's sample syntax. ```python def _parse_table_sample(self, as_modifier: bool = False) -> exp.TableSample | None: # https://duckdb.org/docs/sql/samples.html sample = super()._parse_table_sample(as_modifier=as_modifier) if sample and not sample.args.get("method"): if sample.args.get("size"): sample.set("method", exp.var("RESERVOIR")) else: sample.set("method", exp.var("SYSTEM")) return sample ``` -------------------------------- ### Generate SUBSTRING SQL for DuckDB with Zero Start Handling Source: https://sqlglot.com/sqlglot/generators/duckdb.html Translates SUBSTRING expressions, adjusting for zero-based start positions and negative lengths to match DuckDB's behavior. ```python def substring_sql(self, expression: exp.Substring) -> str: if expression.args.get("zero_start"): start = expression.args.get("start") length = expression.args.get("length") if start := expression.args.get("start"): start = exp.If(this=start.eq(0), true=exp.Literal.number(1), false=start) if length := expression.args.get("length"): length = exp.If(this=length < 0, true=exp.Literal.number(0), false=length) return self.func("SUBSTRING", expression.this, start, length) return self.function_fallback_sql(expression) ``` -------------------------------- ### AthenaParser Initialization and Parsing Logic Source: https://sqlglot.com/sqlglot/parsers/athena.html Demonstrates the initialization of the AthenaParser, which delegates parsing to either a Hive or Trino parser based on the token stream. It also shows how parse_into is handled. ```python from __future__ import annotations import typing as t from sqlglot import exp from sqlglot.errors import ErrorLevel from sqlglot.parser import Parser from sqlglot.parsers.trino import TrinoParser from sqlglot.tokens import TokenType, Token if t.TYPE_CHECKING: from sqlglot.dialects.dialect import DialectType from sqlglot.dialects.hive import Hive from sqlglot.dialects.trino import Trino class AthenaTrinoParser(TrinoParser): STATEMENT_PARSERS = { **TrinoParser.STATEMENT_PARSERS, TokenType.USING: lambda self: self._parse_as_command(self._prev), } class AthenaParser(Parser): def __init( self, error_level: ErrorLevel | None = None, error_message_context: int = 100, max_errors: int = 3, dialect: DialectType = None, hive: Hive | None = None, trino: Trino | None = None, ) -> None: from sqlglot.dialects.hive import Hive from sqlglot.dialects.trino import Trino hive = hive or Hive() trino = trino or Trino() super().__init( error_level=error_level, error_message_context=error_message_context, max_errors=max_errors, dialect=dialect, ) self._hive_parser = hive.parser( error_level=error_level, error_message_context=error_message_context, max_errors=max_errors, ) self._trino_parser = AthenaTrinoParser( error_level=error_level, error_message_context=error_message_context, max_errors=max_errors, dialect=trino, ) def parse(self, raw_tokens: list[Token], sql: str) -> list[exp.Expr | None]: if raw_tokens and raw_tokens[0].token_type == TokenType.HIVE_TOKEN_STREAM: return self._hive_parser.parse(raw_tokens[1:], sql) return self._trino_parser.parse(raw_tokens, sql) def parse_into( self, expression_types: exp.IntoType, raw_tokens: list[Token], sql: str | None = None, ) -> list[exp.Expr | None]: if raw_tokens and raw_tokens[0].token_type == TokenType.HIVE_TOKEN_STREAM: return self._hive_parser.parse_into(expression_types, raw_tokens[1:], sql) return self._trino_parser.parse_into(expression_types, raw_tokens, sql) ``` -------------------------------- ### DuckDB DATE_TRUNC with Week Start Preservation Source: https://sqlglot.com/sqlglot/generators/duckdb.html Generates SQL for DATE_TRUNC in DuckDB, handling week start day preservation and casting the result back to the original temporal type if needed. ```python def datetrunc_sql(self, expression: exp.DateTrunc) -> str: unit = expression.args.get("unit") date = expression.this week_start = _week_unit_to_dow(unit) unit = unit_to_str(expression) if week_start: result = self.sql( _build_week_trunc_expression(date, week_start, preserve_start_day=True) ) else: result = self.func("DATE_TRUNC", unit, date) if ( expression.args.get("input_type_preserved") and date.is_type(*exp.DataType.TEMPORAL_TYPES) and not (is_date_unit(unit) and date.is_type(exp.DType.DATE)) ): return self.sql(exp.Cast(this=result, to=date.type)) return result ``` -------------------------------- ### Build Week Truncation Expression Source: https://sqlglot.com/sqlglot/generators/duckdb.html Constructs a DATE_TRUNC expression for week boundaries, allowing for custom start days. It shifts the date before truncating to align with the specified start day of the week. ```python def _build_week_trunc_expression( date_expr: exp.Expr, start_dow: int, preserve_start_day: bool = False, ) -> exp.Expr: """ Build DATE_TRUNC expression for week boundaries with custom start day. DuckDB's DATE_TRUNC('WEEK', ...) always returns Monday. To align to a different start day, we shift the date before truncating. Args: date_expr: The date expression to truncate. start_dow: ISO 8601 day-of-week number (Monday=1, ..., Sunday=7). preserve_start_day: If True, reverse the shift after truncating so the result lands on the correct week start day. Needed for DATE_TRUNC (absolute result matters) but not for DATE_DIFF (only relative alignment matters). Shift formula: Sunday (7) gets +1, others get (1 - start_dow). """ shift_days = 1 if start_dow == 7 else 1 - start_dow truncated = exp.func("DATE_TRUNC", unit=exp.var("WEEK"), this=date_expr) if shift_days == 0: return truncated shift = exp.Interval(this=exp.Literal.string(str(shift_days)), unit=exp.var("DAY")) shifted_date = exp.DateAdd(this=date_expr, expression=shift) ``` -------------------------------- ### AbstractMappingSchema Initialization Source: https://sqlglot.com/sqlglot/schema.html Initializes a schema with optional mappings for tables and UDFs. It constructs tries for efficient lookups. ```python class AbstractMappingSchema: def __init__( self, mapping: dict[str, object] | None = None, udf_mapping: dict[str, object] | None = None, ) -> None: self.mapping: dict[str, object] = mapping or {} self.mapping_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.mapping, depth=self.depth()) ) self.udf_mapping: dict[str, object] = udf_mapping or {} self.udf_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.udf_mapping, depth=self.udf_depth()) ) self._supported_table_args: tuple[str, ...] = tuple() ``` -------------------------------- ### Oracle Alias Reference Expansion Example Source: https://sqlglot.com/sqlglot/dialects/oracle.html Oracle does not support referencing aliases in projections or WHERE clauses. The original expression must be repeated instead. This example shows the invalid and valid syntax. ```sql SELECT y.foo AS bar, bar * 2 AS baz FROM y -- INVALID ``` ```sql SELECT y.foo AS bar, y.foo * 2 AS baz FROM y -- VALID ``` -------------------------------- ### Context Initialization Source: https://sqlglot.com/sqlglot/executor/context.html Initializes the execution context with data tables and an optional environment dictionary. ```APIDOC ## __init__ ### Description Initializes the execution context with data tables and an optional environment dictionary. ### Parameters #### Arguments - **tables** (dict[str, sqlglot.executor.table.Table]) - representing the scope of the current execution context. - **env** (dict | None) - dictionary of functions within the execution context. ``` -------------------------------- ### Initialize AbstractMappingSchema Source: https://sqlglot.com/sqlglot/schema.html Initializes the schema with optional mapping dictionaries for tables and UDFs. Defaults to empty dictionaries if none are provided. ```python def __init__( self, mapping: dict[str, object] | None = None, udf_mapping: dict[str, object] | None = None, ) -> None: self.mapping: dict[str, object] = mapping or {} self.mapping_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.mapping, depth=self.depth()) ) self.udf_mapping: dict[str, object] = udf_mapping or {} self.udf_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.udf_mapping, depth=self.udf_depth()) ) self._supported_table_args: tuple[str, ...] = tuple() ``` -------------------------------- ### Generate DuckDB SUBSTRING with Zero Start Handling Source: https://sqlglot.com/sqlglot/generators/duckdb.html Translates SUBSTRING expressions, specifically handling cases where the start position is zero by converting it to one. Also ensures negative lengths are treated as zero. ```python 2614 def substring_sql(self, expression: exp.Substring) -> str: if expression.args.get("zero_start"): start = expression.args.get("start") length = expression.args.get("length") if start := expression.args.get("start"): start = exp.If(this=start.eq(0), true=exp.Literal.number(1), false=start) if length := expression.args.get("length"): length = exp.If(this=length < 0, true=exp.Literal.number(0), false=length) return self.func("SUBSTRING", expression.this, start, length) return self.function_fallback_sql(expression) ``` -------------------------------- ### Initialize a Plan with an Expression Source: https://sqlglot.com/sqlglot/planner.html The Plan class initializes with a SQL expression and creates the root of the execution DAG. It also manages the DAG structure and provides access to its leaves. ```python class Plan: def __init__(self, expression: exp.Expr) -> None: self.expression: exp.Expr = expression.copy() self.root: Step = Step.from_expression(self.expression) self._dag: dict[Step, set[Step]] = {} ``` -------------------------------- ### Handle DuckDB Table Sample Methods Source: https://sqlglot.com/sqlglot/parsers/duckdb.html Ensures correct sample methods (SYSTEM or RESERVOIR) are set for DuckDB table samples based on the presence of a size. ```python def _parse_table_sample(self, as_modifier: bool = False) -> exp.TableSample | None: # https://duckdb.org/docs/sql/samples.html sample = super()._parse_table_sample(as_modifier=as_modifier) if sample and not sample.args.get("method"): if sample.args.get("size"): sample.set("method", exp.var("RESERVOIR")) else: sample.set("method", exp.var("SYSTEM")) return sample ``` -------------------------------- ### Transpile Substring with Zero Start Index in DuckDB Source: https://sqlglot.com/sqlglot/generators/duckdb.html Handles SUBSTRING expressions where the start index is 0, converting it to 1 for DuckDB's 1-based indexing. Also handles negative lengths by converting them to 0. ```python def substring_sql(self, expression: exp.Substring) -> str: if expression.args.get("zero_start"): start = expression.args.get("start") length = expression.args.get("length") if start := expression.args.get("start"): start = exp.If(this=start.eq(0), true=exp.Literal.number(1), false=start) if length := expression.args.get("length"): length = exp.If(this=length < 0, true=exp.Literal.number(0), false=length) return self.func("SUBSTRING", expression.this, start, length) return self.function_fallback_sql(expression) ``` -------------------------------- ### Get Table Width Source: https://sqlglot.com/sqlglot/executor/table.html Retrieve the number of columns in the table. ```python table.width ``` -------------------------------- ### AbstractMappingSchema Initialization Source: https://sqlglot.com/sqlglot/schema.html Initializes the AbstractMappingSchema with optional mappings for tables and UDFs. It builds tries for efficient lookups. ```python class AbstractMappingSchema: def __init__( self, mapping: dict[str, object] | None = None, udf_mapping: dict[str, object] | None = None, ) -> None: self.mapping: dict[str, object] = mapping or {} self.mapping_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.mapping, depth=self.depth()) ) self.udf_mapping: dict[str, object] = udf_mapping or {} self.udf_trie: dict[str, object] = new_trie( tuple(reversed(t)) for t in flatten_schema(self.udf_mapping, depth=self.udf_depth()) ) self._supported_table_args: tuple[str, ...] = tuple() ``` -------------------------------- ### TokenizerCore Class Initialization Source: https://sqlglot.com/sqlglot/tokenizer_core.html Initializes the TokenizerCore with various configuration dictionaries and sets that define how SQL syntax is parsed into tokens. ```python class TokenizerCore: __slots__ = ( "sql", "size", "tokens", "_start", "_current", "_line", "_col", "_comments", "_char", "_end", "_peek", "_prev_token_line", "single_tokens", "keywords", "quotes", "format_strings", "identifiers", "comments", "string_escapes", "byte_string_escapes", "identifier_escapes", "escape_follow_chars", "commands", "command_prefix_tokens", "nested_comments", "hint_start", "tokens_preceding_hint", "has_bit_strings", "has_hex_strings", "numeric_literals", "var_single_tokens", "string_escapes_allowed_in_raw_strings", "heredoc_tag_is_identifier", "heredoc_string_alternative", "keyword_trie", "numbers_can_be_underscore_separated", "numbers_can_have_decimals", "identifiers_can_start_with_digit", "unescaped_sequences", ) def __init__( self, single_tokens: dict[str, TokenType], keywords: dict[str, TokenType], quotes: dict[str, str], format_strings: dict[str, tuple[str, TokenType]], identifiers: dict[str, str], comments: dict[str, str | None], string_escapes: set[str], byte_string_escapes: set[str], identifier_escapes: set[str], escape_follow_chars: set[str], commands: set[TokenType], command_prefix_tokens: set[TokenType], nested_comments: bool, hint_start: str, tokens_preceding_hint: set[TokenType], has_bit_strings: bool, has_hex_strings: bool, numeric_literals: dict[str, str], var_single_tokens: set[str], string_escapes_allowed_in_raw_strings: bool, heredoc_tag_is_identifier: bool, heredoc_string_alternative: TokenType, keyword_trie: dict, numbers_can_be_underscore_separated: bool, numbers_can_have_decimals: bool, identifiers_can_start_with_digit: bool, unescaped_sequences: dict[str, str], ) -> None: self.single_tokens = single_tokens self.keywords = keywords self.quotes = quotes self.format_strings = format_strings self.identifiers = identifiers self.comments = comments self.string_escapes = string_escapes self.byte_string_escapes = byte_string_escapes self.identifier_escapes = identifier_escapes self.escape_follow_chars = escape_follow_chars ``` -------------------------------- ### WeekStart Function Source: https://sqlglot.com/sqlglot/expressions/functions.html The WeekStart function returns the start of the week. ```APIDOC ## WEEK_START ### Description Returns the start of the week. ### Method Signature `WEEK_START()` ``` -------------------------------- ### MySQL SHOW Command Parsers Source: https://sqlglot.com/sqlglot/parsers/mysql.html Maps various SHOW commands to their respective parsing functions, handling options like 'FULL' and targets like 'FROM'. ```python { "BINARY LOGS": _show_parser("BINARY LOGS"), "MASTER LOGS": _show_parser("BINARY LOGS"), "BINLOG EVENTS": _show_parser("BINLOG EVENTS"), "CHARACTER SET": _show_parser("CHARACTER SET"), "CHARSET": _show_parser("CHARACTER SET"), "COLLATION": _show_parser("COLLATION"), "FULL COLUMNS": _show_parser("COLUMNS", target="FROM", full=True), "COLUMNS": _show_parser("COLUMNS", target="FROM"), "CREATE DATABASE": _show_parser("CREATE DATABASE", target=True), "CREATE EVENT": _show_parser("CREATE EVENT", target=True), "CREATE FUNCTION": _show_parser("CREATE FUNCTION", target=True), "CREATE PROCEDURE": _show_parser("CREATE PROCEDURE", target=True), "CREATE TABLE": _show_parser("CREATE TABLE", target=True), "CREATE TRIGGER": _show_parser("CREATE TRIGGER", target=True), "CREATE VIEW": _show_parser("CREATE VIEW", target=True), "DATABASES": _show_parser("DATABASES"), "SCHEMAS": _show_parser("DATABASES"), "ENGINE": _show_parser("ENGINE", target=True), "STORAGE ENGINES": _show_parser("ENGINES"), "ENGINES": _show_parser("ENGINES"), "ERRORS": _show_parser("ERRORS"), "EVENTS": _show_parser("EVENTS"), "FUNCTION CODE": _show_parser("FUNCTION CODE", target=True), "FUNCTION STATUS": _show_parser("FUNCTION STATUS"), "GRANTS": _show_parser("GRANTS", target="FOR"), "INDEX": _show_parser("INDEX", target="FROM"), "MASTER STATUS": _show_parser("MASTER STATUS"), "OPEN TABLES": _show_parser("OPEN TABLES"), "PLUGINS": _show_parser("PLUGINS"), "PROCEDURE CODE": _show_parser("PROCEDURE CODE", target=True), "PROCEDURE STATUS": _show_parser("PROCEDURE STATUS"), "PRIVILEGES": _show_parser("PRIVILEGES"), "FULL PROCESSLIST": _show_parser("PROCESSLIST", full=True), "PROCESSLIST": _show_parser("PROCESSLIST"), "PROFILE": _show_parser("PROFILE"), "PROFILES": _show_parser("PROFILES"), "RELAYLOG EVENTS": _show_parser("RELAYLOG EVENTS"), "REPLICAS": _show_parser("REPLICAS"), "SLAVE HOSTS": _show_parser("REPLICAS"), "REPLICA STATUS": _show_parser("REPLICA STATUS"), "SLAVE STATUS": _show_parser("REPLICA STATUS"), "GLOBAL STATUS": _show_parser("STATUS", global_=True), "SESSION STATUS": _show_parser("STATUS"), "STATUS": _show_parser("STATUS"), "TABLE STATUS": _show_parser("TABLE STATUS"), "FULL TABLES": _show_parser("TABLES", full=True), "TABLES": _show_parser("TABLES"), "TRIGGERS": _show_parser("TRIGGERS"), "GLOBAL VARIABLES": _show_parser("VARIABLES", global_=True), "SESSION VARIABLES": _show_parser("VARIABLES"), "VARIABLES": _show_parser("VARIABLES"), "WARNINGS": _show_parser("WARNINGS"), } ``` -------------------------------- ### column_names Source: https://sqlglot.com/sqlglot/schema.html Get the column names for a specified table within the schema. ```APIDOC ## column_names ### Description Get the column names for a table. ### Method `column_names` ### Parameters #### Arguments: - **table** (Union[sqlglot.expressions.query.Table, str]): The `Table` expression instance or string representing the table. - **only_visible** (bool, optional): If True, only return visible column names. Defaults to False. - **dialect** (Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType], optional): The SQL dialect that will be used to parse `table` if it's a string. - **normalize** (bool, optional): Whether to normalize identifiers according to the dialect of interest. ``` -------------------------------- ### StartsWith Source: https://sqlglot.com/sqlglot/expressions/string.html Checks if a string starts with a specified prefix. It can be aliased as STARTSWITH. ```APIDOC ## StartsWith ### Description Checks if a string starts with a specified prefix. ### Method N/A (This is a function signature, not an API endpoint) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Build SQL Query Incrementally Source: https://sqlglot.com/ Demonstrates building a SQL query by incrementally adding conditions using `select` and `condition`. ```python from sqlglot import select, condition where = condition("x=1").and_("y=1") select("*").from_("y").where(where).sql() ``` ```text 'SELECT * FROM y WHERE x = 1 AND y = 1' ``` -------------------------------- ### Deploy New SQLGlot Version Source: https://sqlglot.com/ Steps to deploy a new version of SQLGlot, including pulling the latest changes, tagging the new version, and pushing the tag. ```bash git pull ``` ```bash git tag v28.5.0 ``` ```bash git push && git push --tags ``` -------------------------------- ### Get Expression Name Source: https://sqlglot.com/sqlglot/expressions/core.html Returns the name of the expression. This is a property of the expression object. ```python @property def name(self) -> str: raise NotImplementedError ``` -------------------------------- ### Initialize a Table Source: https://sqlglot.com/sqlglot/executor/table.html Create a new Table instance with specified columns and rows. The constructor handles optional column ranges and initializes readers for row access. ```python table = Table(columns=["col1", "col2"], rows=[(1, "a"), (2, "b")]) ``` -------------------------------- ### Get Fields Property Source: https://sqlglot.com/sqlglot/expressions/query.html Retrieves the list of 'fields' arguments from a Pivot expression. ```python @property def fields(self) -> list[Expr]: return self.args.get("fields", []) ``` -------------------------------- ### Initialize Tables Schema Source: https://sqlglot.com/sqlglot/executor/table.html Create an instance of the Tables class, which acts as a schema for multiple tables. ```python tables_schema = Tables() ``` -------------------------------- ### Get Table Length (Number of Rows) Source: https://sqlglot.com/sqlglot/executor/table.html Retrieve the number of rows in the table. ```python len(table) ``` -------------------------------- ### Substring Source: https://sqlglot.com/sqlglot/expressions/string.html Extracts a portion of a string. It can take optional start and length arguments. ```APIDOC ## SUBSTRING ### Description Extracts a portion of a string. It can take optional start and length arguments. ### Syntax SUBSTRING(this, start, length) ### Arguments - **this** (Expression): The string to extract from. - **start** (Expression, optional): The starting position for the extraction. Defaults to 1. - **length** (Expression, optional): The number of characters to extract. Defaults to extracting to the end of the string. - **zero_start** (Expression, optional): If True, the start position is 0-indexed. Defaults to False. ``` -------------------------------- ### select(*expressions, dialect=None, copy=True, **opts) Source: https://sqlglot.com/sqlglot/expressions/builders.html Initializes a syntax tree from one or multiple SELECT expressions. This function allows you to start building a SELECT statement by providing the columns or expressions to be selected. ```APIDOC ## select(*expressions, dialect=None, copy=True, **opts) ### Description Initializes a syntax tree from one or multiple SELECT expressions. This function allows you to start building a SELECT statement by providing the columns or expressions to be selected. ### Method ```python def select( *expressions: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts: Unpack[ParserNoDialectArgs], ) -> Select: ``` ### Parameters * **expressions**: The SQL code string or expression to parse as the expressions of a SELECT statement. If an Expr instance is passed, it is used as-is. * **dialect**: The dialect used to parse the input expressions (if an input expression is a SQL string). * **copy**: Whether to copy the expressions. Defaults to True. * ** **opts**: Other options to use to parse the input expressions (if an input expression is a SQL string). ### Example ```python >>> select("col1", "col2").from_("tbl").sql() 'SELECT col1, col2 FROM tbl' ``` ### Returns Select: The syntax tree for the SELECT statement. ``` -------------------------------- ### Seq8 Function Source: https://sqlglot.com/sqlglot/expressions/functions.html The Seq8 function generates a sequence of numbers starting from 8. ```APIDOC ## SEQ8 ### Description Generates a sequence of numbers starting from 8. ### Method Signature `SEQ8()` ``` -------------------------------- ### Generate SQL for EXA_SCHEMAS Source: https://sqlglot.com/sqlglot/generators/exasol.html Demonstrates how to generate a SQL query to select schema names from the EXA_SCHEMAS table using the Exasol dialect. ```python select = exp.select(exp.column("SCHEMA_NAME")).from_( exp.table_("EXA_SCHEMAS", db="SYS") ) return self.sql(select) ``` -------------------------------- ### Building a Join USING Clause Source: https://sqlglot.com/sqlglot/expressions/query.html Shows how to append or set the USING expressions for a JOIN clause with the `using` method. ```python >>> import sqlglot >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 'JOIN x USING (foo, bla)' ``` -------------------------------- ### Seq4 Function Source: https://sqlglot.com/sqlglot/expressions/functions.html The Seq4 function generates a sequence of numbers starting from 4. ```APIDOC ## SEQ4 ### Description Generates a sequence of numbers starting from 4. ### Method Signature `SEQ4()` ``` -------------------------------- ### Run Optimization Benchmark Source: https://sqlglot.com/ Execute the benchmark specifically for optimization performance. ```bash make bench-optimize ``` -------------------------------- ### Seq2 Function Source: https://sqlglot.com/sqlglot/expressions/functions.html The Seq2 function generates a sequence of numbers starting from 2. ```APIDOC ## SEQ2 ### Description Generates a sequence of numbers starting from 2. ### Method Signature `SEQ2()` ``` -------------------------------- ### Seq1 Function Source: https://sqlglot.com/sqlglot/expressions/functions.html The Seq1 function generates a sequence of numbers starting from 1. ```APIDOC ## SEQ1 ### Description Generates a sequence of numbers starting from 1. ### Method Signature `SEQ1()` ``` -------------------------------- ### Table Sample SQL Generation Source: https://sqlglot.com/sqlglot/generators/teradata.html Generates the SQL for TableSample, using the 'SAMPLE' keyword. ```python def tablesample_sql( self, expression: exp.TableSample, tablesample_keyword: str | None = None, ) -> str: return f"{self.sql(expression, 'this')} SAMPLE {self.expressions(expression)}" ``` -------------------------------- ### Getting Expression Name Source: https://sqlglot.com/sqlglot/expressions/core.html Returns the 'this' argument of the expression, which typically represents its name. ```python return self.text("this") ``` -------------------------------- ### Run Parsing Benchmark Source: https://sqlglot.com/sqlglot.html Execute the parsing benchmark for SQLGlot. ```bash make bench ``` -------------------------------- ### Get Expression Type Source: https://sqlglot.com/sqlglot/expressions/core.html Returns the data type of the expression. This is a property of the expression object. ```python @property def type(self) -> DataType | None: raise NotImplementedError ``` -------------------------------- ### Run Full Test Suite and Linter Checks Source: https://sqlglot.com/ Perform a comprehensive check including the full test suite and linter checks. ```bash make check ``` -------------------------------- ### AtTimeZone Source: https://sqlglot.com/sqlglot/expressions/core.html Represents an operation to get the time at a specific time zone. It inherits from Expression. ```APIDOC ## Class: AtTimeZone ### Description Represents getting the time at a specific time zone. ### Inheritance Inherits from `Expression`. ### Arguments * `this`: The timestamp expression. * `zone`: The time zone expression. ``` -------------------------------- ### Tokenizer Initialization Source: https://sqlglot.com/sqlglot/tokens.html Initializes the Tokenizer with an optional dialect. The dialect determines how SQL is parsed and tokenized. ```python from sqlglot.dialects import Dialect self.dialect = Dialect.get_or_raise(dialect) self._core = self._core_cache.get_or_build(type(self), self._init_core) ``` -------------------------------- ### Get Unpivot Property Source: https://sqlglot.com/sqlglot/expressions/query.html Retrieves the boolean value of the 'unpivot' argument from a Pivot expression. ```python @property def unpivot(self) -> bool: return bool(self.args.get("unpivot")) ```