Multi-value dimensions

Apache Druid supports “multi-value” string dimensions. Multi-value string dimensions result from input fields that contain an array of values instead of a single value, such as the tags values in the following JSON array example:

  1. {"timestamp": "2011-01-12T00:00:00.000Z", "tags": ["t1","t2","t3"]}

It is important to be aware that multi-value dimensions are distinct from array types. While array types behave like standard SQL arrays, multi-value dimensions do not. This document describes the behavior of multi-value dimensions, and some additional details can be found in the SQL data type documentation.

This document describes inserting, filtering, and grouping behavior for multi-value dimensions. For information about the internal representation of multi-value dimensions, see segments documentation. Examples in this document are in the form of both SQL and native Druid queries. Refer to the Druid SQL documentation for details about the functions available for using multi-value string dimensions in SQL.

The following sections describe inserting, filtering, and grouping behavior based on the following example data, which includes a multi-value dimension, tags.

  1. {"timestamp": "2011-01-12T00:00:00.000Z", "label": "row1", "tags": ["t1","t2","t3"]}
  2. {"timestamp": "2011-01-13T00:00:00.000Z", "label": "row2", "tags": ["t3","t4","t5"]}
  3. {"timestamp": "2011-01-14T00:00:00.000Z", "label": "row3", "tags": ["t5","t6","t7"]}
  4. {"timestamp": "2011-01-14T00:00:00.000Z", "label": "row4", "tags": []}

Ingestion

Native batch and streaming ingestion

When using native batch or streaming ingestion such as with Apache Kafka, the Druid web console data loader can detect multi-value dimensions and configure the dimensionsSpec accordingly.

For TSV or CSV data, you can specify the multi-value delimiters using the listDelimiter field in the inputFormat. JSON data must be formatted as a JSON array to be ingested as a multi-value dimension. JSON data does not require inputFormat configuration.

The following shows an example dimensionsSpec for native ingestion of the data used in this document:

  1. "dimensions": [
  2. {
  3. "type": "string",
  4. "name": "label"
  5. },
  6. {
  7. "type": "string",
  8. "name": "tags",
  9. "multiValueHandling": "SORTED_ARRAY",
  10. "createBitmapIndex": true
  11. }
  12. ],

By default, Druid sorts values in multi-value dimensions. This behavior is controlled by the SORTED_ARRAY value of the multiValueHandling field. Alternatively, you can specify multi-value handling as:

  • SORTED_SET: results in the removal of duplicate values
  • ARRAY: retains the original order of the values

See Dimension Objects for information on configuring multi-value handling.

SQL-based ingestion

Multi-value dimensions can also be inserted with SQL-based ingestion. The functions MV_TO_ARRAY and ARRAY_TO_MV can assist in converting VARCHAR to VARCHAR ARRAY and VARCHAR ARRAY into VARCHAR respectively. multiValueHandling is not available when using the multi-stage query engine to insert data.

For example, to insert the data used in this document:

  1. REPLACE INTO "mvd_example" OVERWRITE ALL
  2. WITH "ext" AS (
  3. SELECT *
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type":"inline","data":"{\"timestamp\": \"2011-01-12T00:00:00.000Z\", \"label\": \"row1\", \"tags\": [\"t1\",\"t2\",\"t3\"]}\n{\"timestamp\": \"2011-01-13T00:00:00.000Z\", \"label\": \"row2\", \"tags\": [\"t3\",\"t4\",\"t5\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row3\", \"tags\": [\"t5\",\"t6\",\"t7\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row4\", \"tags\": []}"}',
  7. '{"type":"json"}',
  8. '[{"name":"timestamp", "type":"STRING"},{"name":"label", "type":"STRING"},{"name":"tags", "type":"ARRAY<STRING>"}]'
  9. )
  10. )
  11. )
  12. SELECT
  13. TIME_PARSE("timestamp") AS "__time",
  14. "label",
  15. ARRAY_TO_MV("tags") AS "tags"
  16. FROM "ext"
  17. PARTITIONED BY DAY

SQL-based ingestion with rollup

