You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ad...@apache.org on 2018/04/20 22:57:17 UTC

kudu git commit: KUDU-2412: Fix python client compilation in el6 environments

Repository: kudu
Updated Branches:
  refs/heads/branch-1.7.x 45b3f2105 -> 4a3a2b718


KUDU-2412: Fix python client compilation in el6 environments

This uses the C preprocessor to check the value of KUDU_INT128_SUPPORTED from the C++ headers
and defines a Cython macro PYKUDU_INT128_SUPPORTED.

PYKUDU_INT128_SUPPORTED is generated by
replacing the KUDU_INT128_SUPPORTED placeholder
in config.pxi.in to generate config.pxi which is then included
in client.pyx.

This is then used to conditionally generate the bits of Python code
that depend on int128-related methods.

See here for documentation:
http://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#conditional-statements

Change-Id: Ibd93b57020b80597baae9c8d3e0434c46f7fc3d7
Reviewed-on: http://gerrit.cloudera.org:8080/10114
Reviewed-by: Adar Dembo <ad...@cloudera.com>
Tested-by: Grant Henke <gr...@apache.org>
(cherry picked from commit 3a801d10438d7e247bf21c4923d46f386c4aa965)
Reviewed-on: http://gerrit.cloudera.org:8080/10140
Tested-by: Adar Dembo <ad...@cloudera.com>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/4a3a2b71
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/4a3a2b71
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/4a3a2b71

Branch: refs/heads/branch-1.7.x
Commit: 4a3a2b718ca7a12e27c6749329b079c95df2023c
Parents: 45b3f21
Author: Grant Henke <gr...@apache.org>
Authored: Wed Apr 18 22:38:05 2018 -0500
Committer: Adar Dembo <ad...@cloudera.com>
Committed: Fri Apr 20 22:56:51 2018 +0000

----------------------------------------------------------------------
 python/.gitignore                   |  1 +
 python/kudu/__init__.py             |  3 +-
 python/kudu/client.pyx              | 27 ++++++++++++++----
 python/kudu/config.pxi.in           | 23 +++++++++++++++
 python/kudu/libkudu_client.pxd      |  3 ++
 python/kudu/schema.pyx              | 12 ++++++--
 python/kudu/tests/test_scanner.py   |  5 ++--
 python/kudu/tests/test_scantoken.py |  5 ++--
 python/kudu/tests/util.py           | 49 ++++++++++++++++++++------------
 python/setup.py                     | 33 +++++++++++++++++++--
 10 files changed, 127 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/.gitignore
----------------------------------------------------------------------
diff --git a/python/.gitignore b/python/.gitignore
index ba75271..aa30be7 100644
--- a/python/.gitignore
+++ b/python/.gitignore
@@ -69,3 +69,4 @@ var/
 
 # automatically generated during local development
 kudu/version.py
