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-46064][SQL][SS] Move out EliminateEventTimeWatermark to the analyzer and change to only take effect on resolved child #43971

Closed
wants to merge 2 commits into from
Closed
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
Expand Up @@ -347,7 +347,9 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor
Batch("Cleanup", fixedPoint,
CleanupAliases),
Batch("HandleSpecialCommand", Once,
HandleSpecialCommand)
HandleSpecialCommand),
Batch("Remove watermark for batch query", Once,
Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Nov 23, 2023

Choose a reason for hiding this comment

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

I guess the node is expected to be already resolved from this point, but I'm checking the flag of resolved in EliminateEventTimeWatermark (again), to make the logic be self-describe.

EliminateEventTimeWatermark)
)

/**
Expand Down Expand Up @@ -3938,7 +3940,7 @@ object CleanupAliases extends Rule[LogicalPlan] with AliasHelper {
object EliminateEventTimeWatermark extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning(
_.containsPattern(EVENT_TIME_WATERMARK)) {
case EventTimeWatermark(_, _, child) if !child.isStreaming => child
case EventTimeWatermark(_, _, child) if child.resolved && !child.isStreaming => child
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1724,4 +1724,27 @@ class AnalysisSuite extends AnalysisTest with Matchers {
checkAnalysis(ident2.select($"a"), testRelation.select($"a").analyze)
}
}

test("SPARK-46064 Basic functionality of elimination for watermark node in batch query") {
val dfWithEventTimeWatermark = EventTimeWatermark($"ts",
IntervalUtils.fromIntervalString("10 seconds"), batchRelationWithTs)

val analyzed = getAnalyzer.executeAndCheck(dfWithEventTimeWatermark, new QueryPlanningTracker)

// EventTimeWatermark node is eliminated via EliminateEventTimeWatermark.
assert(!analyzed.exists(_.isInstanceOf[EventTimeWatermark]))
}

test("SPARK-46064 EliminateEventTimeWatermark properly handles the case where the child of " +
"EventTimeWatermark changes the isStreaming flag during resolution") {
// UnresolvedRelation which is batch initially and will be resolved as streaming
val dfWithTempView = UnresolvedRelation(TableIdentifier("streamingTable"))
val dfWithEventTimeWatermark = EventTimeWatermark($"ts",
IntervalUtils.fromIntervalString("10 seconds"), dfWithTempView)

val analyzed = getAnalyzer.executeAndCheck(dfWithEventTimeWatermark, new QueryPlanningTracker)

// EventTimeWatermark node is NOT eliminated.
assert(analyzed.exists(_.isInstanceOf[EventTimeWatermark]))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ trait AnalysisTest extends PlanTest {
createTempView(catalog, "TaBlE3", TestRelations.testRelation3, overrideIfExists = true)
createGlobalTempView(catalog, "TaBlE4", TestRelations.testRelation4, overrideIfExists = true)
createGlobalTempView(catalog, "TaBlE5", TestRelations.testRelation5, overrideIfExists = true)
createTempView(catalog, "streamingTable", TestRelations.streamingRelation,
overrideIfExists = true)
new Analyzer(catalog) {
override val extendedResolutionRules = extendedAnalysisRules
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ object TestRelations {

val mapRelation = LocalRelation(
AttributeReference("map", MapType(IntegerType, IntegerType))())

val streamingRelation = LocalRelation(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also added in #43966 - I'll rebase when either one is merged.

Seq(
AttributeReference("a", IntegerType)(),
AttributeReference("ts", TimestampType)()
),
isStreaming = true)

val batchRelationWithTs = LocalRelation(
Seq(
AttributeReference("a", IntegerType)(),
AttributeReference("ts", TimestampType)()
),
isStreaming = false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ class FilterPushdownSuite extends PlanTest {

test("watermark pushdown: no pushdown on watermark attribute #1") {
val interval = new CalendarInterval(2, 2, 2000L)
val relation = LocalRelation(attrA, $"b".timestamp, attrC)
val relation = LocalRelation(Seq(attrA, $"b".timestamp, attrC), Nil, isStreaming = true)

// Verify that all conditions except the watermark touching condition are pushed down
// by the optimizer and others are not.
Expand All @@ -1205,7 +1205,7 @@ class FilterPushdownSuite extends PlanTest {

test("watermark pushdown: no pushdown for nondeterministic filter") {
val interval = new CalendarInterval(2, 2, 2000L)
val relation = LocalRelation(attrA, attrB, $"c".timestamp)
val relation = LocalRelation(Seq(attrA, attrB, $"c".timestamp), Nil, isStreaming = true)

// Verify that all conditions except the watermark touching condition are pushed down
// by the optimizer and others are not.
Expand All @@ -1221,7 +1221,7 @@ class FilterPushdownSuite extends PlanTest {

test("watermark pushdown: full pushdown") {
val interval = new CalendarInterval(2, 2, 2000L)
val relation = LocalRelation(attrA, attrB, $"c".timestamp)
val relation = LocalRelation(Seq(attrA, attrB, $"c".timestamp), Nil, isStreaming = true)

// Verify that all conditions except the watermark touching condition are pushed down
// by the optimizer and others are not.
Expand All @@ -1236,7 +1236,7 @@ class FilterPushdownSuite extends PlanTest {

test("watermark pushdown: no pushdown on watermark attribute #2") {
val interval = new CalendarInterval(2, 2, 2000L)
val relation = LocalRelation($"a".timestamp, attrB, attrC)
val relation = LocalRelation(Seq($"a".timestamp, attrB, attrC), Nil, isStreaming = true)

val originalQuery = EventTimeWatermark($"a", interval, relation)
.where($"a" === new java.sql.Timestamp(0) && $"b" === 10)
Expand Down
3 changes: 1 addition & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ class Dataset[T] private[sql](
val parsedDelay = IntervalUtils.fromIntervalString(delayThreshold)
require(!IntervalUtils.isNegative(parsedDelay),
s"delay threshold ($delayThreshold) should not be negative.")
EliminateEventTimeWatermark(
EventTimeWatermark(UnresolvedAttribute(eventTime), parsedDelay, logicalPlan))
EventTimeWatermark(UnresolvedAttribute(eventTime), parsedDelay, logicalPlan)
}
}

Expand Down