mihaibudiu commented on code in PR #3531:
URL: https://github.com/apache/calcite/pull/3531#discussion_r1397708532
##########
site/_docs/reference.md:
##########
@@ -1248,18 +1248,23 @@ The operator precedence and associativity, highest to
lowest.
| . | left
| :: | left
| [ ] (collection element) | left
-| + - (unary plus, minus) | right
+| \! | right
+| + - ~(unary plus, minus) | right
+| ^ | left
| * / % || | left
| + - | left
+| <<, >> | left
+| & | left
+| \| | left
| BETWEEN, IN, LIKE, SIMILAR, OVERLAPS, CONTAINS etc. | -
| < > = <= >= <> != <=> | left
| IS NULL, IS FALSE, IS NOT TRUE etc. | -
| NOT | right
| AND | left
| OR | left
-Note that `::`,`<=>` is dialect-specific, but is shown in this table for
-completeness.
+Note that `::`,`!`,`~`,`^`,`<<`,`>>`,`&`,`|`,`<=>` is dialect-specific,
Review Comment:
are dialect specific.
##########
gradle.properties:
##########
@@ -89,14 +89,14 @@ cassandra-all.version=4.0.1
cassandra-java-driver-core.version=4.13.0
cassandra-unit.version=4.3.1.0
chinook-data-hsqldb.version=0.2
-commons-codec.version=1.16.0
-commons-dbcp2.version=2.11.0
-commons-io.version=2.15.0
-commons-lang3.version=3.13.0
+commons-codec.version=1.13
Review Comment:
These changes are unrelated to this PR, I don't think they should be here.
Are you missing a rebase?
##########
babel/src/test/java/org/apache/calcite/test/BabelTest.java:
##########
@@ -239,6 +239,47 @@ private void checkInfixCast(Statement statement, String
typeName, int sqlType)
"EXPR$0=10\n");
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-5087">[CALCITE-5087]
+ * Support bitwise operators</a>. */
+ @Test void testBitwiseOperator() {
+ final SqlValidatorFixture f = Fixtures.forValidator()
+ .withParserConfig(c -> c.withParserFactory(SqlBabelParserImpl.FACTORY))
+ .withOperatorTable(operatorTableFor(SqlLibrary.MYSQL));
+
+ f.withSql("select 1 | 1")
+ .ok()
+ .type("RecordType(BIGINT NOT NULL EXPR$0) NOT NULL");
+
+ f.withSql("select x'65' & x'77'")
+ .ok()
+ .type("RecordType(BIGINT NOT NULL EXPR$0) NOT NULL");
+
+ f.withSql("select x'65' ^^ 1")
Review Comment:
This seems wrong, the output type should be BINARY or VARBINARY.
Or perhaps shifts of BINARY values should be forbidden.
##########
babel/src/test/java/org/apache/calcite/test/BabelTest.java:
##########
@@ -239,6 +239,47 @@ private void checkInfixCast(Statement statement, String
typeName, int sqlType)
"EXPR$0=10\n");
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-5087">[CALCITE-5087]
+ * Support bitwise operators</a>. */
+ @Test void testBitwiseOperator() {
+ final SqlValidatorFixture f = Fixtures.forValidator()
+ .withParserConfig(c -> c.withParserFactory(SqlBabelParserImpl.FACTORY))
+ .withOperatorTable(operatorTableFor(SqlLibrary.MYSQL));
+
+ f.withSql("select 1 | 1")
Review Comment:
As commented below, BIGINT seems like the wrong type.
I would also add some tests that require implicit casts, like operations
between int and tinyint values, to make sure that the type coercion rules work
correctly.
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -2173,6 +2175,80 @@ private static RelDataType
deriveTypeMapFromEntries(SqlOperatorBinding opBinding
InferTypes.FIRST_KNOWN,
OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
+ /** The Bitwise AND "&" OPERATOR used by MySQL, for example
+ * {@code 1&1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_AND =
+ new SqlBitwiseBinaryOperator(
+ "&",
+ SqlKind.BITWISE_AND,
+ 36,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise OR "|" OPERATOR used by MySQL, for example
+ * {@code 1|1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_OR =
+ new SqlBitwiseBinaryOperator(
+ "|",
+ SqlKind.BITWISE_OR,
+ 34,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise XOR "^" OPERATOR used by MySQL, for example
+ * {@code 1^1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_XOR =
+ new SqlBitwiseBinaryOperator(
+ "^",
+ SqlKind.BITWISE_XOR,
+ 62,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise Left Shift "<<" OPERATOR used by MySQL, for example
+ * {@code 1 << 1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_LEFT_SHIFT =
+ new SqlBitwiseBinaryOperator(
+ "<<",
+ SqlKind.BITWISE_LEFT_SHIFT,
+ 38,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise Right Shift ">>" OPERATOR used by MySQL, for example
+ * {@code 1 >> 1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_RIGHT_SHIFT =
+ new SqlBitwiseBinaryOperator(
+ ">>",
+ SqlKind.BITWISE_RIGHT_SHIFT,
+ 38,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise inversion "~" OPERATOR used by MySQL, for example
+ * {@code ~1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlPrefixOperator BITWISE_INVERSION =
+ new SqlPrefixOperator(
+ "~",
+ SqlKind.BITWISE_RIGHT_SHIFT,
+ 80,
+ ReturnTypes.BIGINT_NULLABLE,
Review Comment:
why BIGINT and not the type of the operand?
##########
core/src/main/java/org/apache/calcite/sql/SqlBitwiseBinaryOperator.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.fun.SqlLibraryOperators;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <code>SqlBitwiseBinaryOperator</code> is a binary bitwise operator.
+ */
+public class SqlBitwiseBinaryOperator extends SqlBinaryOperator {
+
+ public SqlBitwiseBinaryOperator(
+ String name,
+ SqlKind kind,
+ int prec,
+ @Nullable SqlOperandTypeChecker operandTypeChecker) {
+ super(
+ name,
+ kind,
+ prec,
+ true,
+ ReturnTypes.BIGINT_NULLABLE,
Review Comment:
why bigint?
I think it should be the type of the operands - which should be coerced to
have the same type.
Please note that some dialects such as MySQL allow some of these operations
on BINARY types as well.
It's fine to start with just integers for now, but the solution should
extend to BINARY eventually.
##########
core/src/main/java/org/apache/calcite/sql/SqlBitwiseBinaryOperator.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.fun.SqlLibraryOperators;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <code>SqlBitwiseBinaryOperator</code> is a binary bitwise operator.
+ */
+public class SqlBitwiseBinaryOperator extends SqlBinaryOperator {
+
+ public SqlBitwiseBinaryOperator(
+ String name,
+ SqlKind kind,
+ int prec,
+ @Nullable SqlOperandTypeChecker operandTypeChecker) {
+ super(
+ name,
+ kind,
+ prec,
+ true,
+ ReturnTypes.BIGINT_NULLABLE,
Review Comment:
BTW: Calcite already has aggregation functions BIT_AND and so on, and these
return the same type as the input type. So the typing for these operators
should be consistent with the aggregation functions.
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -2173,6 +2175,80 @@ private static RelDataType
deriveTypeMapFromEntries(SqlOperatorBinding opBinding
InferTypes.FIRST_KNOWN,
OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
+ /** The Bitwise AND "&" OPERATOR used by MySQL, for example
+ * {@code 1&1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_AND =
+ new SqlBitwiseBinaryOperator(
+ "&",
+ SqlKind.BITWISE_AND,
+ 36,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise OR "|" OPERATOR used by MySQL, for example
+ * {@code 1|1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_OR =
+ new SqlBitwiseBinaryOperator(
+ "|",
+ SqlKind.BITWISE_OR,
+ 34,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise XOR "^" OPERATOR used by MySQL, for example
+ * {@code 1^1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_XOR =
+ new SqlBitwiseBinaryOperator(
+ "^",
+ SqlKind.BITWISE_XOR,
+ 62,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise Left Shift "<<" OPERATOR used by MySQL, for example
+ * {@code 1 << 1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_LEFT_SHIFT =
+ new SqlBitwiseBinaryOperator(
+ "<<",
+ SqlKind.BITWISE_LEFT_SHIFT,
+ 38,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise Right Shift ">>" OPERATOR used by MySQL, for example
+ * {@code 1 >> 1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlOperator BITWISE_RIGHT_SHIFT =
+ new SqlBitwiseBinaryOperator(
+ ">>",
+ SqlKind.BITWISE_RIGHT_SHIFT,
+ 38,
+ OperandTypes.ANY_ANY);
+
+ /** The Bitwise inversion "~" OPERATOR used by MySQL, for example
+ * {@code ~1}. */
+ @LibraryOperator(libraries = {MYSQL})
+ public static final SqlPrefixOperator BITWISE_INVERSION =
+ new SqlPrefixOperator(
+ "~",
+ SqlKind.BITWISE_RIGHT_SHIFT,
Review Comment:
is this a typo?
--
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]