(List<Expr> conjuncts, Analyzer analyzer)
| 1393 | * exception. |
| 1394 | */ |
| 1395 | public static boolean optimizeConjuncts(List<Expr> conjuncts, Analyzer analyzer) { |
| 1396 | Preconditions.checkNotNull(conjuncts); |
| 1397 | List<Expr> tmpConjuncts = new ArrayList<>(); |
| 1398 | List<Expr> keepConjuncts = new ArrayList<>(); |
| 1399 | try { |
| 1400 | BitSet candidates = new BitSet(conjuncts.size()); |
| 1401 | candidates.set(0, Math.min(conjuncts.size(), CONST_PROPAGATION_EXPR_LIMIT)); |
| 1402 | int transfers = 0; |
| 1403 | tmpConjuncts.addAll(conjuncts); |
| 1404 | // Constant propagation may make other slots constant, so repeat the process |
| 1405 | // until there are no more changes. |
| 1406 | while (!candidates.isEmpty()) { |
| 1407 | // Use tmpConjuncts instead of conjuncts because propagateConstants can |
| 1408 | // change the content of the first input param. We do not want to |
| 1409 | // change the expr in conjucts before make sure the constant propagation |
| 1410 | // does not cause analysis failures. |
| 1411 | BitSet changed = propagateConstants(tmpConjuncts, candidates, keepConjuncts, |
| 1412 | analyzer); |
| 1413 | candidates.clear(); |
| 1414 | int pruned = 0; |
| 1415 | for (int i = changed.nextSetBit(0); i >= 0; i = changed.nextSetBit(i+1)) { |
| 1416 | // When propagating constants, we may de-normalize expressions, so we |
| 1417 | // must normalize binary predicates. Any additional rules will be |
| 1418 | // applied by the rewriter. |
| 1419 | int index = i - pruned; |
| 1420 | Preconditions.checkState(index >= 0); |
| 1421 | ExprRewriter rewriter = analyzer.getExprRewriter(); |
| 1422 | Expr rewritten = rewriter.rewrite(tmpConjuncts.get(index), analyzer); |
| 1423 | // Re-analyze to add implicit casts and update cost |
| 1424 | rewritten.reset(); |
| 1425 | rewritten.analyze(analyzer); |
| 1426 | if (!rewritten.isConstant()) { |
| 1427 | conjuncts.set(index, rewritten); |
| 1428 | tmpConjuncts.set(index, rewritten); |
| 1429 | if (++transfers < CONST_PROPAGATION_EXPR_LIMIT) candidates.set(index, true); |
| 1430 | continue; |
| 1431 | } |
| 1432 | // Remove constant boolean literal expressions. N.B. - we may have |
| 1433 | // expressions determined to be constant which can not yet be discarded |
| 1434 | // because they can't be evaluated if expr rewriting is turned off. |
| 1435 | if (IS_NULL_LITERAL.apply(rewritten) || |
| 1436 | IS_FALSE_LITERAL.apply(rewritten)) { |
| 1437 | conjuncts.clear(); |
| 1438 | conjuncts.add(rewritten); |
| 1439 | return false; |
| 1440 | } |
| 1441 | if (IS_TRUE_LITERAL.apply(rewritten)) { |
| 1442 | pruned++; |
| 1443 | conjuncts.remove(index); |
| 1444 | tmpConjuncts.remove(index); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } catch (AnalysisException e) { |
| 1449 | LOG.warn("Not able to analyze after rewrite: " + e.toString() + " conjuncts: " + |
| 1450 | Expr.debugString(conjuncts)); |
| 1451 | } |
| 1452 | // add the conjuncts that need to be preserved |
no test coverage detected