+kudu/config.pxi
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/__init__.py
----------------------------------------------------------------------
diff --git a/python/kudu/__init__.py b/python/kudu/__init__.py
index 61c729d..8939545 100644
--- a/python/kudu/__init__.py
+++ b/python/kudu/__init__.py
@@ -29,7 +29,8 @@ from kudu.client import (Client, Table, Scanner, Session,  # noqa
                          READ_AT_SNAPSHOT,
                          READ_YOUR_WRITES,
                          EXCLUSIVE_BOUND,
-                         INCLUSIVE_BOUND)
+                         INCLUSIVE_BOUND,
+                         CLIENT_SUPPORTS_DECIMAL)
 
 from kudu.errors import (KuduException, KuduBadStatus, KuduNotFound,  # noqa
                          KuduNotSupported,

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/client.pyx
----------------------------------------------------------------------
diff --git a/python/kudu/client.pyx b/python/kudu/client.pyx
index ab43851..619e66c 100644
--- a/python/kudu/client.pyx
+++ b/python/kudu/client.pyx
@@ -18,6 +18,8 @@
 # distutils: language = c++
 # cython: embedsignature = True
 
+include "config.pxi"
+
 from libcpp.string cimport string
 from libcpp cimport bool as c_bool
 from libcpp.map cimport map
@@ -35,6 +37,15 @@ from errors import KuduException
 
 import six
 
+# True if this python client was compiled with a
+# compiler that supports __int128 which is required
+# for decimal type support. This is generally true
+# except for EL6 environments.
+IF PYKUDU_INT128_SUPPORTED == 1:
+    CLIENT_SUPPORTS_DECIMAL = True
+ELSE:
+    CLIENT_SUPPORTS_DECIMAL = False
+
 # Replica selection enums
 LEADER_ONLY = ReplicaSelection_Leader
 CLOSEST_REPLICA = ReplicaSelection_Closest
@@ -1319,9 +1330,12 @@ cdef class Row:
         return val
 
     cdef inline __get_unscaled_decimal(self, int i):
-        cdef int128_t val
-        check_status(self.row.GetUnscaledDecimal(i, &val))
-        return val
+        IF PYKUDU_INT128_SUPPORTED == 1:
+            cdef int128_t val
+            check_status(self.row.GetUnscaledDecimal(i, &val))
+            return val
+        ELSE:
+            raise KuduException("The decimal type is not supported when GCC version is < 4.6.0" % self)
 
     cdef inline get_decimal(self, int i):
         scale = self.parent.batch.projection_schema().Column(i).type_attributes().scale()
@@ -2454,7 +2468,6 @@ cdef class PartialRow:
         elif t == KUDU_STRING:
             if isinstance(value, unicode):
                 value = value.encode('utf8')
-
             slc = Slice(<char*> value, len(value))
             check_status(self.row.SetStringCopy(i, slc))
         elif t == KUDU_BINARY:
@@ -2468,8 +2481,10 @@ cdef class PartialRow:
             check_status(self.row.SetUnixTimeMicros(i, <int64_t>
                 to_unixtime_micros(value)))
         elif t == KUDU_DECIMAL:
-            check_status(self.row.SetUnscaledDecimal(i, <int128_t>
-                to_unscaled_decimal(value)))
+            IF PYKUDU_INT128_SUPPORTED == 1:
+                check_status(self.row.SetUnscaledDecimal(i, <int128_t>to_unscaled_decimal(value)))
+            ELSE:
+                raise KuduException("The decimal type is not supported when GCC version is < 4.6.0" % self)
         else:
             raise TypeError("Cannot set kudu type <{0}>.".format(_type_names[t]))
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/config.pxi.in
----------------------------------------------------------------------
diff --git a/python/kudu/config.pxi.in b/python/kudu/config.pxi.in
new file mode 100644
index 0000000..90d463a
--- /dev/null
+++ b/python/kudu/config.pxi.in
@@ -0,0 +1,23 @@
+// 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.
+
+// This file is processed by setup.py
+// See generate_config_pxi.
+
+// KUDU_INT128_SUPPORTED is substituted from the C++ macro.
+// PYKUDU_INT128_SUPPORTED becomes a Cython macro.
+DEF PYKUDU_INT128_SUPPORTED = KUDU_INT128_SUPPORTED

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/libkudu_client.pxd
----------------------------------------------------------------------
diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd
index e2bc90d..e8f5913 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -63,6 +63,9 @@ cdef extern from "kudu/util/status.h" namespace "kudu" nogil:
         c_bool IsAborted()
 
 cdef extern from "kudu/util/int128.h" namespace "kudu":
+    # see https://gist.github.com/ricrogz/7f9c405450689866139c49476b959044
+    # This can be defined on the Python side even if it's not defined on the
+    # C++ side.
     ctypedef int int128_t
 
 cdef extern from "kudu/util/monotime.h" namespace "kudu" nogil:

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/schema.pyx
----------------------------------------------------------------------
diff --git a/python/kudu/schema.pyx b/python/kudu/schema.pyx
index ed17378..8a3f9ee 100644
--- a/python/kudu/schema.pyx
+++ b/python/kudu/schema.pyx
@@ -18,6 +18,8 @@
 # distutils: language = c++
 # cython: embedsignature = True
 
+include "config.pxi"
+
 from cython.operator cimport dereference as deref
 
 from kudu.compat import tobytes, frombytes
@@ -25,6 +27,7 @@ from kudu.schema cimport *
 from kudu.errors cimport check_status
 from kudu.client cimport PartialRow
 from kudu.util import get_decimal_scale, to_unixtime_micros, to_unscaled_decimal
+from errors import KuduException
 
 import six
 
@@ -762,9 +765,12 @@ cdef class KuduValue:
             value = to_unixtime_micros(value)
             self._value = C_KuduValue.FromInt(value)
         elif (type_.name == 'decimal'):
-            scale = get_decimal_scale(value)
-            value = to_unscaled_decimal(value)
-            self._value = C_KuduValue.FromDecimal(value, scale)
+            IF PYKUDU_INT128_SUPPORTED == 1:
+                scale = get_decimal_scale(value)
+                value = to_unscaled_decimal(value)
+                self._value = C_KuduValue.FromDecimal(value, scale)
+            ELSE:
+                raise KuduException("The decimal type is not supported when GCC version is < 4.6.0" % self)
         else:
             raise TypeError("Cannot initialize KuduValue for kudu type <{0}>"
                             .format(type_.name))

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/tests/test_scanner.py
----------------------------------------------------------------------
diff --git a/python/kudu/tests/test_scanner.py b/python/kudu/tests/test_scanner.py
index 23d5462..1731ce8 100644
--- a/python/kudu/tests/test_scanner.py
+++ b/python/kudu/tests/test_scanner.py
@@ -267,8 +267,9 @@ class TestScanner(TestScanBase):
         self._test_float_pred()
 
     def test_decimal_pred(self):
-        # Test a decimal predicate
-        self._test_decimal_pred()
+        if kudu.CLIENT_SUPPORTS_DECIMAL:
+            # Test a decimal predicate
+            self._test_decimal_pred()
 
     def test_binary_pred(self):
         # Test a binary predicate

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/tests/test_scantoken.py
----------------------------------------------------------------------
diff --git a/python/kudu/tests/test_scantoken.py b/python/kudu/tests/test_scantoken.py
index 7d664c0..37e7f7d 100644
--- a/python/kudu/tests/test_scantoken.py
+++ b/python/kudu/tests/test_scantoken.py
@@ -254,8 +254,9 @@ class TestScanToken(TestScanBase):
         self._test_unixtime_micros_pred()
 
     def test_decimal_pred(self):
-        # Test decimal value predicate
-        self._test_decimal_pred()
+        if kudu.CLIENT_SUPPORTS_DECIMAL:
+            # Test decimal value predicate
+            self._test_decimal_pred()
 
     def test_bool_pred(self):
         # Test a boolean value predicate

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/kudu/tests/util.py
----------------------------------------------------------------------
diff --git a/python/kudu/tests/util.py b/python/kudu/tests/util.py
index e6d1334..a6779fc 100644
--- a/python/kudu/tests/util.py
+++ b/python/kudu/tests/util.py
@@ -25,7 +25,6 @@ import kudu
 import datetime
 import pytz
 
-
 class TestScanBase(KuduTestBase, unittest.TestCase):
 
     @classmethod
@@ -72,7 +71,8 @@ class TestScanBase(KuduTestBase, unittest.TestCase):
         builder = kudu.schema_builder()
         builder.add_column('key').type(kudu.int64).nullable(False)
         builder.add_column('unixtime_micros_val', type_=kudu.unixtime_micros, nullable=False)
-        builder.add_column('decimal_val', type_=kudu.decimal, precision=5, scale=2)
+        if kudu.CLIENT_SUPPORTS_DECIMAL:
+            builder.add_column('decimal_val', type_=kudu.decimal, precision=5, scale=2)
         builder.add_column('string_val', type_=kudu.string, compression=kudu.COMPRESSION_LZ4, encoding='prefix')
         builder.add_column('bool_val', type_=kudu.bool)
         builder.add_column('double_val', type_=kudu.double)
@@ -104,16 +104,28 @@ class TestScanBase(KuduTestBase, unittest.TestCase):
         self.type_table = self.client.table(table_name)
 
         # Insert new rows
-        self.type_test_rows = [
-            (1, datetime.datetime(2016, 1, 1).replace(tzinfo=pytz.utc), Decimal('111.11'),
-             "Test One", True, 1.7976931348623157 * (10^308), 127,
-             b'\xce\x99\xce\xbf\xcf\x81\xce\xb4\xce\xb1\xce\xbd\xce\xaf\xce\xb1',
-             3.402823 * (10^38)),
-            (2, datetime.datetime.utcnow().replace(tzinfo=pytz.utc), Decimal('0.99'),
-             "测试二", False, 200.1, -1,
-             b'\xd0\x98\xd0\xbe\xd1\x80\xd0\xb4\xd0\xb0\xd0\xbd\xd0\xb8\xd1\x8f',
-             -150.2)
-        ]
+        if kudu.CLIENT_SUPPORTS_DECIMAL:
+            self.type_test_rows = [
+                (1, datetime.datetime(2016, 1, 1).replace(tzinfo=pytz.utc), Decimal('111.11'),
+                 "Test One", True, 1.7976931348623157 * (10^308), 127,
+                 b'\xce\x99\xce\xbf\xcf\x81\xce\xb4\xce\xb1\xce\xbd\xce\xaf\xce\xb1',
+                 3.402823 * (10^38)),
+                (2, datetime.datetime.utcnow().replace(tzinfo=pytz.utc), Decimal('0.99'),
+                 "测试二", False, 200.1, -1,
+                 b'\xd0\x98\xd0\xbe\xd1\x80\xd0\xb4\xd0\xb0\xd0\xbd\xd0\xb8\xd1\x8f',
+                 -150.2)
+            ]
+        else:
+            self.type_test_rows = [
+                (1, datetime.datetime(2016, 1, 1).replace(tzinfo=pytz.utc),
+                 "Test One", True, 1.7976931348623157 * (10 ^ 308), 127,
+                 b'\xce\x99\xce\xbf\xcf\x81\xce\xb4\xce\xb1\xce\xbd\xce\xaf\xce\xb1',
+                 3.402823 * (10 ^ 38)),
+                (2, datetime.datetime.utcnow().replace(tzinfo=pytz.utc),
+                 "测试二", False, 200.1, -1,
+                 b'\xd0\x98\xd0\xbe\xd1\x80\xd0\xb4\xd0\xb0\xd0\xbd\xd0\xb8\xd1\x8f',
+                 -150.2)
+            ]
         session = self.client.new_session()
         for row in self.type_test_rows:
             op = self.type_table.new_insert(row)
@@ -234,12 +246,13 @@ class TestScanBase(KuduTestBase, unittest.TestCase):
         )
 
     def _test_decimal_pred(self):
