Skip to content

Commit 2ddfb0e

Browse files
xuzifu666NobiGo
authored andcommitted
[CALCITE-6821] Support crc32 function for Hive and Spark library
1 parent 929d9b4 commit 2ddfb0e

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COSD;
189189
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COSH;
190190
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COTH;
191+
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CRC32;
191192
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CSC;
192193
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CSCH;
193194
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CURRENT_DATETIME;
@@ -697,6 +698,7 @@ void populate1() {
697698
defineMethod(FROM_HEX, BuiltInMethod.FROM_HEX.method, NullPolicy.STRICT);
698699
defineMethod(BIN, BuiltInMethod.BIN.method, NullPolicy.STRICT);
699700
defineMethod(MD5, BuiltInMethod.MD5.method, NullPolicy.STRICT);
701+
defineMethod(CRC32, BuiltInMethod.CRC32.method, NullPolicy.STRICT);
700702
defineMethod(SHA1, BuiltInMethod.SHA1.method, NullPolicy.STRICT);
701703
defineMethod(SHA256, BuiltInMethod.SHA256.method, NullPolicy.STRICT);
702704
defineMethod(SHA512, BuiltInMethod.SHA512.method, NullPolicy.STRICT);

core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import java.util.regex.Matcher;
134134
import java.util.regex.Pattern;
135135
import java.util.regex.PatternSyntaxException;
136+
import java.util.zip.CRC32;
136137

137138
import static org.apache.calcite.config.CalciteSystemProperty.FUNCTION_LEVEL_CACHE_MAX_SIZE;
138139
import static org.apache.calcite.linq4j.Nullness.castNonNull;
@@ -386,6 +387,24 @@ public static String bin(long value) {
386387
}
387388
}
388389

390+
/** SQL CRC32(string) function. */
391+
public static long crc32(String value) {
392+
final CRC32 crc32 = new CRC32();
393+
crc32.reset();
394+
byte[] bytes = value.getBytes(UTF_8);
395+
crc32.update(bytes, 0, bytes.length);
396+
return crc32.getValue();
397+
}
398+
399+
/** SQL CRC32(string) function for binary string. */
400+
public static long crc32(ByteString value) {
401+
final CRC32 crc32 = new CRC32();
402+
crc32.reset();
403+
byte[] bytes = value.getBytes();
404+
crc32.update(bytes, 0, bytes.length);
405+
return crc32.getValue();
406+
}
407+
389408
/** SQL MD5(string) function. */
390409
public static String md5(String string) {
391410
return DigestUtils.md5Hex(string.getBytes(UTF_8));

core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,13 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
25182518
OperandTypes.STRING.or(OperandTypes.BINARY),
25192519
SqlFunctionCategory.STRING);
25202520

2521+
@LibraryOperator(libraries = {SPARK, HIVE})
2522+
public static final SqlFunction CRC32 =
2523+
SqlBasicFunction.create("CRC32",
2524+
ReturnTypes.BIGINT_NULLABLE,
2525+
OperandTypes.STRING.or(OperandTypes.BINARY),
2526+
SqlFunctionCategory.STRING);
2527+
25212528
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK, HIVE})
25222529
public static final SqlFunction SHA1 =
25232530
SqlBasicFunction.create("SHA1",

core/src/main/java/org/apache/calcite/util/BuiltInMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public enum BuiltInMethod {
440440
FROM_HEX(SqlFunctions.class, "fromHex", String.class),
441441
BIN(SqlFunctions.class, "bin", long.class),
442442
MD5(SqlFunctions.class, "md5", String.class),
443+
CRC32(SqlFunctions.class, "crc32", String.class),
443444
SHA1(SqlFunctions.class, "sha1", String.class),
444445
SHA256(SqlFunctions.class, "sha256", String.class),
445446
SHA512(SqlFunctions.class, "sha512", String.class),

site/_docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,7 @@ In the following:
28422842
| p | COSD(numeric) | Returns the cosine of *numeric* in degrees as a double. Returns NaN if *numeric* is NaN. Fails if *numeric* is greater than the maximum double value.
28432843
| * | COSH(numeric) | Returns the hyperbolic cosine of *numeric*
28442844
| * | COTH(numeric) | Returns the hyperbolic cotangent of *numeric*
2845+
| s h | CRC32(string) | Calculates a cyclic redundancy check value for string or binary argument and returns bigint value
28452846
| * | CSC(numeric) | Returns the cosecant of *numeric* in radians
28462847
| * | CSCH(numeric) | Returns the hyperbolic cosecant of *numeric*
28472848
| b | CURRENT_DATETIME([ timeZone ]) | Returns the current time as a TIMESTAMP from *timezone*

testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5760,8 +5760,7 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
57605760
* <a href="https://issues.apache.org/jira/browse/CALCITE-6815">[CALCITE-6815]
57615761
* Add bin function (enabled in Hive and Spark library)</a>. */
57625762
@Test void testBin() {
5763-
// final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.BIN);
5764-
final SqlOperatorFixture f0 = Fixtures.forOperators(true).setFor(SqlLibraryOperators.BIN);
5763+
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.BIN);
57655764
f0.checkFails("^bin(x'')^",
57665765
"No match found for function signature BIN\\(<BINARY>\\)",
57675766
false);
@@ -5797,6 +5796,31 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
57975796
f0.forEachLibrary(libraries, consumer);
57985797
}
57995798

5799+
/** Test case for
5800+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6821">[CALCITE-6821]
5801+
* Add crc32 function (enabled in Hive and Spark library)</a>. */
5802+
@Test void testCRC32() {
5803+
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.CRC32);
5804+
final List<SqlLibrary> libraries =
5805+
ImmutableList.of(SqlLibrary.SPARK, SqlLibrary.HIVE);
5806+
final Consumer<SqlOperatorFixture> consumer = f -> {
5807+
f.checkString("crc32('ABC')",
5808+
"2743272264",
5809+
"BIGINT NOT NULL");
5810+
f.checkString("crc32(x'414243')",
5811+
"2743272264",
5812+
"BIGINT NOT NULL");
5813+
f.checkString("crc32('')",
5814+
"0",
5815+
"BIGINT NOT NULL");
5816+
f.checkString("crc32(x'')",
5817+
"0",
5818+
"BIGINT NOT NULL");
5819+
f.checkNull("crc32(null)");
5820+
};
5821+
f0.forEachLibrary(libraries, consumer);
5822+
}
5823+
58005824
@Test void testMd5() {
58015825
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.MD5);
58025826
f0.checkFails("^md5(x'')^",

0 commit comments

Comments
 (0)