Unnest arrays within a column

Unnesting arrays - 图1info

If you’re looking for information about how to unnest COMPLEX<json> columns, see Nested columns.

This tutorial demonstrates how to use the unnest datasource to unnest a column that has data stored in arrays. For example, if you have a column named dim3 with values like [a,b] or [c,d,f], the unnest datasource can output the data to a new column with individual rows that contain single values like a and b. When doing this, be mindful of the following:

  • Unnesting data can dramatically increase the total number of rows.
  • You cannot unnest an array within an array.

You can use the Druid console or API to unnest data. To start though, you may want to use the Druid console so that viewing the nested and unnested data is easier.

Prerequisites

You need a Druid cluster, such as the quickstart. The cluster does not need any existing datasources. You’ll load a basic one as part of this tutorial.

Load data with nested values

The data you’re ingesting contains a handful of rows that resemble the following:

  1. t:2000-01-01, m1:1.0, m2:1.0, dim1:, dim2:[a], dim3:[a,b], dim4:[x,y], dim5:[a,b]

The focus of this tutorial is on the nested array of values in dim3.

You can load this data by running a query for SQL-based ingestion or submitting a JSON-based ingestion spec. The example loads data into a table named nested_data:

  • SQL-based ingestion
  • Ingestion spec
  1. REPLACE INTO nested_data OVERWRITE ALL
  2. SELECT
  3. TIME_PARSE("t") as __time,
  4. dim1,
  5. dim2,
  6. dim3,
  7. dim4,
  8. dim5,
  9. m1,
  10. m2
  11. FROM TABLE(
  12. EXTERN(
  13. '{"type":"inline","data":"{\"t\":\"2000-01-01\",\"m1\":\"1.0\",\"m2\":\"1.0\",\"dim1\":\"\",\"dim2\":[\"a\"],\"dim3\":[\"a\",\"b\"],\"dim4\":[\"x\",\"y\"],\"dim5\":[\"a\",\"b\"]},\n{\"t\":\"2000-01-02\",\"m1\":\"2.0\",\"m2\":\"2.0\",\"dim1\":\"10.1\",\"dim2\":[],\"dim3\":[\"c\",\"d\"],\"dim4\":[\"e\",\"f\"],\"dim5\":[\"a\",\"b\",\"c\",\"d\"]},\n{\"t\":\"2001-01-03\",\"m1\":\"6.0\",\"m2\":\"6.0\",\"dim1\":\"abc\",\"dim2\":[\"a\"],\"dim3\":[\"k\",\"l\"]},\n{\"t\":\"2001-01-01\",\"m1\":\"4.0\",\"m2\":\"4.0\",\"dim1\":\"1\",\"dim2\":[\"a\"],\"dim3\":[\"g\",\"h\"]},\n{\"t\":\"2001-01-02\",\"m1\":\"5.0\",\"m2\":\"5.0\",\"dim1\":\"def\",\"dim2\":[\"abc\"],\"dim3\":[\"i\",\"j\"]},\n{\"t\":\"2001-01-03\",\"m1\":\"6.0\",\"m2\":\"6.0\",\"dim1\":\"abc\",\"dim2\":[\"a\"],\"dim3\":[\"k\",\"l\"]},\n{\"t\":\"2001-01-02\",\"m1\":\"5.0\",\"m2\":\"5.0\",\"dim1\":\"def\",\"dim2\":[\"abc\"],\"dim3\":[\"m\",\"n\"]}"}',
  14. '{"type":"json"}',
  15. '[{"name":"t","type":"string"},{"name":"dim1","type":"string"},{"name":"dim2","type":"string"},{"name":"dim3","type":"string"},{"name":"dim4","type":"string"},{"name":"dim5","type":"string"},{"name":"m1","type":"float"},{"name":"m2","type":"double"}]'
  16. )
  17. )
  18. PARTITIONED BY YEAR
  1. {
  2. "type": "index_parallel",
  3. "spec": {
  4. "ioConfig": {
  5. "type": "index_parallel",
  6. "inputSource": {
  7. "type": "inline",
  8. "data":"{\"t\":\"2000-01-01\",\"m1\":\"1.0\",\"m2\":\"1.0\",\"dim1\":\"\",\"dim2\":[\"a\"],\"dim3\":[\"a\",\"b\"],\"dim4\":[\"x\",\"y\"],\"dim5\":[\"a\",\"b\"]},\n{\"t\":\"2000-01-02\",\"m1\":\"2.0\",\"m2\":\"2.0\",\"dim1\":\"10.1\",\"dim2\":[],\"dim3\":[\"c\",\"d\"],\"dim4\":[\"e\",\"f\"],\"dim5\":[\"a\",\"b\",\"c\",\"d\"]},\n{\"t\":\"2001-01-03\",\"m1\":\"6.0\",\"m2\":\"6.0\",\"dim1\":\"abc\",\"dim2\":[\"a\"],\"dim3\":[\"k\",\"l\"]},\n{\"t\":\"2001-01-01\",\"m1\":\"4.0\",\"m2\":\"4.0\",\"dim1\":\"1\",\"dim2\":[\"a\"],\"dim3\":[\"g\",\"h\"]},\n{\"t\":\"2001-01-02\",\"m1\":\"5.0\",\"m2\":\"5.0\",\"dim1\":\"def\",\"dim2\":[\"abc\"],\"dim3\":[\"i\",\"j\"]},\n{\"t\":\"2001-01-03\",\"m1\":\"6.0\",\"m2\":\"6.0\",\"dim1\":\"abc\",\"dim2\":[\"a\"],\"dim3\":[\"k\",\"l\"]},\n{\"t\":\"2001-01-02\",\"m1\":\"5.0\",\"m2\":\"5.0\",\"dim1\":\"def\",\"dim2\":[\"abc\"],\"dim3\":[\"m\",\"n\"]}"
  9. },
  10. "inputFormat": {
  11. "type": "json"
  12. }
  13. },
  14. "tuningConfig": {
  15. "type": "index_parallel",
  16. "partitionsSpec": {
  17. "type": "dynamic"
  18. }
  19. },
  20. "dataSchema": {
  21. "dataSource": "nested_data",
  22. "granularitySpec": {
  23. "type": "uniform",
  24. "queryGranularity": "NONE",
  25. "rollup": false,
  26. "segmentGranularity": "YEAR"
  27. },
  28. "timestampSpec": {
  29. "column": "t",
  30. "format": "auto"
  31. },
  32. "dimensionsSpec": {
  33. "dimensions": [
  34. "dim1",
  35. "dim2",
  36. "dim3",
  37. "dim4",
  38. "dim5"
  39. ]
  40. },
  41. "metricsSpec": [
  42. {
  43. "name": "m1",
  44. "type": "floatSum",
  45. "fieldName": "m1"
  46. },
  47. {
  48. "name": "m2",
  49. "type": "doubleSum",
  50. "fieldName": "m2"
  51. }
  52. ]
  53. }
  54. }
  55. }