-        self.verify_pred_type_scans(
-            preds=[
-                self.type_table['decimal_val'] == Decimal('111.11')
-            ],
-            row_indexes=slice(0, 1),
-        )
+        if kudu.CLIENT_SUPPORTS_DECIMAL:
+            self.verify_pred_type_scans(
+                preds=[
+                    self.type_table['decimal_val'] == Decimal('111.11')
+                ],
+                row_indexes=slice(0, 1),
+            )
 
     def _test_binary_pred(self):
         self.verify_pred_type_scans(

http://git-wip-us.apache.org/repos/asf/kudu/blob/4a3a2b71/python/setup.py
----------------------------------------------------------------------
diff --git a/python/setup.py b/python/setup.py
index 16f7b5a..abd527a 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -27,6 +27,7 @@ from setuptools import setup
 from distutils.command.clean import clean as _clean
 from distutils.extension import Extension
 import os
+import subprocess
 
 # Workaround a Python bug in which multiprocessing's atexit handler doesn't
 # play well with pytest. See http://bugs.python.org/issue15881 for details
@@ -64,13 +65,39 @@ def write_version_py(filename=os.path.join(setup_dir, 'kudu/version.py')):
 class clean(_clean):
     def run(self):
         _clean.run(self)
-        for x in ['kudu/client.cpp', 'kudu/schema.cpp',
-                  'kudu/errors.cpp']:
+        for x in ['kudu/client.cpp',
+                  'kudu/config.pxi',
+                  'kudu/errors.cpp',
+                  'kudu/schema.cpp']:
             try:
                 os.remove(x)
             except OSError:
                 pass
 
+# Identify the cc version used and check for __int128 support
+def generate_config_pxi(include_dirs):
+    """ Generate config.pxi from config.pxi.in """
+    int128_h = None
+    for d in include_dirs:
+        candidate = os.path.join(d, 'kudu/util/int128.h')
+        if os.path.exists(candidate):
+            int128_h = candidate
+            break
+    if int128_h is None:
+        raise Exception("could not find int128.h in Kudu include dirs")
+    src = os.path.join(setup_dir, 'kudu/config.pxi.in')
+    dst = os.path.join(setup_dir, 'kudu/config.pxi')
+    dst_tmp = dst + '.tmp'
+    cc = os.getenv("CC","cc")
+    subprocess.check_call([cc, "-x", "c++", "-o", dst_tmp,
+                           "-E", '-imacros', int128_h, src])
+    # If our generated file is the same as the prior version,
+    # don't replace it. This avoids rebuilding everything on every
+    # run of setup.py.
+    if os.path.exists(dst) and open(dst).read() == open(dst_tmp).read():
+      os.unlink(dst_tmp)
+    else:
+      os.rename(dst_tmp, dst)
 
 # If we're in the context of the Kudu git repository, build against the
 # latest in-tree build artifacts
@@ -111,6 +138,8 @@ INCLUDE_PATHS = kudu_include_dirs
 LIBRARY_DIRS = [kudu_lib_dir]
 RT_LIBRARY_DIRS = LIBRARY_DIRS
 
+generate_config_pxi(INCLUDE_PATHS)
+
 ext_submodules = ['client', 'errors', 'schema']
 
 extensions = []