From c40a0d4e91896dece51969f5308016ecb3aa635c Mon Sep 17 00:00:00 2001 From: Udit Mehrotra Date: Wed, 11 Mar 2020 20:27:46 -0700 Subject: [PATCH] [HUDI-656][Performance] Return a dummy Spark relation after writing the DataFrame (#1394) Co-authored-by: Mehrotra --- .../scala/org/apache/hudi/DefaultSource.scala | 20 ++++++++++- .../org/apache/hudi/HudiEmptyRelation.scala | 35 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 hudi-spark/src/main/scala/org/apache/hudi/HudiEmptyRelation.scala diff --git a/hudi-spark/src/main/scala/org/apache/hudi/DefaultSource.scala b/hudi-spark/src/main/scala/org/apache/hudi/DefaultSource.scala index 931f23bc2..fbdd4ea9c 100644 --- a/hudi-spark/src/main/scala/org/apache/hudi/DefaultSource.scala +++ b/hudi-spark/src/main/scala/org/apache/hudi/DefaultSource.scala @@ -22,6 +22,7 @@ import org.apache.hudi.exception.HoodieException import org.apache.hudi.hadoop.HoodieROTablePathFilter import org.apache.log4j.LogManager import org.apache.spark.sql.execution.datasources.DataSource +import org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand import org.apache.spark.sql.execution.streaming.Sink import org.apache.spark.sql.sources._ import org.apache.spark.sql.streaming.OutputMode @@ -82,6 +83,22 @@ class DefaultSource extends RelationProvider } } + /** + * This DataSource API is used for writing the DataFrame at the destination. For now, we are returning a dummy + * relation here because Spark does not really make use of the relation returned, and just returns an empty + * dataset at [[SaveIntoDataSourceCommand.run()]]. This saves us the cost of creating and returning a parquet + * relation here. + * + * TODO: Revisit to return a concrete relation here when we support CREATE TABLE AS for Hudi with DataSource API. + * That is the only case where Spark seems to actually need a relation to be returned here + * [[DataSource.writeAndRead()]] + * + * @param sqlContext Spark SQL Context + * @param mode Mode for saving the DataFrame at the destination + * @param optParams Parameters passed as part of the DataFrame write operation + * @param df Spark DataFrame to be written + * @return Spark Relation + */ override def createRelation(sqlContext: SQLContext, mode: SaveMode, optParams: Map[String, String], @@ -89,7 +106,8 @@ class DefaultSource extends RelationProvider val parameters = HoodieSparkSqlWriter.parametersWithWriteDefaults(optParams) HoodieSparkSqlWriter.write(sqlContext, mode, parameters, df) - createRelation(sqlContext, parameters, df.schema) + + new HudiEmptyRelation(sqlContext, df.schema) } override def createSink(sqlContext: SQLContext, diff --git a/hudi-spark/src/main/scala/org/apache/hudi/HudiEmptyRelation.scala b/hudi-spark/src/main/scala/org/apache/hudi/HudiEmptyRelation.scala new file mode 100644 index 000000000..8ddbe4650 --- /dev/null +++ b/hudi-spark/src/main/scala/org/apache/hudi/HudiEmptyRelation.scala @@ -0,0 +1,35 @@ +/* + * 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.hudi + +import org.apache.spark.sql.SQLContext +import org.apache.spark.sql.sources.BaseRelation +import org.apache.spark.sql.types.StructType + +/** + * This is a dummy Spark relation that can be used if needed to return a place holder relation that does not get used. + * + * @param sqlContext Spark SQL Context + * @param userSchema Users data schema + */ +class HudiEmptyRelation(val sqlContext: SQLContext, + val userSchema: StructType) extends BaseRelation { + + override def schema: StructType = userSchema +}