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

[SPARK-48003][SQL] Add collation support for hll sketch aggregate #46241

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.icu.text.RuleBasedCollator;
import com.ibm.icu.text.StringSearch;
import com.ibm.icu.util.ULocale;
import com.ibm.icu.text.CollationKey;
import com.ibm.icu.text.Collator;

import org.apache.spark.SparkException;
Expand Down Expand Up @@ -270,4 +271,17 @@ public static Collation fetchCollation(String collationName) throws SparkExcepti
int collationId = collationNameToId(collationName);
return collationTable[collationId];
}

public static UTF8String getCollationKey(UTF8String input, int collationId) {
Collation collation = fetchCollation(collationId);
if (collation.supportsBinaryEquality) {
return input;
} else if (collation.supportsLowercaseEquality) {
return input.toLowerCase();
} else {
CollationKey collationKey = collation.collator.getCollationKey(input.toString());
return UTF8String.fromBytes(collationKey.toByteArray());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import org.apache.spark.SparkUnsupportedOperationException
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, ExpressionDescription, Literal}
import org.apache.spark.sql.catalyst.trees.BinaryLike
import org.apache.spark.sql.catalyst.util.CollationFactory
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.internal.types.StringTypeAnyCollation
import org.apache.spark.sql.types.{AbstractDataType, BinaryType, BooleanType, DataType, IntegerType, LongType, StringType, TypeCollection}
import org.apache.spark.unsafe.types.UTF8String

Expand Down Expand Up @@ -103,7 +105,7 @@ case class HllSketchAgg(
override def prettyName: String = "hll_sketch_agg"

override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(IntegerType, LongType, StringType, BinaryType), IntegerType)
Seq(TypeCollection(IntegerType, LongType, StringTypeAnyCollation, BinaryType), IntegerType)

override def dataType: DataType = BinaryType

Expand Down Expand Up @@ -137,7 +139,9 @@ case class HllSketchAgg(
// TODO: implement support for decimal/datetime/interval types
case IntegerType => sketch.update(v.asInstanceOf[Int])
case LongType => sketch.update(v.asInstanceOf[Long])
case StringType => sketch.update(v.asInstanceOf[UTF8String].toString)
case st: StringType =>
val cKey = CollationFactory.getCollationKey(v.asInstanceOf[UTF8String], st.collationId)
sketch.update(cKey.toString)
case BinaryType => sketch.update(v.asInstanceOf[Array[Byte]])
case dataType => throw new SparkUnsupportedOperationException(
errorClass = "_LEGACY_ERROR_TEMP_3121",
Expand Down
19 changes: 19 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/CollationSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1040,4 +1040,23 @@ class CollationSuite extends DatasourceV2SQLBase with AdaptiveSparkPlanHelper {
checkAnswer(dfNonBinary, dfBinary)
}
}

test("hll sketch aggregate should respect collation") {
case class HllSketchAggTestCase[R](c: String, result: R)
val testCases = Seq(
HllSketchAggTestCase("UTF8_BINARY", 4),
HllSketchAggTestCase("UTF8_BINARY_LCASE", 3),
HllSketchAggTestCase("UNICODE", 4),
HllSketchAggTestCase("UNICODE_CI", 3)
)
testCases.foreach(t => {
withSQLConf(SqlApiConf.DEFAULT_COLLATION -> t.c) {
val q = "SELECT hll_sketch_estimate(hll_sketch_agg(col)) FROM " +
"VALUES ('a'), ('A'), ('b'), ('b'), ('c') tab(col)"
val df = sql(q)
checkAnswer(df, Seq(Row(t.result)))
}
})
}

}