320268 {
320269 auto &expr = bindings[0].get().Cast<BoundComparisonExpression>();
320270 auto comparison_type = expr.GetExpressionType();
320271
320272 auto &date_part = bindings[2].get().Cast<BoundConstantExpression>();
320273
320274 if (bindings[3].
get().GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
320275 return nullptr;
320276 }
320277
320278 auto &column_part = bindings[3].get().Cast<BoundColumnRefExpression>();
320279 auto &rhs = bindings[4].get().Cast<BoundConstantExpression>();
320280
320281
320282 const bool col_is_lhs = (expr.left->GetExpressionClass() == ExpressionClass::BOUND_FUNCTION);
320283
320284
320285
320286 ExpressionType rhs_comparison_type = comparison_type;
320287 if (!col_is_lhs) {
320289 }
320290
320291
320292 const bool is_truncated = DateIsTruncated(date_part, rhs);
320293
320294 switch (rhs_comparison_type) {
320295 case ExpressionType::COMPARE_EQUAL:
320296 case ExpressionType::COMPARE_NOT_DISTINCT_FROM:
320297
320298
320299
320300
320301
320302
320303
320304
320305
320306
320307
320308
320309
320310
320311
320312
320313
320314
320315
320316
320317
320318 {
320319
320320 if (rhs_comparison_type == ExpressionType::COMPARE_NOT_DISTINCT_FROM && rhs.value.IsNull()) {
320321 auto op = make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NULL, LogicalType::BOOLEAN);
320322 op->children.push_back(column_part.Copy());
320323 return std::move(op);
320324 } else {
320325 if (!is_truncated) {
320326 return make_uniq<BoundConstantExpression>(
Value::BOOLEAN(
false));
320327 }
320328
320329 auto trunc = CreateTrunc(date_part, rhs, column_part.return_type);
320330 if (!trunc) {
320331 return nullptr;
320332 }
320333
320334 auto trunc_add = CreateTruncAdd(date_part, rhs, column_part.return_type);
320335 if (!trunc_add) {
320336 return nullptr;
320337 }
320338
320339 auto gteq = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_GREATERTHANOREQUALTO,
320340 column_part.Copy(), std::move(trunc));
320341 auto lt = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_LESSTHAN, column_part.Copy(),
320342 std::move(trunc_add));
320343
320344
320345 if (rhs_comparison_type == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
320346 auto comp = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(gteq),
320347 std::move(lt));
320348
320349 auto isnotnull =
320350 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, LogicalType::BOOLEAN);
320351 isnotnull->children.push_back(column_part.Copy());
320352
320353 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(comp),
320354 std::move(isnotnull));
320355 } else {
320356 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(gteq),
320357 std::move(lt));
320358 }
320359 }
320360 }
320361
320362 case ExpressionType::COMPARE_NOTEQUAL:
320363 case ExpressionType::COMPARE_DISTINCT_FROM:
320364
320365
320366
320367
320368
320369
320370
320371
320372
320373
320374
320375
320376
320377
320378
320379
320380
320381
320382
320383
320384
320385 {
320386 if (rhs_comparison_type == ExpressionType::COMPARE_DISTINCT_FROM && rhs.value.IsNull()) {
320387
320388 auto op =
320389 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, LogicalType::BOOLEAN);
320390 op->children.push_back(column_part.Copy());
320391 return std::move(op);
320392 } else {
320393 if (!is_truncated) {
320394 return make_uniq<BoundConstantExpression>(
Value::BOOLEAN(
true));
320395 }
320396
320397 auto trunc = CreateTrunc(date_part, rhs, column_part.return_type);
320398 if (!trunc) {
320399 return nullptr;
320400 }
320401
320402 auto trunc_add = CreateTruncAdd(date_part, rhs, column_part.return_type);
320403 if (!trunc_add) {
320404 return nullptr;
320405 }
320406
320407 auto lt = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_LESSTHAN, column_part.Copy(),
320408 std::move(trunc));
320409 auto gteq = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_GREATERTHANOREQUALTO,
320410 column_part.Copy(), std::move(trunc_add));
320411
320412
320413 if (rhs_comparison_type == ExpressionType::COMPARE_DISTINCT_FROM) {
320414 auto comp = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(gteq),
320415 std::move(lt));
320416
320417 auto isnull =
320418 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NULL, LogicalType::BOOLEAN);
320419 isnull->children.push_back(column_part.Copy());
320420
320421 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(comp),
320422 std::move(isnull));
320423 } else {
320424 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(gteq),
320425 std::move(lt));
320426 }
320427 }
320428 }
320429 return nullptr;
320430
320431 case ExpressionType::COMPARE_LESSTHAN:
320432 case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
320433
320434
320435
320436
320437 {
320438
320439
320440 if (!is_truncated) {
320441
320442 auto trunc = CreateTruncAdd(date_part, rhs, column_part.return_type);
320443 if (!trunc) {
320444 return nullptr;
320445 }
320446
320447 if (col_is_lhs) {
320448 expr.left = column_part.Copy();
320449 expr.right = std::move(trunc);
320450 } else {
320451 expr.right = column_part.Copy();
320452 expr.left = std::move(trunc);
320453 }
320454 } else {
320455
320456
320457 if (col_is_lhs) {
320458 expr.left = column_part.Copy();
320459
320460 if (rhs.return_type.id() != expr.left->return_type.id()) {
320461 expr.right = CastAndEvaluate(std::move(expr.right), expr.left->return_type);
320462 }
320463 } else {
320464 expr.right = column_part.Copy();
320465
320466 if (rhs.return_type.id() != expr.right->return_type.id()) {
320467 expr.left = CastAndEvaluate(std::move(expr.left), expr.right->return_type);
320468 }
320469 }
320470 }
320471
320472 changes_made = true;
320473 return nullptr;
320474 }
320475
320476 case ExpressionType::COMPARE_LESSTHANOREQUALTO:
320477 case ExpressionType::COMPARE_GREATERTHAN:
320478
320479
320480
320481
320482 {
320483
320484 auto trunc = CreateTruncAdd(date_part, rhs, column_part.return_type);
320485 if (!trunc) {
320486 return nullptr;
320487 }
320488
320489 if (col_is_lhs) {
320490 expr.left = column_part.Copy();
320491 expr.right = std::move(trunc);
320492 } else {
320493 expr.right = column_part.Copy();
320494 expr.left = std::move(trunc);
320495 }
320496
320497
320498 if (rhs_comparison_type == ExpressionType::COMPARE_GREATERTHAN) {
320499 if (col_is_lhs) {
320500 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_GREATERTHANOREQUALTO);
320501 } else {
320502 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_LESSTHANOREQUALTO);
320503 }
320504 } else {
320505 if (col_is_lhs) {
320506 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_LESSTHAN);
320507 } else {
320508 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_GREATERTHAN);
320509 }
320510 }
320511
320512 changes_made = true;
320513 return nullptr;
320514 }
320515
320516 default:
320517 return nullptr;
320518 }
320519}
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
__host__ cudaTextureObject_t get() const noexcept
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052