From 8e79c8ad388d43f3a56a0199ddfc3ba7bfc3cd77 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Mon, 19 Sep 2022 14:23:04 +0200 Subject: [PATCH] Improve code style in UnwrapCastInComparison --- .../iterative/rule/UnwrapCastInComparison.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/UnwrapCastInComparison.java b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/UnwrapCastInComparison.java index 51bcbc4fa653..bd72b003c6b6 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/UnwrapCastInComparison.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/UnwrapCastInComparison.java @@ -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) { @@ -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())) { @@ -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; @@ -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);