mihaibudiu commented on code in PR #4074:
URL: https://github.com/apache/calcite/pull/4074#discussion_r1881167456
##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -2916,6 +2916,46 @@ public static long bitCount(ByteString b) {
return bitsSet;
}
+ /** Helper function for implementing MySQL <code>BIT_COUNT</code>. Counts
the number
+ * of bits set in a boolean value. */
+ public static long bitCountMySQL(Boolean b) {
+ return Long.bitCount(b ? 1L : 0L);
+ }
+
+ /** Helper function for implementing MySQL <code>BIT_COUNT</code>. Counts
the number
+ * of bits set in a string value. */
Review Comment:
if you can point to the MySQL documentation where this is specified, please
add a link to the comments.
##########
core/src/test/resources/sql/functions.iq:
##########
@@ -31,6 +31,80 @@ select bit_count(8);
!ok
+select bit_count('8');
++--------+
+| EXPR$0 |
++--------+
+| 1 |
++--------+
+(1 row)
+
+!ok
+
+select bit_count('a');
++--------+
+| EXPR$0 |
++--------+
+| 0 |
++--------+
+(1 row)
+
+!ok
+
+select bit_count('');
++--------+
+| EXPR$0 |
++--------+
+| 0 |
++--------+
+(1 row)
+
+!ok
+
+select bit_count(null);
++--------+
+| EXPR$0 |
++--------+
+| |
++--------+
+(1 row)
+
+!ok
+
+select bit_count(1 + 1);
++--------+
+| EXPR$0 |
++--------+
+| 1 |
++--------+
+(1 row)
+
+!ok
+
+select bit_count(true);
++--------+
+| EXPR$0 |
++--------+
+| 1 |
++--------+
+(1 row)
+
+!ok
+
+select bit_count(joinedat), bit_count(joinetime), bit_count(joinetimestamp)
from emps_date_time ORDER BY empno;
Review Comment:
have these been validated with MySQL?
if yes, please add a comment about this in the iq file.
##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java:
##########
@@ -2690,6 +2690,48 @@ private static class ContainsSubstrImplementor extends
AbstractRexCallImplemento
}
}
+ /** Implementor for MYSQL {@code BIT_COUNT} function. */
+ private static class BitCountMySQLImplementor extends
AbstractRexCallImplementor {
+ BitCountMySQLImplementor() {
+ super("bitCount", NullPolicy.STRICT, false);
+ }
+
+ @Override Expression implementSafe(final RexToLixTranslator translator,
+ final RexCall call, final List<Expression> argValueList) {
+ Expression expr = argValueList.get(0);
+ RelDataType relDataType = call.getOperands().get(0).getType();
+ if (SqlTypeUtil.isNull(relDataType)) {
+ return argValueList.get(0);
+ }
+ // In MySQL, BIT_COUNT(TIMESTAMP '1996-08-03 16:22:34') is converted to
+ // BIT_COUNT('19960803162234') for calculation, so the internal int value
+ // needs to be converted to DATE/TIME and TIMESTAMP.
+ SqlTypeName type = relDataType.getSqlTypeName();
+ switch (type) {
+ case VARBINARY:
+ case BINARY:
+ return Expressions.call(SqlFunctions.class, "bitCount", expr);
+ case DATE:
+ expr = Expressions.call(BuiltInMethod.INTERNAL_TO_DATE.method, expr);
+ break;
+ case TIME:
+ case TIME_WITH_LOCAL_TIME_ZONE:
+ case TIME_TZ:
+ expr = Expressions.call(BuiltInMethod.INTERNAL_TO_TIME.method, expr);
+ break;
+ case TIMESTAMP:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
Review Comment:
Perhaps tests with the local timezone are difficult, but I don't see tests
with timezone either.
It would be great to cover all these cases with tests.
--
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]