You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2022/07/30 11:18:02 UTC

[doris] 01/02: [fix] the nullable info is lost in ifnull expr (#11212)

This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch dev-1.1.2
in repository https://gitbox.apache.org/repos/asf/doris.git

commit bf77148b4d8863272553a4888cf4eb619334cc91
Author: starocean999 <40...@users.noreply.github.com>
AuthorDate: Fri Jul 29 21:33:58 2022 +0800

    [fix] the nullable info is lost in ifnull expr (#11212)
    
    ifnull function has defect when processing nullable column or const column in some case
---
 be/src/vec/functions/function_ifnull.h | 24 ++++++++++++++++++++----
 be/src/vec/functions/if.cpp            |  3 +++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/be/src/vec/functions/function_ifnull.h b/be/src/vec/functions/function_ifnull.h
index b6627641c0..1a83785c48 100644
--- a/be/src/vec/functions/function_ifnull.h
+++ b/be/src/vec/functions/function_ifnull.h
@@ -40,11 +40,21 @@ public:
 
     bool use_default_implementation_for_constants() const override { return false; }
 
+    // be compatible with fe code
+    /* 
+        if (fn.functionName().equalsIgnoreCase("ifnull") || fn.functionName().equalsIgnoreCase("nvl")) {
+            Preconditions.checkState(children.size() == 2);
+            if (children.get(0).isNullable()) {
+                return children.get(1).isNullable();
+            }
+            return false;
+        }
+    */
     DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
-        if (!arguments[0]->is_nullable() && arguments[1]->is_nullable()) {
-            return reinterpret_cast<const DataTypeNullable*>(arguments[1].get())->get_nested_type();
+        if (arguments[0]->is_nullable()) {
+            return arguments[1];
         }
-        return arguments[1];
+        return arguments[0];
     }
 
     bool use_default_implementation_for_nulls() const override { return false; }
@@ -75,10 +85,16 @@ public:
         const ColumnsWithTypeAndName if_columns {
                 null_column_arg0, block.get_by_position(arguments[1]), nested_column_arg0};
 
+        // see get_return_type_impl
+        // if result is nullable, means both then and else column are nullable, we use original col_left to keep nullable info
+        // if result is not nullable, means both then and else column are not nullable, we use nested_column_arg0 to remove nullable info
+        bool result_nullable = block.get_by_position(result).type->is_nullable();
         Block temporary_block({
                 null_column_arg0,
                 block.get_by_position(arguments[1]),
-                nested_column_arg0,
+                result_nullable
+                        ? col_left
+                        : nested_column_arg0, // if result is nullable, we need pass the original col_left else pass nested_column_arg0
                 block.get_by_position(result),
         });
 
diff --git a/be/src/vec/functions/if.cpp b/be/src/vec/functions/if.cpp
index 714203f492..40132b8dec 100644
--- a/be/src/vec/functions/if.cpp
+++ b/be/src/vec/functions/if.cpp
@@ -139,6 +139,9 @@ public:
     static ColumnPtr get_nested_column(const ColumnPtr& column) {
         if (auto* nullable = check_and_get_column<ColumnNullable>(*column))
             return nullable->get_nested_column_ptr();
+        else if (const auto* column_const = check_and_get_column<ColumnConst>(*column))
+            return ColumnConst::create(get_nested_column(column_const->get_data_column_ptr()),
+                                       column->size());
 
         return column;
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org