AWS Athena中使用contains()方法搜索数组中的子字符串

huangapple go评论115阅读模式
英文:

AWS Athena Search for Substring in array using contains() method

问题

我有一个包含多个元素的数组列。我想查询这些数组,看看它们的任何元素是否包含特定的子字符串。

我已经尝试过使用 contains() 方法来实现这一点,它对于精确匹配有效。但我希望能够类似地处理子字符串。

英文:

I have a column of arrays that are populated with multiple elements. I would like to query these arrays to see if any of their elements contain a specific substring.

I have tried using the contains() method for this, which works for exact matches. But I would like to return something similar for a substring.

答案1

得分: 1

你可以使用 any_match 函数:

  1. -- 示例数据
  2. WITH dataset(str_arr) AS (
  3. VALUES (array['Test1', 'foo', 'bar']),
  4. (array['foo1']),
  5. (array['42']),
  6. (array[])
  7. )
  8. -- 查询
  9. SELECT *,
  10. any_match(str_arr, r -> r like '%1%')
  11. FROM dataset;

输出:

str_arr _col1
[Test1, foo, bar] true
[foo1] true
[42] false
[] false

或者使用 filter + cardinality 的组合,如果 any_match 不可用:

  1. -- 查询
  2. SELECT *,
  3. cardinality(filter(str_arr, r -> r like '%1%')) > 0
  4. FROM dataset;
英文:

You can use any_match:

  1. -- sample data
  2. WITH dataset(str_arr) AS (
  3. VALUES (array['Test1', 'foo', 'bar']),
  4. (array['foo1']),
  5. (array['42']),
  6. (array[])
  7. )
  8. -- query
  9. SELECT *,
  10. any_match(str_arr, r -> r like '%1%')
  11. FROM dataset;

Output:

str_arr _col1
[Test1, foo, bar] true
[foo1] true
[42] false
[] false

Or using combination of filter + cardinality if the any_match is not available:

  1. -- query
  2. SELECT *,
  3. cardinality(filter(str_arr, r -> r like '%1%')) > 0
  4. FROM dataset;

huangapple
  • 本文由 发表于 2023年5月25日 02:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定