Perform "IF", "ELSE" or "CASE" Statement in Druid

Hi,

I’m new to Druid and i would like to know how to perform a CASE or IF/ELSE Statement in my Select Query,
Example:

SELECT

CASE WHEN one = ‘1’ then ‘11’
ELSE ‘22’ END

can this be achieved on druid.

Thanks in advance

You can achieve something like this using a “javascript” extractionFn, see: http://druid.io/docs/latest/querying/dimensionspecs.html, although it’s limited to working on a single input column at a time.

Future versions of Druid will include an expression language that can express this case/when, among other things, without the need for javascript.

Thanks Gian :slight_smile:

Hi Gian,

I know Druid has gone under some changes lately to support Druid SQL. I see there are case statements allowed in that, and wondered if any of those changes allowed for similar case statements in non-sql.

Thanks,

  • Robert

Hi Robert,

They do, and generally will plan into native Druid queries using filtered aggregators or using the (not yet fully documented) expression feature. You can check it out by writing a Druid SQL query and prepending “EXPLAIN PLAN FOR” to the front to see the native Druid query that gets run. Look for “case_searched” expressions. The language is documented here: http://druid.io/docs/latest/misc/math-expr.html and we will be linking it into the rest of the docs soon.