Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Athena Iceberg Tables parsing issue #3126

Closed
matthias-Q opened this issue Mar 12, 2024 · 3 comments · Fixed by #3129
Closed

Athena Iceberg Tables parsing issue #3126

matthias-Q opened this issue Mar 12, 2024 · 3 comments · Fixed by #3129
Assignees

Comments

@matthias-Q
Copy link

Hi,
I want to parse a SQL Statement that creates an Iceberg table on Athena:

create table if not exists tmp.mytable (
    name string
)
location 's3://bucket/tmp/mytable/'
tblproperties (
  'table_type'='iceberg',
  'format'='parquet'
);

running

stmts = sqlglot.parse(sql, read=sqlglot.Dialects.ATHENA)
stmts[0].sql()

returns:

CREATE TABLE IF NOT EXISTS tmp.mytable 
    (name TEXT) 
LOCATION 's3://bucket/tmp/mytable/' 
WITH (
    table_type='iceberg', 
    FORMAT='parquet'
)

Unfortunately, the syntax in Athena is different for Iceberg Tables and Hive-style tables.

The parsed statement should look like this:

CREATE TABLE IF NOT EXISTS tmp.mytable 
    (name STRING) 
LOCATION 's3://bucket/tmp/mytable/' 
TBLPROPERTIES (
    'table_type'='iceberg', 
    'FORMAT'='parquet'
)

Instead of WITH -> TBLPROPERTIES
The keys in the this block are wrapped in upper quotes and iceberg has slightly different data types. In this case STRING instead of TEXT

https://docs.aws.amazon.com/athena/latest/ug/querying-iceberg-supported-data-types.html
https://docs.aws.amazon.com/athena/latest/ug/querying-iceberg-creating-tables.html

@matthias-Q
Copy link
Author

I have tested this and is does not seem to have made a difference.

In [14]: sqlglot.__version__
Out[14]: '22.4.0'

Input SQL:

create table if not exists tmp.mytable (
    name string
)
location 's3://bucket/tmp/mytable/'
tblproperties (
  'table_type'='iceberg',
  'format'='parquet'
);

Output:

In [13]: sqlglot.parse(sql, read=sqlglot.Dialects.ATHENA)[0].sql()
Out[13]: "CREATE TABLE IF NOT EXISTS tmp.mytable (name TEXT) LOCATION 's3://bucket/tmp/mytable/' WITH (table_type='iceberg', FORMAT='parquet')"

@georgesittas
Copy link
Collaborator

georgesittas commented Mar 12, 2024

You need to specify the output dialect:

>>> sql = """
... create table if not exists tmp.mytable (
...     name string
... )
... location 's3://bucket/tmp/mytable/'
... tblproperties (
...   'table_type'='iceberg',
...   'format'='parquet'
... );
... """
>>>
>>> import sqlglot
>>>
>>> sqlglot.parse_one(sql, dialect="athena").sql(dialect="athena")  # or just sqlglot.transpile(sql, read="athena")[0]
"CREATE TABLE IF NOT EXISTS tmp.mytable (name STRING) LOCATION 's3://bucket/tmp/mytable/' TBLPROPERTIES ('table_type'='iceberg', 'FORMAT'='parquet')"

@matthias-Q
Copy link
Author

Ah, thanks. I missed that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants