-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-54446][ML][CONNECT] FPGrowth supports local filesystem with Arrow file format #53232
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -46,7 +46,8 @@ import org.apache.spark.ml.feature.RFormulaModel | |
| import org.apache.spark.ml.linalg.{DenseMatrix, DenseVector, Matrix, SparseMatrix, SparseVector, Vector} | ||
| import org.apache.spark.ml.param.{ParamPair, Params} | ||
| import org.apache.spark.ml.tuning.ValidatorParams | ||
| import org.apache.spark.sql.{SparkSession, SQLContext} | ||
| import org.apache.spark.sql.{DataFrame, SparkSession, SQLContext} | ||
| import org.apache.spark.sql.execution.arrow.ArrowFileReadWrite | ||
| import org.apache.spark.util.{Utils, VersionUtils} | ||
|
|
||
| /** | ||
|
|
@@ -1142,4 +1143,31 @@ private[spark] object ReadWriteUtils { | |
| spark.read.parquet(path).as[T].collect() | ||
| } | ||
| } | ||
|
|
||
| def saveDataFrame(path: String, df: DataFrame): Unit = { | ||
| if (localSavingModeState.get()) { | ||
| val filePath = Paths.get(path) | ||
| Files.createDirectories(filePath.getParent) | ||
|
|
||
| df match { | ||
| case d: org.apache.spark.sql.classic.DataFrame => | ||
| ArrowFileReadWrite.save(d, path) | ||
| case _ => throw new UnsupportedOperationException("Unsupported dataframe type") | ||
| } | ||
| } else { | ||
| df.write.parquet(path) | ||
| } | ||
| } | ||
|
|
||
| def loadDataFrame(path: String, spark: SparkSession): DataFrame = { | ||
| if (localSavingModeState.get()) { | ||
| spark match { | ||
| case s: org.apache.spark.sql.classic.SparkSession => | ||
| ArrowFileReadWrite.load(s, path) | ||
| case _ => throw new UnsupportedOperationException("Unsupported session type") | ||
|
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. Can we show actual session type in the error?
Contributor
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. make sense, will update! |
||
| } | ||
| } else { | ||
| spark.read.parquet(path) | ||
| } | ||
| } | ||
|
Comment on lines
+1147
to
+1172
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. So if we have
Contributor
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. hi @holdenk , as @WeichenXu123 explained #53150 (comment), this is a runtime temporary file in spark connect server side, and will be cleaned after session close.
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.
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. Hmm, even it is just a temporary session file, is there any reason not to use Parquet but Arrow file format?
Contributor
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. we can read/write parquet with arrow, but it requires a new dependency otherwise, I am not sure whether we have utils to read/write parquet. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2340,14 +2340,13 @@ class Dataset[T] private[sql]( | |
| } | ||
|
|
||
| /** Convert to an RDD of serialized ArrowRecordBatches. */ | ||
| private[sql] def toArrowBatchRdd(plan: SparkPlan): RDD[Array[Byte]] = { | ||
| private def toArrowBatchRddImpl( | ||
| plan: SparkPlan, | ||
|
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. nit: 4 spaces indentation |
||
| maxRecordsPerBatch: Int, | ||
| timeZoneId: String, | ||
| errorOnDuplicatedFieldNames: Boolean, | ||
| largeVarTypes: Boolean): RDD[Array[Byte]] = { | ||
| val schemaCaptured = this.schema | ||
| val maxRecordsPerBatch = sparkSession.sessionState.conf.arrowMaxRecordsPerBatch | ||
| val timeZoneId = sparkSession.sessionState.conf.sessionLocalTimeZone | ||
| val errorOnDuplicatedFieldNames = | ||
| sparkSession.sessionState.conf.pandasStructHandlingMode == "legacy" | ||
| val largeVarTypes = | ||
| sparkSession.sessionState.conf.arrowUseLargeVarTypes | ||
| plan.execute().mapPartitionsInternal { iter => | ||
| val context = TaskContext.get() | ||
| ArrowConverters.toBatchIterator( | ||
|
|
@@ -2361,7 +2360,24 @@ class Dataset[T] private[sql]( | |
| } | ||
| } | ||
|
|
||
| // This is only used in tests, for now. | ||
| private[sql] def toArrowBatchRdd( | ||
| maxRecordsPerBatch: Int, | ||
| timeZoneId: String, | ||
| errorOnDuplicatedFieldNames: Boolean, | ||
| largeVarTypes: Boolean): RDD[Array[Byte]] = { | ||
| toArrowBatchRddImpl(queryExecution.executedPlan, | ||
| maxRecordsPerBatch, timeZoneId, errorOnDuplicatedFieldNames, largeVarTypes) | ||
| } | ||
|
|
||
| private[sql] def toArrowBatchRdd(plan: SparkPlan): RDD[Array[Byte]] = { | ||
| toArrowBatchRddImpl( | ||
| plan, | ||
| sparkSession.sessionState.conf.arrowMaxRecordsPerBatch, | ||
| sparkSession.sessionState.conf.sessionLocalTimeZone, | ||
| sparkSession.sessionState.conf.pandasStructHandlingMode == "legacy", | ||
| sparkSession.sessionState.conf.arrowUseLargeVarTypes) | ||
| } | ||
|
|
||
| private[sql] def toArrowBatchRdd: RDD[Array[Byte]] = { | ||
| toArrowBatchRdd(queryExecution.executedPlan) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.arrow | ||
|
|
||
| import java.io.{ByteArrayOutputStream, FileOutputStream} | ||
| import java.nio.channels.Channels | ||
| import java.nio.file.Files | ||
| import java.nio.file.Paths | ||
|
|
||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| import org.apache.arrow.vector._ | ||
| import org.apache.arrow.vector.ipc.{ArrowFileReader, ArrowFileWriter, WriteChannel} | ||
| import org.apache.arrow.vector.ipc.message.MessageSerializer | ||
| import org.apache.arrow.vector.types.pojo.Schema | ||
|
|
||
| import org.apache.spark.sql.classic.{DataFrame, SparkSession} | ||
| import org.apache.spark.sql.util.ArrowUtils | ||
|
|
||
| private[sql] class SparkArrowFileWriter( | ||
| arrowSchema: Schema, | ||
|
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. nit: 4 spaces indentation |
||
| path: String) extends AutoCloseable { | ||
| private val allocator = | ||
| ArrowUtils.rootAllocator.newChildAllocator( | ||
| s"to${this.getClass.getSimpleName}", 0, Long.MaxValue) | ||
|
|
||
| protected val root = VectorSchemaRoot.create(arrowSchema, allocator) | ||
| protected val loader = new VectorLoader(root) | ||
| protected val arrowWriter = ArrowWriter.create(root) | ||
|
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. Where is
Contributor
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. good catch! |
||
|
|
||
| protected val fileWriter = | ||
| new ArrowFileWriter(root, null, Channels.newChannel(new FileOutputStream(path))) | ||
|
|
||
| override def close(): Unit = { | ||
| root.close() | ||
| allocator.close() | ||
| fileWriter.close() | ||
| } | ||
|
|
||
| def write(batchBytesIter: Iterator[Array[Byte]]): Unit = { | ||
|
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. This looks like do such thing: Dataset -> Arrow batches -> Bytes -> Arrow batches -> Write Arrow batches by ArrowFileWriter Looks like the intermediate Bytes could be skipped?
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. I think he's doing it cuz local data has to go to executors, and to do that, the arrow batches should be in ipc.
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. Dataset is already distributed on executors. Rows are written into Arrow batches in executors. If they are not to distributed again, they could be in Arrow batches, no?
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. Below,
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. I guess it's because to write down into Drivers' local file system
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. Oh I see. Okay. |
||
| fileWriter.start() | ||
| while (batchBytesIter.hasNext) { | ||
| val batchBytes = batchBytesIter.next() | ||
| val batch = ArrowConverters.loadBatch(batchBytes, allocator) | ||
|
Contributor
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. The |
||
| loader.load(batch) | ||
| fileWriter.writeBatch() | ||
| } | ||
| fileWriter.close() | ||
| } | ||
| } | ||
|
|
||
| private[sql] class SparkArrowFileReader(path: String) extends AutoCloseable { | ||
| private val allocator = | ||
| ArrowUtils.rootAllocator.newChildAllocator( | ||
| s"to${this.getClass.getSimpleName}", 0, Long.MaxValue) | ||
|
|
||
| protected val fileReader = | ||
| new ArrowFileReader(Files.newByteChannel(Paths.get(path)), allocator) | ||
|
|
||
| override def close(): Unit = { | ||
| allocator.close() | ||
| fileReader.close() | ||
| } | ||
|
|
||
| val schema: Schema = fileReader.getVectorSchemaRoot.getSchema | ||
|
|
||
| def read(): Iterator[Array[Byte]] = { | ||
| fileReader.getRecordBlocks.iterator().asScala.map { block => | ||
| fileReader.loadRecordBatch(block) | ||
| val root = fileReader.getVectorSchemaRoot | ||
| val unloader = new VectorUnloader(root) | ||
| val batch = unloader.getRecordBatch | ||
| val out = new ByteArrayOutputStream() | ||
| val writeChannel = new WriteChannel(Channels.newChannel(out)) | ||
| MessageSerializer.serialize(writeChannel, batch) | ||
| out.toByteArray | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[spark] object ArrowFileReadWrite { | ||
| def save(df: DataFrame, path: String): Unit = { | ||
| val maxRecordsPerBatch = df.sparkSession.sessionState.conf.arrowMaxRecordsPerBatch | ||
| val rdd = df.toArrowBatchRdd(maxRecordsPerBatch, "UTC", true, false) | ||
| val arrowSchema = ArrowUtils.toArrowSchema(df.schema, "UTC", true, false) | ||
| val writer = new SparkArrowFileWriter(arrowSchema, path) | ||
| writer.write(rdd.toLocalIterator) | ||
|
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. Instead, can we call |
||
| } | ||
|
|
||
| def load(spark: SparkSession, path: String): DataFrame = { | ||
| val reader = new SparkArrowFileReader(path) | ||
| val schema = ArrowUtils.fromArrowSchema(reader.schema) | ||
| ArrowConverters.toDataFrame(reader.read(), schema, spark, "UTC", true, false) | ||
| } | ||
| } | ||
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.
can we pass
Pathobject tosaveDataFramedirectly?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.
sounds good!