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-51584][SQL] Add rule that pushes Project through Offset and Suite that tests it #50326

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 @@ -402,6 +402,8 @@ package object dsl {

def localLimit(limitExpr: Expression): LogicalPlan = LocalLimit(limitExpr, logicalPlan)

def globalLimit(limitExpr: Expression): LogicalPlan = GlobalLimit(limitExpr, logicalPlan)

def offset(offsetExpr: Expression): LogicalPlan = Offset(offsetExpr, logicalPlan)

def join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ abstract class Optimizer(catalogManager: CatalogManager)
// Operator push down
PushProjectionThroughUnion,
PushProjectionThroughLimit,
PushProjectionThroughOffset,
ReorderJoin,
EliminateOuterJoin,
PushDownPredicates,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.optimizer

import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Offset, Project}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.TreePattern.{OFFSET, PROJECT}

/**
* Pushes Project operator through Offset operator.
*/
object PushProjectionThroughOffset extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
_.containsAllPatterns(PROJECT, OFFSET)) {

case p @ Project(projectList, offset @ Offset(_, child))
if projectList.forall(_.deterministic) =>
offset.copy(child = p.copy(projectList, child))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ case class Offset(offsetExpr: Expression, child: LogicalPlan) extends OrderPrese
}
override protected def withNewChildInternal(newChild: LogicalPlan): Offset =
copy(child = newChild)

override val nodePatterns: Seq[TreePattern] = Seq(OFFSET)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ object TreePattern extends Enumeration {
val LOCAL_RELATION: Value = Value
val LOGICAL_QUERY_STAGE: Value = Value
val NATURAL_LIKE_JOIN: Value = Value
val OFFSET: Value = Value
val OUTER_JOIN: Value = Value
val PROJECT: Value = Value
val PYTHON_DATA_SOURCE: Value = Value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.optimizer

import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.Add
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.RuleExecutor

class PushProjectionThroughOffsetSuite extends PlanTest {

object Optimize extends RuleExecutor[LogicalPlan] {
val batches = Batch("Optimizer Batch",
FixedPoint(100),
PushProjectionThroughLimit,
PushProjectionThroughOffset,
EliminateLimits,
LimitPushDown) :: Nil
}

test("push projection through offset") {
val testRelation = LocalRelation.fromExternalRows(
Seq("a".attr.int, "b".attr.int, "c".attr.int),
1.to(30).map(_ => Row(1, 2, 3)))

val query1 = testRelation
.offset(5)
.select($"a", $"b", $"c")
.analyze
val optimized1 = Optimize.execute(query1)
val expected1 = testRelation
.select($"a", $"b", $"c")
.offset(5).analyze
comparePlans(optimized1, expected1)

val query2 = testRelation
.limit(15).offset(5)
.select($"a", $"b", $"c")
.analyze
val optimized2 = Optimize.execute(query2)
val expected2 = testRelation
.select($"a", $"b", $"c")
.limit(15).offset(5).analyze
comparePlans(optimized2, expected2)

val query3 = testRelation
.offset(5).limit(15)
.select($"a", $"b", $"c")
.analyze
val optimized3 = Optimize.execute(query3)
val expected3 = testRelation
.select($"a", $"b", $"c")
.localLimit(Add(15, 5)).offset(5).globalLimit(15)
.analyze
comparePlans(optimized3, expected3)

val query4 = testRelation
.offset(5).limit(15)
.select($"a", $"b", $"c")
.limit(10).analyze
val optimized4 = Optimize.execute(query4)
val expected4 = testRelation
.select($"a", $"b", $"c")
.localLimit(Add(10, 5)).offset(5).globalLimit(10)
.analyze
comparePlans(optimized4, expected4)

val query5 = testRelation
.localLimit(10)
.select($"a", $"b", $"c")
.offset(5).limit(10).analyze
val optimized5 = Optimize.execute(query5)
val expected5 = testRelation
.select($"a", $"b", $"c")
.localLimit(10).offset(5).globalLimit(10)
.analyze
comparePlans(optimized5, expected5)

val query6 = testRelation
.localLimit(20)
.select($"a", $"b", $"c")
.offset(5).limit(10).analyze
val optimized6 = Optimize.execute(query6)
val expected6 = testRelation
.select($"a", $"b", $"c")
.localLimit(15).offset(5).globalLimit(10)
.analyze
comparePlans(optimized6, expected6)
}
}