納入 SQL 並參照 LookML 物件

如要編寫功能強大的 LookML,您必須能夠參照現有的維度、測量指標、檢視區塊或衍生資料表,即使這些項目不在目前的範圍內也一樣。您也需要參照基礎表格中的資料欄,並使用資料庫方言的函式呼叫來操控這些值。

替換運算子 ($)

替換運算子 $ 可讓 LookML 程式碼更易於重複使用和模組化,方便您參照其他檢視區塊和衍生資料表、SQL 資料表中的資料欄,或是 LookML 維度和測量指標。這有兩個好處。首先,您可能已經計算出非常複雜的維度或指標,不需要再次撰寫所有複雜的內容。其次,如果您變更維度或指標的任何項目,該變更可能會傳播至依附於該維度或指標的所有其他項目。

您可以使用替換運算子執行下列操作:

${TABLE}.column_name 參照的資料表資料欄與您目前使用的檢視畫面相連。例如:

dimension: customer_id {
  type: number
  sql: ${TABLE}.customer_id ;;
}

${field_name} 參照您正在處理的檢視區塊中的維度或指標。例如:

measure: total_population {
  type: sum
  sql: ${population} ;;
}

${view_name.field_name} 參照其他檢視區的維度或指標。例如:

dimension: lifetime_orders {
  type: number
  sql: ${user_order_facts.lifetime_orders} ;;
}

${view_name.SQL_TABLE_NAME} 參照其他檢視畫面或衍生資料表。請注意,這個參照中的 SQL_TABLE_NAME 是字串常值,不需要替換成任何內容。例如:

explore: trips {
  view_label: "Long Trips"
  # This will ensure that we only see trips that are longer than average!
  sql_always_where: ${trips.trip_duration}>=(SELECT tripduration FROM ${average_trip_duration.SQL_TABLE_NAME});;
}

${view_name.SQL_TABLE_NAME} 無法與 datagroups 搭配使用的 sql_trigger 參數一起運作。

範圍和命名

您可以為探索、檢視區塊、欄位和集合命名。這些 Looker ID 不會加上引號。

LookML 欄位和集合有全名簡稱

  • 全名格式為 <view>.<field-name | set-name>。左側會顯示範圍,也就是包含欄位或集合的檢視畫面。右側會指定特定欄位或集合名稱。
  • 簡稱的形式為 <field-name | set-name>,不含分隔句點。Looker 會根據使用範圍,將簡短名稱擴展為完整名稱。

以下範例顯示多種形式的名稱和範圍。這組欄位並不實際,但可展示各種可能的範圍運算式。

view: orders {                   # "orders" becomes the containing scope
  measure: count {               # short name, equivalent to orders.count
    type: count
  }
  dimension: customer_id {       # short name, equivalent to orders.customer_id
    type: number
    sql: ${TABLE}.customer_id ;;
  }
  dimension: customer_address {  # short name, equivalent to orders.customer_address
    sql: ${customer.address} ;;  # full name, references a field defined in the "customer" view
  }
  set: drill_fields {            # short name, equivalent to orders.drill_fields
    fields: [
      count,                     # short name, equivalent to orders.count
      customer.id                # full name, references a field defined in the "customer" view
    ]
  }
}

dimension: customer_address 宣告中,請注意 SQL 區塊 (customer) 的基礎檢視區塊與封閉檢視區塊範圍 (orders) 不同。當您需要比較兩個不同檢視區塊之間的欄位時,這會很有用。

當檢視區塊 (以下稱為「檢視區塊 A」) 參照在不同檢視區塊 (以下稱為「檢視區塊 B」) 中定義的欄位時,請注意下列事項:

  1. 使用 include 參數,將檢視區塊 B 檔案納入與檢視區塊 A 相同的模型。
  2. 檢視區塊 B 必須在至少一個「探索」中與檢視區塊 A 聯結。如要瞭解彙整,請參閱「在 LookML 中使用彙整」頁面。

SQL 方言

