如何在 Bigquery 查询文本中使用转义引号?

How to use escape quote in Bigquery query text?

我在查询中定义并注册了一个 UDF 函数,但看起来大查询不喜欢函数定义字符串中的转义引号 \",有谁知道如何在其中使用转义引号大查询?

这是我的例子:

SELECT
  Social_Connection,
  Device_Type
FROM
  js(
    -- input table
    (
    SELECT
      user_attribute.Name,
      user_attribute.Value
    FROM
      UDF_TESTING.testing_src ),
    -- input vars
    user_attribute.Name,
    user_attribute.Value,
    -- output schema
    "[{name: 'Social_Connection', type: 'string'},
   {name: 'Device_Type', type: 'string'}]",
    -- the function
    "function(row, emit) {
     var social_connection_index = 0;
     var device_type_index = 0;
  for (var i = 0; i < row.user_attribute.length; i++) {
    if (row.user_attribute[i].Name == \"Social_Connection\") {    // <------big query complains about the escape quote
      social_connection_index = i;
    }
    if (row.user_attribute[i].Name == \"Device_Type\") {   // <----- same as here
      device_type_index = i;
    }
  }
  emit( {Social_Connection: row.user_attribute[social_connection_index].Value,
         Device_Type: row.user_attribute[device_type_index].Value} )
}")

下面是虚拟示例,只是为了演示如何在 BQ SELECT 本身以及 JS 函数中使用转义。
希望对你有帮助!

SELECT Name, HasSingleQuote, HasDoubleQuote
FROM ( JS(
    -- input table
    (SELECT
      Name
    FROM
      (SELECT 'abc' AS Name),
      (SELECT 'a\'bc' AS Name),
      (SELECT 'a\"bc' AS Name),
      (SELECT 'a\"b\'c' AS Name)
      ),
    -- input vars
    Name,
    -- output schema
    "[{name: 'Name', type: 'STRING'},
   {name: 'HasSingleQuote', type: 'BOOLEAN'},
   {name: 'HasDoubleQuote', type: 'BOOLEAN'}]",
    -- the function
    "function(row, emit) {
      var hasSingleQuote = false;
      var hasDoubleQuote = false;
      if (row.Name.indexOf('\'') > -1) hasSingleQuote = true;
      if (row.Name.indexOf('\"') > -1) hasDoubleQuote = true;
      emit( {Name: row.Name,
         HasSingleQuote: hasSingleQuote,
         HasDoubleQuote: hasDoubleQuote
         } )
}"))

输出为:

Name    HasSingleQuote  HasDoubleQuote   
abc     false           false    
a'bc    true            false    
a"bc    false           true
a"b'c   true            true