Skip to content

Commit

Permalink
[GLUTEN-7180][CH] Fix ut `Eliminate NAAJ when BuildSide is HashedRela…
Browse files Browse the repository at this point in the history
…tionWithAllNullKeys` for the CH backend when the aqe is on

Fix ut Eliminate NAAJ when BuildSide is HashedRelationWithAllNullKeys for the CH backend when the aqe is on

Close #7180.
  • Loading branch information
zzcclp committed Sep 10, 2024
1 parent 3edc207 commit 674f03a
Show file tree
Hide file tree
Showing 24 changed files with 345 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public long totalBytes() {

public native void nativeClose(long blockAddress);

public native BlockStats nativeBlockStats(long blockAddress, int columnPosition);

public BlockStats getBlockStats(int columnPosition) {
return nativeBlockStats(blockAddress, columnPosition);
}

public void close() {
if (blockAddress != 0) {
nativeClose(blockAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private static native long nativeBuild(
boolean hasMixedFiltCondition,
boolean isExistenceJoin,
byte[] namedStruct,
boolean isNullAwareAntiJoin);
boolean isNullAwareAntiJoin,
boolean hasNullKeyValues);

private StorageJoinBuilder() {}

Expand All @@ -58,7 +59,8 @@ public static long build(
long rowCount,
BroadCastHashJoinContext broadCastContext,
List<Expression> newBuildKeys,
List<Attribute> newOutput) {
List<Attribute> newOutput,
boolean hasNullKeyValues) {
ConverterUtils$ converter = ConverterUtils$.MODULE$;
List<Expression> keys;
List<Attribute> output;
Expand Down Expand Up @@ -96,7 +98,8 @@ public static long build(
broadCastContext.hasMixedFiltCondition(),
broadCastContext.isExistenceJoin(),
toNameStruct(output).toByteArray(),
broadCastContext.isNullAwareAntiJoin());
broadCastContext.isNullAwareAntiJoin(),
hasNullKeyValues);
}

/** create table named struct */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ object CHBackendSettings extends BackendSettingsApi with Logging {
".runtime_config.max_source_concatenate_bytes"
val GLUTEN_MAX_SHUFFLE_READ_BYTES_DEFAULT = GLUTEN_MAX_BLOCK_SIZE_DEFAULT * 256

val GLUTEN_AQE_PROPAGATEEMPTY: String =
GlutenConfig.GLUTEN_CONFIG_PREFIX + CHBackend.BACKEND_NAME +
".aqe.propagate.empty.relation"

def affinityMode: String = {
SparkEnv.get.conf
.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private object CHRuleApi {
def injectSpark(injector: SparkInjector): Unit = {
// Regular Spark rules.
injector.injectQueryStagePrepRule(FallbackBroadcastHashJoinPrepQueryStage.apply)
injector.injectQueryStagePrepRule(spark => CHAQEPropagateEmptyRelation(spark))
injector.injectParser(
(spark, parserInterface) => new GlutenCacheFilesSqlParser(spark, parserInterface))
injector.injectParser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,12 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
numOutputRows: SQLMetric,
dataSize: SQLMetric): BuildSideRelation = {

val buildKeys: Seq[Expression] = mode match {
val (buildKeys, isNullAware) = mode match {
case mode1: HashedRelationBroadcastMode =>
mode1.key
(mode1.key, mode1.isNullAware)
case _ =>
// IdentityBroadcastMode
Seq.empty
(Seq.empty, false)
}

val (newChild, newOutput, newBuildKeys) =
Expand Down Expand Up @@ -532,8 +532,27 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
}
(newChild, (child.output ++ appendedProjections).map(_.toAttribute), preProjectionBuildKeys)
}

// find the key index in the output
val keyColumnIndex = if (isNullAware) {
def findKeyOrdinal(key: Expression, output: Seq[Attribute]): Int = {
key match {
case b: BoundReference => b.ordinal
case n: NamedExpression =>
output.indexWhere(o => (o.name.equals(n.name) && o.exprId == n.exprId))
case _ => throw new GlutenException(s"Cannot find $key in the child's output: $output")
}
}
if (newBuildKeys.isEmpty) {
findKeyOrdinal(buildKeys(0), newOutput)
} else {
findKeyOrdinal(newBuildKeys(0), newOutput)
}
} else {
0
}
val countsAndBytes =
CHExecUtil.buildSideRDD(dataSize, newChild).collect
CHExecUtil.buildSideRDD(dataSize, newChild, isNullAware, keyColumnIndex).collect

val batches = countsAndBytes.map(_._2)
val totalBatchesSize = batches.map(_.length).sum
Expand All @@ -548,8 +567,15 @@ class CHSparkPlanExecApi extends SparkPlanExecApi with Logging {
s" written bytes is correct.")
}
val rowCount = countsAndBytes.map(_._1).sum
val hasNullKeyValues = countsAndBytes.map(_._3).foldLeft[Boolean](false)((b, a) => { b || a })
numOutputRows += rowCount
ClickHouseBuildSideRelation(mode, newOutput, batches.flatten, rowCount, newBuildKeys)
ClickHouseBuildSideRelation(
mode,
newOutput,
batches.flatten,
rowCount,
newBuildKeys,
hasNullKeyValues)
}

/** Define backend specfic expression mappings. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.extension

import org.apache.gluten.backendsapi.clickhouse.CHBackendSettings
import org.apache.gluten.utils.PhysicalPlanSelector

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.plans.LeftAnti
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.execution.{ColumnarBroadcastExchangeExec, LocalTableScanExec, SparkPlan}
import org.apache.spark.sql.execution.adaptive.BroadcastQueryStageExec
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, ClickHouseBuildSideRelation}

case class CHAQEPropagateEmptyRelation(session: SparkSession) extends Rule[SparkPlan] {

def apply(plan: SparkPlan): SparkPlan = PhysicalPlanSelector.maybe(session, plan) {
if (!(session.conf.get(CHBackendSettings.GLUTEN_AQE_PROPAGATEEMPTY, "true").toBoolean)) {
plan
} else {
plan.transform {
case bhj @ BroadcastHashJoinExec(_, _, joinType, _, _, left, right, isNullAwareAntiJoin)
if (joinType == LeftAnti) && isNullAwareAntiJoin =>
right match {
case BroadcastQueryStageExec(_, plan: SparkPlan, _) =>
val columnarBroadcast = plan match {
case c: ColumnarBroadcastExchangeExec => c
case ReusedExchangeExec(_, c: ColumnarBroadcastExchangeExec) => c
}
val chBuildSideRelation = columnarBroadcast.relationFuture.get().value
chBuildSideRelation match {
case c: ClickHouseBuildSideRelation if c.hasNullKeyValues =>
LocalTableScanExec(bhj.output, Seq.empty)
case _ => bhj
}
case o => bhj
}
case other => other
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.vectorized;

public class BlockStats {
private final long blockRecordCount;
private final boolean hasNullKeyValues;

public BlockStats(long blockRecordCount, boolean hasNullKeyValues) {
this.blockRecordCount = blockRecordCount;
this.hasNullKeyValues = hasNullKeyValues;
}

public long getBlockRecordCount() {
return blockRecordCount;
}

public boolean isHasNullKeyValues() {
return hasNullKeyValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import scala.concurrent.Future
case class GlutenCHCacheDataCommand(
onlyMetaCache: Boolean,
asynExecute: Boolean,
selectedColuman: Option[Seq[String]],
selectedColumn: Option[Seq[String]],
path: Option[String],
table: Option[TableIdentifier],
tsfilter: Option[String],
Expand Down Expand Up @@ -93,8 +93,8 @@ case class GlutenCHCacheDataCommand(
"a Delta table? Refusing to garbage collect.")

val allColumns = snapshot.dataSchema.fieldNames.toSeq
val selectedColumns = if (selectedColuman.nonEmpty) {
selectedColuman.get
val selectedColumns = if (selectedColumn.nonEmpty) {
selectedColumn.get
.filter(allColumns.contains(_))
.map(ConverterUtils.normalizeColName)
.toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ case class ClickHouseBuildSideRelation(
output: Seq[Attribute],
batches: Array[Byte],
numOfRows: Long,
newBuildKeys: Seq[Expression] = Seq.empty)
newBuildKeys: Seq[Expression] = Seq.empty,
hasNullKeyValues: Boolean = false)
extends BuildSideRelation
with Logging {

Expand All @@ -58,7 +59,8 @@ case class ClickHouseBuildSideRelation(
numOfRows,
broadCastContext,
newBuildKeys.asJava,
output.asJava)
output.asJava,
hasNullKeyValues)
(hashTableData, this)
} else {
(StorageJoinBuilder.nativeCloneBuildHashTable(hashTableData), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,49 @@ object CHExecUtil extends Logging {
def toBytes(
dataSize: SQLMetric,
iter: Iterator[ColumnarBatch],
isNullAware: Boolean = false,
keyColumnIndex: Int = 0,
compressionCodec: Option[String] = Some("lz4"),
compressionLevel: Option[Int] = None,
bufferSize: Int = 4 << 10): Iterator[(Int, Array[Byte])] = {
var count = 0
bufferSize: Int = 4 << 10): Iterator[(Long, Array[Byte], Boolean)] = {
var count = 0L
var hasNullKeyValues = false
val bos = new ByteArrayOutputStream()
val buffer = new Array[Byte](bufferSize) // 4K
val level = compressionLevel.getOrElse(Int.MinValue)
val blockOutputStream =
compressionCodec
.map(new BlockOutputStream(bos, buffer, dataSize, true, _, level, bufferSize))
.getOrElse(new BlockOutputStream(bos, buffer, dataSize, false, "", level, bufferSize))
while (iter.hasNext) {
val batch = iter.next()
count += batch.numRows
blockOutputStream.write(batch)
if (isNullAware) {
while (iter.hasNext) {
val batch = iter.next()
val blockStats = CHNativeBlock.fromColumnarBatch(batch).getBlockStats(keyColumnIndex)
count += blockStats.getBlockRecordCount
hasNullKeyValues = hasNullKeyValues || blockStats.isHasNullKeyValues
blockOutputStream.write(batch)
}
} else {
while (iter.hasNext) {
val batch = iter.next()
count += batch.numRows()
blockOutputStream.write(batch)
}
}
blockOutputStream.flush()
blockOutputStream.close()
Iterator((count, bos.toByteArray))
Iterator((count, bos.toByteArray, hasNullKeyValues))
}

def buildSideRDD(dataSize: SQLMetric, newChild: SparkPlan): RDD[(Int, Array[Byte])] = {
def buildSideRDD(
dataSize: SQLMetric,
newChild: SparkPlan,
isNullAware: Boolean,
keyColumnIndex: Int
): RDD[(Long, Array[Byte], Boolean)] = {
newChild
.executeColumnar()
.mapPartitionsInternal(iter => toBytes(dataSize, iter))
.mapPartitionsInternal(iter => toBytes(dataSize, iter, isNullAware, keyColumnIndex))
}

private def buildRangePartitionSampleRDD(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,55 @@ class GlutenClickHouseTPCHSaltNullParquetSuite extends GlutenClickHouseTPCHAbstr
df => {
checkBHJWithIsNullAwareAntiJoin(df)
})

withSQLConf(("spark.sql.adaptive.enabled", "true")) {
def checkAQEBHJWithIsNullAwareAntiJoin(df: DataFrame, isNullAwareBhjCnt: Int = 1): Unit = {
val bhjs = collect(df.queryExecution.executedPlan) {
case bhj: CHBroadcastHashJoinExecTransformer if bhj.isNullAwareAntiJoin => true
}
assert(bhjs.size == isNullAwareBhjCnt)
}

val sql6 =
s"""
|select * from partsupp
|where
|ps_suppkey NOT IN (SELECT suppkey FROM VALUES (null), (6) sub(suppkey))
|""".stripMargin
compareResultsAgainstVanillaSpark(
sql6,
true,
df => {
checkAQEBHJWithIsNullAwareAntiJoin(df, 0)
})

val sql7 =
s"""
|select * from partsupp
|where
|cast(ps_suppkey AS INT) NOT IN (SELECT suppkey FROM VALUES (null), (6) sub(suppkey))
|""".stripMargin
compareResultsAgainstVanillaSpark(
sql7,
true,
df => {
checkAQEBHJWithIsNullAwareAntiJoin(df, 0)
})

val sql8 =
s"""
|select * from partsupp
|where
|ps_suppkey NOT IN (SELECT suppkey FROM VALUES (5), (6) sub(suppkey))
|""".stripMargin
compareResultsAgainstVanillaSpark(
sql8,
true,
df => {
checkAQEBHJWithIsNullAwareAntiJoin(df)
})
}

}

test("soundex") {
Expand Down
Loading

0 comments on commit 674f03a

Please sign in to comment.