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-40000][SQL] Update INSERTs without user-specified fields to not automatically add default values #37430

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@ case class ResolveDefaultColumns(catalog: SessionCatalog) extends Rule[LogicalPl
private def getDefaultExpressionsForInsert(
numQueryOutputs: Int,
schema: StructType): Seq[Expression] = {
val remainingFields: Seq[StructField] = schema.fields.drop(numQueryOutputs)
val numDefaultExpressionsToAdd = getStructFieldsForDefaultExpressions(remainingFields).size
Seq.fill(numDefaultExpressionsToAdd)(UnresolvedAttribute(CURRENT_DEFAULT_COLUMN_NAME))
if (SQLConf.get.getConf(SQLConf.ADD_DEFAULTS_FOR_INSERTS_WITHOUT_USER_SPECIFIED_FIELDS)) {
val remainingFields: Seq[StructField] = schema.fields.drop(numQueryOutputs)
val numDefaultExpressionsToAdd = getStructFieldsForDefaultExpressions(remainingFields).size
Seq.fill(numDefaultExpressionsToAdd)(UnresolvedAttribute(CURRENT_DEFAULT_COLUMN_NAME))
} else {
Seq.empty[Expression]
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,16 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val ADD_DEFAULTS_FOR_INSERTS_WITHOUT_USER_SPECIFIED_FIELDS =
buildConf("spark.sql.defaultColumn.addDefaultsForInsertsWithoutUserSpecifiedFields")
.internal()
.doc("When true, for each INSERT command without any user-specified fields where the " +
dtenedor marked this conversation as resolved.
Show resolved Hide resolved
"of values in the INSERT command is less than the number of columns in the target table, " +
"automatically append DEFAULT values for all remaining columns with explicit DEFAULTs.")
.version("3.4.0")
.booleanConf
.createWithDefault(true)

val ENFORCE_RESERVED_KEYWORDS = buildConf("spark.sql.ansi.enforceReservedKeywords")
.doc(s"When true and '${ANSI_ENABLED.key}' is true, the Spark SQL parser enforces the ANSI " +
"reserved keywords and forbids SQL queries that use reserved keywords as alias names " +
Expand Down Expand Up @@ -4526,6 +4536,9 @@ class SQLConf extends Serializable with Logging {
def useNullsForMissingDefaultColumnValues: Boolean =
getConf(SQLConf.USE_NULLS_FOR_MISSING_DEFAULT_COLUMN_VALUES)

def addDefaultsForInsertsWithoutUserSpecifiedFields: Boolean =
getConf(SQLConf.ADD_DEFAULTS_FOR_INSERTS_WITHOUT_USER_SPECIFIED_FIELDS)

def enforceReservedKeywords: Boolean = ansiEnabled && getConf(ENFORCE_RESERVED_KEYWORDS)

def strictIndexOperator: Boolean = ansiEnabled && getConf(ANSI_STRICT_INDEX_OPERATOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,14 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
sql("insert into t (i, q) select true from (select 1)")
}.getMessage.contains(addTwoColButExpectedThree))
}
withSQLConf(SQLConf.ADD_DEFAULTS_FOR_INSERTS_WITHOUT_USER_SPECIFIED_FIELDS.key -> "false") {
withTable("t") {
sql("create table t(i boolean default true, s bigint default 42) using parquet")
assert(intercept[AnalysisException] {
sql("insert into t (i) values (default)")
}.getMessage.contains("expected 2 columns but found 1 column"))
}
}
// When the USE_NULLS_FOR_MISSING_DEFAULT_COLUMN_VALUES configuration is disabled, and no
// explicit DEFAULT value is available when the INSERT INTO statement provides fewer
// values than expected, the INSERT INTO command fails to execute.
Expand Down