explode

description

Table functions must be used in conjunction with Lateral View, support multi conjunction with Lateral View,support new optimizer only.

explode map column to rows. explode_map_outer will return NULL, while map is NULL or empty. explode_map and explode_map_outer both keep the nested NULL elements of map.

syntax

  1. explode_map(expr)
  2. explode_map_outer(expr)

example

  1. mysql> SET enable_fallback_to_original_planner=false
  2. mysql> CREATE TABLE IF NOT EXISTS `sdu`(
  3. `id` INT NULL,
  4. `name` TEXT NULL,
  5. `score` MAP<TEXT,INT> NULL
  6. ) ENGINE=OLAP
  7. DUPLICATE KEY(`id`)
  8. COMMENT 'OLAP'
  9. DISTRIBUTED BY HASH(`id`) BUCKETS 1
  10. PROPERTIES ("replication_allocation" = "tag.location.default: 1");
  11. Query OK, 0 rows affected (0.15 sec)
  12. mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL);
  13. Query OK, 5 rows affected (0.23 sec)
  14. {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'}
  15. mysql> select * from sdu order by id;
  16. +------+----------+-----------------------------------------+
  17. | id | name | score |
  18. +------+----------+-----------------------------------------+
  19. | 0 | zhangsan | {"Chinese":80, "Math":60, "English":90} |
  20. | 1 | lisi | {"null":null} |
  21. | 2 | wangwu | {"Chinese":88, "Math":90, "English":96} |
  22. | 3 | lisi2 | {null:null} |
  23. | 4 | amory | NULL |
  24. +------+----------+-----------------------------------------+
  25. mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v;
  26. +----------+---------+------+
  27. | name | k | v |
  28. +----------+---------+------+
  29. | zhangsan | Chinese | 80 |
  30. | zhangsan | Math | 60 |
  31. | zhangsan | English | 90 |
  32. | lisi | null | NULL |
  33. | wangwu | Chinese | 88 |
  34. | wangwu | Math | 90 |
  35. | wangwu | English | 96 |
  36. | lisi2 | NULL | NULL |
  37. +----------+---------+------+
  38. mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v;
  39. +----------+---------+------+
  40. | name | k | v |
  41. +----------+---------+------+
  42. | zhangsan | Chinese | 80 |
  43. | zhangsan | Math | 60 |
  44. | zhangsan | English | 90 |
  45. | lisi | null | NULL |
  46. | wangwu | Chinese | 88 |
  47. | wangwu | Math | 90 |
  48. | wangwu | English | 96 |
  49. | lisi2 | NULL | NULL |
  50. | amory | NULL | NULL |
  51. +----------+---------+------+
  52. mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1;
  53. +----------+---------+------+---------+------+
  54. | name | k | v | k1 | v1 |
  55. +----------+---------+------+---------+------+
  56. | zhangsan | Chinese | 80 | Chinese | 80 |
  57. | zhangsan | Chinese | 80 | Math | 60 |
  58. | zhangsan | Chinese | 80 | English | 90 |
  59. | zhangsan | Math | 60 | Chinese | 80 |
  60. | zhangsan | Math | 60 | Math | 60 |
  61. | zhangsan | Math | 60 | English | 90 |
  62. | zhangsan | English | 90 | Chinese | 80 |
  63. | zhangsan | English | 90 | Math | 60 |
  64. | zhangsan | English | 90 | English | 90 |
  65. | lisi | null | NULL | null | NULL |
  66. | wangwu | Chinese | 88 | Chinese | 88 |
  67. | wangwu | Chinese | 88 | Math | 90 |
  68. | wangwu | Chinese | 88 | English | 96 |
  69. | wangwu | Math | 90 | Chinese | 88 |
  70. | wangwu | Math | 90 | Math | 90 |
  71. | wangwu | Math | 90 | English | 96 |
  72. | wangwu | English | 96 | Chinese | 88 |
  73. | wangwu | English | 96 | Math | 90 |
  74. | wangwu | English | 96 | English | 96 |
  75. | lisi2 | NULL | NULL | NULL | NULL |
  76. +----------+---------+------+---------+------+

keywords

EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP