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 @@ -19,8 +19,7 @@
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.RelToSqlConverterUtil;

/**
* A <code>SqlDialect</code> implementation for the Apache Derby database.
Expand All @@ -40,9 +39,7 @@ public DerbySqlDialect(Context context) {
final int rightPrec) {
switch (call.getKind()) {
case CHAR_LENGTH:
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, lengthCall, leftPrec, rightPrec);
RelToSqlConverterUtil.convertCharLengthToLength(writer, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -196,6 +197,9 @@ public OracleSqlDialect(Context context) {
}
writer.endFunCall(frame);
break;
case CHAR_LENGTH:
RelToSqlConverterUtil.convertCharLengthToLength(writer, call, leftPrec, rightPrec);
break;
case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlArrayValueConstructor;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlMapValueConstructor;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
Expand Down Expand Up @@ -252,9 +251,7 @@ private static void unparseUsingLimit(SqlWriter writer, @Nullable SqlNode offset
}
break;
case CHAR_LENGTH:
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, lengthCall, leftPrec, rightPrec);
RelToSqlConverterUtil.convertCharLengthToLength(writer, call, leftPrec, rightPrec);
break;
case TRIM:
RelToSqlConverterUtil.unparseTrimLR(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -62,9 +63,7 @@ public SnowflakeSqlDialect(Context context) {
super.unparseCall(writer, bitOrCall, leftPrec, rightPrec);
break;
case CHAR_LENGTH:
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, lengthCall, leftPrec, rightPrec);
RelToSqlConverterUtil.convertCharLengthToLength(writer, call, leftPrec, rightPrec);
break;
case ENDS_WITH:
SqlCall endsWithCall = SqlLibraryOperators.ENDSWITH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -58,9 +56,7 @@ public SqliteSqlDialect(SqlDialect.Context context) {
int leftPrec, int rightPrec) {
switch (call.getKind()) {
case CHAR_LENGTH:
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, lengthCall, leftPrec, rightPrec);
RelToSqlConverterUtil.convertCharLengthToLength(writer, call, leftPrec, rightPrec);
break;
case TRIM:
RelToSqlConverterUtil.unparseTrimLR(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlTypeNameSpec;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.fun.SqlTrimFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
Expand Down Expand Up @@ -78,6 +79,16 @@ public static void unparseHiveTrim(
}
}

/**
* Convert CHAR_LENGTH to LENGTH for some Dialects such as Oracle which not support CHAR_LENGTH.
*/
public static void convertCharLengthToLength(SqlWriter writer, SqlCall call, int leftPrec,
int rightPrec) {
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
lengthCall.getOperator().unparse(writer, lengthCall, leftPrec, rightPrec);
}

/**
* For usage of TRIM(LEADING 'A' FROM 'ABCA') convert to TRIM, LTRIM and RTRIM,
* can refer to BigQuery and Presto documents:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8274,6 +8274,7 @@ private void checkLiteral2(String expression, String expected) {
final String expected = "SELECT LENGTH(\"brand_name\")\n"
+ "FROM \"foodmart\".\"product\"";
sql(query)
.withOracle().ok(expected)
.withPresto().ok(expected)
.withTrino().ok(expected);
}
Expand Down
Loading