These input arrays can also be grouped prior to converting into a multi-value dimension:

  1. REPLACE INTO "mvd_example_rollup" OVERWRITE ALL
  2. WITH "ext" AS (
  3. SELECT *
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type":"inline","data":"{\"timestamp\": \"2011-01-12T00:00:00.000Z\", \"label\": \"row1\", \"tags\": [\"t1\",\"t2\",\"t3\"]}\n{\"timestamp\": \"2011-01-13T00:00:00.000Z\", \"label\": \"row2\", \"tags\": [\"t3\",\"t4\",\"t5\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row3\", \"tags\": [\"t5\",\"t6\",\"t7\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row4\", \"tags\": []}"}',
  7. '{"type":"json"}',
  8. '[{"name":"timestamp", "type":"STRING"},{"name":"label", "type":"STRING"},{"name":"tags", "type":"ARRAY<STRING>"}]'
  9. )
  10. )
  11. )
  12. SELECT
  13. TIME_PARSE("timestamp") AS "__time",
  14. "label",
  15. ARRAY_TO_MV("tags") AS "tags",
  16. COUNT(*) AS "count"
  17. FROM "ext"
  18. GROUP BY 1, 2, "tags"
  19. PARTITIONED BY DAY

Notice that ARRAY_TO_MV is not present in the GROUP BY clause since we only wish to coerce the type after grouping.

The EXTERN is also able to refer to the tags input type as VARCHAR, which is also how a query on a Druid table containing a multi-value dimension would specify the type of the tags column. If this is the case you must use MV_TO_ARRAY since the multi-stage query engine only supports grouping on multi-value dimensions as arrays. So, they must be coerced first. These arrays must then be coerced back into VARCHAR in the SELECT part of the statement with ARRAY_TO_MV.

  1. REPLACE INTO "mvd_example_rollup" OVERWRITE ALL
  2. WITH "ext" AS (
  3. SELECT *
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type":"inline","data":"{\"timestamp\": \"2011-01-12T00:00:00.000Z\", \"label\": \"row1\", \"tags\": [\"t1\",\"t2\",\"t3\"]}\n{\"timestamp\": \"2011-01-13T00:00:00.000Z\", \"label\": \"row2\", \"tags\": [\"t3\",\"t4\",\"t5\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row3\", \"tags\": [\"t5\",\"t6\",\"t7\"]}\n{\"timestamp\": \"2011-01-14T00:00:00.000Z\", \"label\": \"row4\", \"tags\": []}"}',
  7. '{"type":"json"}'
  8. )
  9. ) EXTEND ("timestamp" VARCHAR, "label" VARCHAR, "tags" VARCHAR)
  10. )
  11. SELECT
  12. TIME_PARSE("timestamp") AS "__time",
  13. "label",
  14. ARRAY_TO_MV(MV_TO_ARRAY("tags")) AS "tags",
  15. COUNT(*) AS "count"
  16. FROM "ext"
  17. GROUP BY 1, 2, MV_TO_ARRAY("tags")
  18. PARTITIONED BY DAY

Querying multi-value dimensions

Filtering

All query types, as well as filtered aggregators, can filter on multi-value dimensions. Filters follow these rules on multi-value dimensions:

  • Value filters (like “selector”, “bound”, and “in”) match a row if any of the values of a multi-value dimension match the filter.
  • The Column Comparison filter will match a row if the dimensions have any overlap.
  • Value filters that match null or "" (empty string) will match empty cells in a multi-value dimension.
  • Logical expression filters behave the same way they do on single-value dimensions: “and” matches a row if all underlying filters match that row; “or” matches a row if any underlying filters match that row; “not” matches a row if the underlying filter does not match the row.

The following example illustrates these rules. This query applies an “or” filter to match row1 and row2 of the dataset above, but not row3:

  1. SELECT *
  2. FROM "mvd_example_rollup"
  3. WHERE tags = 't1' OR tags = 't3'

returns

  1. {"__time":"2011-01-12T00:00:00.000Z","label":"row1","tags":"[\"t1\",\"t2\",\"t3\"]","count":1}
  2. {"__time":"2011-01-13T00:00:00.000Z","label":"row2","tags":"[\"t3\",\"t4\",\"t5\"]","count":1}

Native queries can also perform filtering that would be considered a “contradiction” in SQL, such as this “and” filter which would match only row1 of the dataset above:

  1. {
  2. "type": "and",
  3. "fields": [
  4. {
  5. "type": "selector",
  6. "dimension": "tags",
  7. "value": "t1"
  8. },
  9. {
  10. "type": "selector",
  11. "dimension": "tags",
  12. "value": "t3"
  13. }
  14. ]
  15. }

which returns

  1. {"__time":"2011-01-12T00:00:00.000Z","label":"row1","tags":"[\"t1\",\"t2\",\"t3\"]","count":1}

