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

isapego pushed a commit to branch ignite-17607
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 3d50b796c363ceb3f155a784b54951b7d4dfd24e
Author: Igor Sapego <[email protected]>
AuthorDate: Wed Mar 15 01:02:23 2023 +0300

    IGNITE-17607 Add nullptr to primitive
---
 .../platforms/cpp/ignite/client/detail/utils.cpp   | 14 +++++++++++
 modules/platforms/cpp/ignite/client/primitive.h    | 29 ++++++++++++++++++++--
 .../platforms/cpp/ignite/client/primitive_test.cpp | 17 +++++++++++--
 3 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/modules/platforms/cpp/ignite/client/detail/utils.cpp 
b/modules/platforms/cpp/ignite/client/detail/utils.cpp
index 42a68ba7d9..5a21f1c37f 100644
--- a/modules/platforms/cpp/ignite/client/detail/utils.cpp
+++ b/modules/platforms/cpp/ignite/client/detail/utils.cpp
@@ -47,6 +47,13 @@ void append_type_and_scale(binary_tuple_builder &builder, 
ignite_type typ, std::
 }
 
 void claim_primitive_with_type(binary_tuple_builder &builder, const primitive 
&value) {
+    if (value.is_null()) {
+        builder.claim(std::nullopt); // Type.
+        builder.claim(std::nullopt); // Scale.
+        builder.claim(std::nullopt); // Value.
+        return;
+    }
+
     switch (value.get_type()) {
         case column_type::BOOLEAN: {
             claim_type_and_scale(builder, ignite_type::INT8);
@@ -115,6 +122,13 @@ void claim_primitive_with_type(binary_tuple_builder 
&builder, const primitive &v
 }
 
 void append_primitive_with_type(binary_tuple_builder &builder, const primitive 
&value) {
+    if (value.is_null()) {
+        builder.append(std::nullopt); // Type.
+        builder.append(std::nullopt); // Scale.
+        builder.append(std::nullopt); // Value.
+        return;
+    }
+
     switch (value.get_type()) {
         case column_type::BOOLEAN: {
             append_type_and_scale(builder, ignite_type::INT8);
diff --git a/modules/platforms/cpp/ignite/client/primitive.h 
b/modules/platforms/cpp/ignite/client/primitive.h
index 667fbb233f..a4a9f3b9b7 100644
--- a/modules/platforms/cpp/ignite/client/primitive.h
+++ b/modules/platforms/cpp/ignite/client/primitive.h
@@ -29,6 +29,7 @@
 #include "ignite/common/uuid.h"
 
 #include <cstdint>
+#include <optional>
 #include <type_traits>
 #include <variant>
 #include <vector>
@@ -43,6 +44,16 @@ public:
     // Default
     primitive() = default;
 
+    /**
+     * Null constructor.
+     */
+    primitive(std::nullptr_t) {} // NOLINT(google-explicit-constructor)
+
+    /**
+     * Null option constructor.
+     */
+    primitive(std::nullopt_t) {} // NOLINT(google-explicit-constructor)
+
     /**
      * Constructor for boolean value.
      *
@@ -237,12 +248,25 @@ public:
         }
     }
 
+    /**
+     * Check whether element is null.
+     *
+     * @return Value indicating whether element is null.
+     */
+    [[nodiscard]] bool is_null() const noexcept {
+        return m_value.index() == 0;
+    }
+
     /**
      * Get primitive type.
      *
      * @return Primitive type.
      */
-    [[nodiscard]] column_type get_type() const { return 
static_cast<column_type>(m_value.index()); }
+    [[nodiscard]] column_type get_type() const noexcept {
+        if (is_null())
+            return column_type::UNDEFINED;
+        return static_cast<column_type>(m_value.index() - 1);
+    }
 
     /**
      * @brief Comparison operator.
@@ -271,7 +295,8 @@ private:
     typedef void *unsupported_type;
 
     /** Value type. */
-    typedef std::variant<bool, // Bool = 0
+    typedef std::variant<std::nullptr_t,
+        bool, // Bool = 0
         std::int8_t, // Int8 = 1
         std::int16_t, // Int16 = 2
         std::int32_t, // Int32 = 3
diff --git a/modules/platforms/cpp/ignite/client/primitive_test.cpp 
b/modules/platforms/cpp/ignite/client/primitive_test.cpp
index 46a549f832..b71b3033e3 100644
--- a/modules/platforms/cpp/ignite/client/primitive_test.cpp
+++ b/modules/platforms/cpp/ignite/client/primitive_test.cpp
@@ -23,11 +23,12 @@ using namespace ignite;
 
 template<typename T>
 void check_primitive_type(column_type expected) {
-    primitive val_bool(T{});
-    EXPECT_EQ(val_bool.get_type(), expected);
+    primitive val(T{});
+    EXPECT_EQ(val.get_type(), expected);
 }
 
 TEST(primitive, get_column_type) {
+    check_primitive_type<nullptr_t>(column_type::UNDEFINED);
     check_primitive_type<bool>(column_type::BOOLEAN);
     check_primitive_type<int8_t>(column_type::INT8);
     check_primitive_type<int16_t>(column_type::INT16);
@@ -46,3 +47,15 @@ TEST(primitive, get_column_type) {
     check_primitive_type<std::vector<std::byte>>(column_type::BYTE_ARRAY);
     check_primitive_type<big_integer>(column_type::NUMBER);
 }
+
+TEST(primitive, null_value_by_nullptr) {
+    primitive val(nullptr);
+    EXPECT_EQ(val.get_type(), column_type::UNDEFINED);
+    EXPECT_TRUE(val.is_null());
+}
+
+TEST(primitive, null_value_by_nullopt) {
+    primitive val(std::nullopt);
+    EXPECT_EQ(val.get_type(), column_type::UNDEFINED);
+    EXPECT_TRUE(val.is_null());
+}

Reply via email to