-
Notifications
You must be signed in to change notification settings - Fork 73
Spark 3.4: Enhance FunctionRegistry to support more hash functions #268
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...rc/test/scala/org/apache/spark/sql/clickhouse/cluster/ClickHouseClusterHashUDFSuite.scala
This file contains hidden or 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,96 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.clickhouse.cluster | ||
|
||
import org.apache.spark.sql.clickhouse.TestUtils.om | ||
import xenon.clickhouse.func._ | ||
import java.lang.{Long => JLong} | ||
|
||
class ClickHouseClusterHashUDFSuite extends SparkClickHouseClusterTest { | ||
// only for query function names | ||
val dummyRegistry: CompositeFunctionRegistry = { | ||
val dynamicFunctionRegistry = new DynamicFunctionRegistry | ||
val xxHash64ShardFunc = new ClickHouseXxHash64Shard(Seq.empty) | ||
dynamicFunctionRegistry.register("ck_xx_hash64_shard", xxHash64ShardFunc) // for compatible | ||
dynamicFunctionRegistry.register("clickhouse_shard_xxHash64", xxHash64ShardFunc) | ||
new CompositeFunctionRegistry(Array(StaticFunctionRegistry, dynamicFunctionRegistry)) | ||
} | ||
|
||
def runTest(sparkFuncName: String, ckFuncName: String, stringVal: String): Unit = { | ||
val sparkResult = spark.sql( | ||
s"SELECT $sparkFuncName($stringVal) AS hash_value" | ||
).collect | ||
assert(sparkResult.length == 1) | ||
val sparkHashVal = sparkResult.head.getAs[Long]("hash_value") | ||
|
||
val clickhouseResultJsonStr = runClickHouseSQL( | ||
s"SELECT $ckFuncName($stringVal) AS hash_value " | ||
).head.getString(0) | ||
val clickhouseResultJson = om.readTree(clickhouseResultJsonStr) | ||
val clickhouseHashVal = JLong.parseUnsignedLong(clickhouseResultJson.get("hash_value").asText) | ||
assert( | ||
sparkHashVal == clickhouseHashVal, | ||
s"ck_function: $ckFuncName, spark_function: $sparkFuncName, args: ($stringVal)" | ||
) | ||
} | ||
|
||
Seq( | ||
"clickhouse_xxHash64", | ||
"clickhouse_murmurHash3_64", | ||
"clickhouse_murmurHash3_32", | ||
"clickhouse_murmurHash2_64", | ||
"clickhouse_murmurHash2_32", | ||
"clickhouse_cityHash64" | ||
).foreach { sparkFuncName => | ||
val ckFuncName = dummyRegistry.sparkToClickHouseFunc(sparkFuncName) | ||
test(s"UDF $sparkFuncName") { | ||
Seq( | ||
"spark-clickhouse-connector", | ||
"Apache Spark", | ||
"ClickHouse", | ||
"Yandex", | ||
"热爱", | ||
"在传统的行式数据库系统中,数据按如下顺序存储:", | ||
"🇨🇳" | ||
).map("'" + _ + "'").foreach { stringVal => | ||
runTest(sparkFuncName, ckFuncName, stringVal) | ||
} | ||
} | ||
} | ||
|
||
Seq( | ||
"clickhouse_murmurHash3_64", | ||
"clickhouse_murmurHash3_32", | ||
"clickhouse_murmurHash2_64", | ||
"clickhouse_murmurHash2_32", | ||
"clickhouse_cityHash64" | ||
).foreach { sparkFuncName => | ||
val ckFuncName = dummyRegistry.sparkToClickHouseFunc(sparkFuncName) | ||
test(s"UDF $sparkFuncName multiple args") { | ||
Seq( | ||
"spark-clickhouse-connector", | ||
"Apache Spark", | ||
"ClickHouse", | ||
"Yandex", | ||
"热爱", | ||
"在传统的行式数据库系统中,数据按如下顺序存储:", | ||
"🇨🇳" | ||
).map("'" + _ + "'").combinations(5).foreach { seq => | ||
val stringVal = seq.mkString(", ") | ||
runTest(sparkFuncName, ckFuncName, stringVal) | ||
} | ||
} | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
...it/src/test/scala/org/apache/spark/sql/clickhouse/cluster/ClickHouseClusterUDFSuite.scala
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
spark-3.4/clickhouse-spark/src/main/scala/xenon/clickhouse/func/CityHash64.scala
This file contains hidden or 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,27 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 xenon.clickhouse.func | ||
|
||
import xenon.clickhouse.hash | ||
|
||
// https://github.com/ClickHouse/ClickHouse/blob/v23.5.3.24-stable/src/Functions/FunctionsHashing.h#L694 | ||
object CityHash64 extends MultiStringArgsHash { | ||
|
||
override protected def funcName: String = "clickhouse_cityHash64" | ||
|
||
override val ckFuncNames: Array[String] = Array("cityHash64") | ||
|
||
override def applyHash(input: Array[Any]): Long = hash.CityHash64(input) | ||
} |
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
spark-3.4/clickhouse-spark/src/main/scala/xenon/clickhouse/func/MultiStringArgsHash.scala
This file contains hidden or 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,63 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 xenon.clickhouse.func | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.connector.catalog.functions.{BoundFunction, ScalarFunction, UnboundFunction} | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
abstract class MultiStringArgsHash extends UnboundFunction with ClickhouseEquivFunction { | ||
|
||
def applyHash(input: Array[Any]): Long | ||
|
||
protected def funcName: String | ||
|
||
override val ckFuncNames: Array[String] | ||
|
||
override def description: String = s"$name: (value: string, ...) => hash_value: long" | ||
|
||
private def isExceptedType(dt: DataType): Boolean = | ||
dt.isInstanceOf[StringType] | ||
|
||
final override def name: String = funcName | ||
|
||
final override def bind(inputType: StructType): BoundFunction = { | ||
val inputDataTypes = inputType.fields.map(_.dataType) | ||
if (inputDataTypes.forall(isExceptedType)) { | ||
// need to new a ScalarFunction instance for each bind, | ||
// because we do not know the number of arguments in advance | ||
new ScalarFunction[Long] { | ||
override def inputTypes(): Array[DataType] = inputDataTypes | ||
override def name: String = funcName | ||
override def canonicalName: String = s"clickhouse.$name" | ||
override def resultType: DataType = LongType | ||
override def toString: String = name | ||
override def produceResult(input: InternalRow): Long = { | ||
val inputStrings = new Array[Any](input.numFields) | ||
var i = 0 | ||
do { | ||
inputStrings(i) = input.getUTF8String(i).getBytes | ||
i += 1 | ||
} while (i < input.numFields) | ||
applyHash(inputStrings) | ||
} | ||
} | ||
} else { | ||
throw new UnsupportedOperationException(s"Expect multiple STRING argument. $description") | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
spark-3.4/clickhouse-spark/src/main/scala/xenon/clickhouse/func/MurmurHash2.scala
This file contains hidden or 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,38 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 xenon.clickhouse.func | ||
|
||
import xenon.clickhouse.hash | ||
import xenon.clickhouse.hash.HashUtils | ||
|
||
// https://github.com/ClickHouse/ClickHouse/blob/v23.5.3.24-stable/src/Functions/FunctionsHashing.h#L460 | ||
object MurmurHash2_64 extends MultiStringArgsHash { | ||
|
||
override protected def funcName: String = "clickhouse_murmurHash2_64" | ||
|
||
override val ckFuncNames: Array[String] = Array("murmurHash2_64") | ||
|
||
override def applyHash(input: Array[Any]): Long = hash.Murmurhash2_64(input) | ||
} | ||
|
||
// https://github.com/ClickHouse/ClickHouse/blob/v23.5.3.24-stable/src/Functions/FunctionsHashing.h#L519 | ||
object MurmurHash2_32 extends MultiStringArgsHash { | ||
|
||
override protected def funcName: String = "clickhouse_murmurHash2_32" | ||
|
||
override val ckFuncNames: Array[String] = Array("murmurHash2_32") | ||
|
||
override def applyHash(input: Array[Any]): Long = HashUtils.toUInt32(hash.Murmurhash2_32(input)) | ||
} |
38 changes: 38 additions & 0 deletions
38
spark-3.4/clickhouse-spark/src/main/scala/xenon/clickhouse/func/MurmurHash3.scala
This file contains hidden or 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,38 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 xenon.clickhouse.func | ||
|
||
import xenon.clickhouse.hash | ||
import xenon.clickhouse.hash.HashUtils | ||
|
||
// https://github.com/ClickHouse/ClickHouse/blob/v23.5.3.24-stable/src/Functions/FunctionsHashing.h#L543 | ||
object MurmurHash3_64 extends MultiStringArgsHash { | ||
|
||
override protected def funcName: String = "clickhouse_murmurHash3_64" | ||
|
||
override val ckFuncNames: Array[String] = Array("murmurHash3_64") | ||
|
||
override def applyHash(input: Array[Any]): Long = hash.Murmurhash3_64(input) | ||
} | ||
|
||
// https://github.com/ClickHouse/ClickHouse/blob/v23.5.3.24-stable/src/Functions/FunctionsHashing.h#L519 | ||
object MurmurHash3_32 extends MultiStringArgsHash { | ||
|
||
override protected def funcName: String = "clickhouse_murmurHash3_32" | ||
|
||
override val ckFuncNames: Array[String] = Array("murmurHash3_32") | ||
|
||
override def applyHash(input: Array[Any]): Long = HashUtils.toUInt32(hash.Murmurhash3_32(input)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://github.com/databricks/scala-style-guide#traversal-and-zipwithindex, we should use while for performance-sensitive code.