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

Add map literal syntax #25276

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 uses a comma separated list of key/value entries separated by =>. This is similar to the syntax for a function call with named parameters.

Under the covers the literal is translated to map_from_entries.

Examples

map('x' => 1, 'y' => 2);
map(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, `map(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.

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