thomasrebele commented on code in PR #6293:
URL: https://github.com/apache/hive/pull/6293#discussion_r2910085830


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/stats/FilterSelectivityEstimator.java:
##########
@@ -184,91 +189,383 @@ public Double visitCall(RexCall call) {
     return selectivity;
   }
 
+  /**
+   * Return whether the expression is a removable cast based on stats and type 
bounds.
+   *
+   * <p>
+   * In Hive, if a value cannot be represented by the cast, the result of the 
cast is NULL,
+   * and therefore cannot fulfill the predicate. So the possible range of the 
values
+   * is limited by the range of possible values of the type.
+   * </p>
+   *
+   * @param exp       the expression to check
+   * @param tableScan the table that provides the statistics
+   * @return true if the expression is a removable cast, false otherwise
+   */
+  private boolean isRemovableCast(RexNode exp, HiveTableScan tableScan) {
+    if(SqlKind.CAST != exp.getKind()) {
+      return false;
+    }
+    RexCall cast = (RexCall) exp;
+    RexNode op0 = cast.getOperands().getFirst();
+    if (!(op0 instanceof RexInputRef)) {
+      return false;
+    }
+    int index = ((RexInputRef) op0).getIndex();
+    final List<ColStatistics> colStats = 
tableScan.getColStat(Collections.singletonList(index));
+    if (colStats.isEmpty()) {
+      return false;
+    }
+
+    SqlTypeName sourceType = op0.getType().getSqlTypeName();
+    SqlTypeName targetType = cast.getType().getSqlTypeName();
+
+    switch (sourceType) {
+    case TINYINT, SMALLINT, INTEGER, BIGINT:
+      switch (targetType) {// additional checks are needed
+      case TINYINT, SMALLINT, INTEGER, BIGINT:
+        return isRemovableIntegerCast(cast, op0, colStats);
+      case FLOAT, DOUBLE, DECIMAL:
+        return true;
+      default:
+        return false;

Review Comment:
   The return statements are necessary to prevent a fall-through to the next 
case of the outer switch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to