explore_source

本页提及的 explore_source 参数是 derived_table 的子参数。

explore_source 也可以是 test 的子参数,如 test 参数文档页面中所述。

用法

derived_table: customer_order_facts {
  explore_source:  orders {
    column:  customer_id {
      field:  orders.customer_id
    }
    column:  order_amount {
      field:  orders.sale_price
    }
    column:  item_qty {
      field:  orders.number_items
    }
    derived_column:  average_item_price {
      sql:  order_amount / item_qty  ;;
    }
    timezone:  "America/Los_Angeles"
  }
}
层次结构
explore_source
默认值

接受
派生原生派生表的“探索”的标识符,以及用于定义原生派生表的子参数。

定义

您可以通过两种方式创建派生表,然后像使用数据库中的普通表一样使用该表:原生派生表(使用 LookML 参数定义)和基于 SQL 的派生表(使用 SQL 查询语句定义)。

explore_source 参数用于原生派生表。在此参数中,您可以定义原生派生表中将包含哪些列、要应用于原生派生表的任何过滤条件、是否限制或排序原生派生表行,以及是否将原生派生表的基于时间的字段转换为其他时区。

定义原生派生表

您可以在原生派生表中使用各种参数,其中许多参数都是可选的。用于定义原生派生表的语法如下:

explore_source: identifier {
  bind_all_filters: yes
  column: identifier {
    field: field_name
  }
  derived_column: identifier {
    sql: SQL expression ;;
  }
  expression_custom_filter: [custom filter expression]
  filters: [field_name_1: "string", field_name_2: "string", ...]
  limit: number
  sorts: [field_name_1: asc | desc, field_name_2: asc | desc, ...]
  timezone: "string"
}

下表提供了有关可用于定义原生派生表的每个参数的信息:

参数名称 说明 示例
bind_filters 将“探索”查询中的过滤条件传递到原生派生表子查询中。如需设置此参数,请使用 from_field 子参数指定原生派生表视图中定义的字段,或指定原生派生表所联接的“探索”中可访问的字段。在运行时,“探索”中的任何 from_field 过滤条件都会传递到原生派生表子查询中的 to_field。如需查看示例,请参阅使用 bind_filters 部分。

使用 bind_filters 时,请注意以下几点:
  • 运行时过滤条件必须位于与原生派生表联接的视图中,且该视图与原生派生表位于同一探索中。
  • 原生派生表无法转换为永久性派生表 (PDT)

explore_source 参数可以包含 bind_all_filters 子参数 bind_filters 子参数,但不能同时包含这两个子参数。
bind_filters: {
  to_field: users.created_date
  from_field: user_dt.filter_date
}
bind_all_filters 将“探索”查询中的所有过滤条件传递到原生派生表子查询中。如需设置此参数,请在原生派生表的 explore_source 中指定 bind_all_filters: yes。如需查看示例,请参阅使用 bind_filters 部分。

使用 bind_all_filters: yes 时,请注意以下几点:
  • 运行时过滤条件必须位于与原生派生表联接的视图中,且该视图与原生派生表位于同一探索中。
  • 原生派生表无法转换为 PDT
  • 原生派生表只能联接到原生派生表的 explore_source 参数中指定的“探索”中。这是因为 bind_all_filters 需要将“探索”的过滤字段映射到原生派生表中的字段。
  • explore_source 参数可以具有 bind_all_filters 子参数 bind_filters 子参数,但不能同时具有这两个子参数。
