Skip to main content

MaxCompute dialect plugin for SQLGlot

Project description

sqlglot-maxcompute

A SQLGlot dialect plugin for Alibaba Cloud MaxCompute (formerly ODPS).

Registers the maxcompute dialect via Python entry points so that SQLGlot can parse and generate MaxCompute SQL.

Installation

pip install sqlglot-maxcompute

Usage

import sqlglot

# Parse MaxCompute SQL
ast = sqlglot.parse_one("SELECT DATEADD(dt, 1, 'DAY')", read="maxcompute")

# Transpile from another dialect to MaxCompute
sqlglot.transpile(
    "SELECT DATE_ADD(dt, 1)",
    read="spark",
    write="maxcompute",
)
# ["SELECT DATEADD(dt, 1, 'DAY')"]

# Transpile from MaxCompute to another dialect
sqlglot.transpile(
    "SELECT DATETRUNC(dt, 'MONTH')",
    read="maxcompute",
    write="spark",
)
# ["SELECT TRUNC(dt, 'MONTH')"]

# Round-trip: parse and regenerate MaxCompute SQL
sqlglot.transpile(
    "CREATE TABLE t (id INT) LIFECYCLE 30",
    read="maxcompute",
    write="maxcompute",
)
# ["CREATE TABLE t (id INT) LIFECYCLE 30"]

What's implemented

Parser (MaxCompute → canonical AST)

Category Functions
Date arithmetic DATEADD, DATEDIFF, ADD_MONTHS, MONTHS_BETWEEN
Date extraction DATEPART, DATETRUNC, TRUNC_TIME, DAYOFMONTH, DAYOFWEEK, DAYOFYEAR, HOUR, MINUTE, SECOND, QUARTER, WEEKDAY, WEEKOFYEAR
Date conversion DATE_FORMAT, TO_CHAR, TO_DATE, FROM_UNIXTIME, GETDATE, NOW, CURRENT_TIMESTAMP, CURRENT_TIMEZONE, FROM_UTC_TIMESTAMP
Last/next day LAST_DAY, LASTDAY, NEXT_DAY
String TOLOWER, TOUPPER, REGEXP_COUNT, SPLIT_PART
Aggregate WM_CONCAT, COUNT_IF, ARG_MAX, ARG_MIN, ANY_VALUE, APPROX_DISTINCT, STDDEV_SAMP, COVAR_POP, COVAR_SAMP, CORR, MEDIAN, PERCENTILE_APPROX, BITWISE_AND_AGG, BITWISE_OR_AGG, BITWISE_XOR_AGG
Array ALL_MATCH, ANY_MATCH, ARRAY_SORT, ARRAY_DISTINCT, ARRAY_EXCEPT, ARRAY_JOIN, ARRAY_MAX, ARRAY_MIN, ARRAYS_OVERLAP, ARRAYS_ZIP, ARRAY_INTERSECT, ARRAY_POSITION, ARRAY_REMOVE, ARRAY_CONTAINS
Map MAP_CONCAT, MAP_FROM_ENTRIES
JSON / misc FROM_JSON, GET_USER_ID, REGEXP_SUBSTR, SLICE, TO_MILLIS, ISDATE

Generator (canonical AST → MaxCompute SQL)

  • Date/time: DATEADD, DATEDIFF, DATETRUNC, DATEPART, GETDATE(), NOW()
  • String: TOLOWER, TOUPPER
  • Aggregate: WM_CONCAT, ARG_MAX, ARG_MIN, APPROX_DISTINCT
  • JSON/misc: FROM_JSON, GET_USER_ID(), TO_MILLIS, TO_CHAR
  • Type mapping: VARCHAR/CHAR/TEXTSTRING, DATETIME preserved

DDL

  • LIFECYCLE n — table retention in days
  • RANGE CLUSTERED BY (cols) [SORTED BY (cols)] INTO n BUCKETS
  • AUTO PARTITIONED BY (TRUNC_TIME(col, 'unit') [AS alias])
  • TBLPROPERTIES ('key'='value') coexists correctly with LIFECYCLE

Development

# Install dependencies
uv sync

# Run tests
uv run pytest

# Run a single test
uv run pytest tests/test_maxcompute.py::TestMaxCompute::test_dateadd_roundtrip

License

MIT

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

sqlglot_maxcompute-0.1.0.tar.gz (45.6 kB view details)

Uploaded Source

Built Distribution

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

sqlglot_maxcompute-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlglot_maxcompute-0.1.0.tar.gz.

File metadata

  • Download URL: sqlglot_maxcompute-0.1.0.tar.gz
  • Upload date:
  • Size: 45.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for sqlglot_maxcompute-0.1.0.tar.gz
Algorithm Hash digest
SHA256 24422b253d0b0f1e833456ff543ac58e4ba8ebdc326542de8322fa8d0fa84ea9
MD5 5478bcff611d275c1019cd26f5036ac9
BLAKE2b-256 822efc9af462f63f384a5058dcf18eb84543efbdaa7b3987d5b22787e60f8e6c

See more details on using hashes here.

File details

Details for the file sqlglot_maxcompute-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlglot_maxcompute-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bffde07955a3569b9bde856a8d8ece496c338d68e319091d1b1712e97dcb49b8
MD5 09e91bd62ed48df81a4e7a3dfb472a35
BLAKE2b-256 7ba2ef821b979b48e5ac46be503dddaee65f1f3e389ff42abb18e425ebae577a

See more details on using hashes here.

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