Skip to content

Brainfart: switch(get_value(), v -> if(v = 1, 'First'), v -> if(v = 2, 'Second', 'Default'))#28152

Closed
gertjanal wants to merge 1 commit intotrinodb:masterfrom
gertjanal:SwitchFunction
Closed

Brainfart: switch(get_value(), v -> if(v = 1, 'First'), v -> if(v = 2, 'Second', 'Default'))#28152
gertjanal wants to merge 1 commit intotrinodb:masterfrom
gertjanal:SwitchFunction

Conversation

@gertjanal
Copy link
Contributor

Description

Switch cases in a single expression are usually written like this:
CASE get_value() WHEN v = 1 THEN 'First' WHEN v = 2 THEN 'Second' ELSE 'Default' END

With the function of this PR, the expression can be written like this:
switch(get_value(), v -> if(v = 1, 'First'), v -> if(v = 2, 'Second', 'Default'))

It is shorter, but also allows more complex implementations like:
v -> if(is_valid_value(v), 'First')
or
v -> if(v = 2 OR v = 3, 'Second'):

switch(get_value(), v -> if(is_valid_value(v), 'First'), v -> if(v = 2 OR v = 3, 'Second', 'Default'))

I wonder if anyone sees any potential in this function. It was fun to implement it. When a case returns NULL, the next case is tested.

Release notes

I'll add this later when the PR is approved

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

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 Mar 2, 2026
@gertjanal gertjanal closed this Mar 12, 2026
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.

2 participants