bind_all_filters: yes
column 指定要包含在 explore_source 中的列。具有 field 子形参。
column: cust_id {
  field: orders.customer_id
}
derived_column 指定 explore_source 中的列,该列具有内部列命名空间中的表达式。由于此步骤中没有 SQL 分组,因此聚合 SQL 表达式将无法在此处使用。SQL 窗口函数在此参数中非常有用。具有 sql 子形参。
derived_column: average_order {
  sql: order_amount / item_qty ;;
}
dev_filters 添加于 21.12 指定 Looker 仅应用于派生表的开发版本的过滤条件。当 LookML 开发者在开发模式下测试派生表时,此功能非常有用。借助 dev_filters 参数,Looker 可以构建较小的过滤版表,以便 LookML 开发者能够迭代和测试该表,而无需在每次更改后等待完整表的构建。Looker 仅将 dev_filters 应用于派生表的开发版本,而不应用于用户查询的表的生产版本。如需详细了解如何在开发模式下使用派生表,请参阅 Looker 中的派生表文档页面;如需查看相关示例,请参阅本页的为开发模式创建过滤条件部分。 dev_filters: [orders.created_date: "90 days", orders.products: "sweaters"]
expression_custom_filter (可选)针对 explore_source 查询指定自定义过滤条件表达式 expression_custom_filter: ${orders.status} = "pending" ;;
filters (可选)向 explore_source 查询添加过滤条件。用方括号括起来;包含要过滤的字段名称(使用 view_name.field_name 格式),后跟 : 和应过滤的字段值。过滤条件会添加到原生派生表生成的 SQL 的 WHERE 子句中。 filters: [products.department: "sweaters"]
limit (可选)指定查询的行数限制。 limit: 10
sorts (可选)指定此 explore_source 的排序。用方括号括起来;包含要排序的字段名称(使用 view_name.field_name 格式),后跟 :ascdesc,以指明字段应按升序还是降序排序。您可以添加多个以英文逗号分隔的字段名称和关键字对,按多个字段进行排序。 sorts: [products.brand: asc, products.name: asc]
timezone explore_source 查询设置时区。对于非持久性派生表,请将时区设置为 "query_timezone",以自动使用当前正在运行的查询的时区。如果未指定时区,explore_source 查询不会执行任何时区转换,而是会使用数据库时区。如需查看支持的时区列表,请参阅 timezone 值页面

当您在 IDE 中输入 timezone 参数时,IDE 会自动建议时区值。IDE 还会显示“快捷帮助”面板中支持的时区值列表。
timezone: "America/Los_Angeles"

示例

以下定义提供了原生派生表的基本示例。

创建 user_order_facts 原生派生表:

view: user_order_facts {
  derived_table: {
    explore_source: order_items {
      column: user_id {
        field: order_items.user_id
      }
      column: lifetime_number_of_orders {
        field: order_items.order_count
      }
      column: lifetime_customer_value {
        field: order_items.total_revenue
      }
    }
  }
  # Define the view's fields as desired
  dimension: user_id {
    hidden: yes
  }
  dimension: lifetime_number_of_orders {
    type: number
  }
  dimension: lifetime_customer_value {
    type: number
  }
}

您可以添加过滤条件来创建 user_90_day_facts 原生派生表:

view: user_90_day_facts {
  derived_table: {
    explore_source: order_items {
      column: user_id {
        field: order_items.user_id
      }
      column: number_of_orders_90_day {
        field: order_items.order_count
      }
      column: customer_value_90_day {
        field: order_items.total_revenue
      }
      filters: [order_items.created_date: "90 days"]
    }
  }
  # Add define view's fields as desired
  dimension: user_id {
    hidden: yes
  }
  dimension: number_of_orders_90_day {
    type: number
  }
  dimension: customer_value_90_day {
    type: number
  }
}

为开发模式创建过滤条件

在某些情况下,您创建的原生派生表需要很长时间才能生成,如果您在开发模式下测试大量更改,这可能会非常耗时。对于这些情况,您可以使用 dev_filters 创建原生派生表的较小开发版本:

view: e_faa_pdt {
  derived_table: {
  ...
    datagroup_trigger: e_faa_shared_datagroup
    explore_source: flights {
      dev_filters: [flights.event_date: "90 days"]
      filters: [flights.event_date: "2 years", flights.airport_name: "Yucca Valley Airport"]
      column: id {}
      column: airport_name {}
      column: event_date {}
    }
  }
...
}

此示例包含一个 dev_filters 参数(用于将数据过滤为过去 90 天内的数据)和一个 filters 参数(用于将数据过滤为过去 2 年内的数据以及 Yucca Valley Airport 的数据)。dev_filters 参数与 filters 参数搭配使用,以便将所有过滤条件应用于表的开发版本。如果 dev_filtersfilters 都为同一列指定了过滤条件,则对于表的开发版本,以 dev_filters 为准。在此示例中,表的开发版本会将数据过滤为 Yucca Valley Airport 的过去 90 天的数据。

如果原生派生表具有 dev_filters 参数,则开发表不能用作生产版本,因为开发版本的数据集是缩略版。在这种情况下,在完成表的开发并部署更改之前,您可以将 dev_filters 参数注释掉,然后在开发模式下查询该表。然后,Looker 会构建一个可用于生产环境的完整版表,以便您在部署更改时使用。如需详细了解如何在生产环境中使用开发表,请参阅 Looker 中的派生表文档页面。

