Presto 子查询:键不存在于地图中

Presto subqueries: Key not present in map

我一直在想 Superset -> Presto (PrestoSQL) -> Prometheus 组合(因为 Superset 还不支持 Prometheus),在尝试提取列时遇到了问题来自包含 Prometheus 标签的 Presto 地图类型列。

为了从 Superset 的角度将必要的标签映射为列,我在现有 table 之上的 Superset 中创建了额外的 table(或者我猜在这种情况下是一个视图)有以下 SQL 创建必要的列:

SELECT labels['system_name'] AS "system",labels['instance'] AS "instance","timestamp" AS "timestamp","value" AS "value" FROM "up"

此 table 然后用作 Superset 图表中的数据源,将其视为子查询。由 Superset 创建然后发送到 Presto 的结果 SQL 查询看起来例如像这样:

SELECT "system" AS "system",
       "instance" AS "instance",
       "timestamp" AS "timestamp",
       "value" AS "value"
FROM
  (SELECT labels['system_name'] AS "system",
          labels['instance'] AS "instance",
          "timestamp" AS "timestamp",
          "value" AS "value"
   FROM "up") AS "expr_qry"
WHERE "timestamp" >= from_iso8601_timestamp('2020-10-19T12:00:00.000000')
  AND "timestamp" < from_iso8601_timestamp('2020-10-19T13:00:00.000000')
ORDER BY "timestamp" ASC
LIMIT 250;

然而,我从上面得到的是一个错误:

io.prestosql.spi.PrestoException: Key not present in map: system_name
    at io.prestosql.operator.scalar.MapSubscriptOperator$MissingKeyExceptionFactory.create(MapSubscriptOperator.java:173)
    at io.prestosql.operator.scalar.MapSubscriptOperator.subscript(MapSubscriptOperator.java:143)
    at io.prestosql.$gen.CursorProcessor_20201019_165636_32.filter(Unknown Source)

从 Presto 的用户指南中阅读了一些有关查询的内容后,我尝试使用 WITH:

从命令行修改查询
WITH x AS (SELECT labels['system_name'] AS "system",labels['instance'] AS "instance","timestamp" AS "timestamp","value" AS "value" FROM "up")
SELECT system, timestamp, value FROM x
WHERE "timestamp" >= from_iso8601_timestamp('2020-10-19T12:00:00.000000')
  AND "timestamp" < from_iso8601_timestamp('2020-10-19T13:00:00.000000')
LIMIT 250;

一切顺利进行。但似乎我没有办法定义 Superset 如何执行它的查询,所以我坚持使用第一个选项。问题是,它有什么问题可以修复吗?

我想一个选项(如果其他一切都失败了)是在 Presto 端定义额外的 tables,这将对映射列执行相同的技巧,从而有望避免上述问题。

Presto 中的地图下标运算符要求地图中存在键。否则,您将遇到您描述的故障。

如果某些键可以丢失,您可以使用 element_at 函数代替,这将 return 一个 NULL 结果:

Returns value for given key, or NULL if the key is not contained in the map.