soumyakanti3578 commented on code in PR #5196: URL: https://github.com/apache/hive/pull/5196#discussion_r1943896288
########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java: ########## @@ -1078,6 +1098,36 @@ public ASTNode visitCall(RexCall call) { // proceed correctly if we just ignore the <time_unit> astNodeLst.add(call.operands.get(1).accept(this)); break; + case SEARCH: + ASTNode astNode = call.getOperands().get(0).accept(this); + astNodeLst.add(astNode); + RexLiteral literal = (RexLiteral) call.operands.get(1); + Sarg<?> sarg = Objects.requireNonNull(literal.getValueAs(Sarg.class), "Sarg"); + int minOrClauses = SessionState.getSessionConf().getIntVar(HiveConf.ConfVars.HIVE_POINT_LOOKUP_OPTIMIZER_MIN); + + // convert Sarg to IN when they are points. + if (sarg.isPoints()) { + // just expand SEARCH to ORs when point count is less than HIVE_POINT_LOOKUP_OPTIMIZER_MIN + if (sarg.pointCount < minOrClauses) { + return visitCall((RexCall) call.accept(RexUtil.searchShuttle(rexBuilder, null, -1))); + } + + // else convert to IN + for (Range<?> range : sarg.rangeSet.asRanges()) { + astNodeLst.add(visitLiteral((RexLiteral) rexBuilder.makeLiteral( + range.lowerEndpoint(), literal.getType(), true, true))); + } + + return SqlFunctionConverter.buildAST(HiveIn.INSTANCE, astNodeLst, call.getType()); + // Expand SEARCH operator + } else { + return visitCall((RexCall) transformOrToInAndInequalityToBetween( + rexBuilder, + call.accept(RexUtil.searchShuttle(rexBuilder, null, -1)), + minOrClauses + ) Review Comment: Looking more into it, we can detect if the whole SEARCH operator can be converted to `[NOT] IN` or `[NOT] BETWEEN` as discussed above, but if we have a query like: ``` explain cbo select * from t1 where a between 1 and 5 or a in (10, 12, 15) or a > 100 ``` it's not easy to generate. For this the CBO plan is: ``` CBO PLAN: HiveProject(a=[$0], b=[$1]) HiveFilter(condition=[SEARCH($0, Sarg[[1..5], 10, 12, 15, (100..+∞)])]) HiveTableScan(table=[[default, t1]], table:alias=[t1]) ``` `sarg.isPoints() and sarg.isComplementedPoints()` both will return false for this. Since we have a complex `Sarg`, it is more difficult to generate `filterExpr: (a BETWEEN 1 AND 5 or (a > 100) or (a) IN (10, 12, 15)) (type: boolean)` back from the CBO plan, which we are able to do right now by using `transformOrToInAndInequalityToBetween` -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org