请注意,反之亦然:如果您有一个包含 dev_filters 参数的原生派生表,并且您在开发模式下查询该表,Looker 可以使用生产表来回答您的开发模式查询。除非您更改表的定义,然后在开发模式下查询该表,否则此陈述为真。在这种情况下,Looker 将构建一个开发表来回答查询。

将过滤条件传递到原生派生表

在“探索”中包含原生派生表时,您可以从“探索”中获取运行时过滤条件,并将其应用于原生派生表查询。为此,您可以向原生派生表的 explore_source 添加 bind_all_filtersbind_filters 参数。

将运行时过滤条件从“探索”传递到原生派生表子查询时,运行时过滤条件必须位于与原生派生表联接到同一“探索”的视图中。此外,由于原生派生表必须在运行时重新生成,才能纳入“探索”中的运行时过滤条件,因此原生派生表不能是永久性派生表 (PDT)

使用 bind_all_filters

如需将过滤条件从“探索”传递到原生派生表子查询,最简单的方法莫过于在原生派生表的 explore_source 参数中指定 bind_all_filters: yes。这会将“探索”的所有运行时过滤条件传递到原生派生表子查询。

具有 bind_all_filters: yes 的原生派生表必须联接到原生派生表的 explore_source 参数中指定的同一探索中。如果您想在其他“探索”中使用原生派生表,请改用 bind_filters 参数,如使用 bind_filters 部分中所述。

以下是包含 bind_all_filters: yes 的原生派生表的 LookML:


view: top_10_simple_item_names {
  view_label: "Top 10s"
  derived_table: {
    explore_source: order_items {
      column: total_sale_price {
        field: order_items.total_sale_price
      }
      column: item_name {
        field: products.item_name
      }
      derived_column: rank {
        sql: RANK() OVER (ORDER BY total_sale_price DESC) ;;
      }
      bind_all_filters: yes
      sorts: [order_items.total_sale_price: desc]
      timezone: "query_timezone"
      limit: 10
    }
  }
  dimension: item_name {
    group_label: "Simple Example"
  }
  dimension: rank {
    type: number
    group_label: "Simple Example"
  }
  dimension: item_name_ranked {
    group_label: "Simple Example"
    order_by_field: rank
    type: string
    sql: ${rank} || ') ' || ${item_name} ;;
  }
}

在原生派生表的视图中,explore_sourceorder_items。以下是 order_items“探索”的 LookML,其中原生派生表已联接到该“探索”中:

explore: order_items {
  ...
  join: top_10_simple_item_names {
    type: inner
    relationship: many_to_one
    sql_on: ${products.item_name} = ${top_10_simple_item_names.item_name} ;;
  }
}

如需查看此示例的实际运用情形,请参阅[分析块] - 按前 X 名透视 - bind_all_filters: yes 简介社区帖子。

使用 bind_filters

explore_sourcebind_filters 子参数会将“探索”查询中的特定过滤条件传递到原生派生表子查询中:

  • to_field 是作为过滤条件应用目标的原生派生表中的字段。to_field 必须是底层 explore_source 中的字段。
  • from_field 用于指定从“探索”中获取过滤条件的字段(如果用户在运行时指定了过滤条件)。

在运行时,“探索”中的任何 from_field 过滤条件都会传递到原生派生表子查询中的 to_field

以下 LookML 展示了一个包含 bind_filters 的原生派生表。通过此设置,Looker 会将应用于“探索”中的 filtered_lookml_dt.filter_date 字段的所有过滤条件应用于原生派生表中的 users.created_date 字段。

derived_table: {
  explore_source: order_items {
    bind_filters: {
      to_field: users.created_date
      from_field: filtered_lookml_dt.filter_date
    . . .
    }
  }
}

注意事项

仅使用一个 explore_source 参数

每个原生派生表都只接受一个 explore_source 参数。后续的 explore_source 参数不会引发 LookML 验证错误,但会替换之前的 explore_source 参数。

如需基于不同视图中的字段创建列,请先在同一探索中联接不同的视图。然后使用该探索性分析进行 explore_source

使用 include 语句启用引用字段

创建原生派生表时,您必须包含包含探索定义的相应文件。最好的方法是在单独的探索文件中定义探索,如创建探索文件文档中所述。

如果您创建单独的探索文件,请执行以下操作:

  • 原生派生表的视图文件应包含相应 Explore 的文件。例如:
    • include: "/explores/order_items.explore.lkml"
  • 探索的文件应包含所需的视图文件。例如:
    • include: "/views/order_items.view.lkml"
    • include: "/views/users.view.lkml"
  • 模型应包含“探索”文件。例如:
    • include: "/explores/order_items.explore.lkml"