You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2019/01/10 08:07:51 UTC

[arrow] branch master updated: ARROW-4207: [Gandiva] [GLib] Add support for IfNode

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

kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new ea69e8f  ARROW-4207: [Gandiva] [GLib] Add support for IfNode
ea69e8f is described below

commit ea69e8fe4901329e53455c8d6fafad1c4f35d827
Author: Yosuke Shiro <yo...@gmail.com>
AuthorDate: Thu Jan 10 17:07:39 2019 +0900

    ARROW-4207: [Gandiva] [GLib] Add support for IfNode
    
    Author: Yosuke Shiro <yo...@gmail.com>
    Author: Kouhei Sutou <ko...@clear-code.com>
    
    Closes #3354 from shiro615/glib-add-support-for-if-node and squashes the following commits:
    
    d543ea00 <Kouhei Sutou> Add support for error
    8058b2c7 <Yosuke Shiro>  Add support for IfNode
---
 c_glib/gandiva-glib/node.cpp        | 207 ++++++++++++++++++++++++++++++++++++
 c_glib/gandiva-glib/node.h          |  19 ++++
 c_glib/gandiva-glib/node.hpp        |   6 ++
 c_glib/test/gandiva/test-if-node.rb |  49 +++++++++
 4 files changed, 281 insertions(+)

diff --git a/c_glib/gandiva-glib/node.cpp b/c_glib/gandiva-glib/node.cpp
index 2c68cbe..a3814c1 100644
--- a/c_glib/gandiva-glib/node.cpp
+++ b/c_glib/gandiva-glib/node.cpp
@@ -95,6 +95,8 @@ G_BEGIN_DECLS
  * #GGandivaStringLiteralNode is a class for a node in the expression tree,
  * representing an UTF-8 encoded string literal.
  *
+ * #GGandivaIfNode is a class for a node in the expression tree, representing an if-else.
+ *
  * Since: 0.12.0
  */
 
@@ -1180,6 +1182,194 @@ ggandiva_string_literal_node_get_value(GGandivaStringLiteralNode *node)
   return value.c_str();
 }
 
