Metadata-Version: 2.5
Name: sqlglot-pinot
Version: 0.1.3
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>=29.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 29 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')` |
| `CONCAT(a, '_', b)` | `CONCAT(CONCAT(a, '_'), b)` |
| `CONCAT_WS('-', a, b, c)` | `CONCAT(CONCAT(a, b, '-'), c, '-')` |
| `STRPOS(s, 'a')` | `STRPOS(s, 'a') + 1` |

Some Pinot functions share a name with a standard one but mean something
different, and Pinot runs the standard form without an error. The dialect
translates them:

- **`CONCAT`**: Pinot's third argument is a separator, so `CONCAT(a, '_', b)`
  would join `a` and `'_'` with `b` between them. Pinot also takes at most two
  values, so longer lists become nested calls.
- **`STRPOS`**: Pinot counts from 0 and returns -1 when the string is absent;
  the standard counts from 1 and returns 0.
- **`SUBSTR`**: Pinot's `SUBSTR(s, start, end)` counts from 0 and takes an end
  index, not a length. Its `SUBSTRING` is the standard one, so standard
  `SUBSTRING` is kept as is.

When it parses Pinot SQL, it keeps Pinot's own forms:
- `DATEDIFF(unit, start, end)` keeps all three arguments.
- `CONCAT(a, b, separator)` and `STRPOS(s, sub)` keep Pinot's meaning, so they
  round-trip unchanged and become the right functions in other dialects.
- `SUBSTR(s, start, end)` with literal bounds becomes the equivalent standard
  `SUBSTRING(s, start + 1, end - start)`.
- `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:

- 52 Pinot queries that have to round-trip unchanged;
- 29 queries from Postgres, Trino, MySQL, BigQuery, DuckDB and Snowflake that
  have to become SQL Pinot accepts;
- 23 queries whose results are checked against the value the source dialect
  means, which catches SQL that Pinot runs but reads differently.

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
104 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 (97 of 104) 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. The same goes for `SUBSTR` with non-literal
  bounds and for `STRPOS` with an occurrence argument.

## License

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