Skip to content
Closed
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
8 changes: 8 additions & 0 deletions python/pyarrow/parquet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,8 @@ def write_table(self, table, row_group_size=None):
Parameters
----------
table : Table
Its schema must match the writer schema, including the order
of the columns.
row_group_size : int, default None
Maximum number of rows in each written row group. If None,
the row group size will be the minimum of the Table size (in rows)
Expand All @@ -1220,6 +1222,12 @@ def write_table(self, table, row_group_size=None):
"Table schema does not match schema used to create file: \n"
f"table:\n{table.schema!s} vs. \nfile:\n{self.schema!s}"
)
if (table.schema.names != self.schema.names and
sorted(table.schema.names) == sorted(self.schema.names)):
msg += (
"\nThe columns are in a different order. Use "
"table.select(schema.names) to reorder them."
)
raise ValueError(msg)

self.writer.write_table(table, row_group_size=row_group_size)
Expand Down
11 changes: 11 additions & 0 deletions python/pyarrow/tests/parquet/test_parquet_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def test_validate_schema_write_table(tempdir):
w.write_table(simple_table)


def test_write_table_column_order_mismatch(tempdir):
# GH-39241
table = pa.table({'a': [1], 'b': ['x']})
schema = pa.schema([('b', pa.string()), ('a', pa.int64())])

with pq.ParquetWriter(tempdir / 'test.parquet', schema) as w:
with pytest.raises(ValueError, match="different order"):
w.write_table(table)
w.write_table(table.select(schema.names))


def test_parquet_invalid_writer(tempdir):
# avoid segfaults with invalid construction
with pytest.raises(TypeError):
Expand Down
Loading