Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add tutorial with examples of sql null handling #16185

Merged
merged 17 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 31 additions & 16 deletions docs/querying/sql-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,38 @@ as regular JSON arrays instead of in stringified form.

## NULL values

By default, Druid treats NULL values closely to the SQL standard.
techdocsmith marked this conversation as resolved.
Show resolved Hide resolved
In the default mode:
- numeric NULL is permitted
techdocsmith marked this conversation as resolved.
Show resolved Hide resolved
- NULL values and empty strings are not equal.

This manner of null handling applies to both storage and queries.
The [`druid.generic.useDefaultValueForNull`](../configuration/index.md#sql-compatible-null-handling)
runtime property controls Druid's NULL handling mode. For the most SQL compliant behavior, set this to `false` (the default).

When `druid.generic.useDefaultValueForNull = false` (the default), NULLs are treated more closely to the SQL standard. In this mode,
numeric NULL is permitted, and NULLs and empty strings are no longer treated as interchangeable. This property
affects both storage and querying, and must be set on all Druid service types to be available at both ingestion time
and query time. There is some overhead associated with the ability to handle NULLs; see
the [segment internals](../design/segments.md#handling-null-values) documentation for more details.

When `druid.generic.useDefaultValueForNull = true` (deprecated legacy mode), Druid treats NULLs and empty strings
interchangeably, rather than according to the SQL standard. In this mode Druid SQL only has partial support for NULLs.
For example, the expressions `col IS NULL` and `col = ''` are equivalent, and both evaluate to true if `col` contains
an empty string. Similarly, the expression `COALESCE(col1, col2)` returns `col2` if `col1` is an empty string. While
the `COUNT(*)` aggregator counts all rows, the `COUNT(expr)` aggregator counts the number of rows where `expr` is
neither null nor the empty string. Numeric columns in this mode are not nullable; any null or missing values are
treated as zeroes. This was the default prior to Druid 28.0.0, but will be removed in a future release so that Druid
always behaves in an SQL compatible manner.
runtime property controls Druid's NULL handling mode. For the most SQL compliant behavior, maintain the default value of `false`.

There is some performance impact for null handling. see [segment internals](../design/segments.md#handling-null-values) for more information.
For examples of null handling, see the [null handling tutorial](../tutorials/tutorial-sql-null.md).

### Legacy null handling mode

:::info
To ensure Druid always behaves in an SQL compatible manner, this mode will be removed in a future release.
techdocsmith marked this conversation as resolved.
Show resolved Hide resolved
:::

You can set `druid.generic.useDefaultValueForNull = true` to revert to Druid's deprecated legacy null handling mode, the default for Druid 27.0.0 and prior releases.
techdocsmith marked this conversation as resolved.
Show resolved Hide resolved

When running in the deprecated legacy mode, Druid treats NULL values and empty strings interchangeably.
In this mode:
- Druid does not distinguish between empty strings and nulls.
- Druid SQL only has partial support for NULLs.
- Numeric columns are not nullable; null or missing values are treated as 0.

For example, the following expressions are equivalent:
- col IS NULL
- col = ''
Both evaluate to true if col contains an empty string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs white spaces
image

Similarly, the expression COALESCE(col1, col2) returns col2 if col1 is an empty string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be in code font since they're specific references and not generally talking about COALESCE etc

The COUNT(*) aggregator counts all rows but the COUNT(expr) aggregator counts the number of rows where expr is neither null nor the empty string.

## Boolean logic

Expand Down
214 changes: 214 additions & 0 deletions docs/tutorials/tutorial-sql-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
---
id: tutorial-sql-null
title: Null handling tutorial
sidebar_label: Handling null values
description: Introduction to null handling in Druid
---

<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

This tutorial introduces the basic concepts of null handling for string and numeric columns in Apache Druid.
The tutorial focuses on filters using the logical NOT operation on columns with NULL values.

## Prerequisites

Before starting this tutorial, download and run Apache Druid on your local machine as described in
the [Local quickstart](index.md).

The tutorial assumes you are familiar with using the [Query view](./tutorial-sql-query-view.md) to ingest and query data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we need to call out that this tutorial expects druid.generic.useDefaultValueForNull and druid.generic.useThreeValueLogicForNativeFilters to be set to their default values of false and true respectively or if that would be confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment without detail since we don't want folks changing it if they're just getting started.


The tutorial also assumes you have not changed any of the default settings for null handling.

## Load data with null values

The tutorial loads some data with null values for string and numeric columns as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tutorial doesn't load data for them. Rather, they need to load dat for the tutorial, right?


```json
{"date": "1/1/2024 1:02:00","title": "example_1","string_value": "some_value","numeric_value": 1}
{"date": "1/1/2024 1:03:00","title": "example_2","string_value": "another_value","numeric_value": 2}
{"date": "1/1/2024 1:04:00","title": "example_3","string_value": "", "numeric_value": null}
{"date": "1/1/2024 1:05:00","title": "example_4","string_value": null, "numeric_value": null}
```

Run the following query in the Druid Console to load the data:

```sql
REPLACE INTO "null_example" OVERWRITE ALL
WITH "ext" AS (
SELECT *
FROM TABLE(
EXTERN(
'{"type":"inline","data":"{\"date\": \"1/1/2024 1:02:00\",\"title\": \"example_1\",\"string_value\": \"some_value\",\"numeric_value\": 1}\n{\"date\": \"1/1/2024 1:03:00\",\"title\": \"example_2\",\"string_value\": \"another_value\",\"numeric_value\": 2}\n{\"date\": \"1/1/2024 1:04:00\",\"title\": \"example_3\",\"string_value\": \"\", \"numeric_value\": null}\n{\"date\": \"1/1/2024 1:05:00\",\"title\": \"example_4\",\"string_value\": null, \"numeric_value\": null}"}',
'{"type":"json"}'
)
) EXTEND ("date" VARCHAR, "title" VARCHAR, "string_value" VARCHAR, "numeric_value" BIGINT)
)
SELECT
TIME_PARSE("date", 'd/M/yyyy H:mm:ss') AS "__time",
"title",
"string_value",
"numeric_value"
FROM "ext"
PARTITIONED BY DAY
```

After Druid finishes loading the data, run the following query to see the table:

```sql
SELECT * FROM "null_example"
```

|`__time`|`title`|`string_value`|`numeric_value`|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table has empty columns:
image

|---|---|---|---|---|---|
|`2024-01-01T01:02:00.000Z`|`example_1`|`some_value`|1|
|`2024-01-01T01:03:00.000Z`|`example_2`|`another_value`|2|
|`2024-01-01T01:04:00.000Z`|`example_3`|`empty`|`null`|
|`2024-01-01T01:05:00.000Z`|`example_4`|`null`|`null`|

Note the difference in the empty string value for example 3 and the null string value for example 4.

## String query example

The queries in this section illustrate null handling with strings.
The following query filters rows where the string value is not equal to "some_value":

```sql
SELECT COUNT(*)
FROM "null_example"
WHERE "string_value" != 'some_value'
```

Druid returns 2 for "another_value" and the empty string "". The null value is not counted.

Note that the null value is included in COUNT(*) but not as a count of the values in the column as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code font for COUNT(*) since it's a specific instance of COUNT and not COUNT as a function in general


```sql
SELECT "string_value",
COUNT(*) AS count_all_rows,
COUNT("string_value") AS count_values
FROM "inline_data"
GROUP BY 1
```

Druid returns the following data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your results above odn't have an intro sentence like this, and a subsequent intro sentence is slightly different, but that's not a huge deal


|`string_value`|`count_all_rows`|`count_values`|
|---|---|---|
|`null`|1|0|
|`empty`|1|1|
|`another_value`|1|1|
|`some_value`|1|1|

Also note that GROUP BY expressions yields distinct entries for `null` and the empty string.

### Filter for emplty strings in addition to null

If your queries rely on treating empty strings and null values the same, you can use an OR operator in the filter. For example to select all rows with null values or empty strings:

```sql
SELECT *
FROM "null_example"
WHERE "string_value" IS NULL OR "string_value" = ''
```

Druid returns the following:

|`__time`|`title`|`string_value`|`numeric_value`|
|---|---|---|---|---|---|
|`2024-01-01T01:04:00.000Z`|`example_3`|`empty`|`null`|
|`2024-01-01T01:05:00.000Z`|`example_4`|`null`|`null`|

For another example, if you do not want to count empty strings, use a FILTER to exclude them. For example:

```sql
SELECT COUNT("string_value") FILTER(WHERE "string_value" <> '')
FROM "null_example"
```

Druid returns 2. Both the empty string and null values are excluded.

## Numeric query examples

Druid does does not count null values in numeric comparisons.

```sql
SELECT COUNT(*)
FROM "null_example"
WHERE "numeric_value" < 2
```

Druid returns 1. The `null` values for examples 3 and 4 are excluded.

Additionaly, be aware that null values do not behave as 0. For examples:

```sql
SELECT numeric_value + 1
FROM "null_example"
WHERE "__time" > '2024-01-01 01:04:00.000Z'
```

Druid returns `null` and not 1. One option is to use the COALESCE function for null handling. For example:

```sql
SELECT COALESCE(numeric_value, 0) + 1
FROM "null_example"
WHERE "__time" > '2024-01-01 01:04:00.000Z'
```

In this case, Druid returns 1.

## Ingestion time filtering

The same null handling rules apply at ingestion time.
The following query replaces the example data with data filtered with a WHERE clause:

```sql
REPLACE INTO "null_example" OVERWRITE ALL
WITH "ext" AS (
SELECT *
FROM TABLE(
EXTERN(
'{"type":"inline","data":"{\"date\": \"1/1/2024 1:02:00\",\"title\": \"example_1\",\"string_value\": \"some_value\",\"numeric_value\": 1}\n{\"date\": \"1/1/2024 1:03:00\",\"title\": \"example_2\",\"string_value\": \"another_value\",\"numeric_value\": 2}\n{\"date\": \"1/1/2024 1:04:00\",\"title\": \"example_3\",\"string_value\": \"\", \"numeric_value\": null}\n{\"date\": \"1/1/2024 1:05:00\",\"title\": \"example_4\",\"string_value\": null, \"numeric_value\": null}"}',
'{"type":"json"}'
)
) EXTEND ("date" VARCHAR, "title" VARCHAR, "string_value" VARCHAR, "numeric_value" BIGINT)
)
SELECT
TIME_PARSE("date", 'd/M/yyyy H:mm:ss') AS "__time",
"title",
"string_value",
"numeric_value"
FROM "ext"
WHERE "string_value" != 'some_value'
PARTITIONED BY DAY
```

The resulting data set only includes two rows. Druid has filtered out example 1 (`some_value`) and example 4 (`null`):

|`__time`|`title`|`string_value`|`numeric_value`|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty columns:
image

|---|---|---|---|---|---|
|`2024-01-01T01:03:00.000Z`|`example_2`|`another_value`|2|
|`2024-01-01T01:04:00.000Z`|`example_3`|`empty`|`null`|

## Learn more

See the following for more information:
- [Null values](../querying/sql-data-types.md#null-values)
- "Generating and working with NULL values" notebook at [Learn druid](https://github.com/implydata/learn-druid/)
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"tutorials/docker",
"tutorials/tutorial-kerberos-hadoop",
"tutorials/tutorial-sql-query-view",
"tutorials/tutorial-sql-null",
"tutorials/tutorial-unnest-arrays",
"tutorials/tutorial-query-deep-storage",
"tutorials/tutorial-jdbc"
Expand Down