Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison Operators for Decimals of Different Precisions and Scales #15174

Open
shehabgamin opened this issue Mar 12, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@shehabgamin
Copy link
Contributor

Describe the bug

Decimals of different precisions and scales are properly rebased when working with Literal values. Otherwise, they are not rebased, which leads to an error.

To Reproduce

desktop % datafusion-cli
DataFusion CLI v46.0.0
> SELECT 1 * 1.000::DECIMAL(4,3) > 1.2::decimal(2,1);
+--------------------------------------+
| Int64(1) * Float64(1) > Float64(1.2) |
+--------------------------------------+
| false                                |
+--------------------------------------+
1 row(s) fetched.
Elapsed 0.003 seconds.

> SELECT a * 1.000::DECIMAL(4,3) > 1.2::decimal(2,1) FROM VALUES (1) AS t(a);
Arrow error: Invalid argument error: Invalid comparison operation: Decimal128(20, 0) > Decimal128(25, 3)
>

Expected behavior

No response

Additional context

No response

@shehabgamin shehabgamin added the bug Something isn't working label Mar 12, 2025
@Spaarsh
Copy link
Contributor

Spaarsh commented Mar 12, 2025

From what I've gathered after investigating this, I think the problem lies in how the sql_expr_to_logical_expr is making a plan for evaluating such an expression.
sql_expr_to_logical_expr makes a plan:

  1. sql_expr_to_logical_expr
  2. Eventually binary_op called
  3. binary called
  4. binary_numeric_coercion called
  5. decimal_coercion called
  6. get_wider_decimal called
  7. decimals converted to same scale for comparison

For the first case, the multiplication is done first and then the above flow is done. For the second query, the plan is made first, so the plan converts the decimals and then proceeds to multiply the left one which leads to a scale change in the left decimal. Hence the error.

I've deduced this from dry running this. I'll drop a comment once I go through the stack made by sql_expr_to_logical_expr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants