mihaibudiu commented on code in PR #4339:
URL: https://github.com/apache/calcite/pull/4339#discussion_r2064038850
##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -3226,6 +3226,21 @@ public static ByteString bitNot(ByteString b) {
return new ByteString(result);
}
+ public static int leftShift(int a, int b) {
+ return a << b;
+ }
+
+ public static long leftShift(long a, int b) {
+ return a << b;
+ }
+
+ public static long leftShift(int a, long b) {
+ return a << b;
+ }
+
+ public static long leftShift(Long a, Long b) {
Review Comment:
will this cover all supported types, like TINYINT?
What is the semantics of shift when the shift amount is negative or larger
than the value size?
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -16294,6 +16299,51 @@ private static void
checkLogicalOrFunc(SqlOperatorFixture f) {
f.checkAgg("logical_or(x)", values4, isNullValue());
}
+ @Test void testBitXorOperatorScalarFunc() throws SqlParseException {
+ String sql = "SELECT 5 ^ 3 ";
+ SqlNode sqlNode = SqlParser.create(sql,
SqlParser.Config.DEFAULT).parseStmt();
+
+ assertInstanceOf(SqlSelect.class, sqlNode);
+ SqlSelect select = (SqlSelect) sqlNode;
+
+ SqlNode selectItem = select.getSelectList().get(0);
+ assertInstanceOf(SqlBasicCall.class, selectItem);
+ SqlBasicCall call = (SqlBasicCall) selectItem;
+ assertEquals(SqlStdOperatorTable.BITXOR_OPERATOR, call.getOperator());
+ assertEquals(2, call.getOperandList().size());
+ }
+
+ @Test void testLeftShiftScalarFunc() {
+ final SqlOperatorFixture f = fixture();
+ f.setFor(SqlStdOperatorTable.LEFTSHIFT_OPERATOR, VmName.EXPAND);
+
+
+ // Basic test cases
+ f.checkScalar("2 << 2", "8", "INTEGER NOT NULL");
Review Comment:
can you test with values out of range
- overflows caused by <<
- shift amount too large
- shift amount negative
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java:
##########
@@ -1299,6 +1299,33 @@ public class SqlStdOperatorTable extends
ReflectiveSqlOperatorTable {
ReturnTypes.LARGEST_INT_OR_FIRST_NON_NULL,
OperandTypes.INTEGER_INTEGER.or(OperandTypes.BINARY_BINARY));
+ /**
+ * <code>{@code ^}</code> operator.
+ */
+ public static final SqlBinaryOperator BITXOR_OPERATOR =
+ new SqlMonotonicBinaryOperator(
+ "^",
+ SqlKind.BITXOR,
+ 50, // Precedence between addition (40) and multiplication
(60)
+ true,
+ ReturnTypes.LARGEST_INT_OR_FIRST_NON_NULL, // Returns same type as
inputs when nullable
+ InferTypes.FIRST_KNOWN,
+ OperandTypes.INTEGER_INTEGER.or(OperandTypes.BINARY_BINARY));
+ // Both operands should support bitwise operations
+
+ /**
+ * <code>{@code <<}</code> operator.
+ */
+ public static final SqlBinaryOperator LEFTSHIFT_OPERATOR =
+ new SqlBinaryOperator("<<", // Operator name
+ SqlKind.OTHER_FUNCTION, // SqlKind
+ 32,
+ true,
+ ReturnTypes.LARGEST_INT_OR_FIRST_NON_NULL,
+ null,
+ OperandTypes.family(SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER));
Review Comment:
are you sure that any integer type is fine? This creates a very large number
of configurations. I would expect that at least the shift amount does not
support all integer types.
--
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]