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.3.0.tar.gz (35.8 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.3.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sqlglot_maxcompute-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c9c1f7fb0654fedca28920cfa1d137efd265a1395217db6834c88cbb8a773472
MD5 55f2738d453627656bb367823924e387
BLAKE2b-256 df50682adde414737cee9006c635545c068871673a6042d54ef2eb9ba9277c9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlglot_maxcompute-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bedfb569f3495574e3b07f64ce674f5dec812094c3cfb698788af3f0779d178b
MD5 f84f33b991f7f90f8ba69cff607b1bfa
BLAKE2b-256 f1b3c9eeb6703e11bc6ffc9194530c12910a77d26a0a2999a5cb7c0abb93d847

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