Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,174 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.analysis

import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, ExprId}
import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.StructType

/**
* Shared helpers for the stateful-operator nullability fix. The fix has three
* independent components, all gated by
* [[SQLConf.STATEFUL_OPERATOR_ALWAYS_NULLABLE_OUTPUT]] (pinned per-query via the
* offset log so existing queries keep their pre-fix behavior on restart):
*
* - (a) `widenStateSchema`: explicit `asNullable` at every state-schema construction
* site in each stateful physical exec.
Comment on lines +32 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Component (a) is described as applying "at every state-schema construction site in each stateful physical exec," but several execs are missing the explicit widening:

  • FlatMapGroupsWithStateExec.validateAndMaybeEvolveStateSchema (FlatMapGroupsWithStateExec.scala ~L203): groupingAttributes.toStructType is registered un-widened; and the two StateStore.get / mapPartitionsWithStateStore calls in doExecute (~L247-263) open state stores with the un-widened key schema.
  • FlatMapGroupsInPandasWithStateExec inherits the same base, so it has the same gap.
  • TransformWithStateExec: getColFamilySchemas's defaultSchema (~L143-145), validateAndMaybeEvolveStateSchema (via validateAndWriteStateSchema at ~L380), and the StateStore.get / mapPartitionsWithStateStore calls (~L406-417, ~L428-435) all use keyExpressions.toStructType / keyEncoder.schema un-widened.
  • TransformWithStateInPySparkExec: same pattern.

Grouping attributes are input-derived and subject to the same nullability drift the rest of the fix is preventing. Component (c) may incidentally widen the references via the logical-plan rewrite, but having component (a) skip these execs makes the defense-in-depth claim of the design false and leaves a real gap if (c) misses for any reason (rule excluded, unresolved subplan, etc.). Either add widenStateSchema(...) at these sites for consistency with StateStoreSaveExec / BaseStreamingDeduplicateExec / StreamingSymmetricHashJoinExec, or tighten the wording here to describe which execs are intentionally exempt and why.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let's make sure nullability widening is applied to those operators as well - I thought we did it and looks like we missed it.

* - (b) `widenOutputForStatefulOp`: a per-op `output` override on every stateful logical
* and physical operator, used by the operator's `output` definition.
* - (c) [[WidenStatefulOperatorAttributeNullability]] (defined below in this file): a
* custom optimizer rule that widens `AttributeReference`s inside stateful ops'
* internal expressions and propagates upward to ancestor expressions.
*/
object WidenStatefulOpNullability {

def isEnabled: Boolean =
SQLConf.get.getConf(SQLConf.STATEFUL_OPERATOR_ALWAYS_NULLABLE_OUTPUT)

/**
* Recursively widens an attribute to be fully nullable: outer `nullable = true` plus
* every nested `StructField.nullable`, `ArrayType.containsNull`, and
* `MapType.valueContainsNull` flipped to `true` via
* [[org.apache.spark.sql.types.DataType#asNullable]].
*/
def deepWidenAttribute(a: Attribute): Attribute = a match {
case ref: AttributeReference =>
AttributeReference(
ref.name, ref.dataType.asNullable, nullable = true, ref.metadata)(
ref.exprId, ref.qualifier)
case other => other.withNullability(true)
}

/**
* Component (a): widens a state schema to fully nullable. Stateful physical execs apply
* this at every `validateAndMaybeEvolveStateSchema(...)` call site and every
* `mapPartitionsWith*StateStore(...)` call site. When the conf is off, returns the
* schema unchanged.
*/
def widenStateSchema(schema: StructType): StructType =
if (isEnabled) schema.asNullable else schema

/**
* Component (b): wraps a stateful operator's `output` to be fully nullable. The caller
* is responsible for only calling this from within an `output` definition on a stateful
* operator; gating is handled here via [[isEnabled]].
*/
def widenOutputForStatefulOp(base: Seq[Attribute]): Seq[Attribute] =
if (isEnabled) base.map(deepWidenAttribute) else base

/**
* Recursively walks a schema and replaces any nested `StructType` that
* structurally matches `original` (by field names and base types, ignoring
* nullability) with `widened`. Used by TransformWithState execs to widen
* the grouping-key portion of col-family key schemas without touching
* user-defined key/value portions.
*/
def widenGroupingKeyInSchema(
schema: StructType,
original: StructType,
widened: StructType): StructType = {
if (!isEnabled) return schema
if (structurallyMatches(schema, original)) {
widened
} else {
StructType(schema.fields.map { field =>
field.dataType match {
case st: StructType if structurallyMatches(st, original) =>
field.copy(dataType = widened)
case st: StructType =>
field.copy(dataType =
widenGroupingKeyInSchema(st, original, widened))
case _ => field
}
})
}
}

private def structurallyMatches(
a: StructType, b: StructType): Boolean = {
a.length == b.length && a.zip(b).forall { case (fa, fb) =>
fa.name == fb.name &&
fa.dataType.typeName == fb.dataType.typeName
}
}
}

