DRILL-898: C++ Client. Fix decimal data type.

Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/a3bf05d3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/a3bf05d3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/a3bf05d3

Branch: refs/heads/master
Commit: a3bf05d3e1750187406b19f471570d93f9adde50
Parents: 632f5ca
Author: Xiao Meng <[email protected]>
Authored: Sat May 31 17:40:10 2014 -0700
Committer: Jacques Nadeau <[email protected]>
Committed: Thu Jun 19 20:30:46 2014 -0700

----------------------------------------------------------------------
 .../client/src/clientlib/decimalUtils.cpp       |  2 +-
 .../native/client/src/clientlib/recordBatch.cpp | 12 ++++++++
 .../native/client/src/include/drill/drillc.hpp  |  4 +--
 .../client/src/include/drill/recordBatch.hpp    | 30 +++++++++++++++-----
 4 files changed, 38 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a3bf05d3/contrib/native/client/src/clientlib/decimalUtils.cpp
----------------------------------------------------------------------
diff --git a/contrib/native/client/src/clientlib/decimalUtils.cpp 
b/contrib/native/client/src/clientlib/decimalUtils.cpp
index 3885faa..779ee72 100644
--- a/contrib/native/client/src/clientlib/decimalUtils.cpp
+++ b/contrib/native/client/src/clientlib/decimalUtils.cpp
@@ -88,7 +88,7 @@ DecimalValue getDecimalValueFromByteBuf(SlicedByteBuf& data, 
size_t startIndex,
     val.m_unscaledValue = decimalDigits;
 
     // set the sign
-    if (data.getUint32((startIndex) & 0x80000000) != 0)
+    if ((data.getUint32(startIndex) & 0x80000000) != 0)
     {
         val.m_unscaledValue *= -1;
     }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a3bf05d3/contrib/native/client/src/clientlib/recordBatch.cpp
----------------------------------------------------------------------
diff --git a/contrib/native/client/src/clientlib/recordBatch.cpp 
b/contrib/native/client/src/clientlib/recordBatch.cpp
index 81b9dbe..17073bd 100644
--- a/contrib/native/client/src/clientlib/recordBatch.cpp
+++ b/contrib/native/client/src/clientlib/recordBatch.cpp
@@ -238,6 +238,18 @@ ValueVectorBase* 
ValueVectorFactory::allocateValueVector(const Drill::FieldMetad
                     return new 
NullableValueVectorFixed<float>(b,f.getValueCount());
                 case common::FLOAT8:
                     return new 
NullableValueVectorFixed<double>(b,f.getValueCount());
+                case common::DECIMAL9:
+                    return new 
NullableValueVectorDecimal9(b,f.getValueCount(), f.getScale());
+                case common::DECIMAL18:
+                    return new 
NullableValueVectorDecimal18(b,f.getValueCount(), f.getScale());
+                case common::DECIMAL28DENSE:
+                    return new 
NullableValueVectorDecimal28Dense(b,f.getValueCount(), f.getScale());
+                case common::DECIMAL38DENSE:
+                    return new 
NullableValueVectorDecimal38Dense(b,f.getValueCount(), f.getScale());
+                case common::DECIMAL28SPARSE:
+                    return new 
NullableValueVectorDecimal28Sparse(b,f.getValueCount(), f.getScale());
+                case common::DECIMAL38SPARSE:
+                    return new 
NullableValueVectorDecimal38Sparse(b,f.getValueCount(), f.getScale());
                 case common::DATE:
                     return new NullableValueVectorTyped<DateHolder,
                            ValueVectorTyped<DateHolder, int64_t> 
>(b,f.getValueCount());

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a3bf05d3/contrib/native/client/src/include/drill/drillc.hpp
----------------------------------------------------------------------
diff --git a/contrib/native/client/src/include/drill/drillc.hpp 
b/contrib/native/client/src/include/drill/drillc.hpp
index 817b680..93a6b79 100644
--- a/contrib/native/client/src/include/drill/drillc.hpp
+++ b/contrib/native/client/src/include/drill/drillc.hpp
@@ -22,8 +22,8 @@
 #include "drill/common.hpp"
 #include "drill/drillClient.hpp"
 #include "drill/recordBatch.hpp"
-#include "Types.pb.h"
-#include "User.pb.h"
+#include "drill/protobuf/Types.pb.h"
+#include "drill/protobuf/User.pb.h"
 
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/a3bf05d3/contrib/native/client/src/include/drill/recordBatch.hpp
----------------------------------------------------------------------
diff --git a/contrib/native/client/src/include/drill/recordBatch.hpp 
b/contrib/native/client/src/include/drill/recordBatch.hpp
index 984588f..9798997 100644
--- a/contrib/native/client/src/include/drill/recordBatch.hpp
+++ b/contrib/native/client/src/include/drill/recordBatch.hpp
@@ -303,9 +303,13 @@ template <int DECIMAL_DIGITS, int WIDTH_IN_BYTES, bool 
IS_SPARSE, int MAX_PRECIS
             void getValueAt(size_t index, char* buf, size_t nChars) const {
                 const DecimalValue& val = this->get(index);
                 const std::string& str = 
boost::lexical_cast<std::string>(val.m_unscaledValue);
-                size_t idxDecimalMark = str.length() - m_scale;
-                const std::string& decStr= str.substr(0, idxDecimalMark) + "." 
+ str.substr(idxDecimalMark, m_scale);
-                strncpy(buf, decStr.c_str(), nChars);
+                if (m_scale == 0) {
+                    strncpy(buf, str.c_str(), nChars);
+                } else {
+                    size_t idxDecimalMark = str.length() - m_scale;
+                    const std::string& decStr= str.substr(0, idxDecimalMark) + 
"." + str.substr(idxDecimalMark, m_scale);
+                    strncpy(buf, decStr.c_str(), nChars);
+                }
                 return;
             }
 
@@ -336,9 +340,13 @@ template<typename VALUE_TYPE>
             void getValueAt(size_t index, char* buf, size_t nChars) const {
                 VALUE_TYPE value = m_pBuffer->readAt<VALUE_TYPE>(index * 
sizeof(VALUE_TYPE));
                 const std::string& str = 
boost::lexical_cast<std::string>(value);
-                size_t idxDecimalMark = str.length() - m_scale;
-                const std::string& decStr= str.substr(0, idxDecimalMark) + "." 
+ str.substr(idxDecimalMark, m_scale);
-                strncpy(buf, decStr.c_str(), nChars);
+                if (m_scale == 0) {
+                    strncpy(buf, str.c_str(), nChars);
+                } else {
+                    size_t idxDecimalMark = str.length() - m_scale;
+                    const std::string& decStr= str.substr(0, idxDecimalMark) + 
"." + str.substr(idxDecimalMark, m_scale);
+                    strncpy(buf, decStr.c_str(), nChars);
+                }
                 return;
             }
 
@@ -559,6 +567,13 @@ template <class VALUEHOLDER_CLASS_TYPE, class 
VALUE_VECTOR_TYPE>
                 this->m_pData= new SlicedByteBuf(*b, offsetEnd, 
b->getLength()-offsetEnd);
                 this->m_pVector= new VALUE_VECTOR_TYPE(m_pData, rowCount);
             }
+            // Specialized for Decimal Types
+            NullableValueVectorTyped(SlicedByteBuf *b, size_t rowCount, 
int32_t scale):ValueVectorBase(b, rowCount){
+                size_t offsetEnd = (size_t)ceil(rowCount/8.0);
+                this->m_pBitmap= new SlicedByteBuf(*b, 0, offsetEnd);
+                this->m_pData= new SlicedByteBuf(*b, offsetEnd, 
b->getLength()-offsetEnd);
+                this->m_pVector= new VALUE_VECTOR_TYPE(m_pData, rowCount, 
scale);
+            }
 
             ~NullableValueVectorTyped(){
                 delete this->m_pBitmap;
@@ -732,11 +747,12 @@ class DECLSPEC_DRILL_CLIENT FieldMetadata{
             m_precision=f.major_type().precision();
             m_bufferLength=f.buffer_length();
         }
-        const std::string& getName(){ return m_name;}
+        const std::string& getName() const{ return m_name;}
         common::MinorType getMinorType() const{ return m_minorType;}
         common::DataMode getDataMode() const{return m_dataMode;}
         uint32_t getValueCount() const{return m_valueCount;}
         uint32_t getScale() const{return m_scale;}
+        uint32_t getPrecision() const{return m_precision;}
         uint32_t getBufferLength() const{return m_bufferLength;}
         void copy(Drill::FieldMetadata& f){
             m_name=f.m_name;

Reply via email to