GoogleSQL Documentation

repository·master·Indexed 25 days ago

https://github.com/google/googlesql

GoogleSQL (formerly ZetaSQL) is an analyzer framework that provides a consistent SQL language implementation, including parsing, type checking, and semantics, for various query engines. It is a reusable component for building SQL-compliant systems rather than a standalone database. The framework includes a parser, analyzer, a Resolved AST intermediate representation, and a reference implementation for executing queries via the execute_query tool.

Tokens
163.2K
Snippets
580
Records
901
Agent score
82%

What's inside GoogleSQL

  1. Overview of GoogleSQL Numbering Functions

    master

    GoogleSQL supports numbering functions, which are a subset of window functions. These functions assign values to each row based on their position within a specified window using an OVER clause.

    Supported numbering functions include:

    • CUME_DIST: Cumulative distribution (relative position (0,1]).
    • DENSE_RANK: Dense rank (1-based, no gaps).
    • IS_FIRST: Returns true if the row is within the first k rows.
    • IS_LAST: Returns true if the row is within the last k rows.
    • NTILE: Quantile bucket number (1-based).
    • PERCENT_RANK: Percentile rank (from 0 to 1).
    • RANK: Rank (1-based).
    • ROW_NUMBER: Sequential row number (1-based).
  2. Overview of GoogleSQL Geography functions

    master

    GoogleSQL supports GEOGRAPHY values and provides a suite of functions starting with ST_ to analyze, construct, and manipulate spatial data.

    Key behaviors:

    • Most functions operate on or generate GEOGRAPHY values.
    • Null Handling: All geography functions return NULL if any input argument is NULL.
  3. Overview of GoogleSQL

    master

    GoogleSQL (formerly ZetaSQL) is an analyzer framework for SQL. It defines a SQL language (grammar, types, data model, semantics, and function library) and provides parsing and analysis as a reusable component. It is not a database or query engine itself, but is intended to be used by engines to provide consistent language behavior like name resolution, type checking, and implicit casting.

    Key components include:

    • googlesql/public: Public APIs.
    • googlesql/resolved_ast: The intermediate representation (Resolved AST) produced by the analyzer.
    • googlesql/parser: Grammar and parser implementation.
    • googlesql/analyzer: Internal query analysis implementation.
    • googlesql/reference_impl: Reference implementation for executing queries.
    • googlesql/compliance: Compliance test framework.
    • googlesql/public/functions: Function implementations.
    • googlesql/tools/execute_query: Interactive query execution tool.
    • googlesql/java/com/google/googlesql: Java APIs via local RPC server.
  4. Overview of Graph Query Language (GQL) building blocks

    master
    Graph Query Language (GQL) allows for executing multiple linear graph queries in a single query. Each linear query generates a working table that is passed to the subsequent query. GQL is composed of several building blocks that can be used to construct complex queries:
  5. Overview of GoogleSQL JSON functions

    master

    GoogleSQL provides a comprehensive suite of functions for working with JSON data. These functions are categorized by their primary purpose:

    • Standard Extractors: Extract JSON data using JSON_QUERY, JSON_VALUE, JSON_QUERY_ARRAY, and JSON_VALUE_ARRAY.
    • Legacy Extractors (Deprecated): Older functions like JSON_EXTRACT and JSON_EXTRACT_SCALAR. It is recommended to use standard extractors instead.
    • Lax Converters: Flexibly convert JSON values to SQL values (e.g., LAX_BOOL, LAX_INT64) without returning errors on failure.
    • Converters: Strict conversion of JSON values to SQL values (e.g., BOOL, INT64, STRING).
    • Constructors: Create new JSON structures using JSON_ARRAY and JSON_OBJECT.
    • Mutators: Modify existing JSON using JSON_ARRAY_APPEND, JSON_ARRAY_INSERT, JSON_REMOVE, JSON_SET, and JSON_STRIP_NULLS.
    • Accessors: Retrieve JSON type information via JSON_TYPE.
    • Transformers: Apply transformations like JSON_FLATTEN.
    • Predicates: Check for properties using JSON_CONTAINS.
  6. Use data sketches for efficient aggregation

    master

    GoogleSQL supports data sketches, which are compact, serialized summaries of data aggregations. Sketches are used to reduce query time and storage requirements when computing metrics like cardinality (distinct counts) or quantiles.

    Key properties of sketches:

    • Compactness: They are typically a fixed size and asymptotically smaller than the input data.
    • Re-aggregation: Sketches can be merged to summarize the union of underlying datasets (e.g., merging daily sketches to get a weekly total).
    • Approximation: They trade precision for speed and efficiency, introducing a statistical error represented by a confidence interval (CI).
    • Lossy: Because they use lossy compression, they cannot be used to recover the original raw data.
  7. Understand Collation in GoogleSQL

    master

    Collation defines the rules for sorting and comparing strings in GoogleSQL. By default, GoogleSQL uses case-sensitive sorting (e.g., Z comes before a). Collation allows you to implement case-insensitive sorting or language-specific rules.

    To customize collation for a specific operation, you typically assign a collation specification to at least one string input in that operation.

  8. Understand GoogleSQL Collation Specifications

    master

    A collation specification determines how strings are sorted and compared. GoogleSQL supports two main types of collation specifications:

    1. Binary collation specification: Returns data in Unicode code point order. The only allowed language_tag is binary.
    2. Unicode collation specification: Uses the Unicode Collation Algorithm. It follows the format 'language_tag[:collation_attribute]'.

    If no specification is assigned, the default behavior is used. Note that a column with an empty collation does not inherit the table's default collation, whereas a column with an unassigned collation does.

  9. Understand Pipe Query Syntax

    master

    Pipe query syntax is an extension to GoogleSQL designed to be more concise than standard query syntax. It uses the pipe symbol |> followed by an operator name and arguments.

    Key characteristics:

    • Pipe operators can be appended to any valid query.
    • It can be used in queries, views, table-valued functions (TVFs), and other standard contexts.
    • Pipe syntax can be mixed with standard syntax (e.g., using a standard subquery within a pipe query).
    • A pipe operator can access any alias existing in the table preceding the pipe.
    • Queries can start with a FROM clause, allowing pipe operators to be added immediately after.
  10. Use conditional expressions in GoogleSQL

    master
    GoogleSQL supports conditional expressions that evaluate inputs from left to right with short-circuiting. This means only the chosen output value is evaluated, which can be used for error handling or performance tuning. Unlike regular functions where all inputs are evaluated before the call, conditional expressions stop evaluating once a condition is met.
  11. Use numbering functions in GoogleSQL

    master

    Numbering functions are a subset of window functions that assign values to each row based on their position within a specified window. They require an OVER clause, which can include PARTITION BY and ORDER BY specifications.

    Supported numbering functions include:

    • CUME_DIST: Cumulative distribution (0, 1].
    • DENSE_RANK: Dense rank (1-based, no gaps).
    • IS_FIRST(k): Returns true if the row is within the first k rows.
    • IS_LAST(k): Returns true if the row is within the last k rows.
    • NTILE(n): Assigns rows to n quantile buckets.
    • PERCENT_RANK: Percentile rank (0 to 1).
    • RANK: Rank (1-based, with gaps for ties).
    • ROW_NUMBER: Sequential row number (1-based).
  12. Use approximate aggregate functions in GoogleSQL

    master

    GoogleSQL provides approximate aggregate functions that are scalable in terms of memory usage and time. These functions produce statistical estimates rather than exact results, making them suitable for large data streams where linear memory usage for exact aggregation (like COUNT(DISTINCT ...)) is impractical.

    Note that these functions work directly on input data and do not allow specifying precision via sketches. For precision control using sketches, use HyperLogLog++ functions.