Multi-value dimensions also consider an empty row as null, consider:

  1. SELECT *
  2. FROM "mvd_example_rollup"
  3. WHERE tags is null

which results in:

  1. {"__time":"2011-01-14T00:00:00.000Z","label":"row4","tags":null,"count":1}

Grouping

When grouping on a multi-value dimension with SQL or a native topN or groupBy queries, all values from matching rows will be used to generate one group per value. This behaves similarly to an implicit SQL UNNEST operation. This means it’s possible for a query to return more groups than there are rows. For example, a topN on the dimension tags with filter "t1" AND "t3" would match only row1, and generate a result with three groups: t1, t2, and t3.

If you only need to include values that match your filter, you can use the SQL functions MV_FILTER_ONLY/MV_FILTER_NONE, filtered virtual column, or filtered dimensionSpec. This can also improve performance.

Example: SQL grouping query with no filtering

  1. SELECT label, tags
  2. FROM "mvd_example_rollup"
  3. GROUP BY 1,2

results in:

  1. {"label":"row1","tags":"t1"}
  2. {"label":"row1","tags":"t2"}
  3. {"label":"row1","tags":"t3"}
  4. {"label":"row2","tags":"t3"}
  5. {"label":"row2","tags":"t4"}
  6. {"label":"row2","tags":"t5"}
  7. {"label":"row3","tags":"t5"}
  8. {"label":"row3","tags":"t6"}
  9. {"label":"row3","tags":"t7"}
  10. {"label":"row4","tags":null}

Example: SQL grouping query with a filter

  1. SELECT label, tags
  2. FROM "mvd_example_rollup"
  3. WHERE tags = 't3'
  4. GROUP BY 1,2

results:

  1. {"label":"row1","tags":"t1"}
  2. {"label":"row1","tags":"t2"}
  3. {"label":"row1","tags":"t3"}
  4. {"label":"row2","tags":"t3"}
  5. {"label":"row2","tags":"t4"}
  6. {"label":"row2","tags":"t5"}

Example: native GroupBy query with no filtering

See GroupBy querying for details.

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test",
  4. "intervals": [
  5. "1970-01-01T00:00:00.000Z/3000-01-01T00:00:00.000Z"
  6. ],
  7. "granularity": {
  8. "type": "all"
  9. },
  10. "dimensions": [
  11. {
  12. "type": "default",
  13. "dimension": "tags",
  14. "outputName": "tags"
  15. }
  16. ],
  17. "aggregations": [
  18. {
  19. "type": "count",
  20. "name": "count"
  21. }
  22. ]
  23. }

This query returns the following result:

  1. [
  2. {
  3. "timestamp": "1970-01-01T00:00:00.000Z",
  4. "event": {
  5. "count": 1,
  6. "tags": "t1"
  7. }
  8. },
  9. {
  10. "timestamp": "1970-01-01T00:00:00.000Z",
  11. "event": {
  12. "count": 1,
  13. "tags": "t2"
  14. }
  15. },
  16. {
  17. "timestamp": "1970-01-01T00:00:00.000Z",
  18. "event": {
  19. "count": 2,
  20. "tags": "t3"
  21. }
  22. },
  23. {
  24. "timestamp": "1970-01-01T00:00:00.000Z",
  25. "event": {
  26. "count": 1,
  27. "tags": "t4"
  28. }
  29. },
  30. {
  31. "timestamp": "1970-01-01T00:00:00.000Z",
  32. "event": {
  33. "count": 2,
  34. "tags": "t5"
  35. }
  36. },
  37. {
  38. "timestamp": "1970-01-01T00:00:00.000Z",
  39. "event": {
  40. "count": 1,
  41. "tags": "t6"
  42. }
  43. },
  44. {
  45. "timestamp": "1970-01-01T00:00:00.000Z",
  46. "event": {
  47. "count": 1,
  48. "tags": "t7"
  49. }
  50. }
  51. ]

Notice that original rows are “exploded” into multiple rows and merged.

Example: native GroupBy query with a selector query filter

See query filters for details of selector query filter.

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test",
  4. "intervals": [
  5. "1970-01-01T00:00:00.000Z/3000-01-01T00:00:00.000Z"
  6. ],
  7. "filter": {
  8. "type": "selector",
  9. "dimension": "tags",
  10. "value": "t3"
  11. },
  12. "granularity": {
  13. "type": "all"
  14. },
  15. "dimensions": [
  16. {
  17. "type": "default",
  18. "dimension": "tags",
  19. "outputName": "tags"
  20. }
  21. ],
  22. "aggregations": [
  23. {
  24. "type": "count",
  25. "name": "count"
  26. }
  27. ]
  28. }

