@@ -406,11 +406,7 @@ case class DataSource(
406406 /**
407407 * Writes the given [[DataFrame ]] out in this [[FileFormat ]].
408408 */
409- private def writeInFileFormat (
410- format : FileFormat ,
411- mode : SaveMode ,
412- data : DataFrame ,
413- overwrite : Option [Boolean ]): Unit = {
409+ private def writeInFileFormat (format : FileFormat , mode : SaveMode , data : DataFrame ): Unit = {
414410 // Don't glob path for the write path. The contracts here are:
415411 // 1. Only one output path can be specified on the write path;
416412 // 2. Output path must be a legal HDFS style file system path;
@@ -426,26 +422,18 @@ case class DataSource(
426422 s " got: ${allPaths.mkString(" , " )}" )
427423 }
428424
429- val isOverWrite = overwrite match {
430- case Some (ow) => ow
431- case _ =>
432- if (pathExists) {
433- if (mode == SaveMode .ErrorIfExists ) {
434- throw new AnalysisException (s " path $outputPath already exists. " )
435- }
436- if (mode == SaveMode .Ignore ) {
437- // Since the path already exists and the save mode is Ignore, we will just return.
438- return
439- }
440-
441- if (mode == SaveMode .Append ) false
442- else if (mode == SaveMode .Overwrite ) true
443- else {
444- throw new IllegalStateException (s " unsupported save mode $mode" )
445- }
446- } else true
425+ if (pathExists) {
426+ if (mode == SaveMode .ErrorIfExists ) {
427+ throw new AnalysisException (s " path $outputPath already exists. " )
428+ }
429+ if (mode == SaveMode .Ignore ) {
430+ // Since the path already exists and the save mode is Ignore, we will just return.
431+ return
432+ }
447433 }
448434
435+ // if path does not exist, the ErrorIfExists and Ignore can be transformed to Append
436+ val transformedMode = if (mode != SaveMode .Overwrite ) SaveMode .Append else mode
449437 val caseSensitive = sparkSession.sessionState.conf.caseSensitiveAnalysis
450438 PartitioningUtils .validatePartitionColumn(data.schema, partitionColumns, caseSensitive)
451439
@@ -476,8 +464,7 @@ case class DataSource(
476464 fileFormat = format,
477465 options = options,
478466 query = data.logicalPlan,
479- mode = mode,
480- isOverWrite,
467+ mode = transformedMode,
481468 catalogTable = catalogTable,
482469 fileIndex = fileIndex)
483470 sparkSession.sessionState.executePlan(plan).toRdd
@@ -487,7 +474,7 @@ case class DataSource(
487474 * Writes the given [[DataFrame ]] out to this [[DataSource ]] and returns a [[BaseRelation ]] for
488475 * the following reading.
489476 */
490- def writeAndRead (mode : SaveMode , data : DataFrame , overwrite : Option [ Boolean ] ): BaseRelation = {
477+ def writeAndRead (mode : SaveMode , data : DataFrame ): BaseRelation = {
491478 if (data.schema.map(_.dataType).exists(_.isInstanceOf [CalendarIntervalType ])) {
492479 throw new AnalysisException (" Cannot save interval data type into external storage." )
493480 }
@@ -496,7 +483,7 @@ case class DataSource(
496483 case dataSource : CreatableRelationProvider =>
497484 dataSource.createRelation(sparkSession.sqlContext, mode, caseInsensitiveOptions, data)
498485 case format : FileFormat =>
499- writeInFileFormat(format, mode, data, overwrite )
486+ writeInFileFormat(format, mode, data)
500487 // Replace the schema with that of the DataFrame we just wrote out to avoid re-inferring
501488 copy(userSpecifiedSchema = Some (data.schema.asNullable)).resolveRelation()
502489 case _ =>
@@ -516,7 +503,7 @@ case class DataSource(
516503 case dataSource : CreatableRelationProvider =>
517504 dataSource.createRelation(sparkSession.sqlContext, mode, caseInsensitiveOptions, data)
518505 case format : FileFormat =>
519- writeInFileFormat(format, mode, data, None )
506+ writeInFileFormat(format, mode, data)
520507 case _ =>
521508 sys.error(s " ${providingClass.getCanonicalName} does not allow create table as select. " )
522509 }
0 commit comments