-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23380][PYTHON] Adds a conf for Arrow fallback in toPandas/createDataFrame with Pandas DataFrame #20678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
7f87d25
7641fd0
cfb08a1
229a5f7
ed30c20
af60cb7
b5bea82
4ccaa81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1689,6 +1689,10 @@ using the call `toPandas()` and when creating a Spark DataFrame from a Pandas Da | |
| `createDataFrame(pandas_df)`. To use Arrow when executing these calls, users need to first set | ||
| the Spark configuration 'spark.sql.execution.arrow.enabled' to 'true'. This is disabled by default. | ||
|
|
||
| In addition, optimizations enabled by 'spark.sql.execution.arrow.enabled' could fallback automatically | ||
| to non-optimized implementations if an error occurs before the actual computation within Spark. | ||
| This can be controlled by 'spark.sql.execution.arrow.fallback.enabled'. | ||
|
|
||
| <div class="codetabs"> | ||
| <div data-lang="python" markdown="1"> | ||
| {% include_example dataframe_with_arrow python/sql/arrow.py %} | ||
|
|
@@ -1800,6 +1804,7 @@ working with timestamps in `pandas_udf`s to get the best performance, see | |
| ## Upgrading From Spark SQL 2.3 to 2.4 | ||
|
|
||
| - Since Spark 2.4, Spark maximizes the usage of a vectorized ORC reader for ORC files by default. To do that, `spark.sql.orc.impl` and `spark.sql.orc.filterPushdown` change their default values to `native` and `true` respectively. | ||
| - In PySpark, when Arrow optimization is enabled, previously `toPandas` just failed when Arrow optimization is unabled to be used whereas `createDataFrame` from Pandas DataFrame allowed the fallback to non-optimization. Now, both `toPandas` and `createDataFrame` from Pandas DataFrame allow the fallback by default, which can be switched by `spark.sql.execution.arrow.fallback.enabled`. | ||
|
||
|
|
||
| ## Upgrading From Spark SQL 2.2 to 2.3 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1986,55 +1986,91 @@ def toPandas(self): | |
| timezone = None | ||
|
|
||
| if self.sql_ctx.getConf("spark.sql.execution.arrow.enabled", "false").lower() == "true": | ||
| use_arrow = True | ||
| try: | ||
| from pyspark.sql.types import _check_dataframe_convert_date, \ | ||
| _check_dataframe_localize_timestamps, to_arrow_schema | ||
| from pyspark.sql.types import to_arrow_schema | ||
| from pyspark.sql.utils import require_minimum_pyarrow_version | ||
|
|
||
| require_minimum_pyarrow_version() | ||
| import pyarrow | ||
| to_arrow_schema(self.schema) | ||
| tables = self._collectAsArrow() | ||
| if tables: | ||
| table = pyarrow.concat_tables(tables) | ||
| pdf = table.to_pandas() | ||
| pdf = _check_dataframe_convert_date(pdf, self.schema) | ||
| return _check_dataframe_localize_timestamps(pdf, timezone) | ||
| else: | ||
| return pd.DataFrame.from_records([], columns=self.columns) | ||
| except Exception as e: | ||
| msg = ( | ||
| "Note: toPandas attempted Arrow optimization because " | ||
| "'spark.sql.execution.arrow.enabled' is set to true. Please set it to false " | ||
| "to disable this.") | ||
| raise RuntimeError("%s\n%s" % (_exception_message(e), msg)) | ||
| else: | ||
| pdf = pd.DataFrame.from_records(self.collect(), columns=self.columns) | ||
|
|
||
| dtype = {} | ||
| if self.sql_ctx.getConf("spark.sql.execution.arrow.fallback.enabled", "true") \ | ||
| .lower() == "true": | ||
| msg = ( | ||
| "toPandas attempted Arrow optimization because " | ||
| "'spark.sql.execution.arrow.enabled' is set to true; however, " | ||
| "failed by the reason below:\n %s\n" | ||
| "Attempts non-optimization as " | ||
| "'spark.sql.execution.arrow.fallback.enabled' is set to " | ||
| "true." % _exception_message(e)) | ||
| warnings.warn(msg) | ||
| use_arrow = False | ||
| else: | ||
| msg = ( | ||
| "toPandas attempted Arrow optimization because " | ||
| "'spark.sql.execution.arrow.enabled' is set to true; however, " | ||
| "failed by the reason below:\n %s\n" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm ... I tried to like make a |
||
| "For fallback to non-optimization automatically, please set true to " | ||
| "'spark.sql.execution.arrow.fallback.enabled'." % _exception_message(e)) | ||
| raise RuntimeError(msg) | ||
|
|
||
| # Try to use Arrow optimization when the schema is supported and the required version | ||
| # of PyArrow is found, if 'spark.sql.execution.arrow.enabled' is enabled. | ||
| if use_arrow: | ||
| try: | ||
| from pyspark.sql.types import _check_dataframe_convert_date, \ | ||
| _check_dataframe_localize_timestamps | ||
| import pyarrow | ||
|
|
||
| tables = self._collectAsArrow() | ||
| if tables: | ||
| table = pyarrow.concat_tables(tables) | ||
| pdf = table.to_pandas() | ||
| pdf = _check_dataframe_convert_date(pdf, self.schema) | ||
| return _check_dataframe_localize_timestamps(pdf, timezone) | ||
| else: | ||
| return pd.DataFrame.from_records([], columns=self.columns) | ||
| except Exception as e: | ||
| # We might have to allow fallback here as well but multiple Spark jobs can | ||
| # be executed. So, simply fail in this case for now. | ||
| msg = ( | ||
| "toPandas attempted Arrow optimization because " | ||
| "'spark.sql.execution.arrow.enabled' is set to true; however, " | ||
| "failed unexpectedly:\n %s\n" | ||
| "Note that 'spark.sql.execution.arrow.fallback.enabled' does " | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 good job having this explanation in the exception |
||
| "not have an effect in such failure in the middle of " | ||
| "computation." % _exception_message(e)) | ||
| raise RuntimeError(msg) | ||
|
|
||
| # Below is toPandas without Arrow optimization. | ||
|
||
| pdf = pd.DataFrame.from_records(self.collect(), columns=self.columns) | ||
|
|
||
| dtype = {} | ||
| for field in self.schema: | ||
| pandas_type = _to_corrected_pandas_type(field.dataType) | ||
| # SPARK-21766: if an integer field is nullable and has null values, it can be | ||
| # inferred by pandas as float column. Once we convert the column with NaN back | ||
| # to integer type e.g., np.int16, we will hit exception. So we use the inferred | ||
| # float type, not the corrected type from the schema in this case. | ||
| if pandas_type is not None and \ | ||
| not(isinstance(field.dataType, IntegralType) and field.nullable and | ||
| pdf[field.name].isnull().any()): | ||
| dtype[field.name] = pandas_type | ||
|
|
||
| for f, t in dtype.items(): | ||
| pdf[f] = pdf[f].astype(t, copy=False) | ||
|
|
||
| if timezone is None: | ||
| return pdf | ||
| else: | ||
| from pyspark.sql.types import _check_series_convert_timestamps_local_tz | ||
| for field in self.schema: | ||
| pandas_type = _to_corrected_pandas_type(field.dataType) | ||
| # SPARK-21766: if an integer field is nullable and has null values, it can be | ||
| # inferred by pandas as float column. Once we convert the column with NaN back | ||
| # to integer type e.g., np.int16, we will hit exception. So we use the inferred | ||
| # float type, not the corrected type from the schema in this case. | ||
| if pandas_type is not None and \ | ||
| not(isinstance(field.dataType, IntegralType) and field.nullable and | ||
| pdf[field.name].isnull().any()): | ||
| dtype[field.name] = pandas_type | ||
|
|
||
| for f, t in dtype.items(): | ||
| pdf[f] = pdf[f].astype(t, copy=False) | ||
|
|
||
| if timezone is None: | ||
| return pdf | ||
| else: | ||
| from pyspark.sql.types import _check_series_convert_timestamps_local_tz | ||
| for field in self.schema: | ||
| # TODO: handle nested timestamps, such as ArrayType(TimestampType())? | ||
| if isinstance(field.dataType, TimestampType): | ||
| pdf[field.name] = \ | ||
| _check_series_convert_timestamps_local_tz(pdf[field.name], timezone) | ||
| return pdf | ||
| # TODO: handle nested timestamps, such as ArrayType(TimestampType())? | ||
| if isinstance(field.dataType, TimestampType): | ||
| pdf[field.name] = \ | ||
| _check_series_convert_timestamps_local_tz(pdf[field.name], timezone) | ||
| return pdf | ||
|
|
||
| def _collectAsArrow(self): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very minor nit:
non-optimized implementations-->non-Arrow optimization implementationthis matches the description in the paragraph below