Skip to main content

Analytics Query Analyzer

Project description

analytics-query-analyzer

Analyze analytics SQL to extract referenced tables/columns and time bounds.

Install

Base install:

pip install analytics-query-analyzer

BigQuery extras (only needed for build_schema):

pip install analytics-query-analyzer[bigquery]

Support

BigQuery is currently supported and tested.

Schema format

Schemas follow sqlglot conventions, with nested fields represented as nested dicts.

Usage

analyze

Extract table/column references from a query.

from analytics_query_analyzer import analyze
from sqlglot import dialects

schema = {
    "production": {
        "shop": {
            "orders": {
                "id": "int64",
                "ordered_at": "datetime",
                "user_id": "int64",
                "payment": {
                    "amount": "int64",
                    "method": "string",
                },
            },
        },
    },
}

sql = """
select
    id,
    user_id,
    payment.amount
from
    shop.orders
"""

references = analyze(dialects.BigQuery, sql, schema, "production")
print(references)
# {"production.shop.orders": {"id", "user_id", "payment.amount"}}

analyze_timespan

Extract time bounds from filters.

from analytics_query_analyzer import analyze_timespan
from sqlglot import dialects

schema = {
    "production": {
        "shop": {
            "orders": {
                "id": "int64",
                "ordered_at": "datetime",
                "user_id": "int64",
            },
        },
    },
}

sql = """
select
    *
from
    shop.orders
where
    ordered_at >= "2025-01-01"
    and ordered_at < "2026-01-01"
"""

timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
print(timespans)
# {
#   "production.shop.orders.ordered_at": {
#     "lower": "2025-01-01",
#     "upper": "2026-01-01"
#   }
# }

To make current_date() deterministic, pass a provider:

timespans = analyze_timespan(
    dialects.BigQuery,
    "select * from shop.orders where ordered_at >= current_date()",
    schema,
    "production",
    current_date_provider=lambda: "2026-01-01",
)

build_schema

Fetch a schema dictionary from BigQuery.

from analytics_query_analyzer import build_schema
from sqlglot import dialects

schema = build_schema(dialects.BigQuery, "my_project", "my_schema", "my_table")
print(schema)
  • Authentication uses Application Default Credentials (ADC).
  • Only BigQuery is supported for build_schema.
  • When table is omitted, it scans all tables in the dataset.
  • When both dataset and table are omitted, it scans all datasets in the project.
  • The returned schema can be passed directly to analyze and analyze_timespan.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

analytics_query_analyzer-0.2.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

analytics_query_analyzer-0.2.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file analytics_query_analyzer-0.2.1.tar.gz.

File metadata

  • Download URL: analytics_query_analyzer-0.2.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for analytics_query_analyzer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e623dd0f50946274d65da7802b43bd29cbe884797f4d0ea29e22697aeea5e317
MD5 35462e6604a92b164d198cf2a6777ccf
BLAKE2b-256 11c543551e6b59fe5b298f1603a6950f325953521049e8e6dc31b70fde793339

See more details on using hashes here.

Provenance

The following attestation bundles were made for analytics_query_analyzer-0.2.1.tar.gz:

Publisher: publish-pypi.yml on logicoffee/aqa

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file analytics_query_analyzer-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for analytics_query_analyzer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4973b15bb49c90ea9a4597b55ce5731efd3a82e46a65b6712950cd4ca94bfaeb
MD5 d6d112710c03ae1cfc10310628a39875
BLAKE2b-256 adda4b30a0838d8d5c8a745221eda4ada5f4f0844c81fa2117ab51b464f1176d

See more details on using hashes here.

Provenance

The following attestation bundles were made for analytics_query_analyzer-0.2.1-py3-none-any.whl:

Publisher: publish-pypi.yml on logicoffee/aqa

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page