This query returns the following result:

  1. [
  2. {
  3. "timestamp": "1970-01-01T00:00:00.000Z",
  4. "event": {
  5. "count": 1,
  6. "tags": "t1"
  7. }
  8. },
  9. {
  10. "timestamp": "1970-01-01T00:00:00.000Z",
  11. "event": {
  12. "count": 1,
  13. "tags": "t2"
  14. }
  15. },
  16. {
  17. "timestamp": "1970-01-01T00:00:00.000Z",
  18. "event": {
  19. "count": 2,
  20. "tags": "t3"
  21. }
  22. },
  23. {
  24. "timestamp": "1970-01-01T00:00:00.000Z",
  25. "event": {
  26. "count": 1,
  27. "tags": "t4"
  28. }
  29. },
  30. {
  31. "timestamp": "1970-01-01T00:00:00.000Z",
  32. "event": {
  33. "count": 1,
  34. "tags": "t5"
  35. }
  36. }
  37. ]

You might be surprised to see “t1”, “t2”, “t4” and “t5” included in the results. This is because the query filter is applied on the row before explosion. For multi-value dimensions, a filter for value “t3” would match row1 and row2, after which exploding is done. For multi-value dimensions, a query filter matches a row if any individual value inside the multiple values matches the query filter.

Example: native GroupBy query with selector query and dimension filters

To solve the problem above and to get only rows for “t3”, use a “filtered dimension spec”, as in the query below.

See filtered dimensionSpecs in dimensionSpecs for details.

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "test",
  4. "intervals": [
  5. "1970-01-01T00:00:00.000Z/3000-01-01T00:00:00.000Z"
  6. ],
  7. "filter": {
  8. "type": "selector",
  9. "dimension": "tags",
  10. "value": "t3"
  11. },
  12. "granularity": {
  13. "type": "all"
  14. },
  15. "dimensions": [
  16. {
  17. "type": "listFiltered",
  18. "delegate": {
  19. "type": "default",
  20. "dimension": "tags",
  21. "outputName": "tags"
  22. },
  23. "values": ["t3"]
  24. }
  25. ],
  26. "aggregations": [
  27. {
  28. "type": "count",
  29. "name": "count"
  30. }
  31. ]
  32. }

This query returns the following result:

  1. [
  2. {
  3. "timestamp": "1970-01-01T00:00:00.000Z",
  4. "event": {
  5. "count": 2,
  6. "tags": "t3"
  7. }
  8. }
  9. ]

Note that, for groupBy queries, you could get similar result with a having spec but using a filtered dimensionSpec is much more efficient because that gets applied at the lowest level in the query processing pipeline. Having specs are applied at the outermost level of groupBy query processing.

Disable GroupBy on multi-value columns

You can disable the implicit unnesting behavior for groupBy by setting groupByEnableMultiValueUnnesting: false in your query context. In this mode, the groupBy engine will return an error instead of completing the query. This is a safety feature for situations where you believe that all dimensions are singly-valued and want the engine to reject any multi-valued dimensions that were inadvertently included.

Differences between arrays and multi-value dimensions

Avoid confusing string arrays with multi-value dimensions. Arrays and multi-value dimensions are stored in different column types, and query behavior is different. You can use the functions MV_TO_ARRAY and ARRAY_TO_MV to convert between the two if needed. In general, we recommend using arrays whenever possible, since they are a newer and more powerful feature and have SQL compliant behavior.

Use care during ingestion to ensure you get the type you want.

To get arrays when performing an ingestion using JSON ingestion specs, such as native batch or streaming ingestion such as with Apache Kafka, use dimension type auto or enable useSchemaDiscovery. When performing a SQL-based ingestion, write a query that generates arrays. Arrays may contain strings or numbers.

To get multi-value dimensions when performing an ingestion using JSON ingestion specs, use dimension type string and do not enable useSchemaDiscovery. When performing a SQL-based ingestion, wrap arrays in ARRAY_TO_MV. Multi-value dimensions can only contain strings.

You can tell which type you have by checking the INFORMATION_SCHEMA.COLUMNS table, using a query like:

  1. SELECT COLUMN_NAME, DATA_TYPE
  2. FROM INFORMATION_SCHEMA.COLUMNS
  3. WHERE TABLE_NAME = 'mytable'

Arrays are type ARRAY, multi-value strings are type VARCHAR.