You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "rmnskb (via GitHub)" <gi...@apache.org> on 2025/08/03 18:51:01 UTC
[PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
rmnskb opened a new pull request, #47253:
URL: https://github.com/apache/arrow/pull/47253
### Rationale for this change
Please see Issue #47201
### What changes are included in this PR?
Add support for updating Spark-related part of .parquet metadata
### Are these changes tested?
Not yet
### Are there any user-facing changes?
When users select `flavor="spark"` while writing the parquet file from table, the function will check if there are any changes to existing Spark's schema in metadata, and will update it accordingly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3435747487
That is great @rmnskb, much appreciated!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3416863223
I had another look at it today. I also found a part in the documentation that should fit with this use case:
https://arrow.apache.org/docs/python/parquet.html#writing-metadata-and-common-metadata-files
Unfortunately I wasn't able to make it work with the example from the reported issue.
I am still not comfortable to add spark row group metadata changes in PyArrow though I agree we should make it easier in PyArrow to handle such cases.
Would you be willing to also give a try at the proposed `_metadata` or `_common_metadata` files from linked documentation and see if it would be something we can use and potentially simplify?
@pitrou what do you think about PyArrow updating spark row group metadata in cases where PyArrow is being used for data manipulation with schema changes in between spark workloads?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3148632171
Hey @AlenkaF @rok @raulcd!
I hope you're doing well. Can you please look at the changes, and tell me if they make sense, and if I should continue that or not? :)
I am on the fence with this one: from one hand, I don't like that Arrow should interfere with the metadata from another framework, on the other hand, the use case described in the original issue seems plausible, and I can imagine that they are not the only person doing that.
Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3416894339
On the face of it, I think this can make sense, especially if the code remains simple, but it should be on the C++ side anyway, not in PyArrow.
@wgtmac What do you think?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3222921191
Thank you for continuing to work on PyArrow @rmnskb !
I haven't had a chance to review this in depth yet, but from a quick initial look, I'm not sure these changes should be applied on our side. I’ll need to take a deeper look and will follow up on the issue as soon as possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3431331666
> Looking at the original PR implementing spark schema sanitizer (https://github.com/apache/arrow/pull/1076/files) I would actually agree the proposed fix would fit in PyArrow, if we see it needs to happen on our side. I would still like it to be a bit less hacky, as already mentioned 😊
>
> If I have time, I will try to make the example work with the use of metadata as explained here: https://arrow.apache.org/docs/python/parquet.html#writing-metadata-and-common-metadata-files
Thank you for the update!
I also took a look at the original issue, and also at the way Pandas uses the parquet writer: it leverages both `write_table` and `write_to_dataset`, so we'd have to cover both options.
If we're to continue with the approach that I've proposed, we'd have to inject the updated metadata at the write time, and to do that, we'd also have to map the Arrow data types to Spark ones. I still have to check whether there is an already existing mapping available, either in this repo or in Spark's one, but one way or another, I cannot imagine any other way to ensure compatibility between these two frameworks. I will test the compatibility, and how much forgiving Spark can be when it comes to the schema ingestion.
I will most probably come back with more concrete implementation later this week, will keep you updated :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3718200249
I would rather inline toward documenting it or deprecating `flavor == "spark"` TBH. FWIW, I also have a PR to fix a similar case at https://github.com/apache/arrow/pull/48456 but I actually don't feel strongly about it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3718163254
ack taking a look now
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on code in PR #47253:
URL: https://github.com/apache/arrow/pull/47253#discussion_r2688163647
##########
python/pyarrow/parquet/core.py:
##########
@@ -90,6 +90,69 @@ def _check_filters(filters, check_null_strings=True):
return filters
+def _map_spark_to_arrow_types(datatype: pa.DataType) -> str | None:
+ lookup = {
+ "NA": "null",
+ "BOOL": "boolean",
+ "INT8": "byte",
+ "INT16": "short",
+ **dict.fromkeys(
+ ["UINT8", "UINT16", "UINT32", "UINT64", "INT32"], "integer"),
+ "INT64": "long",
+ **dict.fromkeys(["HALF_FLOAT", "FLOAT"], "float"),
+ "DOUBLE": "double",
+ "BINARY": "binary",
+ "STRING": "string",
+ **dict.fromkeys(
+ ["DECIMAL" + str(2 ** i) for i in range(5, 9)], "decimal"),
+ **dict.fromkeys(
+ ["LIST", "LARGE_LIST", "LIST_VIEW", "LARGE_LIST_VIEW", "FIXED_SIZE_LIST"],
+ "array",
+ ),
+ "MAP": "map",
+ **dict.fromkeys(["DATE32", "DATE64"], "date"),
+ "TIMESTAMP": "timestamp",
+ "INTERVAL_MONTH_DAY_NANO": "Calendar Interval", # TODO: Correct this
Review Comment:
So if we're continue with this approach, do you propose handling a fraction of data types?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3746636849
@HyukjinKwon @AlenkaF
I'm not sure whether deprecating the `flavor == "spark"` is a good idea to be honest, but documenting the issue seems like a reasonable solution, will take a look at that, thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3380875897
Hey @AlenkaF !
Sorry to ping you like this :D Did you have a chance to review the code change? :)
Coincidentally, I have encountered this issue at work when fixing one of the pipelines, and remembered about this PR. I gave it some thought, and I think it makes sense to reflect the changes when writing the table with Arrow if it was previously created with Spark.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3417953132
IMHO, the C++ side does not even know the `flavor` here so it should not take the responsibility. It looks hacky but `python/pyarrow/parquet/core.py` seems to be the right place to fix it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #47253:
URL: https://github.com/apache/arrow/pull/47253#discussion_r2667849420
##########
python/pyarrow/parquet/core.py:
##########
@@ -90,6 +90,69 @@ def _check_filters(filters, check_null_strings=True):
return filters
+def _map_spark_to_arrow_types(datatype: pa.DataType) -> str | None:
+ lookup = {
+ "NA": "null",
+ "BOOL": "boolean",
+ "INT8": "byte",
+ "INT16": "short",
+ **dict.fromkeys(
+ ["UINT8", "UINT16", "UINT32", "UINT64", "INT32"], "integer"),
+ "INT64": "long",
+ **dict.fromkeys(["HALF_FLOAT", "FLOAT"], "float"),
+ "DOUBLE": "double",
+ "BINARY": "binary",
+ "STRING": "string",
+ **dict.fromkeys(
+ ["DECIMAL" + str(2 ** i) for i in range(5, 9)], "decimal"),
+ **dict.fromkeys(
+ ["LIST", "LARGE_LIST", "LIST_VIEW", "LARGE_LIST_VIEW", "FIXED_SIZE_LIST"],
+ "array",
+ ),
+ "MAP": "map",
+ **dict.fromkeys(["DATE32", "DATE64"], "date"),
+ "TIMESTAMP": "timestamp",
+ "INTERVAL_MONTH_DAY_NANO": "Calendar Interval", # TODO: Correct this
+ }
+
+ str_value = pa.types.TypesEnum(datatype.id).name
+
+ try:
+ return lookup[str_value]
+ except KeyError:
+ return None
+
+
+def _substitute_spark_metadata(schema: pa.Schema) -> dict:
+ metadata = schema.metadata
+ spark_key = b"org.apache.spark.sql.parquet.row.metadata"
+
+ try:
+ spark_row_metadata = json.loads(
+ schema.metadata.pop(spark_key, None))
+ except (TypeError, json.JSONDecodeError): # Could not convert Spark's row metadata
+ return metadata
+
+ spark_fields = [field["name"] for field in spark_row_metadata["fields"]]
+
+ for name in schema.names:
+ field = schema.field(name)
+
+ if name in spark_fields:
+ continue
+
+ spark_row_metadata["fields"].append({
+ "name": field.name,
+ "type": _map_spark_to_arrow_types(field.type),
Review Comment:
And seem like we're missing nested types like struct type and array/map
##########
python/pyarrow/parquet/core.py:
##########
@@ -90,6 +90,69 @@ def _check_filters(filters, check_null_strings=True):
return filters
+def _map_spark_to_arrow_types(datatype: pa.DataType) -> str | None:
+ lookup = {
+ "NA": "null",
+ "BOOL": "boolean",
+ "INT8": "byte",
+ "INT16": "short",
+ **dict.fromkeys(
+ ["UINT8", "UINT16", "UINT32", "UINT64", "INT32"], "integer"),
+ "INT64": "long",
+ **dict.fromkeys(["HALF_FLOAT", "FLOAT"], "float"),
+ "DOUBLE": "double",
+ "BINARY": "binary",
+ "STRING": "string",
+ **dict.fromkeys(
+ ["DECIMAL" + str(2 ** i) for i in range(5, 9)], "decimal"),
Review Comment:
and decimal precision
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3718303671
Yeah, I would agree with documenting or even deprecating `flavor == "spark"` if that is an option.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3717902454
@HyukjinKwon wondering what is your take on this issue, discussion and the proposed solution. Any advice/opinion would be most welcome! Also cc @EnricoMi in case this might be interesting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #47253:
URL: https://github.com/apache/arrow/pull/47253#discussion_r2667845476
##########
python/pyarrow/parquet/core.py:
##########
@@ -90,6 +90,69 @@ def _check_filters(filters, check_null_strings=True):
return filters
+def _map_spark_to_arrow_types(datatype: pa.DataType) -> str | None:
+ lookup = {
+ "NA": "null",
+ "BOOL": "boolean",
+ "INT8": "byte",
+ "INT16": "short",
+ **dict.fromkeys(
+ ["UINT8", "UINT16", "UINT32", "UINT64", "INT32"], "integer"),
+ "INT64": "long",
+ **dict.fromkeys(["HALF_FLOAT", "FLOAT"], "float"),
+ "DOUBLE": "double",
+ "BINARY": "binary",
+ "STRING": "string",
+ **dict.fromkeys(
+ ["DECIMAL" + str(2 ** i) for i in range(5, 9)], "decimal"),
+ **dict.fromkeys(
+ ["LIST", "LARGE_LIST", "LIST_VIEW", "LARGE_LIST_VIEW", "FIXED_SIZE_LIST"],
+ "array",
+ ),
+ "MAP": "map",
+ **dict.fromkeys(["DATE32", "DATE64"], "date"),
+ "TIMESTAMP": "timestamp",
+ "INTERVAL_MONTH_DAY_NANO": "Calendar Interval", # TODO: Correct this
Review Comment:
TBH, I think it's too much to handle Spark specific cases like this in PyArrow. Spark is even adding more types such as variant type, and more interval types.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #47253:
URL: https://github.com/apache/arrow/pull/47253#discussion_r2667850543
##########
python/pyarrow/parquet/core.py:
##########
@@ -90,6 +90,69 @@ def _check_filters(filters, check_null_strings=True):
return filters
+def _map_spark_to_arrow_types(datatype: pa.DataType) -> str | None:
+ lookup = {
+ "NA": "null",
+ "BOOL": "boolean",
+ "INT8": "byte",
+ "INT16": "short",
+ **dict.fromkeys(
+ ["UINT8", "UINT16", "UINT32", "UINT64", "INT32"], "integer"),
+ "INT64": "long",
+ **dict.fromkeys(["HALF_FLOAT", "FLOAT"], "float"),
+ "DOUBLE": "double",
+ "BINARY": "binary",
+ "STRING": "string",
+ **dict.fromkeys(
+ ["DECIMAL" + str(2 ** i) for i in range(5, 9)], "decimal"),
+ **dict.fromkeys(
+ ["LIST", "LARGE_LIST", "LIST_VIEW", "LARGE_LIST_VIEW", "FIXED_SIZE_LIST"],
+ "array",
+ ),
+ "MAP": "map",
+ **dict.fromkeys(["DATE32", "DATE64"], "date"),
+ "TIMESTAMP": "timestamp",
Review Comment:
and timestamp ntz and ltz.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3414439383
Hey @rmnskb, thanks for the ping!
I haven't got a chance, sorry. Will do my best to have a look at it today.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-47201: [Python][Parquet] Extending the schema and writing it back does not update Spark schema metadata [arrow]
Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #47253:
URL: https://github.com/apache/arrow/pull/47253#issuecomment-3430709480
Looking at the original PR implementing spark schema sanitizer (https://github.com/apache/arrow/pull/1076/files) I would actually agree the proposed fix would fit in PyArrow, if we see it needs to happen on our side. I would still like it to be a bit less hacky, as already mentioned 😊
If I have time, I will try to make the example work with the use of metadata as explained here: https://arrow.apache.org/docs/python/parquet.html#writing-metadata-and-common-metadata-files
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org