Skip to content

Commit

Permalink
[CALCITE-2980] Implement the FORMAT clause of the CAST operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthrino committed Feb 12, 2024
1 parent f837ffa commit 3070c02
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 183 deletions.
2 changes: 2 additions & 0 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -6181,6 +6181,7 @@ SqlNode BuiltinFunctionCall() :
final SqlNode node;
final SqlLiteral style; // mssql convert 'style' operand
final SqlFunction f;
final SqlNode format;
}
{
//~ FUNCTIONS WITH SPECIAL SYNTAX ---------------------------------------
Expand All @@ -6197,6 +6198,7 @@ SqlNode BuiltinFunctionCall() :
|
<INTERVAL> e = IntervalQualifier() { args.add(e); }
)
[ <FORMAT> format = StringLiteral() { args.add(format); } ]
<RPAREN> {
return f.createCall(s.end(this), args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3262,11 +3262,21 @@ private static class CastImplementor extends AbstractRexCallImplementor {

@Override Expression implementSafe(final RexToLixTranslator translator,
final RexCall call, final List<Expression> argValueList) {
assert call.getOperands().size() == 1;
assert call.getOperands().size() <= 2;
final RelDataType sourceType = call.getOperands().get(0).getType();

// Short-circuit if no cast is required
RexNode arg = call.getOperands().get(0);
ConstantExpression formatExpr;
if (call.getOperands().size() > 1) {
RexLiteral format = (RexLiteral) call.getOperands().get(1);
formatExpr =
(ConstantExpression) RexToLixTranslator.translateLiteral(format, format.getType(),
translator.typeFactory, NullAs.NULL);
} else {
formatExpr = NULL_EXPR;
}

// Short-circuit if no cast is required
if (call.getType().equals(sourceType)) {
// No cast required, omit cast
return argValueList.get(0);
Expand All @@ -3282,7 +3292,7 @@ private static class CastImplementor extends AbstractRexCallImplementor {
nullifyType(translator.typeFactory, call.getType(), false);
boolean safe = call.getKind() == SqlKind.SAFE_CAST;
return translator.translateCast(sourceType,
targetType, argValueList.get(0), safe);
targetType, argValueList.get(0), safe, formatExpr);
}

private static RelDataType nullifyType(JavaTypeFactory typeFactory,
Expand Down
Loading

0 comments on commit 3070c02

Please sign in to comment.