Looker 支援多種資料庫類型,例如 MySQL、Postgres、Redshift 和 BigQuery。每個資料庫支援的功能集略有不同,函式名稱也不一樣,這稱為 SQL 方言

LookML 的設計可搭配所有 SQL 方言使用,且 LookML 不會偏好任何一種方言。不過,您需要在特定 LookML 參數中加入 SQL 程式碼運算式 (稱為 SQL 區塊)。有了這些參數,Looker 會直接將 SQL 運算式傳遞至資料庫,因此您必須使用與資料庫相符的 SQL 方言。舉例來說,如果您使用 SQL 函式,該函式必須是資料庫支援的函式。

SQL 區塊

部分 LookML 參數需要您提供原始 SQL 運算式,Looker 才能瞭解如何從資料庫擷取資料。

sql_ 開頭的 LookML 參數會預期某種形式的 SQL 運算式。例如:sql_always_wheresql_onsql_table_name。SQL 區塊最常見的 LookML 參數是 sql,用於維度和測量指標欄位定義,指定定義維度或測量指標的 SQL 運算式。

您在 SQL 區塊中指定的程式碼可以是單一欄位名稱,也可以是相關子查詢等複雜程式碼。內容可能相當複雜,幾乎可滿足您在原始 SQL 中表達自訂查詢邏輯的任何需求。請注意,SQL 區塊中使用的程式碼必須與資料庫使用的 SQL 方言相符。

維度和測量指標的 SQL 區塊範例

以下是維度和測量指標的 SQL 區塊範例。LookML 替代運算子 ($) 會讓這些 sql 宣告看起來不像 SQL。不過,完成替代作業後,產生的字串就是純 SQL,Looker 會將其插入查詢的 SELECT 子句。

dimension: id {
  primary_key: yes
  sql: ${TABLE}.id ;;   # Specify the primary key, id
}
measure: average_cost {
  type: average
  value_format: "0.00"
  sql: ${order_items.cost} ;;   # Specify the field that you want to average
}
dimension: name {
  sql: CONCAT(${first_name}, ' ', ${last_name}) ;;
}
dimension: days_in_inventory {
  type: int
  sql: DATEDIFF(${sold_date}, ${created_date}) ;;
}

如最後兩個維度所示,SQL 區塊可以使用基礎資料庫支援的函式 (例如本例中的 MySQL 函式 CONCATDATEDIFF)。

含有相關子查詢的 SQL 區塊範例

您可以在欄位的 SQL 區塊中放置任何 SQL 陳述式,包括相關的子選取項目。範例如下:

view: customers {
  dimension: id {
    primary_key: yes
    sql: ${TABLE}.id ;;
  }
  dimension: first_order_id {
    sql: (SELECT MIN(id) FROM orders o WHERE o.customer_id=customers.id) ;;
         # correlated subselect to derive the value for "first_order_id"
  }
}

衍生資料表的 SQL 區塊範例

衍生資料表會使用 SQL 區塊指定衍生資料表的查詢。範例如下:

view: user_order_facts {
  derived_table: {
    sql:            # Get the number of orders for each user
      SELECT
        user_id
        , COUNT(*) as lifetime_orders
      FROM orders
      GROUP BY 1 ;;
  }
  # later, dimension declarations reference the derived column(s)

  dimension: lifetime_orders {
    type: number
  }
}

LookML 欄位類型參考資料

在另一個欄位中參照現有 LookML 欄位時,您可以使用雙冒號 (::) 加上所選類型,指示 Looker 將參照欄位視為特定資料類型。舉例來說,如果您在另一個欄位中參照 orders.created_date 維度,可以使用 ${orders.created_date::date} 語法,確保 created_date 欄位在 Looker 產生的 SQL 中會視為日期欄位,而不是轉換為字串。

