Skip to content

Commit

Permalink
Improve code style in UnwrapCastInComparison
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Sep 19, 2022
1 parent ea9036f commit 47c1fb1
Showing 1 changed file with 4 additions and 9 deletions.
Expand Up @@ -174,14 +174,13 @@ public Expression rewriteComparisonExpression(ComparisonExpression node, Void co
private Expression unwrapCast(ComparisonExpression expression)
{
// Canonicalization is handled by CanonicalizeExpressionRewriter
if (!(expression.getLeft() instanceof Cast)) {
if (!(expression.getLeft() instanceof Cast cast)) {
return expression;
}

Object right = new ExpressionInterpreter(expression.getRight(), plannerContext, session, typeAnalyzer.getTypes(session, types, expression.getRight()))
.optimize(NoOpSymbolResolver.INSTANCE);

Cast cast = (Cast) expression.getLeft();
ComparisonExpression.Operator operator = expression.getOperator();

if (right == null || right instanceof NullLiteral) {
Expand Down Expand Up @@ -423,8 +422,7 @@ private boolean hasInjectiveImplicitCoercion(Type source, Type target, Object va
}
}

if (target instanceof TimestampWithTimeZoneType) {
TimestampWithTimeZoneType timestampWithTimeZoneType = (TimestampWithTimeZoneType) target;
if (target instanceof TimestampWithTimeZoneType timestampWithTimeZoneType) {
if (source instanceof TimestampType) {
// Cast from TIMESTAMP WITH TIME ZONE to TIMESTAMP and back to TIMESTAMP WITH TIME ZONE does not round trip, unless the value's zone is equal to sesion zone
if (!getTimeZone(timestampWithTimeZoneType, value).equals(session.getTimeZoneKey())) {
Expand Down Expand Up @@ -452,10 +450,7 @@ private boolean hasInjectiveImplicitCoercion(Type source, Type target, Object va
}

boolean coercible = new TypeCoercion(plannerContext.getTypeManager()::getType).canCoerce(source, target);
if (source instanceof VarcharType && target instanceof CharType) {
VarcharType sourceVarchar = (VarcharType) source;
CharType targetChar = (CharType) target;

if (source instanceof VarcharType sourceVarchar && target instanceof CharType targetChar) {
if (sourceVarchar.isUnbounded() || sourceVarchar.getBoundedLength() > targetChar.getLength()) {
// Truncation, not injective.
return false;
Expand Down Expand Up @@ -492,7 +487,7 @@ private int compare(Type type, Object first, Object second)
// choice of placing unordered values first or last does not matter for this code
MethodHandle comparisonOperator = plannerContext.getTypeOperators().getComparisonUnorderedLastOperator(type, InvocationConvention.simpleConvention(FAIL_ON_NULL, NEVER_NULL, NEVER_NULL));
try {
return (int) (long) comparisonOperator.invoke(first, second);
return toIntExact((long) comparisonOperator.invoke(first, second));
}
catch (Throwable throwable) {
Throwables.throwIfUnchecked(throwable);
Expand Down

0 comments on commit 47c1fb1

Please sign in to comment.