Here we check if we can apply the expression on the constant side We can do this if the cast itself is invertible and casting the constant is invertible in practice.
We can cast, now we change our column_ref_expression from an operator cast to a column reference
319825 {
319826 auto &expr = bindings[0].get().Cast<BoundComparisonExpression>();
319827 auto &constant_expr = bindings[1].get();
319828 bool column_ref_left = expr.left.get() != &constant_expr;
319829 auto column_ref_expr = !column_ref_left ? expr.right.get() : expr.left.get();
319830
319831
319832 D_ASSERT(constant_expr.IsFoldable());
319833 Value constant_value;
319835 return nullptr;
319836 }
319837 if (constant_value.IsNull() && !(expr.GetExpressionType() == ExpressionType::COMPARE_NOT_DISTINCT_FROM ||
319838 expr.GetExpressionType() == ExpressionType::COMPARE_DISTINCT_FROM)) {
319839
319840 return make_uniq<BoundConstantExpression>(Value(LogicalType::BOOLEAN));
319841 }
319842 if (column_ref_expr->GetExpressionClass() == ExpressionClass::BOUND_CAST) {
319846 auto &cast_expression = column_ref_expr->Cast<BoundCastExpression>();
319847 auto target_type = cast_expression.source_type();
319849 return nullptr;
319850 }
319851
319852
319853 string error_message;
319854 Value cast_constant;
319855 auto new_constant =
319856 constant_value.
TryCastAs(
rewriter.context, target_type, cast_constant, &error_message,
true);
319857 if (!new_constant) {
319858 return nullptr;
319859 }
319860
319861
319862 if (!cast_constant.IsNull() &&
319864
319865 return nullptr;
319866 }
319867
319869 auto child_expression = std::move(cast_expression.child);
319870 auto new_constant_expr = make_uniq<BoundConstantExpression>(cast_constant);
319871 if (column_ref_left) {
319872 expr.left = std::move(child_expression);
319873 expr.right = std::move(new_constant_expr);
319874 } else {
319875 expr.left = std::move(new_constant_expr);
319876 expr.right = std::move(child_expression);
319877 }
319878 changes_made = true;
319879 }
319880 return nullptr;
319881}
static bool CastIsInvertible(const LogicalType &source_type, const LogicalType &target_type)
Definition duckdb.cpp:375885
static DUCKDB_API bool TryEvaluateScalar(ClientContext &context, const Expression &expr, Value &result)
Try to evaluate a scalar expression and fold it into a single value, returns false if an exception is...
Definition duckdb.cpp:150008
DUCKDB_API bool TryCastAs(CastFunctionSet &set, GetCastFunctionInput &get_input, const LogicalType &target_type, Value &new_value, string *error_message, bool strict=false) const
Tries to cast this value to another type, and stores the result in "new_value".
Definition duckdb.cpp:133682