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

Support broadcast nested loop join for LeftSemi and LeftAnti #3184

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
57 changes: 56 additions & 1 deletion integration_tests/src/main/python/join_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@

double_gen = [pytest.param(DoubleGen(), marks=[incompat])]

# data types supported by AST expressions
ast_gen = [boolean_gen, byte_gen, short_gen, int_gen, long_gen, timestamp_gen]

_sortmerge_join_conf = {'spark.sql.autoBroadcastJoinThreshold': '-1',
'spark.sql.join.preferSortMergeJoin': 'True',
'spark.sql.shuffle.partitions': '2',
Expand Down Expand Up @@ -345,7 +348,7 @@ def do_join(spark):
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['Inner', 'Cross'], ids=idfn)
@pytest.mark.parametrize('batch_size', ['100', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
def test_broadcast_nested_loop_join_with_conditionals(data_gen, join_type, batch_size):
def test_broadcast_nested_loop_innerlike_join_with_conditionals(data_gen, join_type, batch_size):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
Expand All @@ -358,6 +361,58 @@ def do_join(spark):
conf.update(allow_negative_scale_of_decimal_conf)
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)

# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', ast_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['LeftSemi', 'LeftAnti'], ids=idfn)
@pytest.mark.parametrize('batch_size', ['100', '1g'], ids=idfn) # set the batch size so we can test multiple stream batches
def test_broadcast_nested_loop_join_with_ast_condition(data_gen, join_type, batch_size):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
return left.join(broadcast(right),
(left.b >= right.r_b), join_type)
conf = {'spark.rapids.sql.batchSizeBytes': batch_size}
conf.update(allow_negative_scale_of_decimal_conf)
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)

@pytest.mark.parametrize('data_gen', all_gen + single_level_array_gens, ids=idfn)
@pytest.mark.parametrize('join_type', ['Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
def test_broadcast_nested_loop_join_condition_missing(data_gen, join_type):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
# This test is impacted by https://github.com/NVIDIA/spark-rapids/issues/294
# if the sizes are large enough to have both 0.0 and -0.0 show up 500 and 250
# but these take a long time to verify so we run with smaller numbers by default
# that do not expose the error
return left.join(broadcast(right), how=join_type)
conf = allow_negative_scale_of_decimal_conf
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)

@pytest.mark.parametrize('data_gen', all_gen + single_level_array_gens, ids=idfn)
@pytest.mark.parametrize('join_type', ['Inner', 'LeftSemi', 'LeftAnti'], ids=idfn)
def test_broadcast_nested_loop_join_condition_missing_count(data_gen, join_type):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return left.join(broadcast(right), how=join_type).selectExpr('COUNT(*)')
conf = allow_negative_scale_of_decimal_conf
assert_gpu_and_cpu_are_equal_collect(do_join, conf=conf)

@allow_non_gpu('BroadcastExchangeExec', 'BroadcastNestedLoopJoinExec', 'GreaterThanOrEqual')
@ignore_order(local=True)
@pytest.mark.parametrize('data_gen', all_gen, ids=idfn)
@pytest.mark.parametrize('join_type', ['LeftSemi', 'LeftAnti'], ids=idfn)
def test_broadcast_nested_loop_join_with_conditionals_build_left_fallback(data_gen, join_type):
def do_join(spark):
left, right = create_df(spark, data_gen, 50, 25)
return broadcast(left).join(right, (left.b >= right.r_b), join_type)
conf = allow_negative_scale_of_decimal_conf
assert_gpu_fallback_collect(do_join, 'BroadcastNestedLoopJoinExec', conf=conf)

# local sort because of https://github.com/NVIDIA/spark-rapids/issues/84
# After 3.1.0 is the min spark version we can drop this
@ignore_order(local=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import java.io.{IOException, ObjectInputStream, ObjectOutputStream}
import scala.collection.mutable

import ai.rapids.cudf.{JCudfSerialization, NvtxColor, NvtxRange}
import com.nvidia.spark.rapids.{Arm, GpuBindReferences, GpuBuildLeft, GpuColumnVector, GpuExec, GpuMetric, GpuSemaphore, LazySpillableColumnarBatch, MetricsLevel}
import ai.rapids.cudf.ast
import com.nvidia.spark.rapids.{Arm, GpuBindReferences, GpuBuildLeft, GpuColumnVector, GpuExec, GpuExpression, GpuMetric, GpuSemaphore, LazySpillableColumnarBatch, MetricsLevel}
import com.nvidia.spark.rapids.RapidsBuffer.SpillCallback
import com.nvidia.spark.rapids.RapidsPluginImplicits._

import org.apache.spark.{Dependency, NarrowDependency, Partition, SparkContext, TaskContext}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression}
import org.apache.spark.sql.catalyst.plans.Cross
import org.apache.spark.sql.execution.{BinaryExecNode, ExplainUtils, SparkPlan}
import org.apache.spark.sql.rapids.execution.GpuBroadcastNestedLoopJoinExecBase
import org.apache.spark.sql.types.DataType
Expand Down Expand Up @@ -111,7 +113,8 @@ class GpuCartesianPartition(

class GpuCartesianRDD(
sc: SparkContext,
boundCondition: Option[Expression],
boundCondition: Option[GpuExpression],
numFirstTableColumns: Int,
spillCallback: SpillCallback,
targetSize: Long,
joinTime: GpuMetric,
Expand Down Expand Up @@ -182,9 +185,9 @@ class GpuCartesianRDD(
spillBatchBuffer.toIterator.map(LazySpillableColumnarBatch.spillOnly)
}

GpuBroadcastNestedLoopJoinExecBase.innerLikeJoin(
batch, streamIterator, targetSize, GpuBuildLeft, boundCondition,
numOutputRows, joinOutputRows, numOutputBatches,
GpuBroadcastNestedLoopJoinExecBase.nestedLoopJoin(
Cross, numFirstTableColumns, batch, streamIterator, targetSize, GpuBuildLeft,
boundCondition, spillCallback, numOutputRows, joinOutputRows, numOutputBatches,
joinTime, totalTime)
}
}
Expand Down Expand Up @@ -265,9 +268,11 @@ case class GpuCartesianProductExec(
numOutputBatches)
} else {
val spillCallback = GpuMetric.makeSpillCallback(allMetrics)
val numFirstTableColumns = left.output.size

new GpuCartesianRDD(sparkContext,
boundCondition,
numFirstTableColumns,
spillCallback,
targetSizeBytes,
joinTime,
Expand Down
Loading