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.2.0.tar.gz (33.9 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.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sqlglot_maxcompute-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b61785b44c206c0548af6e9dd53bee28cc07b2b7cbd9513098b55700c8b6d824
MD5 444d2b174d153e82c7b1b201100168be
BLAKE2b-256 b9693f37b6383ca72da5344ec72bb137dcd17dc0e7edef86622ef5b919394ca2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlglot_maxcompute-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 557ee74eb4165618799d7d2ecebebeac8ed063ad9d6bfae54815da8a4c46f6dd
MD5 fca3825806b39494f813bf679aa5d742
BLAKE2b-256 12b09ea3eb79d07ab016983230a23f1c56dae0cff94f48d9decdbdccf7d199f9

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