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 group-limit optimization for ROW_NUMBER #11886

Merged
merged 2 commits into from
Dec 19, 2024
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
37 changes: 3 additions & 34 deletions integration_tests/src/main/python/window_function_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,9 @@ def assert_query_runs_on(exec, conf):
'DENSE_RANK() OVER (PARTITION BY a ORDER BY b, c) ',
'RANK() OVER (ORDER BY a,b,c) ',
'DENSE_RANK() OVER (ORDER BY a,b,c) ',
])
# ROW_NUMBER() on an un-partitioned window does not invoke WindowGroupLimit optimization.
'ROW_NUMBER() OVER (PARTITION BY a ORDER BY b,c) ',
])
def test_window_group_limits_for_ranking_functions(data_gen, batch_size, rank_clause):
"""
This test verifies that window group limits are applied for queries with ranking-function based
Expand All @@ -2133,39 +2135,6 @@ def test_window_group_limits_for_ranking_functions(data_gen, batch_size, rank_cl
conf=conf)


@allow_non_gpu('WindowGroupLimitExec')
@pytest.mark.skipif(condition=not (is_spark_350_or_later() or is_databricks133_or_later()),
reason="WindowGroupLimit not available for spark.version < 3.5 "
" and Databricks version < 13.3")
@ignore_order(local=True)
@approximate_float
def test_window_group_limits_fallback_for_row_number():
"""
This test verifies that window group limits are applied for queries with ranking-function based
row filters.
This test covers RANK() and DENSE_RANK(), for window function with and without `PARTITIONED BY`
clauses.
"""
conf = {'spark.rapids.sql.batchSizeBytes': '1g',
'spark.rapids.sql.castFloatToDecimal.enabled': True}

data_gen = _grpkey_longs_with_no_nulls
query = """
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY a ORDER BY b, c) AS rnk
FROM window_agg_table
)
WHERE rnk < 3
"""

assert_gpu_sql_fallback_collect(
lambda spark: gen_df(spark, data_gen, length=512),
cpu_fallback_class_name="WindowGroupLimitExec",
table_name="window_agg_table",
sql=query,
conf=conf)


def test_lru_cache_datagen():
# log cache info at the end of integration tests, not related to window functions
info = gen_df_help.cache_info()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import ai.rapids.cudf.GroupByOptions
import com.nvidia.spark.rapids.{BaseExprMeta, DataFromReplacementRule, GpuBindReferences, GpuBoundReference, GpuColumnVector, GpuExec, GpuExpression, GpuMetric, GpuOverrides, GpuProjectExec, RapidsConf, RapidsMeta, SparkPlanMeta, SpillableColumnarBatch, SpillPriorities}
import com.nvidia.spark.rapids.Arm.withResource
import com.nvidia.spark.rapids.RmmRapidsRetryIterator.{splitSpillableInHalfByRows, withRetry}
import com.nvidia.spark.rapids.window.{GpuDenseRank, GpuRank}
import com.nvidia.spark.rapids.window.{GpuDenseRank, GpuRank, GpuRowNumber}

import org.apache.spark.internal.Logging
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, DenseRank, Expression, Rank, SortOrder}
import org.apache.spark.sql.catalyst.expressions.{Attribute, DenseRank, Expression, Rank, RowNumber, SortOrder}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.window.{Final, Partial, WindowGroupLimitExec, WindowGroupLimitMode}
import org.apache.spark.sql.types.DataType
Expand All @@ -44,6 +44,7 @@ import org.apache.spark.sql.vectorized.ColumnarBatch
sealed trait RankFunctionType
case object RankFunction extends RankFunctionType
case object DenseRankFunction extends RankFunctionType
case object RowNumberFunction extends RankFunctionType

class GpuWindowGroupLimitExecMeta(limitExec: WindowGroupLimitExec,
conf: RapidsConf,
Expand All @@ -63,8 +64,8 @@ class GpuWindowGroupLimitExecMeta(limitExec: WindowGroupLimitExec,
wrapped.rankLikeFunction match {
case DenseRank(_) =>
case Rank(_) =>
// case RowNumber() => // TODO: Future.
case _ => willNotWorkOnGpu("Only Rank() and DenseRank() are " +
case RowNumber() =>
case _ => willNotWorkOnGpu("Only rank, dense_rank and row_number are " +
"currently supported for window group limits")
}

Expand Down Expand Up @@ -166,6 +167,7 @@ class GpuWindowGroupLimitingIterator(input: Iterator[ColumnarBatch],
rankFunctionType match {
case RankFunction => GpuRank(sortColumns)
case DenseRankFunction => GpuDenseRank(sortColumns)
case RowNumberFunction => GpuRowNumber
case _ => throw new IllegalArgumentException("Unexpected ranking function")
}
}
Expand Down Expand Up @@ -300,8 +302,10 @@ case class GpuWindowGroupLimitExec(
private def getRankFunctionType(expr: Expression): RankFunctionType = expr match {
case GpuRank(_) => RankFunction
case GpuDenseRank(_) => DenseRankFunction
case GpuRowNumber => RowNumberFunction
case _ =>
throw new UnsupportedOperationException("Only Rank() is currently supported for group limits")
throw new UnsupportedOperationException("Only rank, dense_rank and row_number are " +
"currently supported for group limits")
}

override protected def internalDoExecuteColumnar(): RDD[ColumnarBatch] = {
Expand Down
Loading