1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License
17 
18 #pragma once
19 
20 #include <cstdint>
21 #include <string>
22 #include "arrow/util/decimal.h"
23 #include "gandiva/basic_decimal_scalar.h"
24 
25 namespace gandiva {
26 
27 using Decimal128 = arrow::Decimal128;
28 
29 /// Represents a 128-bit decimal value along with its precision and scale.
30 ///
31 /// BasicDecimalScalar128 can be safely compiled to IR without references to libstdc++.
32 /// This class has additional functionality on top of BasicDecimalScalar128 to deal with
33 /// strings and streams.
34 class DecimalScalar128 : public BasicDecimalScalar128 {
35  public:
36   using BasicDecimalScalar128::BasicDecimalScalar128;
37 
DecimalScalar128(const std::string & value,int32_t precision,int32_t scale)38   DecimalScalar128(const std::string& value, int32_t precision, int32_t scale)
39       : BasicDecimalScalar128(Decimal128(value), precision, scale) {}
40 
41   /// \brief constructor creates a DecimalScalar128 from a BasicDecimalScalar128.
DecimalScalar128(const BasicDecimalScalar128 & scalar)42   constexpr DecimalScalar128(const BasicDecimalScalar128& scalar) noexcept
43       : BasicDecimalScalar128(scalar) {}
44 
ToString()45   inline std::string ToString() const {
46     Decimal128 dvalue(value());
47     return dvalue.ToString(0) + "," + std::to_string(precision()) + "," +
48            std::to_string(scale());
49   }
50 
51   friend std::ostream& operator<<(std::ostream& os, const DecimalScalar128& dec) {
52     os << dec.ToString();
53     return os;
54   }
55 };
56 
57 }  // namespace gandiva
58