You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/11 13:50:01 UTC

[GitHub] [arrow-datafusion] liukun4515 opened a new pull request, #2200: add sql level test for decimal data type

liukun4515 opened a new pull request, #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850029487


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   good point.
   c1 is decimal(10,6), and the cast(xxx as decimal(10,8)) is decimal(10,8), the result type should be the `decimal(12,8)`.
   In the filter expr, we should get the `decimal(12,8)`.
   I will add this test case.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850020035


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;

Review Comment:
   if other agg functions support the decimal data type, we can add the test in the function or scope.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850035454


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   > is there a reason this test only checks the schema of c1 and not c2?
   
   this test just wants to verify the decimal data type, I will add the test for c5.
   
   There is a test about c5 `let sql = "select * from decimal_simple where c1 > c5";`
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850022213


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   thanks for your point.
   I will add the checker for the output schema in all test cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r849651184


##########
datafusion/core/tests/decimal_data.csv:
##########
@@ -0,0 +1,16 @@
+c1,c2,c3,c4,c5
+0.00001,0.000000000001,1,true,0.000014
+0.00002,0.000000000002,2,true,0.000025
+0.00002,0.000000000002,3,false,0.000019
+0.00003,0.000000000003,4,true,0.000032
+0.00003,0.000000000003,5,false,0.000035
+0.00003,0.000000000003,5,true,0.000011
+0.00004,0.000000000004,5,true,0.000044
+0.00004,0.000000000004,12,false,0.000040
+0.00004,0.000000000004,14,true,0.000040
+0.00004,0.000000000004,8,false,0.000044
+0.00005,0.000000000005,9,true,0.000052
+0.00005,0.000000000005,4,true,0.000078
+0.00005,0.000000000005,8,false,0.000033
+0.00005,0.000000000005,100,true,0.000068
+0.00005,0.000000000005,1,false,0.000100

Review Comment:
   😂 I often find there's not new line for PR because of neglection.
   
   I think I can add a editor config for this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850029487


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   good point.
   c1 is decimal(10,6), and the cast(xxx as decimal(10,8)) is decimal(10,8), the result type should be the `decimal(12,8)`.
   I will add this test case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] Dandandan merged pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
Dandandan merged PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850035454


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   > is there a reason this test only checks the schema of c1 and not c2?
   this test just wants to verify the decimal data type, I will add the test for c5.
   
   



##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   > is there a reason this test only checks the schema of c1 and not c2?
   
   this test just wants to verify the decimal data type, I will add the test for c5.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850035454


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   > is there a reason this test only checks the schema of c1 and not c2?
   
   this test just wants to verify the decimal data type, I will add the test for c5.
   
   There is a test about c5 `let sql = "select * from decimal_simple where c1 > c5";`
   
   filter is about:
   1. filter by scalar
   2. filter by array/column. The c5 is used to do that.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r849771146


##########
datafusion/core/tests/decimal_data.csv:
##########
@@ -0,0 +1,16 @@
+c1,c2,c3,c4,c5
+0.00001,0.000000000001,1,true,0.000014
+0.00002,0.000000000002,2,true,0.000025
+0.00002,0.000000000002,3,false,0.000019
+0.00003,0.000000000003,4,true,0.000032
+0.00003,0.000000000003,5,false,0.000035
+0.00003,0.000000000003,5,true,0.000011
+0.00004,0.000000000004,5,true,0.000044
+0.00004,0.000000000004,12,false,0.000040
+0.00004,0.000000000004,14,true,0.000040
+0.00004,0.000000000004,8,false,0.000044
+0.00005,0.000000000005,9,true,0.000052
+0.00005,0.000000000005,4,true,0.000078
+0.00005,0.000000000005,8,false,0.000033
+0.00005,0.000000000005,100,true,0.000068
+0.00005,0.000000000005,1,false,0.000100

Review Comment:
   https://github.com/apache/arrow-datafusion/pull/2224



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850371499


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   > I find the bug in the precision and scale for logic comparison operation.
   
   Testing for the win!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850018300


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;

Review Comment:
   I just want to cover the agg functions which support the decimal data type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r849772825


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;

