使用DuckDB,可以像这样展开 struct :

    SELECT
        parent_id,
        top_level_struct.*
    FROM 
        arrow_table AS root

我在try 解包嵌套 struct 时遇到了一个问题(参见下面的示例代码):

    SELECT
        parent_id,
        my_list_unnested.my_list.* EXCLUDE (list_struct),  -- not working
        my_list_unnested.my_list.list_struct.* -- not working
    FROM 
        arrow_table AS root,
        UNNEST(root.my_list) AS my_list_unnested        

Code to reproduce the issue

import duckdb
import pyarrow as pa

test_records = [
    {
        "parent_id": 123,
        "top_level_struct": {
            "top_level_hello": "World",
            "top_level_foo": "bar",
            "top_level_baz": "qux"
        },
        "my_list": [
            {
                "item_id": 123,
                "list_struct": {
                    "list_hello": "World",
                    "list_foo": "bar",
                    "list_baz": "qux"
                }
            }
        ]
    }
]


arrow_table = pa.Table.from_pylist(test_records)

# this is working
WORKING_SQL = """
    SELECT
        parent_id,
        top_level_struct.*
    FROM 
        arrow_table AS root
"""
df = duckdb.sql(WORKING_SQL)

# this is not working
NOT_WORKING_SQL = """
    SELECT
        parent_id,
        my_list_unnested.my_list.* EXCLUDE (list_struct),  -- not working
        my_list_unnested.my_list.list_struct.* -- not working
    FROM 
        arrow_table AS root,
        UNNEST(root.my_list) AS my_list_unnested        
"""

df = duckdb.sql(NOT_WORKING_SQL)

# Gives
# duckdb.duckdb.ParserException: Parser Error: syntax error at or near "*"

What I try to achieve

我正在try 将上述记录展平为如下所示的 struct ,由于我正在处理的实际 case ,我需要使用DuckDB:

desired_structure = [
    {
        "parent_id": 123,
        "top_level_hello": "World",
        "top_level_foo": "bar",
        "top_level_baz": "qux",
        "item_id": 123,
        "list_hello": "World",
        "list_foo": "bar",
        "list_baz": "qux"
    }
]

Environment/versions

  • DuckDB:0.9.2
  • 巨 Python :3.10.12
  • Ubuntu 22.04

推荐答案

所需的 struct 看起来像是Recursive Unnest

duckdb.sql("""
from arrow_table
select 
   parent_id,
   unnest(top_level_struct),
   unnest(my_list, recursive := true)
""")
┌───────────┬───────────────┬───────────────┬─────────────────┬─────────┬──────────┬──────────┬────────────┐
│ parent_id │ top_level_baz │ top_level_foo │ top_level_hello │ item_id │ list_baz │ list_foo │ list_hello │
│   int64   │    varchar    │    varchar    │     varchar     │  int64  │ varchar  │ varchar  │  varchar   │
├───────────┼───────────────┼───────────────┼─────────────────┼─────────┼──────────┼──────────┼────────────┤
│       123 │ qux           │ bar           │ World           │     123 │ qux      │ bar      │ World      │
└───────────┴───────────────┴───────────────┴─────────────────┴─────────┴──────────┴──────────┴────────────┘

Python-3.x相关问答推荐

Pandas 中每行的最大值范围

使用pybind11时,在sys.exit(0)处成功完成测试后,Python单元测试冻结

CONNEXION.EXCEPTIONS.ResolverError:运行pyz文件时未命名模块

如何转换Pandas中的数据,以使我 Select 的列名变为行值并增加行?

将字符串转换为python日期时间时出错

如何使用TensorFlow Keras子类化来构建和训练模型

为什么我无法在django中按月筛选事件?

将列表转换为 pandas 数据框,其中列表包含字典

可以在 Python 的上下文管理器中调用 sys.exit() 吗?

找到在指定列的另一个分组中存在重复的行.

隐藏Cartopy中高纬度非矩形投影的右侧轴(纬度)标签

在不改变 python 中原始数组顺序的情况下,对多维字符串数组进行降序排序?

这种类型提示有什么作用?

命名元组内命名元组的 Python 语法

为什么不切换到 Python 3.x?

基本 Flask 应用程序未运行(TypeError:模块中缺少必填字段type_ignores)

为什么 Django South 1.0 使用 iteritems()?

为什么`multiprocessing.Queue.get`这么慢?

如何将文档字符串放在 Enums 上?

python中的绝对导入是什么?