| 1429 | } // namespace |
| 1430 | |
| 1431 | Result<Expression> SimplifyWithGuarantee(Expression expr, |
| 1432 | const Expression& guaranteed_true_predicate) { |
| 1433 | KnownFieldValues known_values; |
| 1434 | auto conjunction_members = GuaranteeConjunctionMembers(guaranteed_true_predicate); |
| 1435 | |
| 1436 | RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values)); |
| 1437 | |
| 1438 | ARROW_ASSIGN_OR_RAISE(expr, |
| 1439 | ReplaceFieldsWithKnownValues(known_values, std::move(expr))); |
| 1440 | |
| 1441 | auto CanonicalizeAndFoldConstants = [&expr] { |
| 1442 | ARROW_ASSIGN_OR_RAISE(expr, Canonicalize(std::move(expr))); |
| 1443 | ARROW_ASSIGN_OR_RAISE(expr, FoldConstants(std::move(expr))); |
| 1444 | return Status::OK(); |
| 1445 | }; |
| 1446 | RETURN_NOT_OK(CanonicalizeAndFoldConstants()); |
| 1447 | |
| 1448 | for (const auto& guarantee : conjunction_members) { |
| 1449 | if (!guarantee.call()) continue; |
| 1450 | |
| 1451 | if (auto inequality = Inequality::ExtractOne(guarantee)) { |
| 1452 | ARROW_ASSIGN_OR_RAISE(auto simplified, |
| 1453 | ModifyExpression( |
| 1454 | std::move(expr), [](Expression expr) { return expr; }, |
| 1455 | [&](Expression expr, ...) -> Result<Expression> { |
| 1456 | return inequality->Simplify(std::move(expr)); |
| 1457 | })); |
| 1458 | |
| 1459 | if (Expression::Identical(simplified, expr)) continue; |
| 1460 | |
| 1461 | expr = std::move(simplified); |
| 1462 | RETURN_NOT_OK(CanonicalizeAndFoldConstants()); |
| 1463 | } |
| 1464 | |
| 1465 | if (guarantee.call()->function_name == "is_valid") { |
| 1466 | ARROW_ASSIGN_OR_RAISE( |
| 1467 | auto simplified, |
| 1468 | SimplifyIsValidGuarantee(std::move(expr), *CallNotNull(guarantee))); |
| 1469 | |
| 1470 | if (Expression::Identical(simplified, expr)) continue; |
| 1471 | |
| 1472 | expr = std::move(simplified); |
| 1473 | RETURN_NOT_OK(CanonicalizeAndFoldConstants()); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return expr; |
| 1478 | } |
| 1479 | |
| 1480 | Result<Expression> RemoveNamedRefs(Expression src) { |
| 1481 | if (!src.IsBound()) { |