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

Filter operation (GT AND LT) on Float type result error #13078

Open
loloxwg opened this issue Oct 23, 2024 · 6 comments
Open

Filter operation (GT AND LT) on Float type result error #13078

loloxwg opened this issue Oct 23, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@loloxwg
Copy link
Contributor

loloxwg commented Oct 23, 2024

Describe the bug

SQL Query: Drone Information Table Operations

Create Table

CREATE TABLE droneinfo_test_where (ts timestamp, signal_strength INT, speed FLOAT, calibration_method STRING);

Insert Data

INSERT INTO droneinfo_test_where VALUES(NOW(), 80, 500.2, 'method2'); 

Select Query

SELECT * FROM droneinfo_test_where WHERE signal_strength >= 80 AND speed <= 500.2;

Expected Result

+----+-----------------+-------+--------------------+
| ts | signal_strength | speed | calibration_method  |
+----+-----------------+-------+--------------------+
|    |        80       | 500.2 | method2            |
+----+-----------------+-------+--------------------+

This result should match the output in DuckDB.

To Reproduce

run datadusion-cli
input
CREATE TABLE droneinfo_test_where (ts timestamp, signal_strength INT, speed FLOAT, calibration_method STRING);

INSERT INTO droneinfo_test_where VALUES(NOW(),80,500.2,'method2');

SELECT * FROM droneinfo_test_where WHERE signal_strength >= 80 AND speed <= 500.2;

Expected behavior

Same sql excute on duckdb
image

Additional context

No response

@loloxwg loloxwg added the bug Something isn't working label Oct 23, 2024
@loloxwg
Copy link
Contributor Author

loloxwg commented Oct 23, 2024

image

@buraksenn
Copy link
Contributor

I'm not sure about the internals but this is probably about floating precision while casting as far as I can see:
image

@loloxwg
Copy link
Contributor Author

loloxwg commented Oct 24, 2024

It may be that the literal (500.2) should be cast to float32. I think

@JasonLi-cn
Copy link
Contributor

JasonLi-cn commented Oct 24, 2024

Float is DataType::Float32 in datafusion, and 500.2 resolves to DataType::Float64 at the parser stage. Therefore, the speed field is casted to DataType::Float64 when the query plan is generated.

SQLDataType::Float(_) => Ok(DataType::Float32),

signed_number.parse::<f64>().map(lit).map_err(|_| {
DataFusionError::from(ParserError(format!(
"Cannot parse {signed_number} as f64"
)))

Try:

SELECT * FROM droneinfo_test_where WHERE signal_strength >= 80 AND speed <= 500.2::float;
DataFusion CLI v42.0.0
> CREATE TABLE droneinfo_test_where (ts timestamp, signal_strength INT, speed FLOAT, calibration_method STRING);


0 row(s) fetched.
Elapsed 0.021 seconds.

> INSERT INTO droneinfo_test_where VALUES(NOW(),80,500.2,'method2');
+-------+
| count |
+-------+
| 1     |
+-------+
1 row(s) fetched.
Elapsed 0.020 seconds.

> SELECT * FROM droneinfo_test_where WHERE signal_strength >= 80 AND speed <= 500.2;


+----+-----------------+-------+--------------------+
| ts | signal_strength | speed | calibration_method |
+----+-----------------+-------+--------------------+
+----+-----------------+-------+--------------------+
0 row(s) fetched.
Elapsed 0.013 seconds.

> SELECT * FROM droneinfo_test_where WHERE signal_strength >= 80 AND speed <= 500.2::float;
+----------------------------+-----------------+-------+--------------------+
| ts                         | signal_strength | speed | calibration_method |
+----------------------------+-----------------+-------+--------------------+
| 2024-10-24T02:57:17.767451 | 80              | 500.2 | method2            |
+----------------------------+-----------------+-------+--------------------+
1 row(s) fetched.
Elapsed 0.009 seconds.

@JasonLi-cn
Copy link
Contributor

take

@andygrove
Copy link
Member

Also, worth noting that the parser is incorrect (at least not ANSI SQL compliant) when parsing numeric literals. It should be parsing them as Decimal rather than Float64.

#12817

@JasonLi-cn JasonLi-cn removed their assignment Oct 26, 2024
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

4 participants