-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f099a67
[SPARK-24583] Wrong schema type in InsertIntoDataSourceCommand
maryannxue 8add169
remove applying schema type for insert input
maryannxue bb9fa03
Simplify test case
maryannxue 03c3c90
change indentation
maryannxue 049844e
Code clean-up
maryannxue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,47 @@ 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, | ||
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.foreach { row => | ||
schema.fields.zipWithIndex.filter(!_._1.nullable).foreach { field => | ||
if (row.get(field._2) == null) { | ||
throw new NotNullableViolationException(field._1.name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
class NotNullableViolationException(val message: String) | ||
extends Exception(message) with Serializable { | ||
override def getMessage: String = s"Value for column '$message' cannot be null." | ||
} | ||
|
||
class InsertSuite extends DataSourceTest with SharedSQLContext { | ||
import testImplicits._ | ||
|
||
|
@@ -520,4 +555,48 @@ class InsertSuite extends DataSourceTest with SharedSQLContext { | |
} | ||
} | ||
} | ||
|
||
test("SPARK-24583 Wrong schema type in InsertIntoDataSourceCommand") { | ||
withTable("test_table") { | ||
val schema = new StructType() | ||
.add("i", IntegerType, 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")) | ||
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. use |
||
|
||
spark.sessionState.catalog.createTable(newTable, false) | ||
|
||
def verifyException(e: Exception, column: String): Unit = { | ||
var ex = e.getCause | ||
while (ex != null && | ||
!ex.isInstanceOf[NotNullableViolationException]) { | ||
ex = ex.getCause | ||
} | ||
if (ex == null) { | ||
fail(s"Expected a NotNullableViolationException but got '${e.getMessage}'.") | ||
} | ||
assert(ex.getMessage.contains(s"Value for column '$column' cannot be null.")) | ||
} | ||
|
||
sql("INSERT INTO TABLE test_table SELECT 1, 'a'") | ||
verifyException( | ||
intercept[SparkException] { | ||
sql("INSERT INTO TABLE test_table SELECT null, 'b'") | ||
}, "i") | ||
verifyException( | ||
intercept[SparkException] { | ||
sql("INSERT INTO TABLE test_table SELECT 2, null") | ||
}, "s") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
4 spaces indentation