Skip to content

Commit

Permalink
[CALCITE-6416] Remove unnecessary SUBSTRING rewrite in SparkSqlDialect
Browse files Browse the repository at this point in the history
  • Loading branch information
NobiGo committed May 24, 2024
1 parent d274429 commit 4def070
Showing 1 changed file with 36 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@
import org.apache.calcite.avatica.util.TimeUnitRange;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.sql.JoinType;
import org.apache.calcite.sql.SqlBasicFunction;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlFloorFunction;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;

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

Expand All @@ -48,10 +43,6 @@ public class SparkSqlDialect extends SqlDialect {

public static final SqlDialect DEFAULT = new SparkSqlDialect(DEFAULT_CONTEXT);

private static final SqlFunction SPARKSQL_SUBSTRING =
SqlBasicFunction.create("SUBSTRING", ReturnTypes.ARG0_NULLABLE_VARYING,
OperandTypes.VARIADIC, SqlFunctionCategory.STRING);

/**
* Creates a SparkSqlDialect.
*/
Expand Down Expand Up @@ -115,48 +106,44 @@ public SparkSqlDialect(SqlDialect.Context context) {

@Override public void unparseCall(SqlWriter writer, SqlCall call,
int leftPrec, int rightPrec) {
if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
SqlUtil.unparseFunctionSyntax(SPARKSQL_SUBSTRING, writer, call, false);
} else {
switch (call.getKind()) {
case ARRAY_VALUE_CONSTRUCTOR:
case MAP_VALUE_CONSTRUCTOR:
final String keyword =
call.getKind() == SqlKind.ARRAY_VALUE_CONSTRUCTOR ? "array" : "map";

writer.keyword(keyword);

final SqlWriter.Frame frame = writer.startList("(", ")");
for (SqlNode operand : call.getOperandList()) {
writer.sep(",");
operand.unparse(writer, leftPrec, rightPrec);
}
writer.endList(frame);
break;

case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}

final SqlLiteral timeUnitNode = call.operand(1);
final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);

SqlCall call2 =
SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(),
timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
break;
case TRIM:
unparseHiveTrim(writer, call, leftPrec, rightPrec);
break;
case POSITION:
SqlUtil.unparseFunctionSyntax(SqlStdOperatorTable.POSITION, writer, call, false);
break;
default:
switch (call.getKind()) {
case ARRAY_VALUE_CONSTRUCTOR:
case MAP_VALUE_CONSTRUCTOR:
final String keyword =
call.getKind() == SqlKind.ARRAY_VALUE_CONSTRUCTOR ? "array" : "map";

writer.keyword(keyword);

final SqlWriter.Frame frame = writer.startList("(", ")");
for (SqlNode operand : call.getOperandList()) {
writer.sep(",");
operand.unparse(writer, leftPrec, rightPrec);
}
writer.endList(frame);
break;

case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}

final SqlLiteral timeUnitNode = call.operand(1);
final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);

SqlCall call2 =
SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(),
timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
break;
case TRIM:
unparseHiveTrim(writer, call, leftPrec, rightPrec);
break;
case POSITION:
SqlUtil.unparseFunctionSyntax(SqlStdOperatorTable.POSITION, writer, call, false);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}
}

0 comments on commit 4def070

Please sign in to comment.