andygrove commented on code in PR #2812: URL: https://github.com/apache/datafusion-comet/pull/2812#discussion_r2551142974
########## spark/src/main/scala/org/apache/comet/serde/operator/CometDataWritingCommand.scala: ########## @@ -0,0 +1,148 @@ +/* + * 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.comet.serde.operator + +import scala.jdk.CollectionConverters._ + +import org.apache.spark.sql.comet.{CometNativeExec, CometNativeWriteExec} +import org.apache.spark.sql.execution.command.DataWritingCommandExec +import org.apache.spark.sql.execution.datasources.{InsertIntoHadoopFsRelationCommand, WriteFilesExec} +import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat +import org.apache.spark.sql.internal.SQLConf + +import org.apache.comet.{CometConf, ConfigEntry} +import org.apache.comet.CometSparkSessionExtensions.withInfo +import org.apache.comet.serde.{CometOperatorSerde, OperatorOuterClass} +import org.apache.comet.serde.OperatorOuterClass.Operator +import org.apache.comet.serde.QueryPlanSerde.serializeDataType + +/** + * CometOperatorSerde implementation for DataWritingCommandExec that converts Parquet write + * operations to use Comet's native Parquet writer. + */ +object CometDataWritingCommandExec extends CometOperatorSerde[DataWritingCommandExec] { + + override def enabledConfig: Option[ConfigEntry[Boolean]] = + Some(CometConf.COMET_NATIVE_PARQUET_WRITE_ENABLED) + + override def convert( + op: DataWritingCommandExec, + builder: Operator.Builder, + childOp: Operator*): Option[OperatorOuterClass.Operator] = { + op.cmd match { + case cmd: InsertIntoHadoopFsRelationCommand => + // Check if this is a Parquet write + cmd.fileFormat match { + case _: ParquetFileFormat => + try { + // Create native ParquetWriter operator + val scanOp = OperatorOuterClass.Scan + .newBuilder() + .setSource("write_source") + .setArrowFfiSafe(true) + + // Add fields from the query output schema + val scanTypes = cmd.query.output.flatMap { attr => + serializeDataType(attr.dataType) + } + + if (scanTypes.length != cmd.query.output.length) { + withInfo(op, "Cannot serialize data types for native write") + return None + } + + scanTypes.foreach(scanOp.addFields) + + val scanOperator = Operator + .newBuilder() + .setPlanId(op.id) + .setScan(scanOp.build()) + .build() + + // Get output path + val outputPath = cmd.outputPath.toString + + val codecName = cmd.options.getOrElse( + "compression", + SQLConf.get.getConfString( + SQLConf.PARQUET_COMPRESSION.key, + SQLConf.PARQUET_COMPRESSION.defaultValueString)) Review Comment: This isn't 100% comprehensive. I filed follow on issue https://github.com/apache/datafusion-comet/issues/2814 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
