Repository navigation
Description
Motivation
This proposal is to improve handling of null values in druid by treating nulls as missing values which don’t actually exist. This will make it more compatible to SQL standard and help with integration with other existing BI systems which support ODBC/JDBC.
Current Behavior
Current Null handling in druid is as follows -
- String Dimensions - Nulls and Empty Strings are considered equivalent
- Long/Float columns Nulls are considered equivalent to 0.
Proposed Behavior Changes for Null Handling
Null semantics after the changes described in proposal -
- Null and Empty Strings will be handled differently
- For every column type, null value will be considered equivalent to an absent/missing value.
- Aggregate functions will ignore null values. If the data set contains no rows, or contains only rows with nulls as arguments to the aggregate function, then the function returns null except count which will return 0 instead of null.
e.g- a min aggregation over null and 5 will give a result of 5
- longSum/doubleSum over values (null, 1, null) will return 1.
- Count over values (null, 1, null) will return 1
- Count over values (null, null) will return 0
- Rollup of metrics - if a null and a non-null value are rolled up using an aggregator at ingestion time the result will be defined by the behavior of aggregator specified above.
- Math expressions - the result value will be null if any of the components is null.
- missing column will be considered equivalent to a column having null values for all the rows.
- In case of Sorting, by default nulls will be sorted as the lowest value. Users can specify to sort nulls as the highest value as part of the query.
- Users will be able to specify default values to replace NULL values at ingestion time.
- Comparisons between null and non-null values will follow three-valued logic and the result of a comparison involving NULL is not a boolean value – it is a null value.
Implementation Changes -
Ability to specify default values for dimensions/metrics while ingestion
This idea is to allow users to specify a default value for each column that can be used to replace nulls during ingestion.. e.g
{
"type" : "String",
"name" : "column1",
"replaceMissingValuesWith" : "abc"
},
{
"type" : "Long",
"Name": "column2",
"replaceMissingValuesWith" : 5
} In this case, any null value in column1 will be replaced with String “abc” and any null value for long column will be replaced with 5. The final segment created will store these default values instead of nulls.
Storage Format changes
The current storage format is not sufficient for being able to differentiate between the fact that whether a value is null or not. We need to change the storage format to also store the information about the presence of a value. we can choose following options -
- For String columns - we can choose one of the following -
- In the dictionary encoding reserve 0 as being mapped to null value, any other value will get dictionary encoding value > 0. This will also work for multi value column, where an array of values can store 0 as the id. This will need incrementing segment version for backwards compatibility.
- Add a presence bitmap which tells whether the value is present or absent in the row. For a row with multiple value, it may be possible that both the presence bit is not set and the row have some values, which will indicate that the value is a multi value including null.
- For long/Float columns - Add a presence bitmap to the columns which tells whether the value is present or not.
For String column, it seems approach (a) will be better as it will allow simplifying handling of multi values columns also.
We can also skip creating a presence bitmap for the case when all the values in a column are known to be non-null.
After the changes the schema for the columns will be as follows -
- String Column -
- Dictionary Encoding starting will 1, 0 will be reserved for mapping null value.
- Column Data
- Bitmap Indexes
- Numeric Column -
- Column Data
- Presence Bitmap
Nullability for Aggregators/Metrics
As per the current implementation, most aggregators are coded to replace null values with default e.g. sum treats them as zeroes, min treats them as positive infinity, etc.
To match the new semantics we need to make following changes -
- Make aggregators nullable, where if an aggregator encounters only the null values the result will be null value.
- The behavior will be same for max,min and sum except that they will return null in case all the encountered values are null.
- Count aggregator will ignore null values and only count non-null values, If all the encountered values are null, the result will be 0. This is different from current behavior where null values are also counted.
Math Expressions Null Handling
Null handling for math expressions will be similar to the aggregators. For general calculations like sum, full expression will be considered as null if any of the components is null. Specifying a default value for null will be supported by the use of NVL or IF clause to assign default values at query time.
Filtering on Null values
SelectorDimFilter currently specifies filtering on null values but the implementation assumes null and empty strings as equivalent. The implementation will be changed to consider null and empty string differently. Cache key for selectorDimFilter also assumes.
Generation of cache key for selectorDimFilter also needs to be modified to support null.
Changes to Druid build-in SQL layer
- IS NULL and IS NOT NULL will be mapped to SelectorDimFilter with value null.
- NVL, IFNULL and COALESCE will be mapped to DimensionSpec replaceMissingValueWith property.
Misc Changes
Above are the major changes in the user facing APIs and behavior. Other than these there are multiple places in the code where we convert empty Strings to null and vice-versa. They will be changed accordingly in order to treat null and String values separately.
Backwards Compatibility
- For backwards compatibility we can add a new runtime property to preserve old behavior. e.g 'druid.null.behavior' If this property is set to 'sql' we will follow the sql behavior, If this is not set we will preserve backwards compatibility.
- Query execution backwards compatibility - There are lots of places in code that are based on current druid null handling e.g count aggregator. They will need to be made backwards compatible in order to preserve current behavior where null and "" the treated the same. This would mean having two branches of the code at all the places which currently assumes null and empty strings to be equivalent during query execution e.g filters, extraction functions, lookups, count aggregator and more.
- Ingestion time - At ingestion we replace null with empty strings, the same behavior will be preserved by having multiple code paths based on the 'druid.null.behavior' property
- Increment the versions of columns for supporting backwards compatibility in storage changes and be able to read columns with older version.
Activity
gianm commentedon Jun 2, 2017
I thought this day would never come.
@nishantmonu51 you didn't mention motivation in the description, but I guess it's so Druid's runtime would be able to treat nulls in a more SQL standard way. Some initial thoughts:
If the goal is more SQL-like behavior, you don't want this, since it's not how SQL works. SQL aggregators like
SUM(expr)skip NULLs but still aggregate all non-NULLs. It's still different from what Druid does though (especially for MIN/MAX where Druid would treat missing as 0 and standard SQL behavior would skip it).You're missing a big one, which is backwards compatibility in the runtime: preserving current behavior where
nulland""the treated the same. There's a lot of code out there that expects Druid's null handling to work the way it currently does, and we don't want to break it.drcrallen commentedon Jun 2, 2017
In SQL if you have this data
(null)and do
SELECT SUM(value) WHERE `ATTR` != 'z'you will get 3 if I recall correctly, where most people expect 4.Which is not necessarily a good thing.
Here's an example using DERBY embedded:
this gives the following:
IMHO this behavior is super annoying and detrimental to the flexible schema approach that druid uses. So I don't think just blindly taking SQL null handling is a good idea.
b-slim commentedon Jun 2, 2017
@drcrallen I think the idea behind following a standard is to make interoperability between druid and other system easy, is there a better standard ?
Also by adding this support it will actually add more flexibility for instance you can elect to replace
nullwith empty therefore youSELECT SUM(value) WHEREATTR!= 'z'will return4instead of3and vice versa. Anyway i agree with you SQL is not god's word and we can elect to not follow it as it is.jihoonson commentedon Jun 3, 2017
@nishantmonu51 nice proposal! I think three-valued logic also needs to be considered for comparing nulls and non-null values.
@drcrallen @gianm I vote for making our null handing compatible with the SQL standard. I think choosing a SQL-like but non-standard language is like reinventing another wheel which encompasses a lot of pains. We eventually need to follow another better standard or make our own standard. This means we need to worry about the ecosystem around druid's special language.
Instead, if druid speaks a language compatible with the SQL standard, SQL is one of the most popular languages, and thus we can get a lot of advantages like easy integration with many existing BI tools which support ODBC/JDBC.
nishantmonu51 commentedon Jun 5, 2017
@gianm @jihoonson : Thanks for the comments have updated the proposal to include your comments.
@drcrallen: As @b-slim and @jihoonson pointed out following the SQL standard makes it easier to make Druid work with other existing tools. For the above case you mentioned, with the above proposal, the user has the ability to replace null with any default value at the ingestion time and achieve the desired behavior.
drcrallen commentedon Jun 5, 2017
gives the expected result of
D.As long as there isn't a performance hit for doing it this way instead of just the druid not filter and selector, it should be ok.
Can you give more information on the backwards compatibility / migration plan for "already indexed" data at query time?
gianm commentedon Jun 6, 2017
@nishantmonu51,
I don't think the replacement at ingestion time is enough to preserve the current behavior. There's a lot of places in the query runtime where nulls and empty strings are treated the same, like comparisons in filters (consider
"filter":{"type":"selector","dimension":"foo", "value":null}and"filter":{"type":"selector","dimension":"foo", "value":""}which currently have identical behavior). Other examples are args to extractionFns, return values from extractionFns, and behavior of keys into lookups. It seems to me that if we want to maintain compatibility then a lot of places in the runtime will need to have two code paths.nishantmonu51 commentedon Jun 6, 2017
@gianm @drcrallen, I see, yeah, to be fully backwards compatible the places at query time which treats empty string and nulls as same will need to have two separate code paths based on a druid runtime property e.g druid.null.behavior by default it will treat null and empty strings as same and preserve the current behavior and users can set it to 'sql' to get the new sql behavior.
I also feel that having two separate code paths and behavior at lots of places may not be a good and will lead to confusion for users in long run and I would propose to move to sql behavior in future major releases.
nishantmonu51 commentedon Jun 6, 2017
Also updated the backwards compatibility section in the description
julianhyde commentedon Jun 6, 2017
This would be awesome. Thank you for proposing it.
@drcrallen Your statement "most people expect 4" is debatable. I mean that in the best sense of "debatable". In the Druid community, you are probably right that most people expect 4. In the broader data community, people have gotten used to SQL semantics, and so they would expect 3.
Suppose I am using a database I've not used before -- say MongoDB or Elasticsearch -- and I write their equivalent of
where attr <> 3. I don't want to have to go to their manual to see how they handle null values. I want to be able to assume that their behavior is standard. Whatever their behavior, it's pretty easy to achieve the effect I want (e.g.where attr <> 3 or attr is null), but they're wasting my time if they violate the principle of least surprise.Can you generalize "Math expressions" to "All operators and functions unless stated otherwise". Almost all the built-in functions and operators in SQL generate NULL if any of their arguments are NULL. (Notable exceptions are the
ISoperators likeIS NULL, boolean operatorsOR,ANDandNOT,COALESCE.) But evenSUBSTRINGandCHAR_LENGTH` adhere to this rule. (Remember, the empty string is not null.)In my opinion, the ability to store and retrieve null values is more important than 3-valued logic. And 3-valued logic will take a lot of time & effort to engineer. So, could you consider doing 3-valued logic in a second phase?
In phase 1 you could do all of the other features but still retain 2-valued logic (with
x = yreturning false if one of them is null).Don't get me wrong: we need 3-valued logic to be SQL compliant. But if Druid can simply store and retrieve null values, and has a consistent semantics, we (by which I mean us folks building a SQL adapter using Calcite and Hive) can work with that in the short to medium term.
jihoonson commentedon Jun 7, 2017
I thought that this proposal is for a long term goal making Druid SQL-compliant. I agree on going forward step by step.
gianm commentedon Mar 23, 2018
I'm not sure where to bring this up, so I'll do it here.
I was reading through the null handling code and started thinking that it doesn't make sense for
druid.generic.useDefaultValueForNullto determine indexing time behavior. IMO the system-wide setting should really only control query-time behavior. And what it should do is simple: if it's true (the default) then:But it should not affect indexing at all. That should be driven by the indexing spec.
The reason is that it should be possible to specify a column as nullable or non-nullable on a per column basis. You shouldn't have to be locked into all your columns being one way or the other via a systemwide property. It's similar to how a relational db would let you say column
xisBIGINT NOT NULLbut columnyisBIGINT NULLin the same table./cc @nishantmonu51
17 remaining items
leventov commentedon Aug 1, 2019
@WilliamWhispell there is nothing wrong with this line.
emptyToNullIfNeeded()method may be misused somewhere, but the method itself is fine.pantaoran commentedon Aug 8, 2019
I just learned of the existence of the config field
druid.generic.useDefaultValueForNullfrom the 0.13.0 release notes, but I can't find any mention of the field in the general configuration reference at https://druid.apache.org/docs/latest/configuration/index.htmlAnd if the flag is implemented and released, why is this issue still open? Sorry I don't have time to read through all the replies here. But could you please document the config field?
pan3793 commentedon May 27, 2020
Has this proposal been completely finished now?
noskcire11 commentedon Aug 29, 2020
i guess proposal no 6 means to preserve column metadata on all null columns, right?
in my case(1 of 2 cases): i have this column which only contain null values (for now). after ingesting it to druid, the column is missing (the datasource doesn't have that column). so my query in some app to druid using that column ended up as an error (no column). and when some other segment ingested with the column contains non null values, the datasource finally have that column (even though only 1 segment have that non null value)
the other case is with turnilo, which i assume only using the last segment metadata as the datasource structure when the turnilo refresh it. so if the last segment ingested contains an 'all-null' column, it resulted in an error
bharadwajrembar commentedon Dec 16, 2020
Hey @noskcire11 How did you overcome this? All segments have null values for the dimension so the dimension ends up being removed in the column metadata for that datasource.
steliosph commentedon May 13, 2021
From my understanding and testing it appears that if Druid has no Values in any of the rows (all nulls) it will automatically drop that column in end when completing the ingestion.
Don't know if there is any handling to prevent this.
jihoonson commentedon May 14, 2021
This is the SQL layer issue that it cannot make a query plan with a missing column. Druid query engine treats the missing column and the column of null values the same.
github-actions commentedon Jun 1, 2023
This issue has been marked as stale due to 280 days of inactivity.
It will be closed in 4 weeks if no further activity occurs. If this issue is still
relevant, please simply write any comment. Even if closed, you can still revive the
issue at any time or discuss it on the [email protected] list.
Thank you for your contributions.
github-actions commentedon Jun 30, 2023
This issue has been closed due to lack of activity. If you think that
is incorrect, or the issue requires additional review, you can revive the issue at
any time.
jmitterh commentedon Jun 30, 2023
Float columns Nulls are considered equivalent to 0. Is this still true?
Update two Javadocs to cite druid.generic.useDefaultValueForNull (apa…