View the data

Now that the data is loaded, run the following query:

  1. SELECT * FROM nested_data

In the results, notice that the column named dim3 has nested values like ["a","b"]. The example queries that follow unnest dim3 and run queries against the unnested records. Depending on the type of queries you write, see either Unnest using SQL queries or Unnest using native queries.

Unnest using SQL queries

The following is the general syntax for UNNEST:

  1. SELECT column_alias_name FROM datasource CROSS JOIN UNNEST(source_expression) AS table_alias_name(column_alias_name)

For more information about the syntax, see UNNEST.

Unnest a single source expression in a datasource

The following query returns a column called d3 from the table nested_data. d3 contains the unnested values from the source column dim3:

  1. SELECT d3 FROM "nested_data" CROSS JOIN UNNEST(MV_TO_ARRAY(dim3)) AS example_table(d3)

Notice the MV_TO_ARRAY helper function, which converts the multi-value records in dim3 to arrays. It is required since dim3 is a multi-value string dimension.

If the column you are unnesting is not a string dimension, then you do not need to use the MV_TO_ARRAY helper function.

Unnest a virtual column

You can unnest into a virtual column (multiple columns treated as one). The following query returns the two source columns and a third virtual column containing the unnested data:

  1. SELECT dim4,dim5,d45 FROM nested_data CROSS JOIN UNNEST(ARRAY[dim4,dim5]) AS example_table(d45)

