Skip to content

Add map literal syntax #25276

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add map literal syntax #25276

wants to merge 1 commit into from

Conversation

dain
Copy link
Member

@dain dain commented Mar 11, 2025

Description

This PR adds a simple map literal syntax to the grammar. The grammar is similar to JSON object creation except that keys can be any hashable type.

Under the covers the literal is translated to map_from_entries.

Examples

{}
{'x' : 1, 'y' : 2}
{1.1 : 'a', 2.2 : 'b'}

Release notes

(X) Release notes are required, with the following suggested text:

## Section
* Add map literal syntax. For example, `{1.1 : 'a', 2.2 : 'b'}` ({issue}`issuenumber`)

@martint
Copy link
Member

martint commented Mar 12, 2025

The proposed syntax is too similar to a function call, but has very subtle differences (keys in place of argument names, arbitrary numbers of arguments) that make it confusing and potentially ambiguous.

For instance, consider the following case:

map(a => 1, b => 2)

vs

custom_map(a => 1, b => 2)

where map is the proposed map literal syntax, and custom_map is a user defined function. In the first case, a and b are references that must come from an outer scope (e.g., column names). In the second case, they are argument names that values 1 and 2 get bound to. They look visually very similar, but have vastly different semantics.

For map literals, I'd prefer to go more traditional syntax, such as {key1 : value1, key2 : value2, ... } or {key1 => value1, key2 => value2, ...} (as a side note, we may want to consider the same for arrays to be able to omit the array prefix in array[1,2,3] at some point).

See previous discussions on this topic: prestodb/presto#5065

@electrum
Copy link
Member

electrum commented Mar 12, 2025

Let's use the {key1 : value1, key2 : value2, ... } syntax, which matches JSON, JavaScript, Python, Ruby, Snowflake, CQL, etc.

@dain
Copy link
Member Author

dain commented Mar 13, 2025

Updated to the {} syntax

@martint
Copy link
Member

martint commented Mar 13, 2025

Rehashing some additional conversations we had over Slack, for future reference.

I like that the {key1 : value1, key2 : value2, ... } syntax is similar to how JSON/Javascript, Python, etc. represent maps and dictionaries, so it will be familiar to users. On the other hand, they are only superficially similar. In JSON and Javascript, they represent "objects", with arbitrary types for values and only strings for keys. In Python, dictionaries support arbitrary types for keys and values.

It could be argued that using this syntax for Trino maps prevents us from using it in the future for more general struct-like entities. Today, the way to create struct-like values in Trino is via JSON_OBJECT and the JSON type.

Copy link

github-actions bot commented Apr 4, 2025

This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack.

@github-actions github-actions bot added the stale label Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants