2017-04-05 173 views
0

今天我試圖在Web編輯器UI中使用標準SQL語言編寫UDF,並且我已經取消選中「使用傳統SQL」選項,但它向我返回了以下錯誤消息: 未實施:您不能使用具有標準SQL查詢的傳統SQL UDF。請參閱https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql#differences_in_user-defined_javascript_functionsBigQuery:具有標準SQL的外部UDF

因此,我嘗試了一個在Google雲端平臺上提供的外部UDF示例:https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions。但它仍然返回給我同樣的錯誤信息。在以下示例中:

CREATE TEMPORARY FUNCTION multiplyInputs(x FLOAT64, y FLOAT64) 
RETURNS FLOAT64 
LANGUAGE js AS """ 
    return x*y; 
"""; 
WITH numbers AS 
    (SELECT 1 AS x, 5 as y 
    UNION ALL 
    SELECT 2 AS x, 10 as y 
    UNION ALL 
    SELECT 3 as x, 15 as y) 
SELECT x, y, multiplyInputs(x, y) as product 
FROM numbers; 

問題:如何在Web UI中將標準SQL使用外部UDF?

回答

1

確保不是在「UDF編輯器」面板中輸入輸入。它應該與您的其餘查詢。一個例子見the topic in the migration guide

#standardSQL 
-- Computes the harmonic mean of the elements in 'arr'. 
-- The harmonic mean of x_1, x_2, ..., x_n can be expressed as: 
-- n/((1/x_1) + (1/x_2) + ... + (1/x_n)) 
CREATE TEMPORARY FUNCTION HarmonicMean(arr ARRAY<FLOAT64>) 
    RETURNS FLOAT64 LANGUAGE js AS """ 
var sum_of_reciprocals = 0; 
for (var i = 0; i < arr.length; ++i) { 
    sum_of_reciprocals += 1/arr[i]; 
} 
return arr.length/sum_of_reciprocals; 
"""; 

WITH T AS (
    SELECT GENERATE_ARRAY(1.0, x * 4, x) AS arr 
    FROM UNNEST([1, 2, 3, 4, 5]) AS x 
) 
SELECT arr, HarmonicMean(arr) AS h_mean 
FROM T; 
+0

我想,當'使用傳統的SQL'未被選中 - '的UDF Editor'甚至沒有可供用戶選擇 –

+1

有可能在UDF編輯器,同時使用'寫一些內容檢查舊版SQL',然後切換回查詢編輯器並取消選中'使用舊版SQL'並嘗試運行查詢。 –

+0

是的,看起來像這是唯一的方式來到案例PO進入 –