Github user kumarvishal09 commented on a diff in the pull request: https://github.com/apache/carbondata/pull/1647#discussion_r156671241 --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CastExpressionOptimization.scala --- @@ -343,32 +294,106 @@ object CastExpressionOptimization { Some(CastExpr(c)) } case i: IntegerType if t.sameType(DoubleType) => - val value = v.asInstanceOf[Double].toInt - if (value.toDouble.equals(v)) { - Some(sources.LessThanOrEqual(a.name, value)) - } else { - Some(CastExpr(c)) - } + updateFilterForInt(v, c) + case s: ShortType if t.sameType(IntegerType) => + updateFilterForShort(v, c) case _ => Some(CastExpr(c)) } case c@LessThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) => a.dataType match { case ts: TimestampType if t.sameType(StringType) => - val value = typeCastStringToLong(v) - if (!value.equals(v)) { - Some(sources.GreaterThanOrEqual(a.name, value)) - } else { - Some(CastExpr(c)) - } + updateFilterForTimeStamp(v, c) case i: IntegerType if t.sameType(DoubleType) => - val value = v.asInstanceOf[Double].toInt - if (value.toDouble.equals(v)) { - Some(sources.GreaterThanOrEqual(a.name, value)) - } else { - Some(CastExpr(c)) - } + updateFilterForInt(v, c) + case s: ShortType if t.sameType(IntegerType) => + updateFilterForShort(v, c) case _ => Some(CastExpr(c)) } } } + + /** + * the method removes the cast for short type columns + * @param actualValue + * @param exp + * @return + */ + def updateFilterForShort(actualValue: Any, exp: Expression): Option[sources.Filter] = { + val newValue = actualValue.asInstanceOf[Integer].toShort + if (newValue.toInt.equals(actualValue)) { + updateFilterBasedOnFilterType(exp, newValue) + } else { + Some(CastExpr(exp)) + } + } + + /** + * the method removes the cast for int type columns + * + * @param actualValue + * @param exp + * @return + */ + def updateFilterForInt(actualValue: Any, exp: Expression): Option[sources.Filter] = { + val newValue = actualValue.asInstanceOf[Double].toInt + if (newValue.toDouble.equals(actualValue)) { + updateFilterBasedOnFilterType(exp, newValue) + } else { + Some(CastExpr(exp)) + } + } + + /** + * the method removes the cast for timestamp type columns + * + * @param actualValue + * @param exp + * @return + */ + def updateFilterForTimeStamp(actualValue: Any, exp: Expression): Option[sources.Filter] = { + val newValue = typeCastStringToLong(actualValue) + if (!newValue.equals(actualValue)) { + updateFilterBasedOnFilterType(exp, newValue) + } else { + Some(CastExpr(exp)) + } + } + + /** + * the method removes the cast for the respective filter type + * + * @param exp + * @param newValue + * @return + */ + def updateFilterBasedOnFilterType(exp: Expression, --- End diff -- Is is possible to handle this method in checkifcastcanberemove??
---