/**
* Component (c) of the stateful-operator nullability fix: a custom optimizer rule that
* widens `AttributeReference`s inside streaming-stateful operators' internal expressions
* and propagates the widening upward to ancestor operators' expressions.
*
* The rule does NOT introduce any new logical or physical node. It is purely an
* attribute-rewrite pass using `resolveOperatorsUp` (bottom-up): for every node whose
* subtree contains a stateful operator, collect `exprId`s from children's output, then
* deep-widen every `AttributeReference` in the node's expressions whose `exprId` is in
* that set via [[WidenStatefulOpNullability#deepWidenAttribute]].
*
* At a stateful operator itself, all children's output attributes are included because
* the operator's internal expressions (e.g. grouping keys) reference them directly.
* At non-stateful ancestor operators, only children whose subtrees contain a stateful
* operator are included, to avoid unnecessary widening of non-stateful siblings.
* The node's own `p.output` is not needed for non-stateful ancestors because the
* bottom-up traversal guarantees children are already transformed, so their output
* attributes are already nullable and the ancestor's expressions reference those
* children's `exprId`s.
*
* '''Scope.''' The walk only fires on nodes whose subtree contains a stateful operator.
*
* '''Ordering constraint.''' This rule must run AFTER every `UpdateAttributeNullability`
* invocation in both the main optimizer and AQE.
*
* '''Idempotence.''' [[WidenStatefulOpNullability#deepWidenAttribute]] is idempotent.
*/
object WidenStatefulOperatorAttributeNullability extends Rule[LogicalPlan] {

override def apply(plan: LogicalPlan): LogicalPlan = {
if (!conf.getConf(SQLConf.STATEFUL_OPERATOR_ALWAYS_NULLABLE_OUTPUT) ||
!plan.containsStatefulOperator) {
return plan
}
plan.resolveOperatorsUp {
case p if !p.resolved => p
case p: LeafNode => p
case p if !p.containsStatefulOperator => p
case p =>
val widenableAttrs = if (p.isStateful) {
p.output ++ p.children.flatMap(_.output)
} else {
p.children.filter(_.containsStatefulOperator).flatMap(_.output)
}
val widenableExprIds: Set[ExprId] = widenableAttrs
.iterator.collect { case ar: AttributeReference => ar.exprId }.toSet
if (widenableExprIds.isEmpty) {
p
} else {
p.transformExpressions {
case ar: AttributeReference if widenableExprIds.contains(ar.exprId) =>
val widened = WidenStatefulOpNullability.deepWidenAttribute(ar)
if (ar.dataType == widened.dataType && ar.nullable == widened.nullable) {
ar
} else {
widened
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.plans.logical

import org.apache.spark.sql.catalyst.{AliasIdentifier, InternalRow, SQLConfHelper}
import org.apache.spark.sql.catalyst.analysis.{Analyzer, AnsiTypeCoercion, MultiInstanceRelation, Resolver, TypeCoercion, TypeCoercionBase, UnresolvedUnaryNode}
import org.apache.spark.sql.catalyst.analysis.{Analyzer, AnsiTypeCoercion, MultiInstanceRelation, Resolver, TypeCoercion, TypeCoercionBase, UnresolvedUnaryNode, WidenStatefulOpNullability}
import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTable}
import org.apache.spark.sql.catalyst.catalog.CatalogTable.VIEW_STORING_ANALYZED_PLAN
import org.apache.spark.sql.catalyst.expressions._
Expand Down Expand Up @@ -746,7 +746,10 @@ case class Join(
}
}

override def output: Seq[Attribute] = Join.computeOutput(joinType, left.output, right.output)
override def output: Seq[Attribute] = {
val base = Join.computeOutput(joinType, left.output, right.output)
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}

override def metadataOutput: Seq[Attribute] = {
joinType match {
Expand Down Expand Up @@ -1225,7 +1228,10 @@ case class Aggregate(
expressions.forall(_.resolved) && childrenResolved && !hasWindowExpressions
}

override def output: Seq[Attribute] = aggregateExpressions.map(_.toAttribute)
override def output: Seq[Attribute] = {
val base = aggregateExpressions.map(_.toAttribute)
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
override def metadataOutput: Seq[Attribute] = Nil
override def maxRows: Option[Long] = {
if (groupingExpressions.isEmpty) {
Expand Down Expand Up @@ -1749,7 +1755,10 @@ object Limit {
* order.
*/
case class GlobalLimit(limitExpr: Expression, child: LogicalPlan) extends UnaryNode {
override def output: Seq[Attribute] = child.output
override def output: Seq[Attribute] = {
val base = child.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
override def maxRows: Option[Long] = {
limitExpr match {
case IntegerLiteral(limit) => Some(limit)
Expand Down Expand Up @@ -2004,7 +2013,10 @@ case class Sample(
*/
case class Distinct(child: LogicalPlan) extends UnaryNode {
override def maxRows: Option[Long] = child.maxRows
override def output: Seq[Attribute] = child.output
override def output: Seq[Attribute] = {
val base = child.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
final override val nodePatterns: Seq[TreePattern] = Seq(DISTINCT_LIKE)
override protected def withNewChildInternal(newChild: LogicalPlan): Distinct =
copy(child = newChild)
Expand Down Expand Up @@ -2174,7 +2186,10 @@ case class Deduplicate(
keys: Seq[Attribute],
child: LogicalPlan) extends UnaryNode {
override def maxRows: Option[Long] = child.maxRows
override def output: Seq[Attribute] = child.output
override def output: Seq[Attribute] = {
val base = child.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
final override val nodePatterns: Seq[TreePattern] = Seq(DISTINCT_LIKE)
override protected def withNewChildInternal(newChild: LogicalPlan): Deduplicate =
copy(child = newChild)
Expand All @@ -2186,7 +2201,10 @@ case class DeduplicateWithinWatermark(keys: Seq[Attribute], child: LogicalPlan)
override def references: AttributeSet = AttributeSet(keys) ++
AttributeSet(child.output.filter(_.metadata.contains(EventTimeWatermark.delayKey)))
override def maxRows: Option[Long] = child.maxRows
override def output: Seq[Attribute] = child.output
override def output: Seq[Attribute] = {
val base = child.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
final override val nodePatterns: Seq[TreePattern] = Seq(DISTINCT_LIKE)
override protected def withNewChildInternal(newChild: LogicalPlan): DeduplicateWithinWatermark =
copy(child = newChild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.catalyst.plans.logical
import org.apache.spark.api.java.function.FilterFunction
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.sql.{catalyst, Encoder, Row}
import org.apache.spark.sql.catalyst.analysis.{Resolver, UnresolvedDeserializer}
import org.apache.spark.sql.catalyst.analysis.{Resolver, UnresolvedDeserializer, WidenStatefulOpNullability}
import org.apache.spark.sql.catalyst.encoders._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.objects.Invoke
Expand Down Expand Up @@ -568,6 +568,11 @@ case class FlatMapGroupsWithState(
newLeft: LogicalPlan, newRight: LogicalPlan): FlatMapGroupsWithState =
copy(child = newLeft, initialState = newRight)
override def isStateful: Boolean = child.isStreaming

override def output: Seq[Attribute] = {
val base = super.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
}

object TransformWithState {
Expand Down Expand Up @@ -657,6 +662,11 @@ case class TransformWithState(
newLeft: LogicalPlan, newRight: LogicalPlan): TransformWithState =
copy(child = newLeft, initialState = newRight)
override def isStateful: Boolean = child.isStreaming

override def output: Seq[Attribute] = {
val base = super.output
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(base) else base
}
}

/** Factory for constructing new `FlatMapGroupsInR` nodes. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql.catalyst.plans.logical

import org.apache.spark.resource.ResourceProfile
import org.apache.spark.sql.catalyst.SQLConfHelper
import org.apache.spark.sql.catalyst.analysis.{FunctionRegistryBase, MultiInstanceRelation, UnresolvedAttribute, UnresolvedStar}
import org.apache.spark.sql.catalyst.analysis.{FunctionRegistryBase, MultiInstanceRelation, UnresolvedAttribute, UnresolvedStar, WidenStatefulOpNullability}
import org.apache.spark.sql.catalyst.analysis.TableFunctionRegistry.TableFunctionBuilder
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeSet, Expression, ExpressionDescription, ExpressionInfo, JsonToStructs, PythonUDF, PythonUDTF}
import org.apache.spark.sql.catalyst.trees.TreePattern._
Expand Down Expand Up @@ -159,7 +159,9 @@ case class FlatMapGroupsInPandasWithState(
timeout: GroupStateTimeout,
child: LogicalPlan) extends UnaryNode {

override def output: Seq[Attribute] = outputAttrs
override def output: Seq[Attribute] =
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(outputAttrs)
else outputAttrs

override def producedAttributes: AttributeSet = AttributeSet(outputAttrs)

Expand Down Expand Up @@ -206,7 +208,9 @@ case class TransformWithStateInPySpark(

override def right: LogicalPlan = initialState

override def output: Seq[Attribute] = outputAttrs
override def output: Seq[Attribute] =
if (isStateful) WidenStatefulOpNullability.widenOutputForStatefulOp(outputAttrs)
else outputAttrs

override def producedAttributes: AttributeSet = AttributeSet(outputAttrs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,24 @@ object SQLConf {
.booleanConf
.createWithDefault(true)

val STATEFUL_OPERATOR_ALWAYS_NULLABLE_OUTPUT =
buildConf("spark.sql.streaming.statefulOperator.alwaysNullableOutput.enabled")
.internal()
.withBindingPolicy(ConfigBindingPolicy.SESSION)
.doc("When true, every streaming stateful operator reports its output schema with " +
"nullable=true on all columns (including nested struct fields, array elements, and " +
"map values), and the state schema is widened at every construction site, so the " +
"existing state schema " +
"compatibility check trivially passes regardless of input nullability. " +
"This prevents query-optimizer decisions (e.g., PropagateEmptyRelation dropping a " +
"Union branch) from flipping the state schema nullability across microbatches or " +
"restarts. The effective value is pinned per query via the offset log at batch 0, " +
"so pre-existing queries keep their original behavior; only newly started queries " +
"pick this up.")
.version("4.3.0")
.booleanConf
.createWithDefault(true)

val FILESTREAM_SINK_METADATA_IGNORED =
buildConf("spark.sql.streaming.fileStreamSink.ignoreMetadata")
.internal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ClientStreamingQuerySuite extends QueryTest with RemoteSparkSession with L
.count()
.selectExpr("window.start as timestamp", "count as num_events")

assert(countsDF.schema.toDDL == "timestamp TIMESTAMP,num_events BIGINT NOT NULL")
assert(countsDF.schema.toDDL == "timestamp TIMESTAMP,num_events BIGINT")

// Start the query
val queryName = "sparkConnectStreamingQuery"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.execution.adaptive

import org.apache.spark.internal.LogKeys.{BATCH_NAME, RULE_NAME}
import org.apache.spark.sql.catalyst.analysis.UpdateAttributeNullability
import org.apache.spark.sql.catalyst.analysis.{UpdateAttributeNullability, WidenStatefulOperatorAttributeNullability}
import org.apache.spark.sql.catalyst.optimizer.{ConvertToLocalRelation, EliminateLimits, OptimizeOneRowPlan}
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, LogicalPlanIntegrity}
import org.apache.spark.sql.catalyst.rules.{Rule, RuleExecutor}
Expand All @@ -44,7 +44,8 @@ class AQEOptimizer(conf: SQLConf, extendedRuntimeOptimizerRules: Seq[Rule[Logica
Batch("Dynamic Join Selection", Once, DynamicJoinSelection),
Batch("Eliminate Limits", fixedPoint, EliminateLimits),
Batch("Optimize One Row Plan", fixedPoint, OptimizeOneRowPlan)) :+
Batch("User Provided Runtime Optimizers", fixedPoint, extendedRuntimeOptimizerRules: _*)
Batch("User Provided Runtime Optimizers", fixedPoint, extendedRuntimeOptimizerRules: _*) :+
Batch("Widen Stateful Op Nullability", Once, WidenStatefulOperatorAttributeNullability)

final override protected def batches: Seq[Batch] = {
val excludedRules = conf.getConf(SQLConf.ADAPTIVE_OPTIMIZER_EXCLUDED_RULES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.apache.spark.{JobArtifactSet, SparkException, SparkUnsupportedOperati
import org.apache.spark.api.python.{ChainedPythonFunctions, PythonEvalType}
import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.WidenStatefulOpNullability
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical.{EventTimeTimeout, ProcessingTimeTimeout}
Expand Down Expand Up @@ -81,7 +82,8 @@ case class FlatMapGroupsInPandasWithStateExec(
override protected val stateEncoder: ExpressionEncoder[Any] =
ExpressionEncoder(stateType).resolveAndBind().asInstanceOf[ExpressionEncoder[Any]]

override def output: Seq[Attribute] = outAttributes
override def output: Seq[Attribute] =
WidenStatefulOpNullability.widenOutputForStatefulOp(outAttributes)

private val sessionLocalTimeZone = conf.sessionLocalTimeZone
private val pythonRunnerConf = ArrowPythonRunner.getPythonRunnerConfMap(conf)
Expand Down
Loading