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

Optimize aggregation push down column logic #143

Merged
merged 3 commits into from
Dec 27, 2017
Merged
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
17 changes: 17 additions & 0 deletions src/main/scala/org/apache/spark/sql/TiStrategy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.apache.spark.sql

import com.pingcap.tikv.expression.scalar.TiScalarFunction
import java.time.ZonedDateTime

import com.pingcap.tikv.expression.{ExpressionBlacklist, TiByItem, TiColumnRef, TiExpr}
Expand All @@ -35,6 +36,7 @@ import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._

import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
import scala.collection.{JavaConversions, mutable}

// TODO: Too many hacks here since we hijack the planning
Expand Down Expand Up @@ -141,6 +143,15 @@ class TiStrategy(context: SQLContext) extends Strategy with Logging {
dagRequest
}

def extractColumnFromFilter(tiFilter: TiExpr, result: ArrayBuffer[TiColumnRef]): Unit =
tiFilter match {
case fun: TiScalarFunction =>
fun.getArgs.foreach(extractColumnFromFilter(_, result))
case col: TiColumnRef =>
result.add(col)
case _ =>
}

def filterToDAGRequest(
filters: Seq[Expression],
source: TiDBRelation,
Expand Down Expand Up @@ -264,6 +275,7 @@ class TiStrategy(context: SQLContext) extends Strategy with Logging {
}

def groupAggregateProjection(
filters: Seq[Expression],
groupingExpressions: Seq[NamedExpression],
aggregateExpressions: Seq[AggregateExpression],
resultExpressions: Seq[NamedExpression],
Expand Down Expand Up @@ -373,6 +385,10 @@ class TiStrategy(context: SQLContext) extends Strategy with Logging {

val projectSeq: Seq[Attribute] = projects.asInstanceOf[Seq[Attribute]]
projectSeq.foreach(attr => dagReq.addRequiredColumn(TiColumnRef.create(attr.name)))
val pushDownCols = ArrayBuffer[TiColumnRef]()
val tiFilters: Seq[TiExpr] = filters.collect { case BasicExpression(expr) => expr }
tiFilters.foreach(extractColumnFromFilter(_, pushDownCols))
pushDownCols.foreach(dagReq.addRequiredColumn)

aggregate.AggUtils.planAggregateWithoutDistinct(
groupingExpressions,
Expand Down Expand Up @@ -444,6 +460,7 @@ class TiStrategy(context: SQLContext) extends Strategy with Logging {
) if isValidAggregates(groupingExpressions, aggregateExpressions, filters, source) =>
val dagReq: TiDAGRequest = filterToDAGRequest(filters, source)
groupAggregateProjection(
filters,
groupingExpressions,
aggregateExpressions,
resultExpressions,
Expand Down