321543 {
321544 auto &comparison = bindings[0].get().Cast<BoundComparisonExpression>();
321545 auto &outer_constant = bindings[1].get().Cast<BoundConstantExpression>();
321546 auto &arithmetic = bindings[2].get().Cast<BoundFunctionExpression>();
321547 auto &inner_constant = bindings[3].get().Cast<BoundConstantExpression>();
321548 D_ASSERT(arithmetic.return_type.IsIntegral());
321549 D_ASSERT(arithmetic.children[0]->return_type.IsIntegral());
321550 if (inner_constant.value.IsNull() || outer_constant.value.IsNull()) {
321551 if (comparison.GetExpressionType() == ExpressionType::COMPARE_DISTINCT_FROM ||
321552 comparison.GetExpressionType() == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
321553 return nullptr;
321554 }
321555 return make_uniq<BoundConstantExpression>(Value(comparison.return_type));
321556 }
321557 auto &constant_type = outer_constant.return_type;
321558 hugeint_t outer_value = IntegralValue::Get(outer_constant.value);
321559 hugeint_t inner_value = IntegralValue::Get(inner_constant.value);
321560
321561 idx_t arithmetic_child_index = arithmetic.children[0].get() == &inner_constant ? 1 : 0;
321562 auto &op_type = arithmetic.function.name;
321563 if (op_type == "+") {
321564
321565
321566
321567 if (!Hugeint::TrySubtractInPlace(outer_value, inner_value)) {
321568 return nullptr;
321569 }
321571 if (!result_value.DefaultTryCastAs(constant_type)) {
321572 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321573 return nullptr;
321574 }
321575
321576
321577
321578 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321580 }
321581 outer_constant.value = std::move(result_value);
321582 } else if (op_type == "-") {
321583
321584
321585 if (arithmetic_child_index == 0) {
321586
321587
321588 if (!Hugeint::TryAddInPlace(outer_value, inner_value)) {
321589 return nullptr;
321590 }
321592 if (!result_value.DefaultTryCastAs(constant_type)) {
321593
321594 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321595 return nullptr;
321596 }
321597 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321599 }
321600 outer_constant.value = std::move(result_value);
321601 } else {
321602
321603
321604 if (!Hugeint::TrySubtractInPlace(inner_value, outer_value)) {
321605 return nullptr;
321606 }
321608 if (!result_value.DefaultTryCastAs(constant_type)) {
321609
321610 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321611 return nullptr;
321612 }
321613 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321615 }
321616 outer_constant.value = std::move(result_value);
321617
321618
321620 }
321621 } else {
321622 D_ASSERT(op_type == "*");
321623
321624
321625
321626
321627 if (inner_value == 0) {
321628
321629
321630 return nullptr;
321631 }
321632
321633
321634 if ((outer_value == NumericLimits<hugeint_t>::Minimum() && inner_value == -1) ||
321635 outer_value % inner_value != 0) {
321636 bool is_equality = comparison.GetExpressionType() == ExpressionType::COMPARE_EQUAL;
321637 bool is_inequality = comparison.GetExpressionType() == ExpressionType::COMPARE_NOTEQUAL;
321638 if (is_equality || is_inequality) {
321639
321640
321641
321642 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321644 } else {
321645
321646 return nullptr;
321647 }
321648 }
321649 if (inner_value < 0) {
321650
321652 }
321653
321654
321655
321657 if (!result_value.DefaultTryCastAs(constant_type)) {
321658 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321660 }
321661 outer_constant.value = std::move(result_value);
321662 }
321663
321664
321665 auto arithmetic_child = std::move(arithmetic.children[arithmetic_child_index]);
321666
321667 if (comparison.left.get() == &outer_constant) {
321668 comparison.right = std::move(arithmetic_child);
321669 } else {
321670 comparison.left = std::move(arithmetic_child);
321671 }
321672 changes_made = true;
321673 return nullptr;
321674}
static DUCKDB_API Value HUGEINT(hugeint_t value)
Create a hugeint Value from a specified value.
Definition duckdb.cpp:132153
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237