Skip to content

Commit

Permalink
[CALCITE-6805] Support hex function for Hive and Spark Library
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyu authored and caicancai committed Jan 30, 2025
1 parent b901b41 commit 8539f51
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FROM_BASE64;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.FROM_HEX;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.GETBIT;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.HEX;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ILIKE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.IS_INF;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.IS_NAN;
Expand Down Expand Up @@ -686,6 +687,7 @@ void populate1() {
defineMethod(FROM_BASE64, BuiltInMethod.FROM_BASE64.method, NullPolicy.STRICT);
defineMethod(TO_BASE32, BuiltInMethod.TO_BASE32.method, NullPolicy.STRICT);
defineMethod(FROM_BASE32, BuiltInMethod.FROM_BASE32.method, NullPolicy.STRICT);
defineMethod(HEX, BuiltInMethod.HEX.method, NullPolicy.STRICT);
defineMethod(TO_HEX, BuiltInMethod.TO_HEX.method, NullPolicy.STRICT);
defineMethod(FROM_HEX, BuiltInMethod.FROM_HEX.method, NullPolicy.STRICT);
defineMethod(MD5, BuiltInMethod.MD5.method, NullPolicy.STRICT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ public static String toHex(ByteString byteString) {
return Hex.encodeHexString(byteString.getBytes());
}

/** SQL HEX(varchar) function. */
public static String hex(String value) {
return Hex.encodeHexString(value.getBytes(UTF_8));
}

/** SQL MD5(string) function. */
public static String md5(String string) {
return DigestUtils.md5Hex(string.getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,16 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.BINARY,
SqlFunctionCategory.STRING);

/**
* The "HEX(string)" function; converts {@code string} into a hexadecimal varchar.
*/
@LibraryOperator(libraries = {HIVE, SPARK})
public static final SqlFunction HEX =
SqlBasicFunction.create("HEX",
ReturnTypes.VARCHAR_NULLABLE,
OperandTypes.CHARACTER,
SqlFunctionCategory.STRING);

/** The "FORMAT_NUMBER(value, decimalOrFormat)" function. */
@LibraryOperator(libraries = {HIVE, SPARK})
public static final SqlFunction FORMAT_NUMBER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ public enum BuiltInMethod {
FROM_BASE64(SqlFunctions.class, "fromBase64", String.class),
TO_BASE32(SqlFunctions.class, "toBase32", String.class),
FROM_BASE32(SqlFunctions.class, "fromBase32", String.class),
HEX(SqlFunctions.class, "hex", String.class),
TO_HEX(SqlFunctions.class, "toHex", ByteString.class),
FROM_HEX(SqlFunctions.class, "fromHex", String.class),
MD5(SqlFunctions.class, "md5", String.class),
Expand Down
1 change: 1 addition & 0 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,7 @@ In the following:
| b | FROM_BASE32(string) | Returns the decoded result of a base-32 *string* as a string
| m | TO_BASE64(string) | Converts the *string* to base-64 encoded form and returns a encoded string
| b m | FROM_BASE64(string) | Returns the decoded result of a base-64 *string* as a string. If the input argument is an invalid base-64 *string* the function returns `NULL`
| h s | HEX(string) | Converts *string* into a hexadecimal varchar
| b | TO_HEX(binary) | Converts *binary* into a hexadecimal varchar
| b | FROM_HEX(varchar) | Converts a hexadecimal-encoded *varchar* into bytes
| b o p r s | LTRIM(string) | Returns *string* with all blanks removed from the start
Expand Down
29 changes: 29 additions & 0 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5823,6 +5823,35 @@ void testBitGetFunc(SqlOperatorFixture f, String functionName) {
f0.forEachLibrary(libraries, consumer);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6805">[CALCITE-6805]
* Add hex function (enabled in Hive, Spark library)</a>. */
@Test void testHex() {
SqlOperatorFixture sqlOperatorFixture = fixture();
final SqlOperatorFixture f0 = sqlOperatorFixture.setFor(SqlLibraryOperators.HEX);
f0.checkFails("^hex('')^",
"No match found for function signature HEX\\(<CHARACTER>\\)",
false);
final Consumer<SqlOperatorFixture> consumer = f -> {
f.checkString("hex('abc')",
"616263",
"VARCHAR NOT NULL");
f.checkString("hex('1')",
"31",
"VARCHAR NOT NULL");
f.checkString("hex('0')",
"30",
"VARCHAR NOT NULL");
f.checkString("hex('-1')",
"2d31",
"VARCHAR NOT NULL");
f.checkString("hex('')", "", "VARCHAR NOT NULL");
f.checkNull("hex(null)");
f.checkNull("hex(cast(null as varbinary))");
};
f0.forEachLibrary(list(SqlLibrary.HIVE, SqlLibrary.SPARK), consumer);
}

@Test void testToHex() {
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.TO_HEX);
f0.checkFails("^to_hex(x'')^",
Expand Down

0 comments on commit 8539f51

Please sign in to comment.