question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use COALESCE instead of NVL for nested ??

See original GitHub issue

This is not a bug, everything works fine. It’s a small SQL codegen improvement.

linq2db uses nvl to translate ??, which is nice because it’s shorter than SQL standard coalesce.

The trick with nvl is that it only accepts 2 parameters, whereas coalesce accepts many.

In our codebase, we have a query that computes an error message based on a list of checks, it looks like this: image

That’s 7 ?? chained. The resulting SQL is of course 7 nested nvls: nvl(.., nvl(.., nvl(.., /*4 more*/))))))). It works well enough, I just think it’d be easier to read if it was a single coalesce(.., .., .., .., .., .., ..).

So my suggestion is to convert nested ?? expressions into a single coalesce instead of nested nvl. This expression tree: image Could be converted into coalesce(A, B, C, D, E) (i.e. a depth-first traversal).

A related Oracle SQL codegen you could have is convert patterns a == null ? c : d (and a != null) into nvl2(a, d, c) (resp. a, c, d). nvl2 is another Oracle proprietary function that returns the 2nd arg if the first is not null, and the 2rd arg is the first is null.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
ddas09commented, Jun 14, 2023

Seems like I need to dig up the codebase a little bit for this one. Let me raise a PR for the first part. Would take care this one in a separate PR.

1reaction
jods4commented, Jun 14, 2023

Yes, COALESCE is the SQL standard function for that purpose. NVL is an Oracle proprietary function that performs the same, but only for 2 parameters.

So you’re right, this conversion is unnecessary. As it is in place and generates shorter SQL I’d say it’s nice to keep it in? I don’t know who wrote this piece of code but it was the intention to generate “optimized” code per provider. Plus having only 2 parameters is quite a common case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

G-4230: Always use a COALESCE instead of a NVL ...
The nvl function always evaluates both parameters before deciding which one to use. This can be harmful if parameter 2 is either a...
Read more >
Why prefer COALESCE over NVL - Oracle Database
NVL always evaluates both arguments. COALESCE doesn't evaluate anything after it finds the first non-NULL argument. Did you run it multple times and...
Read more >
NVL vs. COALESCE at EXPLAIN EXTENDED
The obvious differences are that COALESCE will return the first non- NULL item in its parameter list whereas NVL only takes two parameters...
Read more >
SQL general functions | NVL, NVL2, DECODE, COALESCE ...
In simple words COALESCE() function returns the first non-null expression in the list. SELECT last_name, COALESCE(commission_pct, salary, 10) ...
Read more >
NVL and COALESCE functions
Returns the value of the first expression that isn't null in a series of expressions. An NVL expression is identical to a COALESCE...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found