xuzifu666 commented on code in PR #4180:
URL: https://github.com/apache/calcite/pull/4180#discussion_r1944511371
##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -369,6 +369,23 @@ public static String hex(String value) {
return Hex.encodeHexString(value.getBytes(UTF_8));
}
+ /** SQL BIN(long) function. */
+ public static String bin(long value) {
+ int zeros = Long.numberOfLeadingZeros(value);
+ if (zeros == Long.SIZE) {
+ return "0";
+ } else {
+ int length = Long.SIZE - zeros;
+ byte[] bytes = new byte[length];
+ do {
+ bytes[--length] = (byte) ((value & 0x1) == 1 ? '1' : '0');
Review Comment:
Done
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5756,6 +5756,46 @@ void testBitGetFunc(SqlOperatorFixture f, String
functionName) {
f.checkNull("from_base32(cast (null as varchar))");
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6815">[CALCITE-6815]
+ * Add bin function (enabled in Hive and Spark library)</a>. */
+ @Test void testBin() {
+ final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.BIN);
+ f0.checkFails("^bin(x'')^",
+ "No match found for function signature BIN\\(<BINARY>\\)",
+ false);
+ final List<SqlLibrary> libraries =
+ ImmutableList.of(SqlLibrary.SPARK, SqlLibrary.HIVE);
+ final Consumer<SqlOperatorFixture> consumer = f -> {
+ f.checkString("bin(12)",
+ "1100",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(1)",
+ "1",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(01)",
+ "1",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(000)",
+ "0",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(-000)",
+ "0",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(-11)",
+ "1111111111111111111111111111111111111111111111111111111111110101",
+ "VARCHAR NOT NULL");
+ f.checkString("bin(-1)",
+
"1111111111111111111111111111111111111111111111111111111111111111",
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]