Choose division result precision

Hello, would it be possible to have a field to choose the precision of the result during a division? Or something like that already exists? I use native queries

Thanks !

Relates to Apache Druid 0.22.1


I’m hoping you may find some way of doing that in the math functions you’ll find here:

Notice that there is more information in the java.lang.Math documentation:

I wondered whether you could do a variation of this:

I haven’t tested this (!) … would be interesting to know if you do come up with a good formula :slight_smile:

Hi @jrobin
As @petermarshallio, mentioned you should be able to use math functions with Native Queries. The function you are looking for is round( , precision) and this can be used in expressions in a Native Query.

Hi and thank you both for your answers. I would like to avoid using a function and do it with the “arithmetic” type:

			"type"   : "arithmetic",
			"name"   : "ratio",
			"fn"     : "/",
			"fields" : [
				{ "type" : "fieldAccess", "name" : "totalCompleteImpressions", "fieldName" : "totalCompleteImpressions" },
				{ "type" : "fieldAccess", "name" : "totalImpressions", "fieldName" : "totalImpressions" }

In other queries I have used:

"function": "function(revenueWithPrecision) { return revenueWithPrecision.toFixed(2); }"

But it seems overkill to me to use a javascript type for a simple ratio :thinking:

I see. You are asking about extending the arithmetic operation with a “precision” property. At a minimum it would imply some code changes here: druid/ at 741b4ed516044ef95e685419ef5cf4aa5c0d1e8e · apache/druid · GitHub

But before you or others in the community add that kind of functionality, it begs the question of why not use the built in math function round(,) ?
According to the docs, you are correct that javascript functions have potentially significant overhead, but I wonder if the built-in function overhead is really negligible. Do you want to venture a test?

1 Like

Exactly, I’ll do some testing when I have some time for them. I’ll keep you posted

1 Like