Skip to content
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 @@ -73,6 +73,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog)
CombineLimits,
CombineUnions,
// Constant folding and strength reduction
TransposeWindow,
NullPropagation,
ConstantPropagation,
FoldablePropagation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package org.apache.spark.sql
import org.scalatest.Matchers.the

import org.apache.spark.TestUtils.{assertNotSpilled, assertSpilled}
import org.apache.spark.sql.catalyst.optimizer.TransposeWindow
import org.apache.spark.sql.execution.exchange.Exchange
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction, Window}
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -668,18 +670,30 @@ class DataFrameWindowFunctionsSuite extends QueryTest with SharedSQLContext {
("S2", "P2", 300)
).toDF("sno", "pno", "qty")

val w1 = Window.partitionBy("sno")
val w2 = Window.partitionBy("sno", "pno")

checkAnswer(
df.select($"sno", $"pno", $"qty", sum($"qty").over(w2).alias("sum_qty_2"))
.select($"sno", $"pno", $"qty", col("sum_qty_2"), sum("qty").over(w1).alias("sum_qty_1")),
Seq(
Row("S1", "P1", 100, 800, 800),
Row("S1", "P1", 700, 800, 800),
Row("S2", "P1", 200, 200, 500),
Row("S2", "P2", 300, 300, 500)))

Seq(true, false).foreach { transposeWindowEnabled =>
val excludedRules = if (transposeWindowEnabled) "" else TransposeWindow.ruleName
withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> excludedRules) {
val w1 = Window.partitionBy("sno")
val w2 = Window.partitionBy("sno", "pno")

val select = df.select($"sno", $"pno", $"qty", sum($"qty").over(w2).alias("sum_qty_2"))
.select($"sno", $"pno", $"qty", col("sum_qty_2"), sum("qty").over(w1).alias("sum_qty_1"))

val expectedNumExchanges = if (transposeWindowEnabled) 1 else 2
val actualNumExchanges = select.queryExecution.executedPlan.collect {
case e: Exchange => e
}.length
assert(actualNumExchanges == expectedNumExchanges)

checkAnswer(
select,
Seq(
Row("S1", "P1", 100, 800, 800),
Row("S1", "P1", 700, 800, 800),
Row("S2", "P1", 200, 200, 500),
Row("S2", "P2", 300, 300, 500)))
}
}
}

test("NaN and -0.0 in window partition keys") {
Expand Down