-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GLUTEN-7110][VL][DELTA] support IncrementMetric in gluten (#7111)
* [GLUTEN-7110][VL][DELTA] support IncrementMetric in gluten * init DeltaFilterExecTransformer * init DeltaProjectExecTransformer * update metric * add FilterTransformerRegistor * fix scala 213 compile error * add ProjectTransformerRegister * remove redundant function * update supportedDelta * update veloxSparkPlanExecApi * use createUnsafe for ProjectExec * init ReplaceDeltaTransformer * use delta write rule * fix error * use copy-resources to reduce redundant resources
- Loading branch information
1 parent
3dc1980
commit 27f30ad
Showing
14 changed files
with
415 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
gluten-delta/src/main/delta-32/org/apache/gluten/execution/DeltaFilterExecTransformer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.gluten.execution | ||
|
||
import org.apache.gluten.backendsapi.BackendsApiManager | ||
import org.apache.gluten.expression.{ConverterUtils, ExpressionConverter} | ||
import org.apache.gluten.metrics.MetricsUpdater | ||
import org.apache.gluten.substrait.`type`.TypeBuilder | ||
import org.apache.gluten.substrait.SubstraitContext | ||
import org.apache.gluten.substrait.extensions.ExtensionBuilder | ||
import org.apache.gluten.substrait.rel.{RelBuilder, RelNode} | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression} | ||
import org.apache.spark.sql.delta.metric.IncrementMetric | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.sql.execution.metric.SQLMetric | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
case class DeltaFilterExecTransformer(condition: Expression, child: SparkPlan) | ||
extends FilterExecTransformerBase(condition, child) { | ||
|
||
private var extraMetrics: Seq[(String, SQLMetric)] = Seq.empty | ||
|
||
override def metricsUpdater(): MetricsUpdater = | ||
BackendsApiManager.getMetricsApiInstance.genFilterTransformerMetricsUpdater( | ||
metrics, | ||
extraMetrics.toSeq) | ||
|
||
override def getRelNode( | ||
context: SubstraitContext, | ||
condExpr: Expression, | ||
originalInputAttributes: Seq[Attribute], | ||
operatorId: Long, | ||
input: RelNode, | ||
validation: Boolean): RelNode = { | ||
assert(condExpr != null) | ||
val args = context.registeredFunction | ||
val condExprNode = condExpr match { | ||
case IncrementMetric(child, metric) => | ||
extraMetrics :+= (condExpr.prettyName, metric) | ||
ExpressionConverter | ||
.replaceWithExpressionTransformer(child, attributeSeq = originalInputAttributes) | ||
.doTransform(args) | ||
case _ => | ||
ExpressionConverter | ||
.replaceWithExpressionTransformer(condExpr, attributeSeq = originalInputAttributes) | ||
.doTransform(args) | ||
} | ||
|
||
if (!validation) { | ||
RelBuilder.makeFilterRel(input, condExprNode, context, operatorId) | ||
} else { | ||
// Use a extension node to send the input types through Substrait plan for validation. | ||
val inputTypeNodeList = originalInputAttributes | ||
.map(attr => ConverterUtils.getTypeNode(attr.dataType, attr.nullable)) | ||
.asJava | ||
val extensionNode = ExtensionBuilder.makeAdvancedExtension( | ||
BackendsApiManager.getTransformerApiInstance.packPBMessage( | ||
TypeBuilder.makeStruct(false, inputTypeNodeList).toProtobuf)) | ||
RelBuilder.makeFilterRel(input, condExprNode, extensionNode, context, operatorId) | ||
} | ||
} | ||
|
||
override protected def withNewChildInternal(newChild: SparkPlan): DeltaFilterExecTransformer = | ||
copy(child = newChild) | ||
} |
116 changes: 116 additions & 0 deletions
116
gluten-delta/src/main/delta-32/org/apache/gluten/execution/DeltaProjectExecTransformer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.gluten.execution | ||
|
||
import org.apache.gluten.backendsapi.BackendsApiManager | ||
import org.apache.gluten.expression.{ConverterUtils, ExpressionConverter, ExpressionTransformer} | ||
import org.apache.gluten.metrics.MetricsUpdater | ||
import org.apache.gluten.substrait.`type`.TypeBuilder | ||
import org.apache.gluten.substrait.SubstraitContext | ||
import org.apache.gluten.substrait.extensions.ExtensionBuilder | ||
import org.apache.gluten.substrait.rel.{RelBuilder, RelNode} | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, CaseWhen, NamedExpression} | ||
import org.apache.spark.sql.delta.metric.IncrementMetric | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.sql.execution.metric.SQLMetric | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable | ||
|
||
case class DeltaProjectExecTransformer(projectList: Seq[NamedExpression], child: SparkPlan) | ||
extends ProjectExecTransformerBase(projectList, child) { | ||
|
||
private var extraMetrics = mutable.Seq.empty[(String, SQLMetric)] | ||
|
||
override def metricsUpdater(): MetricsUpdater = | ||
BackendsApiManager.getMetricsApiInstance.genProjectTransformerMetricsUpdater( | ||
metrics, | ||
extraMetrics.toSeq) | ||
|
||
override def getRelNode( | ||
context: SubstraitContext, | ||
projectList: Seq[NamedExpression], | ||
originalInputAttributes: Seq[Attribute], | ||
operatorId: Long, | ||
input: RelNode, | ||
validation: Boolean): RelNode = { | ||
val args = context.registeredFunction | ||
val newProjectList = genNewProjectList(projectList) | ||
val columnarProjExprs: Seq[ExpressionTransformer] = ExpressionConverter | ||
.replaceWithExpressionTransformer(newProjectList, attributeSeq = originalInputAttributes) | ||
val projExprNodeList = columnarProjExprs.map(_.doTransform(args)).asJava | ||
val emitStartIndex = originalInputAttributes.size | ||
if (!validation) { | ||
RelBuilder.makeProjectRel(input, projExprNodeList, context, operatorId, emitStartIndex) | ||
} else { | ||
// Use a extension node to send the input types through Substrait plan for validation. | ||
val inputTypeNodeList = originalInputAttributes | ||
.map(attr => ConverterUtils.getTypeNode(attr.dataType, attr.nullable)) | ||
.asJava | ||
val extensionNode = ExtensionBuilder.makeAdvancedExtension( | ||
BackendsApiManager.getTransformerApiInstance.packPBMessage( | ||
TypeBuilder.makeStruct(false, inputTypeNodeList).toProtobuf)) | ||
RelBuilder.makeProjectRel( | ||
input, | ||
projExprNodeList, | ||
extensionNode, | ||
context, | ||
operatorId, | ||
emitStartIndex) | ||
} | ||
} | ||
|
||
override protected def withNewChildInternal(newChild: SparkPlan): DeltaProjectExecTransformer = | ||
copy(child = newChild) | ||
|
||
def genNewProjectList(projectList: Seq[NamedExpression]): Seq[NamedExpression] = { | ||
projectList.map { | ||
case alias: Alias => | ||
alias.child match { | ||
case IncrementMetric(child, metric) => | ||
extraMetrics :+= (alias.child.prettyName, metric) | ||
Alias(child = child, name = alias.name)() | ||
|
||
case CaseWhen(branches, elseValue) => | ||
val newBranches = branches.map { | ||
case (expr1, expr2: IncrementMetric) => | ||
extraMetrics :+= (expr2.prettyName, expr2.metric) | ||
(expr1, expr2.child) | ||
case other => other | ||
} | ||
|
||
val newElseValue = elseValue match { | ||
case Some(IncrementMetric(child: IncrementMetric, metric)) => | ||
extraMetrics :+= (child.prettyName, metric) | ||
extraMetrics :+= (child.prettyName, child.metric) | ||
Some(child.child) | ||
case _ => elseValue | ||
} | ||
|
||
Alias( | ||
child = CaseWhen(newBranches, newElseValue), | ||
name = alias.name | ||
)(alias.exprId) | ||
|
||
case _ => | ||
alias | ||
} | ||
case other => other | ||
} | ||
} | ||
} |
Oops, something went wrong.