探索来源

本页引用的 explore_source 参数是 derived_table 的子参数。

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

用量

层次结构
explore_source
默认值


接受
从中派生原生派生表的探索的标识符,以及定义原生派生表的子参数。

定义

可通过以下两种方式创建派生表(将它视为数据库中的普通表):原生派生表(使用 LookML 参数定义)和基于 SQL 的派生表(使用 SQL 查询语句定义)。

explore_source 参数用于原生派生表。在此参数中,您可以定义将添加到原生派生表中的列、要应用于原生派生表的任何过滤条件、是否对原生派生表中的行进行限制或排序,以及是否将原生派生表中基于时间的字段转换为不同的时区。

提示:创建原生派生表的最简单方法是使用探索创建所需的查询,然后从探索中选择 Get LookML 选项。Looker 会为查询生成派生的 LookML,您可以将该表复制到 LookML 项目的视图文件中。如需了解详情,请参阅创建原生派生表文档页面。

定义原生派生表

您可以在原生派生表中使用各种参数,其中许多参数都是可选的。定义原生派生表的语法如下所示,后跟每个参数的其他信息。

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 时,请注意以下事项:
运行时过滤条件必须位于与原生派生表联接到的探索所在的视图中。
无法将原生派生表转换为永久性派生表 (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_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 年的数据以及针对尤卡谷机场的数据)。dev_filters 参数与 filters 参数结合使用,可将所有过滤条件应用到表的开发版本。如果 dev_filtersfilters 为同一列指定了过滤条件,则 dev_filters 的优先级高于表的开发版本。在本例中,表格的开发版会过滤到尤卡谷机场最近 90 天的数据。

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

请注意,反之亦然:如果您有一个具有 dev_filters 参数的原生派生表,并在开发模式下进行查询,Looker 可以使用生产表来回答开发模式查询。除非您更改表的定义,然后在“开发模式”中查询该表,否则 Looker 会构建一个开发表来回答查询。

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

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

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

使用 bind_all_filters

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

必须将原生派生表与 bind_all_filters: yes 联接到在原生派生表的 explore_source 参数中指定的同一探索中。如果您想在其他探索中使用原生派生表,请改为使用 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} ;;
  }
}

如需了解此示例的实际应用情况,请参阅 [Analytic Block] – Pivot by Top X – introduction 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 语句启用引用字段

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

如果您创建单独的探索文件:

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