The virtual column d45 is the product of the two source columns. Notice how the total number of rows has grown. The table nested_data had only seven rows originally.

Another way to unnest a virtual column is to concatenate them with ARRAY_CONCAT:

  1. SELECT dim4,dim5,d45 FROM nested_data CROSS JOIN UNNEST(ARRAY_CONCAT(dim4,dim5)) AS example_table(d45)

Decide which method to use based on what your goals are.

Unnest multiple source expressions

You can include multiple UNNEST clauses in a single query. Each UNNEST clause needs the following:

  1. UNNEST(source_expression) AS table_alias_name(column_alias_name)

The table_alias_name and column_alias_name for each UNNEST clause should be unique.

The example query returns the following from the nested_data datasource:

  • the source columns dim3, dim4, and dim5
  • an unnested version of dim3 aliased to d3
  • an unnested virtual column composed of dim4 and dim5 aliased to d45
  1. SELECT dim3,dim4,dim5,d3,d45 FROM "nested_data" CROSS JOIN UNNEST(MV_TO_ARRAY("dim3")) AS foo1(d3) CROSS JOIN UNNEST(ARRAY[dim4,dim5]) AS foo2(d45)

Unnest a column from a subset of a table

The following query uses only three columns from the nested_data table as the datasource. From that subset, it unnests the column dim3 into d3 and returns d3.

  1. SELECT d3 FROM (SELECT dim1, dim2, dim3 FROM "nested_data") CROSS JOIN UNNEST(MV_TO_ARRAY(dim3)) AS example_table(d3)

Unnest with a filter

You can specify which rows to unnest by including a filter in your query. The following query:

  • Filters the source expression based on dim2
  • Unnests the records in dim3 into d3
  • Returns the records for the unnested d3 that have a dim2 record that matches the filter
  1. SELECT d3 FROM (SELECT * FROM nested_data WHERE dim2 IN ('abc')) CROSS JOIN UNNEST(MV_TO_ARRAY(dim3)) AS example_table(d3)

You can also filter the results of an UNNEST clause. The following example unnests the inline array [1,2,3] but only returns the rows that match the filter:

  1. SELECT * FROM UNNEST(ARRAY[1,2,3]) AS example_table(d1) WHERE d1 IN ('1','2')

This means that you can run a query like the following where Druid only return rows that meet the following conditions:

  • The unnested values of dim3 (aliased to d3) matches IN ('b', 'd')
  • The value of m1 is less than 2.
  1. SELECT * FROM nested_data CROSS JOIN UNNEST(MV_TO_ARRAY("dim3")) AS foo(d3) WHERE d3 IN ('b', 'd') and m1 < 2

The query only returns a single row since only one row meets the conditions. You can see the results change if you modify the filter.

Unnest and then GROUP BY

The following query unnests dim3 and then performs a GROUP BY on the output d3.

  1. SELECT d3 FROM nested_data CROSS JOIN UNNEST(MV_TO_ARRAY(dim3)) AS example_table(d3) GROUP BY d3

You can further transform your results by including clauses like ORDER BY d3 DESC or LIMIT.

Unnest using native queries

The following section shows examples of how you can use the unnest datasource in queries. They all use the nested_data table you created earlier in the tutorial.

You can use a single unnest datasource to unnest multiple columns. Be careful when doing this though because it can lead to a very large number of new rows.

Scan query

The following native Scan query returns the rows of the datasource and unnests the values in the dim3 column by using the unnest datasource type:

Show the query

  1. {
  2. "queryType": "scan",
  3. "dataSource": {
  4. "type": "unnest",
  5. "base": {
  6. "type": "table",
  7. "name": "nested_data"
  8. },
  9. "virtualColumn": {
  10. "type": "expression",
  11. "name": "unnest-dim3",
  12. "expression": "\"dim3\""
  13. }
  14. },
  15. "intervals": {
  16. "type": "intervals",
  17. "intervals": [
  18. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  19. ]
  20. },
  21. "limit": 100,
  22. "columns": [
  23. "__time",
  24. "dim1",
  25. "dim2",
  26. "dim3",
  27. "m1",
  28. "m2",
  29. "unnest-dim3"
  30. ],
  31. "legacy": false,
  32. "granularity": {
  33. "type": "all"
  34. },
  35. "context": {
  36. "debug": true,
  37. "useCache": false
  38. }
  39. }