+
+typedef struct GGandivaIfNodePrivate_ {
+  GGandivaNode *condition_node;
+  GGandivaNode *then_node;
+  GGandivaNode *else_node;
+} GGandivaIfNodePrivate;
+
+enum {
+  PROP_CONDITION_NODE = 1,
+  PROP_THEN_NODE,
+  PROP_ELSE_NODE,
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(GGandivaIfNode,
+                           ggandiva_if_node,
+                           GGANDIVA_TYPE_NODE)
+
+#define GGANDIVA_IF_NODE_GET_PRIVATE(object)                 \
+  static_cast<GGandivaIfNodePrivate *>(                      \
+    ggandiva_if_node_get_instance_private(                   \
+      GGANDIVA_IF_NODE(object)))
+
+static void
+ggandiva_if_node_dispose(GObject *object)
+{
+  auto priv = GGANDIVA_IF_NODE_GET_PRIVATE(object);
+
+  if (priv->condition_node) {
+    g_object_unref(priv->condition_node);
+    priv->condition_node = nullptr;
+  }
+
+  if (priv->then_node) {
+    g_object_unref(priv->then_node);
+    priv->then_node = nullptr;
+  }
+
+  if (priv->else_node) {
+    g_object_unref(priv->else_node);
+    priv->else_node = nullptr;
+  }
+
+  G_OBJECT_CLASS(ggandiva_if_node_parent_class)->dispose(object);
+}
+
+static void
+ggandiva_if_node_set_property(GObject *object,
+                              guint prop_id,
+                              const GValue *value,
+                              GParamSpec *pspec)
+{
+  auto priv = GGANDIVA_IF_NODE_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_CONDITION_NODE:
+    priv->condition_node = GGANDIVA_NODE(g_value_dup_object(value));
+    break;
+  case PROP_THEN_NODE:
+    priv->then_node = GGANDIVA_NODE(g_value_dup_object(value));
+    break;
+  case PROP_ELSE_NODE:
+    priv->else_node = GGANDIVA_NODE(g_value_dup_object(value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+ggandiva_if_node_get_property(GObject *object,
+                              guint prop_id,
+                              GValue *value,
+                              GParamSpec *pspec)
+{
+  auto priv = GGANDIVA_IF_NODE_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_CONDITION_NODE:
+    g_value_set_object(value, priv->condition_node);
+    break;
+  case PROP_THEN_NODE:
+    g_value_set_object(value, priv->then_node);
+    break;
+  case PROP_ELSE_NODE:
+    g_value_set_object(value, priv->else_node);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+ggandiva_if_node_init(GGandivaIfNode *if_node)
+{
+}
+
+static void
+ggandiva_if_node_class_init(GGandivaIfNodeClass *klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->dispose      = ggandiva_if_node_dispose;
+  gobject_class->set_property = ggandiva_if_node_set_property;
+  gobject_class->get_property = ggandiva_if_node_get_property;
+
+  GParamSpec *spec;
+  spec = g_param_spec_object("condition-node",
+                             "Condition node",
+                             "The condition node",
+                             GGANDIVA_TYPE_NODE,
+                             static_cast<GParamFlags>(G_PARAM_READWRITE |
+                                                      G_PARAM_CONSTRUCT_ONLY));
+  g_object_class_install_property(gobject_class, PROP_CONDITION_NODE, spec);
+
+  spec = g_param_spec_object("then-node",
+                             "Then node",
+                             "The then node",
+                             GGANDIVA_TYPE_NODE,
+                             static_cast<GParamFlags>(G_PARAM_READWRITE |
+                                                      G_PARAM_CONSTRUCT_ONLY));
+  g_object_class_install_property(gobject_class, PROP_THEN_NODE, spec);
+
+  spec = g_param_spec_object("else-node",
+                             "Else node",
+                             "The else node",
+                             GGANDIVA_TYPE_NODE,
+                             static_cast<GParamFlags>(G_PARAM_READWRITE |
+                                                      G_PARAM_CONSTRUCT_ONLY));
+  g_object_class_install_property(gobject_class, PROP_ELSE_NODE, spec);
+}
+
+/**
+ * ggandiva_if_node_new:
+ * @condition_node: the node with the condition for if-else expression.
+ * @then_node: the node in case the condition node is true.
+ * @else_node: the node in case the condition node is false.
+ * @return_type: A #GArrowDataType.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: (nullable): A newly created #GGandivaIfNode or %NULl on error.
+ *
+ * Since: 0.12.0
+ */
+GGandivaIfNode *
+ggandiva_if_node_new(GGandivaNode *condition_node,
+                     GGandivaNode *then_node,
+                     GGandivaNode *else_node,
+                     GArrowDataType *return_type,
+                     GError **error)
+{
+  if (!condition_node || !then_node || !else_node || !return_type) {
+    /* TODO: Improve error message to show which arguments are invalid. */
+    g_set_error(error,
+                GARROW_ERROR,
+                GARROW_ERROR_INVALID,
+                "[gandiva][if-literal-node][new] "
+                "all arguments must not NULL");
+    return NULL;
+  }
+  auto gandiva_condition_node = ggandiva_node_get_raw(condition_node);
+  auto gandiva_then_node = ggandiva_node_get_raw(then_node);
+  auto gandiva_else_node = ggandiva_node_get_raw(else_node);
+  auto arrow_return_type = garrow_data_type_get_raw(return_type);
+  auto gandiva_node = gandiva::TreeExprBuilder::MakeIf(gandiva_condition_node,
+                                                       gandiva_then_node,
+                                                       gandiva_else_node,
+                                                       arrow_return_type);
+  if (!gandiva_node) {
+    g_set_error(error,
+                GARROW_ERROR,
+                GARROW_ERROR_INVALID,
+                "[gandiva][if-literal-node][new] "
+                "failed to create: if (<%s>) {<%s>} else {<%s>} -> <%s>",
+                gandiva_condition_node->ToString().c_str(),
+                gandiva_then_node->ToString().c_str(),
+                gandiva_else_node->ToString().c_str(),
+                arrow_return_type->ToString().c_str());
+    return NULL;
+  }
+  return ggandiva_if_node_new_raw(&gandiva_node,
+                                  condition_node,
+                                  then_node,
+                                  else_node,
+                                  return_type);
+}
+
 G_END_DECLS
 
 std::shared_ptr<gandiva::Node>
@@ -1305,3 +1495,20 @@ ggandiva_literal_node_new_raw(std::shared_ptr<gandiva::Node> *gandiva_node,
 
   return literal_node;
 }
+
+GGandivaIfNode *
+ggandiva_if_node_new_raw(std::shared_ptr<gandiva::Node> *gandiva_node,
+                         GGandivaNode *condition_node,
+                         GGandivaNode *then_node,
+                         GGandivaNode *else_node,
+                         GArrowDataType *return_type)
+{
+  auto if_node = g_object_new(GGANDIVA_TYPE_IF_NODE,
+                              "node", gandiva_node,
+                              "condition-node", condition_node,
+                              "then-node", then_node,
+                              "else-node", else_node,
+                              "return-type", return_type,
+                              NULL);
+  return GGANDIVA_IF_NODE(if_node);
+}
diff --git a/c_glib/gandiva-glib/node.h b/c_glib/gandiva-glib/node.h
index d9e67e2..ffcf41d 100644
--- a/c_glib/gandiva-glib/node.h
+++ b/c_glib/gandiva-glib/node.h
@@ -320,4 +320,23 @@ ggandiva_string_literal_node_new(const gchar *value);
 const gchar *
 ggandiva_string_literal_node_get_value(GGandivaStringLiteralNode *node);
 
+
+#define GGANDIVA_TYPE_IF_NODE (ggandiva_if_node_get_type())
+G_DECLARE_DERIVABLE_TYPE(GGandivaIfNode,
+                         ggandiva_if_node,
+                         GGANDIVA,
+                         IF_NODE,
+                         GGandivaNode)
+struct _GGandivaIfNodeClass
+{
+  GGandivaNodeClass parent_class;
+};
+
+GGandivaIfNode *
+ggandiva_if_node_new(GGandivaNode *condition_node,
+                     GGandivaNode *then_node,
+                     GGandivaNode *else_node,
+                     GArrowDataType *return_type,
+                     GError **error);
+
 G_END_DECLS
diff --git a/c_glib/gandiva-glib/node.hpp b/c_glib/gandiva-glib/node.hpp
index 40f9d1b..9a6ae98 100644
--- a/c_glib/gandiva-glib/node.hpp
+++ b/c_glib/gandiva-glib/node.hpp
@@ -38,3 +38,9 @@ ggandiva_function_node_new_raw(std::shared_ptr<gandiva::Node> *gandiva_node,
 GGandivaLiteralNode *
 ggandiva_literal_node_new_raw(std::shared_ptr<gandiva::Node> *gandiva_node,
                               GArrowDataType *return_type);
+GGandivaIfNode *
+ggandiva_if_node_new_raw(std::shared_ptr<gandiva::Node> *gandiva_node,
+                         GGandivaNode *condition_node,
+                         GGandivaNode *then_node,
+                         GGandivaNode *else_node,
+                         GArrowDataType *return_type);
diff --git a/c_glib/test/gandiva/test-if-node.rb b/c_glib/test/gandiva/test-if-node.rb
new file mode 100644
index 0000000..b003595
--- /dev/null
+++ b/c_glib/test/gandiva/test-if-node.rb
@@ -0,0 +1,49 @@
+# 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.
+
+class TestGandivaIfNode < Test::Unit::TestCase
+  def setup
+    omit("Gandiva is required") unless defined?(::Gandiva)
+    field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new)
+    field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new)
+    @then_node = Gandiva::FieldNode.new(field1)
+    @else_node = Gandiva::FieldNode.new(field2)
+    @return_type = Arrow::Int32DataType.new
+    @condition_node = Gandiva::FunctionNode.new("greater_than",
+                                                [@then_node, @else_node],
+                                                @return_type)
+    @if_node = Gandiva::IfNode.new(@condition_node,
+                                   @then_node,
+                                   @else_node,
+                                   @return_type)
+  end
+
+  def test_readers
+    assert_equal([
+                   @condition_node,
+                   @then_node,
+                   @else_node,
+                   @return_type
+                 ],
+                 [
+                   @if_node.condition_node,
+                   @if_node.then_node,
+                   @if_node.else_node,
+                   @if_node.return_type
+                 ])
+  end
+end