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

Google bigquery 如何在Bigquery查询文本中使用转义引号?,google-bigquery,user-defined-functions,Google Bigquery,User Defined Functions,我在一个查询中定义并注册了一个UDF函数,但大查询似乎不喜欢函数定义字符串中的转义引号\有人知道如何在bigquery中使用转义引号吗 这是我的例子: SELECT Social_Connection, Device_Type FROM js( -- input table ( SELECT user_attribute.Name, user_attribute.Value FROM UDF_TESTING.test

我在一个查询中定义并注册了一个UDF函数,但大查询似乎不喜欢函数定义字符串中的转义引号\有人知道如何在bigquery中使用转义引号吗

这是我的例子:

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函数中使用escape。 希望这对你有帮助

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

你能用单引号吗?row.user\u属性[i].Name=='Social\u Connection'在本例中,我可以,但我有一些嵌套字符串使用单引号和双引号,我必须使用转义字符..在您的示例中,引号不是字符串的一部分,因此您可以安全地使用单引号。\当您在字符串中使用它们时,应该使用\。如果不是这样的话,很高兴看到这样的例子。我刚刚做了一个快速的虚拟测试,它工作得非常完美,但我想知道我是否可以在大查询查询文本中使用转义字符正如我所说的,它对我有效-如果你能提供困扰你的示例-我希望我能向你展示