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/05/07 20:11:58 UTC

[GitHub] [arrow-datafusion] WinkerDu opened a new pull request, #2481: Numeric, String, Boolean comparisons with literal `NULL`

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

   # 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 #1179 .
   
    # 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.  
   -->
   This pr aims to solve Numeric, String, Boolean comparisons (Binary Expressions) with literal `NULL`
   A typical SQL is like
   ```
   select column1 < NULL from (VALUES (1, 'foo' ,2.3), (2, 'bar', 5.4)) as t
   ```
   
   # 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.
   -->
   - Introduces `null_coercion` in `comparison_eq_coercion` and `comparison_order_coercion`
   - Enhances binary expression macros to support `NULL` scalar value comparing with array, like `binary_array_op_dyn_scalar`
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   No.
   <!--
   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] yjshen merged pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


-- 
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] yjshen commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   After checking Postgres and MySQL, I found I'm wrong on the expected behavior while comparing columns with NULL using `<` `>` ..., they will return NULL in all cases. I was expecting to apply null first or null last while comparing column with NULL, so always true or always false at the first time.



-- 
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] WinkerDu commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   Thanks @alamb @yjshen 
   I agree this macro is just used in expression `evaluate`, maybe we can ignore it in this pr.
   I'll look into this case `NULL != column` carefully and add more cases in ut.
   Besides, I am super busy these days, I'll do it ASAP 🤟 



-- 
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] WinkerDu commented on pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

Posted by GitBox <gi...@apache.org>.
WinkerDu commented on PR #2481:
URL: https://github.com/apache/arrow-datafusion/pull/2481#issuecomment-1120390346

   cc @alamb @andygrove @yjshen @xudong963 
   Please have a review, thank you!


-- 
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] yjshen commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   1. I think we should respect `SortOptions` here as well for ScalaValue.
   2. Question: how about the `NULL != column` case, do we have an expression normalizer that asserts Nulls often coming as the right-hand op for a binary comparison?



-- 
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 #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   I will spend some time writing the `NULL != column` 



-- 
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] yjshen commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   1. I think we should respect `SortOptions` here for ScalaValue.
   2. Question: how about the `NULL != column` case? Do we have an expression normalizer that asserts Nulls often coming as the right-hand operand for a binary comparison?



-- 
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] yjshen commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   1. I think we should respect `SortOptions` here as well for ScalaValue.
   2. Question: how about the `NULL != column` case, do we have an expression normalizer that asserts `Null's often coming as the right-hand op for a binary comparison?



-- 
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] WinkerDu commented on a diff in pull request #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   Thanks @alamb @yjshen 
   I agree this macro is just used in expression `evaluate`, maybe we can ignore `SortOptions` in this pr.
   I'll look into this case `NULL != column` carefully and add more cases in ut.
   Besides, I am super busy these days, I'll do it ASAP 🤟 



-- 
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 #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   @yjshen  what do you mean `SortOptions`?  I don't think these expressions are used directly in ORDER BY clauses -- they are used instead for expressions like `a < b` 
   
   Adding some more tests in `sql/expr.rs`  for `NULL < column` and `NULL > column` sounds like a good idea. 



-- 
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 #2481: Numeric, String, Boolean comparisons with literal `NULL`

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -657,17 +657,15 @@ macro_rules! compute_utf8_op_scalar {
 
 /// Invoke a compute kernel on a data array and a scalar value
 macro_rules! compute_utf8_op_dyn_scalar {
-    ($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
+    ($LEFT:expr, $RIGHT:expr, $OP:ident, $OP_TYPE:expr) => {{
         if let Some(string_value) = $RIGHT {
             Ok(Arc::new(paste::expr! {[<$OP _dyn_utf8_scalar>]}(
                 $LEFT,
                 &string_value,
             )?))
         } else {
-            Err(DataFusionError::Internal(format!(
-                "compute_utf8_op_scalar for '{}' failed with literal 'none' value",
-                stringify!($OP),
-            )))
+            // when the $RIGHT is a NULL, generate a NULL array of $OP_TYPE
+            Ok(Arc::new(new_null_array($OP_TYPE, $LEFT.len())))

Review Comment:
   Follow on PR: https://github.com/apache/arrow-datafusion/pull/2510# turns out there was a bug!



-- 
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