Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit b3c81a0

Browse files
authored
Merge pull request #1031 from pervazea/master
Support Abs SQL Function
2 parents 873cbe9 + 6a23ba5 commit b3c81a0

File tree

12 files changed

+646
-88
lines changed

12 files changed

+646
-88
lines changed

src/catalog/catalog.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,11 @@ void Catalog::InitializeFunctions() {
10421042
/**
10431043
* decimal functions
10441044
*/
1045+
AddBuiltinFunction(
1046+
"abs", {type::TypeId::DECIMAL}, type::TypeId::DECIMAL, internal_lang,
1047+
"Abs", function::BuiltInFuncType{OperatorId::Abs,
1048+
function::DecimalFunctions::_Abs},
1049+
txn);
10451050
AddBuiltinFunction(
10461051
"sqrt", {type::TypeId::TINYINT}, type::TypeId::DECIMAL, internal_lang,
10471052
"Sqrt", function::BuiltInFuncType{OperatorId::Sqrt,
@@ -1078,6 +1083,34 @@ void Catalog::InitializeFunctions() {
10781083
/**
10791084
* integer functions
10801085
*/
1086+
AddBuiltinFunction(
1087+
"abs", {type::TypeId::TINYINT}, type::TypeId::TINYINT,
1088+
internal_lang, "Abs",
1089+
function::BuiltInFuncType{OperatorId::Abs,
1090+
function::DecimalFunctions::_Abs},
1091+
txn);
1092+
1093+
AddBuiltinFunction(
1094+
"abs", {type::TypeId::SMALLINT}, type::TypeId::SMALLINT,
1095+
internal_lang, "Abs",
1096+
function::BuiltInFuncType{OperatorId::Abs,
1097+
function::DecimalFunctions::_Abs},
1098+
txn);
1099+
1100+
AddBuiltinFunction(
1101+
"abs", {type::TypeId::INTEGER}, type::TypeId::INTEGER,
1102+
internal_lang, "Abs",
1103+
function::BuiltInFuncType{OperatorId::Abs,
1104+
function::DecimalFunctions::_Abs},
1105+
txn);
1106+
1107+
AddBuiltinFunction(
1108+
"abs", {type::TypeId::BIGINT}, type::TypeId::BIGINT,
1109+
internal_lang, "Abs",
1110+
function::BuiltInFuncType{OperatorId::Abs,
1111+
function::DecimalFunctions::_Abs},
1112+
txn);
1113+
10811114
AddBuiltinFunction(
10821115
"floor", {type::TypeId::INTEGER}, type::TypeId::DECIMAL,
10831116
internal_lang, "Floor",

src/codegen/proxy/decimal_functions_proxy.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace peloton {
1919
namespace codegen {
20+
DEFINE_METHOD(peloton::function, DecimalFunctions, Abs);
2021

2122
DEFINE_METHOD(peloton::function, DecimalFunctions, Floor);
2223

src/codegen/type/bigint_type.cpp

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ namespace type {
2828

2929
namespace {
3030

31+
/** Forward declarations:
32+
* Subtraction
33+
*/
34+
struct Sub : public TypeSystem::BinaryOperatorHandleNull {
35+
bool SupportsTypes(const Type &left_type,
36+
const Type &right_type) const override;
37+
38+
Type ResultType(UNUSED_ATTRIBUTE const Type &left_type,
39+
UNUSED_ATTRIBUTE const Type &right_type) const override;
40+
41+
Value Impl(CodeGen &codegen, const Value &left, const Value &right,
42+
const TypeSystem::InvocationContext &ctx) const override;
43+
};
44+
3145
////////////////////////////////////////////////////////////////////////////////
3246
///
3347
/// Casting
@@ -165,6 +179,33 @@ struct CompareBigInt : public TypeSystem::SimpleComparisonHandleNull {
165179
///
166180
////////////////////////////////////////////////////////////////////////////////
167181

182+
// Abs
183+
struct Abs : public TypeSystem::UnaryOperatorHandleNull {
184+
bool SupportsType(const Type &type) const override {
185+
return type.GetSqlType() == BigInt::Instance();
186+
}
187+
188+
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
189+
return Type{BigInt::Instance()};
190+
}
191+
192+
Value Impl(CodeGen &codegen, const Value &val,
193+
const TypeSystem::InvocationContext &ctx)
194+
const override {
195+
PL_ASSERT(SupportsType(val.GetType()));
196+
// The BigInt subtraction implementation
197+
Sub sub;
198+
// Zero place-holder
199+
auto zero = codegen::Value{type::BigInt::Instance(), codegen.Const64(0)};
200+
201+
// We want: raw_ret = (val < 0 ? 0 - val : val)
202+
auto sub_result = sub.Impl(codegen, zero, val, ctx);
203+
auto *lt_zero = codegen->CreateICmpSLT(val.GetValue(), zero.GetValue());
204+
auto *raw_ret = codegen->CreateSelect(lt_zero, sub_result.GetValue(), val.GetValue());
205+
return Value{BigInt::Instance(), raw_ret};
206+
}
207+
};
208+
168209
// Negation
169210
struct Negate : public TypeSystem::UnaryOperatorHandleNull {
170211
bool SupportsType(const Type &type) const override {
@@ -291,34 +332,32 @@ struct Add : public TypeSystem::BinaryOperatorHandleNull {
291332
};
292333

293334
// Subtraction
294-
struct Sub : public TypeSystem::BinaryOperatorHandleNull {
295-
bool SupportsTypes(const Type &left_type,
296-
const Type &right_type) const override {
297-
return left_type.GetSqlType() == BigInt::Instance() &&
298-
left_type == right_type;
299-
}
335+
bool Sub::SupportsTypes(const Type &left_type,
336+
const Type &right_type) const {
337+
return left_type.GetSqlType() == BigInt::Instance() &&
338+
left_type == right_type;
339+
}
300340

301-
Type ResultType(UNUSED_ATTRIBUTE const Type &left_type,
302-
UNUSED_ATTRIBUTE const Type &right_type) const override {
303-
return Type{BigInt::Instance()};
304-
}
341+
Type Sub::ResultType(UNUSED_ATTRIBUTE const Type &left_type,
342+
UNUSED_ATTRIBUTE const Type &right_type) const {
343+
return Type{BigInt::Instance()};
344+
}
305345

306-
Value Impl(CodeGen &codegen, const Value &left, const Value &right,
307-
const TypeSystem::InvocationContext &ctx) const override {
308-
PL_ASSERT(SupportsTypes(left.GetType(), right.GetType()));
346+
Value Sub::Impl(CodeGen &codegen, const Value &left, const Value &right,
347+
const TypeSystem::InvocationContext &ctx) const {
348+
PL_ASSERT(SupportsTypes(left.GetType(), right.GetType()));
309349

310-
// Do subtraction
311-
llvm::Value *overflow_bit = nullptr;
312-
llvm::Value *result = codegen.CallSubWithOverflow(
350+
// Do subtraction
351+
llvm::Value *overflow_bit = nullptr;
352+
llvm::Value *result = codegen.CallSubWithOverflow(
313353
left.GetValue(), right.GetValue(), overflow_bit);
314354

315-
if (ctx.on_error == OnError::Exception) {
316-
codegen.ThrowIfOverflow(overflow_bit);
317-
}
318-
319-
// Return result
320-
return Value{BigInt::Instance(), result};
355+
if (ctx.on_error == OnError::Exception) {
356+
codegen.ThrowIfOverflow(overflow_bit);
321357
}
358+
359+
// Return result
360+
return Value{BigInt::Instance(), result};
322361
};
323362

324363
// Multiplication
@@ -495,11 +534,13 @@ std::vector<TypeSystem::ComparisonInfo> kComparisonTable = {{kCompareBigInt}};
495534

496535
// Unary operators
497536
Negate kNegOp;
537+
Abs kAbsOp;
498538
Ceil kCeilOp;
499539
Floor kFloorOp;
500540
Sqrt kSqrt;
501541
std::vector<TypeSystem::UnaryOpInfo> kUnaryOperatorTable = {
502542
{OperatorId::Negation, kNegOp},
543+
{OperatorId::Abs, kAbsOp},
503544
{OperatorId::Ceil, kCeilOp},
504545
{OperatorId::Floor, kFloorOp},
505546
{OperatorId::Sqrt, kSqrt}};

src/codegen/type/decimal_type.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ struct Negate : public TypeSystem::UnaryOperatorHandleNull {
180180
}
181181
};
182182

183+
// Abs
184+
struct Abs : public TypeSystem::UnaryOperatorHandleNull {
185+
bool SupportsType(const Type &type) const override {
186+
return type.GetSqlType() == Decimal::Instance();
187+
}
188+
189+
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
190+
return Type{Decimal::Instance()};
191+
}
192+
193+
Value Impl(CodeGen &codegen, const Value &val,
194+
UNUSED_ATTRIBUTE const TypeSystem::InvocationContext &ctx)
195+
const override {
196+
llvm::Value *raw_ret =
197+
codegen.Call(DecimalFunctionsProxy::Abs, {val.GetValue()});
198+
return Value{Decimal::Instance(), raw_ret};
199+
}
200+
};
201+
183202
// Floor
184203
struct Floor : public TypeSystem::UnaryOperatorHandleNull {
185204
bool SupportsType(const Type &type) const override {
@@ -476,12 +495,14 @@ std::vector<TypeSystem::ComparisonInfo> kComparisonTable = {{kCompareDecimal}};
476495

477496
// Unary operators
478497
Negate kNegOp;
498+
Abs kAbsOp;
479499
Floor kFloorOp;
480500
Round kRound;
481501
Ceil kCeilOp;
482502
Sqrt kSqrt;
483503
std::vector<TypeSystem::UnaryOpInfo> kUnaryOperatorTable = {
484504
{OperatorId::Negation, kNegOp},
505+
{OperatorId::Abs, kAbsOp},
485506
{OperatorId::Floor, kFloorOp},
486507
{OperatorId::Round, kRound},
487508
{OperatorId::Ceil, kCeilOp},

src/codegen/type/integer_type.cpp

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ namespace type {
2727

2828
namespace {
2929

30+
/** Forward declarations:
31+
* Subtraction
32+
*/
33+
struct Sub : public TypeSystem::BinaryOperatorHandleNull {
34+
bool SupportsTypes(const Type &left_type,
35+
const Type &right_type) const override;
36+
37+
Type ResultType(UNUSED_ATTRIBUTE const Type &left_type,
38+
UNUSED_ATTRIBUTE const Type &right_type) const override;
39+
40+
Value Impl(CodeGen &codegen, const Value &left, const Value &right,
41+
const TypeSystem::InvocationContext &ctx) const override;
42+
};
43+
3044
////////////////////////////////////////////////////////////////////////////////
3145
///
3246
/// Casting
@@ -162,6 +176,32 @@ struct CompareInteger : public TypeSystem::SimpleComparisonHandleNull {
162176
///
163177
////////////////////////////////////////////////////////////////////////////////
164178

179+
// Abs
180+
struct Abs : public TypeSystem::UnaryOperatorHandleNull {
181+
bool SupportsType(const Type &type) const override {
182+
return type.GetSqlType() == Integer::Instance();
183+
}
184+
185+
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
186+
return Type{Integer::Instance()};
187+
}
188+
189+
Value Impl(CodeGen &codegen, const Value &val,
190+
const TypeSystem::InvocationContext &ctx)
191+
const override {
192+
// The integer subtraction implementation
193+
Sub sub;
194+
// Zero place-holder
195+
auto zero = codegen::Value{type::Integer::Instance(), codegen.Const32(0)};
196+
197+
// We want: raw_ret = (val < 0 ? 0 - val : val)
198+
auto sub_result = sub.Impl(codegen, zero, val, ctx);
199+
auto *lt_zero = codegen->CreateICmpSLT(val.GetValue(), zero.GetValue());
200+
auto *raw_ret = codegen->CreateSelect(lt_zero, sub_result.GetValue(), val.GetValue());
201+
return Value{Integer::Instance(), raw_ret};
202+
}
203+
};
204+
165205
// Negation
166206
struct Negate : public TypeSystem::UnaryOperatorHandleNull {
167207
bool SupportsType(const Type &type) const override {
@@ -288,34 +328,32 @@ struct Add : public TypeSystem::BinaryOperatorHandleNull {
288328
};
289329

290330
// Subtraction
291-
struct Sub : public TypeSystem::BinaryOperatorHandleNull {
292-
bool SupportsTypes(const Type &left_type,
293-
const Type &right_type) const override {
294-
return left_type.GetSqlType() == Integer::Instance() &&
295-
left_type == right_type;
296-
}
331+
bool Sub::SupportsTypes(const Type &left_type,
332+
const Type &right_type) const {
333+
return left_type.GetSqlType() == Integer::Instance() &&
334+
left_type == right_type;
335+
}
297336

298-
Type ResultType(UNUSED_ATTRIBUTE const Type &left_type,
299-
UNUSED_ATTRIBUTE const Type &right_type) const override {
300-
return Type{Integer::Instance()};
301-
}
337+
Type Sub::ResultType(UNUSED_ATTRIBUTE const Type &left_type,
338+
UNUSED_ATTRIBUTE const Type &right_type) const {
339+
return Type{Integer::Instance()};
340+
}
302341

303-
Value Impl(CodeGen &codegen, const Value &left, const Value &right,
304-
const TypeSystem::InvocationContext &ctx) const override {
305-
PL_ASSERT(SupportsTypes(left.GetType(), right.GetType()));
342+
Value Sub::Impl(CodeGen &codegen, const Value &left, const Value &right,
343+
const TypeSystem::InvocationContext &ctx) const {
344+
PL_ASSERT(SupportsTypes(left.GetType(), right.GetType()));
306345

307-
// Do subtraction
308-
llvm::Value *overflow_bit = nullptr;
309-
llvm::Value *result = codegen.CallSubWithOverflow(
346+
// Do subtraction
347+
llvm::Value *overflow_bit = nullptr;
348+
llvm::Value *result = codegen.CallSubWithOverflow(
310349
left.GetValue(), right.GetValue(), overflow_bit);
311350

312-
if (ctx.on_error == OnError::Exception) {
313-
codegen.ThrowIfOverflow(overflow_bit);
314-
}
315-
316-
// Return result
317-
return Value{Integer::Instance(), result};
351+
if (ctx.on_error == OnError::Exception) {
352+
codegen.ThrowIfOverflow(overflow_bit);
318353
}
354+
355+
// Return result
356+
return Value{Integer::Instance(), result};
319357
};
320358

321359
// Multiplication
@@ -495,11 +533,13 @@ std::vector<TypeSystem::ComparisonInfo> kComparisonTable = {
495533

496534
// Unary operators
497535
Negate kNegOp;
536+
Abs kAbsOp;
498537
Ceil kCeilOp;
499538
Floor kFloorOp;
500539
Sqrt kSqrt;
501540
std::vector<TypeSystem::UnaryOpInfo> kUnaryOperatorTable = {
502541
{OperatorId::Negation, kNegOp},
542+
{OperatorId::Abs, kAbsOp},
503543
{OperatorId::Ceil, kCeilOp},
504544
{OperatorId::Floor, kFloorOp},
505545
{OperatorId::Sqrt, kSqrt}};

0 commit comments

Comments
 (0)