您可以在參照中使用的資料類型,取決於所參照原始欄位的資料類型。舉例來說,如果您要參照字串欄位,唯一可以指定的資料類型是 ::string。以下是各類型欄位可用的完整欄位類型參照清單:

  • 在參照字串欄位時,您可以使用 ::string
  • 在參照數字欄位時,您可以使用 ::string::number
  • 如要參照日期或時間欄位,可以使用 ::string::date::datetime

    使用 ::string::date 的參照會以查詢時區傳回資料,而使用 ::datetime 的參照則會以資料庫時區傳回資料。
  • 在參照 yesno 欄位時,您可以使用 ::string::number::boolean

    如果資料庫方言不支援布林資料類型,就無法使用 ::boolean 類型的欄位參照。
  • 在位置欄位的參照中,您可以使用 ::latitude::longitude

搭配日期欄位使用 LookML 欄位類型參照

舉例來說,假設您有 enrollment_month 維度和 graduation_month 維度,這兩個維度都是在type: time 的維度群組中建立。在本範例中,enrollment_month 維度是由下列維度群組的 type: time 產生:


dimension_group: enrollment {
  type: time
  timeframes: [time, date, week, month, year, raw]
  sql: ${TABLE}.enrollment_date ;;
}

同樣地,graduation_month 維度是由下列 type: time 維度群組所建立:


dimension_group: graduation {
  type: time
  timeframes: [time, date, week, month, year, raw]
  sql: ${TABLE}.graduation_date ;;
}

使用 enrollment_monthgraduation_month 維度,您可以建立 type: duration 的維度群組,計算學生從入學到畢業之間經過的月數或年數。不過,由於 Looker 產生的 SQL 中,部分日期欄位會轉換為字串,因此將 enrollment_monthgraduation_month 維度設為 sql_startsql_end 的值可能會導致錯誤。

為避免這些時間欄位轉換為字串時發生錯誤,其中一個做法是建立 type: duration 維度群組,並在 sql_startsql_end 參數中參照 enrollmentgraduation 維度群組的 raw 時間範圍:


dimension_group: enrolled {
  type: duration
  intervals: [month, year]
  sql_start: ${enrollment_raw} ;;
  sql_end: ${graduation_raw} ;;
}

在「探索」使用者介面中,這會產生名為「註冊時間」的維度群組,其中包含「註冊月數」和「註冊年數」這兩個維度。

如要簡化在 type: duration 的維度群組中使用 raw 時間範圍的做法,可以為 sql_startsql_end 參數參照的欄位指定 ::date::datetime 參照類型。


dimension_group: enrolled {
  type: duration
  intervals: [month, year]
  sql_start: ${enrollment_month::date} ;;
  sql_end: ${graduation_month::date} ;;
}

這個範例中的 LookML 也會建立「註冊時間長度」維度群組,但使用 ::date 參照可讓 enrollment_monthgraduation_month 維度在不使用 raw 時間範圍或以 SQL 將其轉換為字串的情況下使用。

如需如何使用 LookML 欄位型別參照建立 type: duration 自訂維度群組的其他範例,請參閱 dimension_group 參數說明文件頁面。

這個語法不適用於 type: list 的量值,因為從 Looker 6.8 開始,就無法參照這些量值。

LookML 常數

constant 參數可讓您指定可在整個 LookML 專案中使用的常數。使用 LookML 常數,您只需定義一次值,即可在專案中接受字串的任何部分參照該值,進而減少 LookML 程式碼中的重複內容。

常數必須在專案資訊清單檔案中宣告,且常數值必須是字串。舉例來說,您可以定義值為 "Okayama" 的常數 city,如下所示:

constant: city {
  value: "Okayama"
}

然後,您可以使用 @{city} 語法,在整個專案中參照 city 常數。舉例來說,您可以在 users 探索中使用 city 常數和 label 參數:


explore: users {
  label: "@{city} Users"
}

接著,Looker 會在「探索」選單和探索標題中顯示「岡山使用者」,而非預設的「使用者」

如要進一步瞭解如何使用 LookML 常數編寫可重複使用的程式碼,請參閱 constant 參數說明文件頁面。