Review Comment:
   For anyone else following along, `decimal_group_function` covers grouping by decimal columns 👍 
   
   Something like
   ```sql
   select max(c1) from decimal_simple group by c2;
   ```
   
   



##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),

Review Comment:
   is there a reason this test only checks the schema of `c1` and not `c2`? 
   
   It also isn't clear to me why this test is checkout the output schema but `decimal_by_filter` isn't (not that this is a problem I am just curious)



##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   Is it worth checking somewhere what happens with different decimal scales?
   
   Something like
   ```sql 
   select * from decimal_simple where c1 >= CAST(0.00002 as Decimal(10,8))
   ```
   
   I am not sure if the coercion to an appropriate decimal type is covered elsewhere



##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq
+    let sql = "select * from decimal_simple where c1 >= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2   | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3   | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_sort() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select * from decimal_simple where c1 >= 0.00004 order by c1";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 >= 0.00004 order by c1 desc";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 < 0.00003 order by c1 desc,c4";

Review Comment:
   👍 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850030108


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   I find the bug in the precision and scale for logic comparison operation.
   ```
   ❯ explain select * from food where a >= CAST(0.00002 as Decimal(10,8));
   +---------------+----------------------------------------------------------------------------------------------------------+
   | plan_type     | plan                                                                                                     |
   +---------------+----------------------------------------------------------------------------------------------------------+
   | logical_plan  | Projection: #food.a, #food.b, #food.c                                                                    |
   |               |   Filter: #food.a >= Decimal128(Some(2000),10,8)                                                         |
   |               |     TableScan: food projection=Some([0, 1, 2]), partial_filters=[#food.a >= Decimal128(Some(2000),10,8)] |
   | physical_plan | ProjectionExec: expr=[a@0 as a, b@1 as b, c@2 as c]                                                      |
   |               |   CoalesceBatchesExec: target_batch_size=4096                                                            |
   |               |     FilterExec: CAST(a@0 AS Decimal(10, 8)) >= Some(2000),10,8                                           |
   |               |       RepartitionExec: partitioning=RoundRobinBatch(16)                                                  |
   |               |         CsvExec: files=[aggregate_simple.csv], has_header=false, limit=None, projection=[a, b, c]        |
   |               |                                                                                                          |
   +---------------+----------------------------------------------------------------------------------------------------------+
   ```
   @alamb 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] liukun4515 commented on a diff in pull request #2200: add sql level test for decimal data type

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2200:
URL: https://github.com/apache/arrow-datafusion/pull/2200#discussion_r850220772


