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
12 changes: 11 additions & 1 deletion crates/core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,17 @@ impl PySessionContext {
let schema = if let Some(schema) = schema {
SchemaRef::from(schema.0)
} else {
partitions.0[0][0].schema()
partitions
.0
.iter()
.find_map(|partition| partition.first())
.ok_or_else(|| {
PyValueError::new_err(
"Cannot create a DataFrame without a schema: the \
provided partitions contain no record batches.",
)
})?
.schema()
};

let table = MemTable::try_new(schema, partitions.0)?;
Expand Down
21 changes: 21 additions & 0 deletions python/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ def test_register_record_batches_empty(ctx):
assert ctx.sql("SELECT a FROM t2").collect()[0].column(0) == pa.array([1, 2, 3])


@pytest.mark.parametrize(
"partitions", [[], [[]], [[], []]], ids=["no_partitions", "one_empty", "all_empty"]
)
def test_create_dataframe_no_batches_no_schema_raises(ctx, partitions):
with pytest.raises(ValueError, match="no record batches"):
ctx.create_dataframe(partitions)


def test_create_dataframe_empty_batches_with_explicit_schema(ctx):
schema = pa.schema([("a", pa.int64())])
df = ctx.create_dataframe([[]], schema=schema)
assert df.schema() == schema
assert df.count() == 0


def test_create_dataframe_infers_schema_from_later_partition(ctx):
batch = pa.RecordBatch.from_arrays([pa.array([1, 2, 3])], names=["a"])
df = ctx.create_dataframe([[], [batch]])
assert df.to_pydict() == {"a": [1, 2, 3]}


def test_create_dataframe_registers_unique_table_name(ctx):
# create a RecordBatch and register it as memtable
batch = pa.RecordBatch.from_arrays(
Expand Down
Loading