Metadata-Version: 2.5
Name: sqlglot-pinot
Version: 0.1.0
Summary: An Apache Pinot dialect for sqlglot
Project-URL: Homepage, https://github.com/hashroute3/sqlglot-pinot
Project-URL: Issues, https://github.com/hashroute3/sqlglot-pinot/issues
Author-email: Mahendran Vasagam <mahendran.vasagam@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: apache-pinot,dialect,pinot,sql,sqlglot,transpiler
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: SQL
Classifier: Topic :: Database
Requires-Python: >=3.10
Requires-Dist: sqlglot>=30.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# sqlglot-pinot

An [Apache Pinot](https://pinot.apache.org/) dialect for
[sqlglot](https://github.com/tobymao/sqlglot), packaged as a
[sqlglot dialect plugin](https://github.com/tobymao/sqlglot#creating-a-dialect-plugin).

With it, sqlglot can turn SQL written for another engine into SQL Pinot runs,
turn Pinot SQL into SQL for other engines, and parse and regenerate Pinot SQL
without breaking it.

```python
import sqlglot

sqlglot.transpile("SELECT STDDEV(x), APPROX_DISTINCT(y) FROM t", read="trino", write="pinot")
# ['SELECT STDDEV_SAMP(x), DISTINCTCOUNTHLL(y) FROM t']

sqlglot.transpile(
    "SELECT DATEDIFF('DAY', a, b), PERCENTILETDIGEST(x, 90) FROM t", read="pinot", write="bigquery"
)
# ['SELECT DATE_DIFF(b, a, DAY), APPROX_QUANTILES(x, 10)[9] FROM t']
```

## Install

```bash
pip install sqlglot-pinot
```

Installing the package registers the dialect with sqlglot under the name
`pinot`; there is nothing to import. You can also use the class directly:

```python
from sqlglot_pinot import Pinot
```

Requires Python 3.10+ and sqlglot 30 or later.

## What it handles

The dialect builds on sqlglot's Trino dialect. Pinot's multi-stage engine is
based on Apache Calcite and shares Trino's quoting rules and most of its SQL, so
only the differences are overridden.

| SQL from other dialects | Pinot SQL generated |
|---|---|
| `STDDEV(x)`, `VARIANCE(x)` | `STDDEV_SAMP(x)`, `VAR_SAMP(x)` |
| `APPROX_DISTINCT(x)`, `APPROX_COUNT_DISTINCT(x)` | `DISTINCTCOUNTHLL(x)` |
| `APPROX_PERCENTILE(x, 0.9)` | `PERCENTILETDIGEST(x, 90)` |
| `CURRENT_TIMESTAMP` | `NOW()` |
| `DATE_DIFF('day', a, b)`, `DATEDIFF(b, a)` | `DATEDIFF('DAY', a, b)` |
| `CAST(x AS VARCHAR(10))`, `BIGINT`, `DECIMAL(10, 2)` | `CAST(x AS STRING)`, `LONG`, `BIG_DECIMAL` |
| `TIMESTAMP WITH TIME ZONE` | `TIMESTAMP` (Pinot stores UTC milliseconds) |
| `` `col` `` (MySQL) | `"col"` |
| `j ->> 'a'` | `JSON_EXTRACT_SCALAR(j, '$.a', 'STRING')` |

When it parses Pinot SQL, it keeps Pinot's own forms:
- `DATEDIFF(unit, start, end)` keeps all three arguments.
- `JSON_EXTRACT_SCALAR(json, path, type, default)` keeps its type and default.
- Function names keep the case they were written in, such as `ToDateTime` and `ago`.
- Pinot's types (`LONG`, `STRING`, `BIG_DECIMAL`, `BYTES`) and `LIMIT n OFFSET m` are unchanged.

## How it is tested

[`tests/corpus.py`](tests/corpus.py) is a set of queries against the tables in
Pinot's batch QuickStart. It has 50 Pinot queries that have to round-trip
unchanged, and 29 queries from Postgres, Trino, MySQL, BigQuery, DuckDB and
Snowflake that have to become SQL Pinot accepts.

The unit tests check the generated SQL text. The live tests send every query to
a real Pinot broker, and CI runs them against Pinot 1.4.0, 1.5.1 and 1.6.0. All
79 pass on each of those releases with the multi-stage engine.
`scripts/check_versions.sh 1.6.0 1.5.1` runs the same check against fresh
QuickStart containers on both engines.

Run them yourself:

```bash
docker run -p 8000:8000 -p 9000:9000 apachepinot/pinot:1.6.0 QuickStart -type batch
PINOT_BROKER_URL=http://127.0.0.1:8000 pytest
```

## Limitations

- **The target is the multi-stage engine.** Pinot's single-stage engine has no
  joins, CTEs, window functions, `||`, interval arithmetic or `CAST(... AS
  DATE)`, so queries that use them fail there whatever dialect wrote them. The
  rest of the corpus (73 of 79) also passes on the single-stage engine.
- **Percentiles are approximate.** `APPROX_PERCENTILE` maps to Pinot's
  t-digest estimate. Exact `PERCENTILE_CONT` is not translated.
- **Pinot-only functions pass through as written.** Functions such as
  `ToDateTime`, `JSON_MATCH` or `ago` survive a Pinot round trip but are not
  rewritten for other dialects.

## License

Apache License 2.0. See [LICENSE](LICENSE).
