如何获得ARRAY_CONSTRUCT作为阵列?

问题描述

我正在尝试使用雪花API查询我的仓库并作为响应来获取值数组。为了做到这一点,我使用了ARRAY_CONSTRUCT。但是,我得到的不是单个字符串,而是一个带有内部值的字符串。

相关代码

import sNowflake.connector

class SNowflakeClient:
    def __init__(self,connection: Dict)
      self._connection = sNowflake.connector.connect(..)
      self._cursor = self._connection.cursor()

    def execute_query(self,query: str) -> None:
        self._cursor.execute(query)

    def fetch_results(self,num_of_results: int) -> List:
        return self._cursor.fetchmany(num_of_results)

我正在使用的查询类似于:

SELECT  ARRAY_CONSTRUCT(col1,col2,col3) as some_value
FROM table1

而不是-[value1,value2,value3] 我正在-'[\n value1 \n value2 \n value3 ]'

有什么主意我该如何更改查询获取所需的内容

解决方法

我检查了文档,您是正确的 https://docs.snowflake.com/en/sql-reference/functions/array_construct.html

它确实返回如下。但是在Snowflake UI中,它确实删除了换行符。

select array_construct(10,20,30);
+-----------------------------+
| ARRAY_CONSTRUCT(10,30) |
|-----------------------------|
| [                           |
|   10,|
|   20,|
|   30                        |
| ]                           |
+-----------------------------+

现在,因为这是python,所以最好对结果对象进行如下尝试。

from snowflake.connector import DictCursor
cursor = con.cursor(DictCursor)
res = cursor.execute(<select query>)
result = cursor.fetchall()
result[0]
{'COLUMN_NAME': '[\n  100,\n  "abc"\n]'}
result[0]['COLUMN_NAME'].replace('\n','')

让我知道这是否有帮助。谢谢