Skip to content
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

[SPARK-24583][SQL] Wrong schema type in InsertIntoDataSourceCommand #21585

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ case class InsertIntoDataSourceCommand(
override def run(sparkSession: SparkSession): Seq[Row] = {
val relation = logicalRelation.relation.asInstanceOf[InsertableRelation]
val data = Dataset.ofRows(sparkSession, query)
// Apply the schema of the existing table to the new data.
val df = sparkSession.internalCreateDataFrame(data.queryExecution.toRdd, logicalRelation.schema)
relation.insert(df, overwrite)
// Data should have been casted to the schema of the insert relation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to mention which rule did it

relation.insert(data, overwrite)

// Re-cache all cached plans(including this relation itself, if it's cached) that refer to this
// data source relation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,36 @@ package org.apache.spark.sql.sources
import java.io.File

import org.apache.spark.SparkException
import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTable, CatalogTableType}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf.PartitionOverwriteMode
import org.apache.spark.sql.test.SharedSQLContext
import org.apache.spark.sql.types._
import org.apache.spark.util.Utils

class SimpleInsertSource extends SchemaRelationProvider {
override def createRelation(
sqlContext: SQLContext,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces indentation

parameters: Map[String, String],
schema: StructType): BaseRelation = {
SimpleInsert(schema)(sqlContext.sparkSession)
}
}

case class SimpleInsert(userSpecifiedSchema: StructType)(@transient val sparkSession: SparkSession)
extends BaseRelation with InsertableRelation {

override def sqlContext: SQLContext = sparkSession.sqlContext

override def schema: StructType = userSpecifiedSchema

override def insert(input: DataFrame, overwrite: Boolean): Unit = {
input.collect
}
}

class InsertSuite extends DataSourceTest with SharedSQLContext {
import testImplicits._

Expand Down Expand Up @@ -520,4 +544,29 @@ class InsertSuite extends DataSourceTest with SharedSQLContext {
}
}
}

test("SPARK-24583 Wrong schema type in InsertIntoDataSourceCommand") {
withTable("test_table") {
val schema = new StructType()
.add("i", LongType, false)
.add("s", StringType, false)
val newTable = CatalogTable(
identifier = TableIdentifier("test_table", None),
tableType = CatalogTableType.EXTERNAL,
storage = CatalogStorageFormat(
locationUri = None,
inputFormat = None,
outputFormat = None,
serde = None,
compressed = false,
properties = Map.empty),
schema = schema,
provider = Some("org.apache.spark.sql.sources.SimpleInsertSource"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use classOf[SimpleInsertSource].getName instead of hardcoding


spark.sessionState.catalog.createTable(newTable, false)

sql("INSERT INTO TABLE test_table SELECT 1, 'a'")
sql("INSERT INTO TABLE test_table SELECT 2, null")
}
}
}