Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -39,6 +39,7 @@ import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.execution.{RowDataSourceScanExec, SparkPlan}
import org.apache.spark.sql.execution.command._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf.StoreAssignmentPolicy
import org.apache.spark.sql.sources._
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String
Expand All @@ -59,7 +60,8 @@ case class DataSourceAnalysis(conf: SQLConf) extends Rule[LogicalPlan] with Cast
sourceAttributes: Seq[Attribute],
providedPartitions: Map[String, Option[String]],
targetAttributes: Seq[Attribute],
targetPartitionSchema: StructType): Seq[NamedExpression] = {
targetPartitionSchema: StructType,
conf: SQLConf): Seq[NamedExpression] = {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need conf here, the constructor has conf though?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed, thanks!


assert(providedPartitions.exists(_._2.isDefined))

Expand Down Expand Up @@ -104,7 +106,13 @@ case class DataSourceAnalysis(conf: SQLConf) extends Rule[LogicalPlan] with Cast
None
} else if (potentialSpecs.size == 1) {
val partValue = potentialSpecs.head._2
Some(Alias(cast(Literal(partValue), field.dataType), field.name)())
conf.storeAssignmentPolicy match {
case StoreAssignmentPolicy.ANSI | StoreAssignmentPolicy.STRICT =>
Some(Alias(AnsiCast(Literal(partValue), field.dataType,
Option(conf.sessionLocalTimeZone)), field.name)())
case _ =>
Some(Alias(cast(Literal(partValue), field.dataType), field.name)())
}
} else {
throw new AnalysisException(
s"Partition column ${field.name} have multiple values specified, " +
Expand Down Expand Up @@ -176,7 +184,8 @@ case class DataSourceAnalysis(conf: SQLConf) extends Rule[LogicalPlan] with Cast
sourceAttributes = query.output,
providedPartitions = parts,
targetAttributes = l.output,
targetPartitionSchema = t.partitionSchema)
targetPartitionSchema = t.partitionSchema,
conf)
Project(projectList, query)
} else {
query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import org.scalatest.BeforeAndAfterAll
import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, Cast, Expression, Literal}
import org.apache.spark.sql.catalyst.expressions.{Alias, AnsiCast, Attribute, Cast, Expression, Literal}
import org.apache.spark.sql.execution.datasources.DataSourceAnalysis
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf.StoreAssignmentPolicy
import org.apache.spark.sql.types.{DataType, IntegerType, StructType}

class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
Expand Down Expand Up @@ -52,7 +53,12 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
Seq(true, false).foreach { caseSensitive =>
val conf = new SQLConf().copy(SQLConf.CASE_SENSITIVE -> caseSensitive)
def cast(e: Expression, dt: DataType): Expression = {
Cast(e, dt, Option(conf.sessionLocalTimeZone))
conf.storeAssignmentPolicy match {
case StoreAssignmentPolicy.ANSI | StoreAssignmentPolicy.STRICT =>
AnsiCast(e, dt, Option(conf.sessionLocalTimeZone))
case _ =>
Cast(e, dt, Option(conf.sessionLocalTimeZone))
}
}
val rule = DataSourceAnalysis(conf)
test(
Expand All @@ -63,7 +69,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> None, "c" -> None),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}
}

Expand All @@ -74,7 +81,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int),
providedPartitions = Map("b" -> Some("1"), "c" -> None),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}
}

Expand All @@ -85,7 +93,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> Some("1")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}

// Missing partitioning columns.
Expand All @@ -94,7 +103,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int, 'g.int),
providedPartitions = Map("b" -> Some("1")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}

// Wrong partitioning columns.
Expand All @@ -103,7 +113,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> Some("1"), "d" -> None),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}
}

Expand All @@ -114,7 +125,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> Some("1"), "d" -> Some("2")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}

// Wrong partitioning columns.
Expand All @@ -123,7 +135,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int),
providedPartitions = Map("b" -> Some("1"), "c" -> Some("3"), "d" -> Some("2")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}

if (caseSensitive) {
Expand All @@ -133,7 +146,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> Some("1"), "C" -> Some("3")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}
}
}
Expand All @@ -147,7 +161,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = Seq('e.int, 'f.int),
providedPartitions = Map("b" -> None, "c" -> Some("3")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
}
}

Expand All @@ -160,7 +175,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = nonPartitionedAttributes,
providedPartitions = Map("b" -> Some("1"), "C" -> Some("3")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
checkProjectList(actual, expected)
}

Expand All @@ -172,7 +188,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = nonPartitionedAttributes,
providedPartitions = Map("b" -> Some("1"), "c" -> Some("3")),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
checkProjectList(actual, expected)
}

Expand All @@ -184,7 +201,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = nonPartitionedAttributes,
providedPartitions = Map("b" -> Some("1")),
targetAttributes = Seq('a.int, 'd.int, 'b.int),
targetPartitionSchema = new StructType().add("b", IntegerType))
targetPartitionSchema = new StructType().add("b", IntegerType),
conf)
checkProjectList(actual, expected)
}
}
Expand All @@ -200,7 +218,8 @@ class DataSourceAnalysisSuite extends SparkFunSuite with BeforeAndAfterAll {
sourceAttributes = nonPartitionedAttributes ++ dynamicPartitionAttributes,
providedPartitions = Map("b" -> Some("1"), "c" -> None),
targetAttributes = targetAttributes,
targetPartitionSchema = targetPartitionSchema)
targetPartitionSchema = targetPartitionSchema,
conf)
checkProjectList(actual, expected)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,19 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
}
}

test("SPARK-30844: static partition should also follow StoreAssignmentPolicy") {
withSQLConf(
SQLConf.STORE_ASSIGNMENT_POLICY.key -> SQLConf.StoreAssignmentPolicy.ANSI.toString) {
Copy link
Member

Choose a reason for hiding this comment

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

How about testing the other modes here, e.g., LEGACY and STRICT?

Copy link
Member Author

Choose a reason for hiding this comment

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

updated!

withTable("t") {
sql("create table t(a int, b string) using parquet partitioned by (a)")
val errorMsg = intercept[NumberFormatException] {
sql("insert into t partition(a='ansi') values('ansi')")
}.getMessage
assert(errorMsg.contains("invalid input syntax for type numeric: ansi"))
}
}
}

test("SPARK-24860: dynamic partition overwrite specified per source without catalog table") {
withTempPath { path =>
Seq((1, 1), (2, 2)).toDF("i", "part")
Expand Down