Skip to content

Commit 4e970bf

Browse files
resetiusGazizonoki
authored andcommitted
Moved commit "Add decimal support to arrow reader" from ydb repo
1 parent ab3783c commit 4e970bf

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/library/yql_common/decimal/yql_decimal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ TInt128 FromString(const std::string_view& str, ui8 precision, ui8 scale) {
139139
if (IsInf(s))
140140
return neg ? -Inf() : Inf();
141141
if (IsNan(s))
142-
return neg ? -Nan() : Nan();
142+
return Nan();
143143
}
144144

145145
TUint128 v = 0U;

src/library/yql_common/decimal/yql_decimal.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,61 @@ TInt128 MulAndDivNormalDivider(TInt128 a, TInt128 b, TInt128 c);
156156
// a*b/c Only for non zero normal positive multiplier.
157157
TInt128 MulAndDivNormalMultiplier(TInt128 a, TInt128 b, TInt128 c);
158158

159+
struct TDecimal {
160+
TInt128 Value = 0;
161+
162+
TDecimal() = default;
163+
164+
template<typename T>
165+
TDecimal(T t): Value(t) { }
166+
167+
explicit operator TInt128() const {
168+
return Value;
169+
}
170+
171+
TDecimal& operator+=(TDecimal right) {
172+
const auto l = Value;
173+
const auto r = right.Value;
174+
const auto a = l + r;
175+
if (IsNormal(l) && IsNormal(r) && IsNormal(a)) {
176+
Value = a;
177+
} else if (IsNan(l) || IsNan(r) || !a /* inf - inf*/) {
178+
Value = Nan();
179+
} else {
180+
Value = a > 0
181+
? +Inf()
182+
: -Inf();
183+
}
184+
return *this;
185+
}
186+
187+
TDecimal& operator*=(TDecimal right) {
188+
Value = Mul(Value, right.Value);
189+
return *this;
190+
}
191+
192+
TDecimal& operator/=(TDecimal right) {
193+
Value = Div(Value, right.Value);
194+
return *this;
195+
}
196+
197+
// TODO: implement '-' and '%'
198+
199+
friend TDecimal operator+(TDecimal left, TDecimal right) {
200+
left += right;
201+
return left;
202+
}
203+
204+
friend TDecimal operator*(TDecimal left, TDecimal right) {
205+
left *= right;
206+
return left;
207+
}
208+
209+
friend TDecimal operator/(TDecimal left, TDecimal right) {
210+
left /= right;
211+
return left;
212+
}
213+
};
214+
159215
}
160216
}

0 commit comments

Comments
 (0)