Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import static org.apache.calcite.linq4j.tree.ExpressionType.OrElse;
import static org.apache.calcite.linq4j.tree.ExpressionType.Subtract;
import static org.apache.calcite.linq4j.tree.ExpressionType.UnaryPlus;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BIT_NOT;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.DAYNAME;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.DIFFERENCE;
Expand Down Expand Up @@ -406,6 +407,9 @@ public Expression implement(RexToLixTranslator translator,

map.put(PI, (translator, call, nullAs) -> Expressions.constant(Math.PI));

// bitwise
defineMethod(BIT_NOT, BuiltInMethod.BIT_NOT.method, NullPolicy.STRICT);

// datetime
defineImplementor(DATETIME_PLUS, NullPolicy.STRICT,
new DatetimeArithmeticImplementor(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,12 @@ public static long bitAnd(long b0, long b1) {
return b0 & b1;
}

// ~
/** Helper function for implementing <code>BIT_NOT</code> */
public static long bitNot(long b) {
return ~b;
}

// |
/** Helper function for implementing <code>BIT_OR</code> */
public static long bitOr(long b0, long b1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ private SqlLibraryOperators() {
OperandTypes.STRING_STRING,
SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {MYSQL})
public static final SqlFunction BIT_NOT =
new SqlFunction(
"BIT_NOT",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG0,
null,
OperandTypes.INTEGER,
SqlFunctionCategory.NUMERIC);

/** The "CONCAT(arg, ...)" function that concatenates strings.
* For example, "CONCAT('a', 'bc', 'd')" returns "abcd". */
@LibraryOperator(libraries = {MYSQL, POSTGRESQL, ORACLE})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public enum BuiltInMethod {
LESSER(SqlFunctions.class, "lesser", Comparable.class, Comparable.class),
GREATER(SqlFunctions.class, "greater", Comparable.class, Comparable.class),
BIT_AND(SqlFunctions.class, "bitAnd", long.class, long.class),
BIT_NOT(SqlFunctions.class, "bitNot", long.class),
BIT_OR(SqlFunctions.class, "bitOr", long.class, long.class),
BIT_XOR(SqlFunctions.class, "bitXor", long.class, long.class),
MODIFIABLE_TABLE_GET_MODIFIABLE_COLLECTION(ModifiableTable.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8810,6 +8810,26 @@ protected void checkAggType(SqlTester tester, String expr, String type) {
tester.checkAgg("bit_and(x)", values, 2, 0);
}

@Test public void testBitNotFunc() {
SqlTester tester = tester(SqlLibrary.MYSQL);
tester.checkFails("bit_not(^*^)", "Unknown identifier '\\*'", false);
tester.checkType("bit_not(1)", "INTEGER NOT NULL");
tester.checkType("bit_not(CAST(2 AS TINYINT))", "TINYINT NOT NULL");
tester.checkType("bit_not(CAST(2 AS SMALLINT))", "SMALLINT NOT NULL");
tester.checkFails("^bit_not(1.2)^",
"Cannot apply 'BIT_NOT' to arguments of type 'BIT_NOT\\(<DECIMAL\\(2, 1\\)>\\)'\\. Supported form\\(s\\): 'BIT_NOT\\(<INTEGER>\\)'",
false);
tester.checkFails(
"^bit_not()^",
"Invalid number of arguments to function 'BIT_NOT'. Was expecting 1 arguments",
false);
tester.checkFails(
"^bit_not(1, 2)^",
"Invalid number of arguments to function 'BIT_NOT'. Was expecting 1 arguments",
false);
tester.checkScalar("bit_not(3)", "-4", "INTEGER NOT NULL");
}

@Test public void testBitOrFunc() {
tester.setFor(SqlStdOperatorTable.BIT_OR, VM_FENNEL, VM_JAVA);
tester.checkFails("bit_or(^*^)", "Unknown identifier '\\*'", false);
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/resources/sql/functions.iq
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
!use mysqlfunc
!set outputformat mysql

# BIT_NOT
select bit_not(3);
+--------+
| EXPR$0 |
+--------+
| -4 |
+--------+
(1 row)

!ok

# MATH Functions

# CBRT
Expand Down
1 change: 1 addition & 0 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,7 @@ semantics.
| C | Operator syntax | Description
|:- |:-----------------------------------------------|:-----------
| p | expr :: type | Casts *expr* to *type*
| m | BIT_NOT(value) | Returns the bitwise NOT of non-null *numeric*
| o | CHR(integer) | Returns the character having the binary equivalent to *integer* as a CHAR value
| m o p | CONCAT(string [, string ]*) | Concatenates two or more strings
| p | CONVERT_TIMEZONE(tz1, tz2, datetime) | Converts the timezone of *datetime* from *tz1* to *tz2*
Expand Down