Skip to content

Commit

Permalink
Calcite 1.30 implement CHAR function
Browse files Browse the repository at this point in the history
  • Loading branch information
gleonSun committed Feb 13, 2023
1 parent 6dd5345 commit 0e1f6b0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -6919,6 +6919,7 @@ SqlIdentifier ReservedFunctionName() :
| <AVG>
| <CARDINALITY>
| <CEILING>
| <CHAR>
| <CHAR_LENGTH>
| <CHARACTER_LENGTH>
| <COALESCE>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_REVERSE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_AND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BOOL_OR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHAR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CHR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COMPRESS;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONCAT2;
Expand Down Expand Up @@ -368,6 +369,8 @@ public class RexImpTable {
defineMethod(REPLACE, BuiltInMethod.REPLACE.method, NullPolicy.STRICT);
defineMethod(TRANSLATE3, BuiltInMethod.TRANSLATE3.method, NullPolicy.STRICT);
defineMethod(CHR, "chr", NullPolicy.STRICT);
// Calcite 1.30 removed SqlFunction CHAR, support this function
defineMethod(CHAR, BuiltInMethod.CHAR.method, NullPolicy.STRICT);
defineMethod(CHARACTER_LENGTH, BuiltInMethod.CHAR_LENGTH.method,
NullPolicy.STRICT);
defineMethod(CHAR_LENGTH, BuiltInMethod.CHAR_LENGTH.method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ public static int octetLength(ByteString s) {
return s.length();
}

/** Calcite 1.30 removed SqlFunction CHAR, support this function */
public static String charN(long n) {
if (n < 0) {
return null;
}
return String.valueOf(Character.toChars((int) (n % 256)));
}

/** SQL CHARACTER_LENGTH(string) function. */
public static int charLength(String s) {
if (s == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ private JdbcToInternalLookupTable() {
map.put("ATAN2", simple(SqlStdOperatorTable.ATAN2));
map.put("CBRT", simple(SqlStdOperatorTable.CBRT));
map.put("CEILING", simple(SqlStdOperatorTable.CEIL));
// Calcite 1.30 removed SqlFunction CHAR, support this function
map.put("CHAR", simple(SqlLibraryOperators.CHAR));
map.put("COS", simple(SqlStdOperatorTable.COS));
map.put("COT", simple(SqlStdOperatorTable.COT));
map.put("DEGREES", simple(SqlStdOperatorTable.DEGREES));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ private SqlLibraryOperators() {
OperandTypes.INTEGER,
SqlFunctionCategory.STRING);

// Calcite 1.30 removed SqlFunction CHAR, support this function
@LibraryOperator(libraries = {MYSQL, SPARK})
public static final SqlFunction CHAR =
new SqlFunction("CHAR",
SqlKind.OTHER_FUNCTION,
ReturnTypes.CHAR_FORCE_NULLABLE,
null,
OperandTypes.INTEGER,
SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {ORACLE})
public static final SqlFunction TANH =
new SqlFunction("TANH",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ public RelDataType inferReturnType(
public static final SqlReturnTypeInference CHAR =
explicit(SqlTypeName.CHAR);

/**
* Calcite 1.30 removed SqlFunction CHAR, support this function
* Type-inference strategy whereby the result type of a call is a nullable
* CHAR(1).
*/
public static final SqlReturnTypeInference CHAR_FORCE_NULLABLE =
ReturnTypes.cascade(ReturnTypes.CHAR, SqlTypeTransforms.FORCE_NULLABLE);

/**
* Type-inference strategy whereby the result type of a call is an Integer.
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ public enum BuiltInMethod {
SUBSTRING(SqlFunctions.class, "substring", String.class, int.class,
int.class),
OCTET_LENGTH(SqlFunctions.class, "octetLength", ByteString.class),
// Calcite 1.30 removed SqlFunction CHAR, support this function
CHAR(SqlFunctions.class, "charN", long.class),
CHAR_LENGTH(SqlFunctions.class, "charLength", String.class),
STRING_CONCAT(SqlFunctions.class, "concat", String.class, String.class),
MULTI_STRING_CONCAT(SqlFunctions.class, "concatMulti", String[].class),
Expand Down
19 changes: 19 additions & 0 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,25 @@ protected static Calendar getCalendarNotTooNear(int timeUnit) {

}

// Calcite 1.30 removed SqlFunction CHAR, support this function
@Test void testChar() {
final SqlOperatorFixture f0 = fixture()
.setFor(SqlLibraryOperators.CHAR, VM_FENNEL, VM_JAVA);
f0.checkFails("^char(97)^",
"No match found for function signature CHAR\\(<NUMERIC>\\)", false);
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.MYSQL);
f.checkScalar("char(null)", isNullValue(), "CHAR(1)");
f.checkScalar("char(-1)", isNullValue(), "CHAR(1)");
f.checkScalar("char(97)", "a", "CHAR(1)");
f.checkScalar("char(48)", "0", "CHAR(1)");
f.checkScalar("char(0)", String.valueOf('\u0000'), "CHAR(1)");
f.checkFails("^char(97.1)^",
"Cannot apply 'CHAR' to arguments of type 'CHAR\\(<DECIMAL\\(3, 1\\)>\\)'\\. "
+ "Supported form\\(s\\): 'CHAR\\(<INTEGER>\\)'",
false);
f.checkScalarExact("char('abc')", "CHAR(1)", "CHAR");
}

@Test void testChr() {
final SqlOperatorFixture f0 = fixture()
.setFor(SqlLibraryOperators.CHR, VM_FENNEL, VM_JAVA);
Expand Down

0 comments on commit 0e1f6b0

Please sign in to comment.