lookml-fields
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInstructions
使用说明
1. Field Types Overview
1. 字段类型概述
LookML fields are the building blocks of your data model. Each type serves a specific purpose in generating SQL.
| Field Type | Purpose | SQL Generation Phase |
|---|---|---|
| Dimension | Describes data (attributes). Groups results. | |
| Measure | Aggregates data (metrics). Calculates results. | |
| Filter | Restricts data based on conditions. | |
| Parameter | Captures user input for dynamic logic. | None directly (injects values into other fields). |
| Dimension Group | Generates a set of time-based dimensions. | |
LookML字段是数据模型的构建模块。每种类型在生成SQL时都有特定用途。
| 字段类型 | 用途 | SQL生成阶段 |
|---|---|---|
| Dimension | 描述数据(属性),对结果进行分组。 | |
| Measure | 聚合数据(指标),计算结果。 | |
| Filter | 根据条件限制数据。 | |
| Parameter | 捕获用户输入以实现动态逻辑。 | 无直接生成(将值注入其他字段)。 |
| Dimension Group | 生成一组基于时间的维度。 | |
2. The Role of sql
Parameter
sql2. sql
参数的作用
sqlThe parameter behaves differently strictly based on the field type.
sqlsqlDimensions: The "What"
Dimension:“描述内容”
- Role: Defines the raw transformation of the column before any aggregation.
- SQL Context: The expression is placed directly into the clause.
GROUP BY - Input: Can reference table columns (), other dimensions (
${TABLE}.col), or raw SQL functions.${dim} - Example:
lookml
dimension: full_name { sql: CONCAT(${first_name}, ' ', ${last_name}) ;; } -- Generates: CONCAT(table.first_name, ' ', table.last_name)
- 作用:定义列在聚合前的原始转换。
- SQL上下文:表达式直接放入子句中。
GROUP BY - 输入:可引用表列()、其他Dimension(
${TABLE}.col)或原生SQL函数。${dim} - 示例:
lookml
dimension: full_name { sql: CONCAT(${first_name}, ' ', ${last_name}) ;; } -- 生成SQL:CONCAT(table.first_name, ' ', table.last_name)
Measures: The "How Much"
Measure:“统计数值”
- Role: Defines the value to be aggregated or the calculation involving other aggregates.
- SQL Context: Puts the expression inside the aggregation function (e.g., ), or as a standalone calculation for
SUM(sql).type: number - Input:
- For : References dimensions or columns.
type: sum/avg/min/max - For : References other measures.
type: number - For :
type: countis ignored (alwayssqlorCOUNT(*)).COUNT(primary_key)
- For
- Example:
lookml
measure: total_profit { type: sum sql: ${sale_price} - ${cost} ;; } -- Generates: SUM(sale_price - cost)
- 作用:定义要聚合的值,或涉及其他聚合结果的计算。
- SQL上下文:将表达式放入聚合函数内部(例如),或作为
SUM(sql)类型的独立计算。type: number - 输入:
- 对于:引用Dimension或列。
type: sum/avg/min/max - 对于:引用其他Measure。
type: number - 对于:
type: count参数会被忽略(始终为sql或COUNT(*))。COUNT(primary_key)
- 对于
- 示例:
lookml
measure: total_profit { type: sum sql: ${sale_price} - ${cost} ;; } -- 生成SQL:SUM(sale_price - cost)
Filters: The "Which"
Filter:“筛选范围”
- Role: Defines the condition logic, usually for Templated Filters used in Derived Tables or .
sql_always_where - SQL Context: The parameter in a
sqlfield is rarely used directly in modern LookML. Instead, the input to the filter is used infiltertags.{% condition %} - Best Practice: Identify if you need a field or just a
filter+parameter.dimension - Example (Templated Filter):
lookml
filter: date_filter { type: date } -- Usage in Derived Table SQL: -- WHERE {% condition date_filter %} created_at {% endcondition %}
- 作用:定义条件逻辑,通常用于派生表或中的模板过滤器。
sql_always_where - SQL上下文:在现代LookML中,字段的
filter参数很少直接使用。而是使用过滤器的输入值放入sql标签中。{% condition %} - 最佳实践:确定你需要的是字段,还是仅需
filter+parameter组合。dimension - 示例(模板过滤器):
lookml
filter: date_filter { type: date } -- 在派生表SQL中的用法: -- WHERE {% condition date_filter %} created_at {% endcondition %}
Parameters: The "User Input"
Parameter:“用户输入”
- Role: Does NOT generate SQL itself. It holds a user-selected value to be injected into other fields.
- SQL Context: Accessed via Liquid variables () inside Dimensions, Measures, or Derived Tables.
{% parameter name %} - Input: User selects from a UI list or types a value.
- Example:
lookml
parameter: timeframe_selector { type: unquoted allowed_value: { value: "month" } allowed_value: { value: "year" } } dimension: dynamic_date { sql: DATE_TRUNC({% parameter timeframe_selector %}, ${created_raw}) ;; }
- 作用:本身不生成SQL。它存储用户选择的值,以便注入到其他字段中。
- SQL上下文:通过Liquid变量()在Dimension、Measure或派生表中访问。
{% parameter name %} - 输入:用户从UI列表中选择或输入值。
- 示例:
lookml
parameter: timeframe_selector { type: unquoted allowed_value: { value: "month" } allowed_value: { value: "year" } } dimension: dynamic_date { sql: DATE_TRUNC({% parameter timeframe_selector %}, ${created_raw}) ;; }
Dimension Groups: The "Time Generator"
Dimension Group:“时间生成器”
- Role: Defines the source timestamp or date column. Looker then generates multiple dimension fields based on the list.
timeframes - SQL Context: Casts and truncates the source column for each timeframe.
- Input: Must be a standardized timestamp or date expression.
- Example:
lookml
dimension_group: created { type: time timeframes: [date, month] sql: ${TABLE}.created_at ;; } -- Generates: -- created_date -> CAST(table.created_at AS DATE) -- created_month -> DATE_TRUNC(table.created_at, MONTH)
- 作用:定义源时间戳或日期列。Looker会基于列表生成多个维度字段。
timeframes - SQL上下文:为每个时间范围转换并截断源列。
- 输入:必须是标准化的时间戳或日期表达式。
- 示例:
lookml
dimension_group: created { type: time timeframes: [date, month] sql: ${TABLE}.created_at ;; } -- 生成字段: -- created_date -> CAST(table.created_at AS DATE) -- created_month -> DATE_TRUNC(table.created_at, MONTH)
3. Summary of Differences
3. 差异总结
| Type | | Can reference Measures? | Aggregated? |
|---|---|---|---|
| Dimension | Columns, Other Dimensions | NO | No |
| Measure (Agg) | Columns, Dimensions | NO | Yes |
| Measure (Num) | Other Measures | YES | Yes (already agg) |
| Filter | (Rarely used) | No | N/A |
| Parameter | (None) | No | N/A |
| Value Format | (None) | No | N/A |
| 类型 | | 可引用Measure? | 是否聚合? |
|---|---|---|---|
| Dimension | 列、其他Dimension | 否 | 否 |
| Measure(聚合型) | 列、Dimension | 否 | 是 |
| Measure(数值型) | 其他Measure | 是 | 是(已聚合) |
| Filter | (极少使用) | 否 | 不适用 |
| Parameter | (无) | 否 | 不适用 |
| Value Format | (无) | 否 | 不适用 |
Reference Skills
参考技能
For detailed standards on specific field types, refer to:
- Dimensions: Naming, labels, and type-specific rules.
- Measures: Aggregation types, filters, and formats.
- Filters & Parameters: Templated filters and user input.
- Dimension Groups: Timeframes and intervals.
- Value Formats: Named and custom currency/number formats.
如需了解特定字段类型的详细标准,请参考:
- Dimensions:命名、标签及类型特定规则。
- Measures:聚合类型、过滤器及格式。
- Filters & Parameters:模板过滤器与用户输入。
- Dimension Groups:时间范围与间隔。
- Value Formats:命名及自定义货币/数字格式。