Skip to content

Commit c642c0a

Browse files
committed
Return PyArrow Dataset instead of RecordBatchReader
1 parent 02902e4 commit c642c0a

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

xarray_sql/df.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import pandas as pd
66
import pyarrow as pa
7+
import pyarrow.dataset
78
import xarray as xr
89
from datafusion.context import ArrowStreamExportable
910

@@ -171,19 +172,32 @@ def _parse_schema(ds) -> pa.Schema:
171172
return pa.schema(columns)
172173

173174

174-
def read_xarray(ds: xr.Dataset, chunks: Chunks = None) -> pa.RecordBatchReader:
175-
"""Pivots an Xarray Dataset into a PyArrow Table, partitioned by chunks.
175+
# def read_xarray(ds: xr.Dataset, chunks: Chunks = None) -> pa.RecordBatchReader:
176+
# """Pivots an Xarray Dataset into a PyArrow Table, partitioned by chunks.
176177

177-
Args:
178-
ds: An Xarray Dataset. All `data_vars` mush share the same dimensions.
179-
chunks: Xarray-like chunks. If not provided, will default to the Dataset's
180-
chunks. The product of the chunk sizes becomes the standard length of each
181-
dataframe partition.
178+
# Args:
179+
# ds: An Xarray Dataset. All `data_vars` mush share the same dimensions.
180+
# chunks: Xarray-like chunks. If not provided, will default to the Dataset's
181+
# chunks. The product of the chunk sizes becomes the standard length of each
182+
# dataframe partition.
182183

183-
Returns:
184-
A PyArrow Table, which is a table representation of the input Dataset.
185-
"""
184+
# Returns:
185+
# A PyArrow Table, which is a table representation of the input Dataset.
186+
# """
187+
188+
# def pivot_block(b: Block):
189+
# return pivot(ds.isel(b))
190+
191+
# fst = next(iter(ds.values())).dims
192+
# assert all(
193+
# da.dims == fst for da in ds.values()
194+
# ), "All dimensions must be equal. Please filter data_vars in the Dataset."
186195

196+
# schema = _parse_schema(ds)
197+
# blocks = block_slices(ds, chunks)
198+
# return from_map_batched(pivot_block, blocks, schema=schema)
199+
200+
def read_xarray(ds: xr.Dataset, chunks: Chunks = None) -> pa.dataset.Dataset:
187201
def pivot_block(b: Block):
188202
return pivot(ds.isel(b))
189203

@@ -194,4 +208,16 @@ def pivot_block(b: Block):
194208

195209
schema = _parse_schema(ds)
196210
blocks = block_slices(ds, chunks)
197-
return from_map_batched(pivot_block, blocks, schema=schema)
211+
212+
213+
214+
children = []
215+
for block in blocks:
216+
def map_batches():
217+
df = pivot_block(block)
218+
yield pa.RecordBatch.from_pandas(df, schema=schema)
219+
220+
children.append(pa.dataset.InMemoryDataset(pa.RecordBatchReader.from_batches(schema, map_batches()),schema))
221+
222+
out = pa.dataset.UnionDataset(schema,children)
223+
return out

xarray_sql/sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def from_dataset(
1515
):
1616
arrow_table = read_xarray(input_table, chunks)
1717
table = Table.from_dataset(arrow_table)
18-
return self.register_table(table_name, arrow_table )
18+
return self.register_table(table_name, table )

0 commit comments

Comments
 (0)