Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloudquery/sdk/scalar/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def set(self, value: Any):
scalar = self._type()
scalar.set(item)
self._value.append(scalar)
self._valid = True
self._valid = True
return

raise ScalarInvalidTypeError(f"Invalid type {type(value)} for List")
Expand Down
14 changes: 12 additions & 2 deletions cloudquery/sdk/schema/resource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .table import Table
from typing import Any
import pyarrow as pa
from cloudquery.sdk.scalar import ScalarFactory
from cloudquery.sdk.scalar import List, Scalar, ScalarFactory


class Resource:
Expand All @@ -21,9 +21,19 @@ def set(self, column_name: str, value: Any):
self._data[index_column].set(value)

def to_list_of_arr(self):
return [[self._data[i].value] for i, _ in enumerate(self._table.columns)]
return [
[_arrow_value(self._data[i])] for i, _ in enumerate(self._table.columns)
]

def to_arrow_record(self):
return pa.record_batch(
self.to_list_of_arr(), schema=self._table.to_arrow_schema()
)


def _arrow_value(scalar: Scalar):
if not isinstance(scalar, List):
return scalar.value
if not scalar.is_valid:
return None
return [_arrow_value(item) for item in scalar.value]
26 changes: 26 additions & 0 deletions tests/schema/test_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pyarrow as pa

from cloudquery.sdk.schema import Column, Table
from cloudquery.sdk.schema.resource import Resource


def test_resource_to_arrow_record_with_list_columns():
table = Table(
"test_table",
[
Column("strings", pa.list_(pa.string())),
Column("ints", pa.list_(pa.int64())),
Column("empty", pa.list_(pa.string())),
Column("null", pa.list_(pa.string())),
Column("unset", pa.list_(pa.string())),
],
)
resource = Resource(table, None, None)
resource.set("strings", ["a", None, "b"])
resource.set("ints", [1, 2])
resource.set("empty", [])
resource.set("null", None)
assert resource.to_arrow_record() == pa.record_batch(
[[["a", None, "b"]], [[1, 2]], [[]], [None], [None]],
schema=table.to_arrow_schema(),
)