In the results, notice that there are more rows than before and an additional column named unnest-dim3. The values of unnest-dim3 are the same as the dim3 column except the nested values are no longer nested and are each a separate record.

You can implement filters. For example, you can add the following to the Scan query to filter results to only rows that have the values "a" or "abc" in "dim2":

  1. "filter": {
  2. "type": "in",
  3. "dimension": "dim2",
  4. "values": [
  5. "a",
  6. "abc",
  7. ]
  8. },

groupBy query

The following query returns an unnested version of the column dim3 as the column unnest-dim3 sorted in descending order.

Show the query

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": {
  4. "type": "unnest",
  5. "base": "nested_data",
  6. "virtualColumn": {
  7. "type": "expression",
  8. "name": "unnest-dim3",
  9. "expression": "\"dim3\""
  10. }
  11. },
  12. "intervals": ["-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"],
  13. "granularity": "all",
  14. "dimensions": [
  15. "unnest-dim3"
  16. ],
  17. "limitSpec": {
  18. "type": "default",
  19. "columns": [
  20. {
  21. "dimension": "unnest-dim3",
  22. "direction": "descending"
  23. }
  24. ],
  25. "limit": 1001
  26. },
  27. "context": {
  28. "debug": true
  29. }
  30. }

topN query

The example topN query unnests dim3 into the column unnest-dim3. The query uses the unnested column as the dimension for the topN query. The results are outputted to a column named topN-unnest-d3 and are sorted numerically in ascending order based on the column a0, an aggregate value representing the minimum of m1.

Show the query

  1. {
  2. "queryType": "topN",
  3. "dataSource": {
  4. "type": "unnest",
  5. "base": {
  6. "type": "table",
  7. "name": "nested_data"
  8. },
  9. "virtualColumn": {
  10. "type": "expression",
  11. "name": "unnest-dim3",
  12. "expression": "\"dim3\""
  13. },
  14. },
  15. "dimension": {
  16. "type": "default",
  17. "dimension": "unnest-dim3",
  18. "outputName": "topN-unnest-d3",
  19. "outputType": "STRING"
  20. },
  21. "metric": {
  22. "type": "inverted",
  23. "metric": {
  24. "type": "numeric",
  25. "metric": "a0"
  26. }
  27. },
  28. "threshold": 3,
  29. "intervals": {
  30. "type": "intervals",
  31. "intervals": [
  32. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  33. ]
  34. },
  35. "granularity": {
  36. "type": "all"
  37. },
  38. "aggregations": [
  39. {
  40. "type": "floatMin",
  41. "name": "a0",
  42. "fieldName": "m1"
  43. }
  44. ],
  45. "context": {
  46. "debug": true
  47. }
  48. }

Unnest with a JOIN query

This query joins the nested_data table with itself and outputs the unnested data into a new column called unnest-dim3.

