.(点号)运算符

适用于:勾选“是” Databricks SQL 勾选“是” Databricks Runtime

返回 fieldIdentifier 中的 STRUCT 值,或者 keyIdentifier 中 MAP 表示的值。

语法

structExpr . fieldIdentifier

mapExpr . keyIdentifier

参数

  • structExpr:一个 STRUCT 表达式。
  • fieldIdentifier: 中字段的structExpr。
  • mapExpr:具有 MAP 类型的键的 STRING 表达式。
  • keyIdentifier:与 中的键值匹配的mapExpr。

返回

与 fieldIdentifier 的类型或者与 mapExpr 值的类型匹配的类型。

names 的解优先于此运算符的解。 也就是说,如果用点分隔一系列标识符,则 Azure Databricks 将求解可能最长的限定名称。 如果解出的名称是 MAP 或 STRUCT,则 Azure Databricks 将使用点号运算符来解释剩余的标识符。

与 STRUCT 一起使用时,Azure Databricks 将在编译语句时验证 fieldIdentifier 是否在结构中存在。

当与 MAP 一起使用并且没有与 keyIdentifier 匹配的键时,Azure Databricks 将返回 null。 若要返回 NULL,请改用 try_element_at 函数。

警告

在 Databricks Runtime 中,如果 spark.sql.ansi.enabled 为 false,在找不到 NULL 的匹配键的情况下,结果为 mapExpr。

示例

-- Names take precedence over the dot sign operator
> CREATE SCHEMA a;
> CREATE TABLE a.a(a struct<a INT, b STRING>);
> INSERT INTO a.a VALUES (named_struct('a', 5, 'b', 'Spark'));

-- Column `a` in table `a`
> SELECT a.a FROM a.a;
  {"a":5,"b":"Spark"}

-- Field `b` in column `a`
> SELECT a.b FROM a.a;
  Spark

-- Column `a` in table `a.a`
> SELECT a.a.a FROM a.a;
  {"a":5,"b":"Spark"}

-- Field `a` in column `a` in table `a.a`
> SELECT a.a.a.a FROM a.a;
  5

-- Resolving a map value:
> SELECT map('three', 3).three;
 3

-- Resolving a map value using the [ ] notation:
> SELECT map('three', 3)['three']
 3

-- Resolving a map value using back quotes:
> SELECT map('서울시', 'Seoul').`서울시`;
  Seoul

-- Cannot resolve a non existing key
> SELECT map('three', 3).four;
  NULL