ValueError: Table schema does not match schema used to create file
MEMBER
We'll do a new release of `datasets` soon to make the fix available :)

In the meantime you can use `datasets` from source (main)
2023-06-27T15:33:25Z
https://github.com/huggingface/datasets/issues/5993#issuecomment-1609761600
https://api.github.com/repos/huggingface/datasets/issues/5993
IC_kwDODunzps5f8wNA
https://api.github.com/repos/huggingface/datasets/issues/comments/1609761600/reactions
2023-06-27T15:33:25Z
https://api.github.com/repos/huggingface/datasets/issues/comments/1609761600
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
NONE
Thank you very much @lhoestq ! 🚀 
2023-06-27T15:36:41Z
https://github.com/huggingface/datasets/issues/5993#issuecomment-1609766729
https://api.github.com/repos/huggingface/datasets/issues/5993
IC_kwDODunzps5f8xdJ
https://api.github.com/repos/huggingface/datasets/issues/comments/1609766729/reactions
2023-06-27T15:36:41Z
https://api.github.com/repos/huggingface/datasets/issues/comments/1609766729
https://avatars.githubusercontent.com/u/128361578?v=4
https://api.github.com/users/exs-avianello/events{/privacy}
https://api.github.com/users/exs-avianello/followers
https://api.github.com/users/exs-avianello/following{/other_user}
https://api.github.com/users/exs-avianello/gists{/gist_id}
https://github.com/exs-avianello
exs-avianello
U_kgDOB6akag
https://api.github.com/users/exs-avianello/orgs
https://api.github.com/users/exs-avianello/received_events
https://api.github.com/users/exs-avianello/repos
https://api.github.com/users/exs-avianello/starred{/owner}{/repo}
https://api.github.com/users/exs-avianello/subscriptions
User
https://api.github.com/users/exs-avianello
public
### Describe the bug

Saving a dataset as parquet fails with a `ValueError: Table schema does not match schema used to create file`  if the dataset was obtained out of a `.select_columns()` call with columns selected out of order.

### Steps to reproduce the bug

```python
import datasets

dataset = datasets.Dataset.from_dict(
    {
        "x1": [1, 2, 3],
        "x2": [10, 11, 12],
    }
)

ds = dataset.select_columns(["x2", "x1"])

ds.to_parquet("demo.parquet")
```

```shell
>>>
ValueError: Table schema does not match schema used to create file: 
table:
x2: int64
x1: int64
-- schema metadata --
huggingface: '{"info": {"features": {"x2": {"dtype": "int64", "_type": "V' + 53 vs. 
file:
x1: int64
x2: int64
-- schema metadata --
huggingface: '{"info": {"features": {"x1": {"dtype": "int64", "_type": "V' + 53
```

--- 

I think this is because after the `.select_columns()` call with out of order columns, the output dataset features' schema ends up being out of sync with the schema of the arrow table backing it. 

```python
ds.features.arrow_schema
>>>
x1: int64
x2: int64
-- schema metadata --
huggingface: '{"info": {"features": {"x1": {"dtype": "int64", "_type": "V' + 53

ds.data.schema
>>>
x2: int64
x1: int64
-- schema metadata --
huggingface: '{"info": {"features": {"x2": {"dtype": "int64", "_type": "V' + 53
```


So when we call `.to_parquet()`, the call behind the scenes to `datasets.io.parquet.ParquetDatasetWriter(...).write()` which initialises the backend `pyarrow.parquet.ParquetWriter` with `schema = self.dataset.features.arrow_schema` triggers `pyarrow` on write when [it checks](https://github.com/apache/arrow/blob/11b140a734a516e436adaddaeb35d23f30dcce44/python/pyarrow/parquet/core.py#L1086-L1090) that the `ParquetWriter` schema matches the schema of the table being written 🙌 

https://github.com/huggingface/datasets/blob/6ed837325cb539a5deb99129e5ad181d0269e050/src/datasets/io/parquet.py#L139-L141


### Expected behavior

The dataset gets successfully saved as parquet. 

*In the same way as it does if saving it as csv:

```python
import datasets

dataset = datasets.Dataset.from_dict(
    {
        "x1": [1, 2, 3],
        "x2": [10, 11, 12],
    }
)

ds = dataset.select_columns(["x2", "x1"])

ds.to_csv("demo.csv")
```

### Environment info

`python==3.11`
`datasets==2.13.1`