Show the query

  1. {
  2. "queryType": "scan",
  3. "dataSource": {
  4. "type": "unnest",
  5. "base": {
  6. "type": "join",
  7. "left": {
  8. "type": "table",
  9. "name": "nested_data"
  10. },
  11. "right": {
  12. "type": "query",
  13. "query": {
  14. "queryType": "scan",
  15. "dataSource": {
  16. "type": "table",
  17. "name": "nested_data"
  18. },
  19. "intervals": {
  20. "type": "intervals",
  21. "intervals": [
  22. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  23. ]
  24. },
  25. "virtualColumns": [
  26. {
  27. "type": "expression",
  28. "name": "v0",
  29. "expression": "\"m2\"",
  30. "outputType": "FLOAT"
  31. }
  32. ],
  33. "resultFormat": "compactedList",
  34. "columns": [
  35. "__time",
  36. "dim1",
  37. "dim2",
  38. "dim3",
  39. "m1",
  40. "m2",
  41. "v0"
  42. ],
  43. "legacy": false,
  44. "context": {
  45. "sqlOuterLimit": 1001,
  46. "useNativeQueryExplain": true
  47. },
  48. "granularity": {
  49. "type": "all"
  50. }
  51. }
  52. },
  53. "rightPrefix": "j0.",
  54. "condition": "(\"m1\" == \"j0.v0\")",
  55. "joinType": "INNER"
  56. },
  57. "virtualColumn": {
  58. "type": "expression",
  59. "name": "unnest-dim3",
  60. "expression": "\"dim3\""
  61. }
  62. },
  63. "intervals": {
  64. "type": "intervals",
  65. "intervals": [
  66. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  67. ]
  68. },
  69. "resultFormat": "compactedList",
  70. "limit": 1001,
  71. "columns": [
  72. "__time",
  73. "dim1",
  74. "dim2",
  75. "dim3",
  76. "j0.__time",
  77. "j0.dim1",
  78. "j0.dim2",
  79. "j0.dim3",
  80. "j0.m1",
  81. "j0.m2",
  82. "m1",
  83. "m2",
  84. "unnest-dim3"
  85. ],
  86. "legacy": false,
  87. "context": {
  88. "sqlOuterLimit": 1001,
  89. "useNativeQueryExplain": true
  90. },
  91. "granularity": {
  92. "type": "all"
  93. }
  94. }

Unnest a virtual column

The unnest datasource supports unnesting virtual columns, which is a queryable composite column that can draw data from multiple source columns.

The following query returns the columns dim45 and m1. The dim45 column is the unnested version of a virtual column that contains an array of the dim4 and dim5 columns.

Show the query

  1. {
  2. "queryType": "scan",
  3. "dataSource":{
  4. "type": "unnest",
  5. "base": {
  6. "type": "table",
  7. "name": "nested_data"
  8. },
  9. "virtualColumn": {
  10. "type": "expression",
  11. "name": "dim45",
  12. "expression": "array_concat(\"dim4\",\"dim5\")",
  13. "outputType": "ARRAY<STRING>"
  14. },
  15. }
  16. "intervals": {
  17. "type": "intervals",
  18. "intervals": [
  19. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  20. ]
  21. },
  22. "resultFormat": "compactedList",
  23. "limit": 1001,
  24. "columns": [
  25. "dim45",
  26. "m1"
  27. ],
  28. "legacy": false,
  29. "granularity": {
  30. "type": "all"
  31. },
  32. "context": {
  33. "debug": true,
  34. "useCache": false
  35. }
  36. }

Unnest a column and a virtual column

The following Scan query unnests the column dim3 into d3 and a virtual column composed of dim4 and dim5 into the column d45. It then returns those source columns and their unnested variants.

Show the query

  1. {
  2. "queryType": "scan",
  3. "dataSource": {
  4. "type": "unnest",
  5. "base": {
  6. "type": "unnest",
  7. "base": {
  8. "type": "table",
  9. "name": "nested_data"
  10. },
  11. "virtualColumn": {
  12. "type": "expression",
  13. "name": "d3",
  14. "expression": "\"dim3\"",
  15. "outputType": "STRING"
  16. },
  17. },
  18. "virtualColumn": {
  19. "type": "expression",
  20. "name": "d45",
  21. "expression": "array(\"dim4\",\"dim5\")",
  22. "outputType": "ARRAY<STRING>"
  23. },
  24. },
  25. "intervals": {
  26. "type": "intervals",
  27. "intervals": [
  28. "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
  29. ]
  30. },
  31. "resultFormat": "compactedList",
  32. "limit": 1001,
  33. "columns": [
  34. "dim3",
  35. "d3",
  36. "dim4",
  37. "dim5",
  38. "d45"
  39. ],
  40. "legacy": false,
  41. "context": {
  42. "queryId": "2618b9ce-6c0d-414e-b88d-16fb59b9c481",
  43. "sqlOuterLimit": 1001,
  44. "sqlQueryId": "2618b9ce-6c0d-414e-b88d-16fb59b9c481",
  45. "useNativeQueryExplain": true
  46. },
  47. "granularity": {
  48. "type": "all"
  49. }
  50. }

Learn more

For more information, see the following: