forked from NVIDIA/spark-rapids
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support GpuScalarSubquery (NVIDIA#1639)
Signed-off-by: sperlingxx <lovedreamf@gmail.com>
- Loading branch information
1 parent
bcdab58
commit 609696e
Showing
6 changed files
with
188 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
# | ||
# Licensed 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. | ||
|
||
import pytest | ||
from asserts import assert_gpu_and_cpu_are_equal_sql | ||
from data_gen import * | ||
from marks import * | ||
|
||
gens = [('l', LongGen()), ('i', IntegerGen()), ('f', FloatGen()), ( | ||
's', StringGen())] | ||
|
||
|
||
@ignore_order | ||
@pytest.mark.parametrize('data_gen', [gens], ids=idfn) | ||
def test_scalar_subquery(data_gen): | ||
assert_gpu_and_cpu_are_equal_sql( | ||
lambda spark: gen_df(spark, data_gen, length=2048), | ||
'table', | ||
''' | ||
select l, i, f, (select count(s) from table) as c | ||
from table | ||
where l > (select max(i) from table) or f < (select min(i) from table) | ||
''') |
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
71 changes: 71 additions & 0 deletions
71
sql-plugin/src/main/scala/org/apache/spark/sql/rapids/GpuScalarSubquery.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,71 @@ | ||
/* | ||
* Copyright (c) 2021, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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.rapids | ||
|
||
import com.nvidia.spark.rapids.GpuExpression | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Expression, ExprId} | ||
import org.apache.spark.sql.execution.{BaseSubqueryExec, ExecSubqueryExpression} | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.DataType | ||
import org.apache.spark.sql.vectorized.ColumnarBatch | ||
|
||
/** | ||
* GPU placeholder of ScalarSubquery, which returns the scalar result with columnarEval method. | ||
* This placeholder is to make ScalarSubquery working as a GPUExpression to cooperate | ||
* other GPU overrides. | ||
*/ | ||
case class GpuScalarSubquery( | ||
plan: BaseSubqueryExec, | ||
exprId: ExprId) | ||
extends ExecSubqueryExpression with GpuExpression { | ||
|
||
override def dataType: DataType = plan.schema.fields.head.dataType | ||
override def children: Seq[Expression] = Seq.empty | ||
override def nullable: Boolean = true | ||
override def toString: String = plan.simpleString(SQLConf.get.maxToStringFields) | ||
override def withNewPlan(query: BaseSubqueryExec): GpuScalarSubquery = copy(plan = query) | ||
|
||
override def semanticEquals(other: Expression): Boolean = other match { | ||
case s: GpuScalarSubquery => plan.sameResult(s.plan) | ||
case _ => false | ||
} | ||
|
||
// the first column in first row from `query`. | ||
@volatile private var result: Any = _ | ||
@volatile private var updated: Boolean = false | ||
|
||
override def updateResult(): Unit = { | ||
val rows = plan.executeCollect() | ||
if (rows.length > 1) { | ||
sys.error(s"more than one row returned by a subquery used as an expression:\n$plan") | ||
} else if (rows.length == 1) { | ||
assert(rows.head.numFields == 1, | ||
s"Expects 1 field, but got ${rows.head.numFields}; something went wrong in analysis") | ||
result = rows.head.get(0, dataType) | ||
} else { | ||
// If there is no rows returned, the result should be null. | ||
result = null | ||
} | ||
updated = true | ||
} | ||
|
||
override def columnarEval(batch: ColumnarBatch): Any = { | ||
require(updated, s"$this has not finished") | ||
result | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/src/test/scala/com/nvidia/spark/rapids/ScalarSubquerySuite.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,42 @@ | ||
/* | ||
* Copyright (c) 2021, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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 com.nvidia.spark.rapids | ||
|
||
import org.apache.spark.sql.execution.{ScalarSubquery, SparkPlan} | ||
|
||
class ScalarSubquerySuite extends SparkQueryCompareTestSuite { | ||
|
||
private def checkExecPlan(plan: SparkPlan): Unit = { | ||
if (!plan.conf.getAllConfs(RapidsConf.SQL_ENABLED.key).toBoolean) return | ||
plan.find(_.expressions.exists(e => e.find(_.isInstanceOf[ScalarSubquery]).nonEmpty)) match { | ||
case Some(plan) => | ||
throw new AssertionError(s"Assume no (cpu)ScalarSubquery, but found in $plan") | ||
case None => | ||
} | ||
} | ||
|
||
testSparkResultsAreEqual("Uncorrelated Scalar Subquery", longsFromCSVDf, | ||
repart = 0) { | ||
frame => { | ||
frame.createOrReplaceTempView("table") | ||
val ret = frame.sparkSession.sql( | ||
"SELECT longs, (SELECT max(more_longs) FROM table) FROM table") | ||
checkExecPlan(ret.queryExecution.executedPlan) | ||
ret | ||
} | ||
} | ||
} |