##########
datafusion/core/tests/sql/decimal.rs:
##########
@@ -0,0 +1,428 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::*;
+
+#[tokio::test]
+async fn decimal_cast() -> Result<()> {
+    let ctx = SessionContext::new();
+    let sql = "select cast(1.23 as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------+",
+        "| CAST(Float64(1.23) AS Decimal(10, 4)) |",
+        "+---------------------------------------+",
+        "| 1.2300                                |",
+        "+---------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(cast(1.23 as decimal(10,3)) as decimal(10,4))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 4),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+---------------------------------------------------------------+",
+        "| CAST(CAST(Float64(1.23) AS Decimal(10, 3)) AS Decimal(10, 4)) |",
+        "+---------------------------------------------------------------+",
+        "| 1.2300                                                        |",
+        "+---------------------------------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select cast(1.2345 as decimal(24,2))";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(24, 2),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+-----------------------------------------+",
+        "| CAST(Float64(1.2345) AS Decimal(24, 2)) |",
+        "+-----------------------------------------+",
+        "| 1.23                                    |",
+        "+-----------------------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_sql() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "SELECT c1 from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000010 |",
+        "| 0.000020 |",
+        "| 0.000020 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000030 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_by_filter() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    let sql = "select c1 from decimal_simple where c1 > 0.000030";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+",
+        "| c1       |",
+        "+----------+",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000040 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "| 0.000050 |",
+        "+----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    let sql = "select * from decimal_simple where c1 > c5";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "| 0.000030 | 0.000000000003 | 5  | true  | 0.0000110 |",
+        "| 0.000050 | 0.000000000005 | 8  | false | 0.0000330 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_agg_function() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // min
+    let sql = "select min(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MIN(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000020               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // max
+    let sql = "select max(c1) from decimal_simple where c4=false";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| MAX(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000050               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // sum
+    let sql = "select sum(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    // inferred precision is 10+10
+    assert_eq!(
+        &DataType::Decimal(20, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| SUM(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.000550               |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // avg
+    let sql = "select avg(c1) from decimal_simple";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(14, 10),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+------------------------+",
+        "| AVG(decimal_simple.c1) |",
+        "+------------------------+",
+        "| 0.0000366666           |",
+        "+------------------------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    Ok(())
+}
+
+#[tokio::test]
+async fn decimal_logic_op() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_decimal_csv_table_by_sql(&ctx).await;
+    // logic operation: eq
+    let sql = "select * from decimal_simple where c1=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    assert_eq!(
+        &DataType::Decimal(10, 6),
+        actual[0].schema().field(0).data_type()
+    );
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: not eq
+    let sql = "select c2,c3 from decimal_simple where c1!=0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------------+-----+",
+        "| c2             | c3  |",
+        "+----------------+-----+",
+        "| 0.000000000001 | 1   |",
+        "| 0.000000000003 | 4   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000003 | 5   |",
+        "| 0.000000000004 | 5   |",
+        "| 0.000000000004 | 12  |",
+        "| 0.000000000004 | 14  |",
+        "| 0.000000000004 | 8   |",
+        "| 0.000000000005 | 9   |",
+        "| 0.000000000005 | 4   |",
+        "| 0.000000000005 | 8   |",
+        "| 0.000000000005 | 100 |",
+        "| 0.000000000005 | 1   |",
+        "+----------------+-----+",
+    ];
+    assert_batches_eq!(expected, &actual);
+    // logic operation: lt
+    let sql = "select * from decimal_simple where c1 < 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+------+-----------+",
+        "| c1       | c2             | c3 | c4   | c5        |",
+        "+----------+----------------+----+------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true | 0.0000140 |",
+        "+----------+----------------+----+------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: lteq
+    let sql = "select * from decimal_simple where c1 <= 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+----+-------+-----------+",
+        "| c1       | c2             | c3 | c4    | c5        |",
+        "+----------+----------------+----+-------+-----------+",
+        "| 0.000010 | 0.000000000001 | 1  | true  | 0.0000140 |",
+        "| 0.000020 | 0.000000000002 | 2  | true  | 0.0000250 |",
+        "| 0.000020 | 0.000000000002 | 3  | false | 0.0000190 |",
+        "+----------+----------------+----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gt
+    let sql = "select * from decimal_simple where c1 > 0.00002";
+    let actual = execute_to_batches(&ctx, sql).await;
+    let expected = vec![
+        "+----------+----------------+-----+-------+-----------+",
+        "| c1       | c2             | c3  | c4    | c5        |",
+        "+----------+----------------+-----+-------+-----------+",
+        "| 0.000030 | 0.000000000003 | 4   | true  | 0.0000320 |",
+        "| 0.000030 | 0.000000000003 | 5   | false | 0.0000350 |",
+        "| 0.000030 | 0.000000000003 | 5   | true  | 0.0000110 |",
+        "| 0.000040 | 0.000000000004 | 5   | true  | 0.0000440 |",
+        "| 0.000040 | 0.000000000004 | 12  | false | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 14  | true  | 0.0000400 |",
+        "| 0.000040 | 0.000000000004 | 8   | false | 0.0000440 |",
+        "| 0.000050 | 0.000000000005 | 9   | true  | 0.0000520 |",
+        "| 0.000050 | 0.000000000005 | 4   | true  | 0.0000780 |",
+        "| 0.000050 | 0.000000000005 | 8   | false | 0.0000330 |",
+        "| 0.000050 | 0.000000000005 | 100 | true  | 0.0000680 |",
+        "| 0.000050 | 0.000000000005 | 1   | false | 0.0001000 |",
+        "+----------+----------------+-----+-------+-----------+",
+    ];
+    assert_batches_eq!(expected, &actual);
+
+    // logic operation: gteq

Review Comment:
   https://github.com/apache/arrow-datafusion/issues/2232



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org