Permalink
Browse files

Initial compiled version of date_trunc.

  • Loading branch information...
malin1993ml authored and hzxa21 committed Nov 10, 2017
1 parent bbcf739 commit 319ad866ff6ab55f548b112d410fb28902643793
View
@@ -13,18 +13,18 @@
#include "catalog/catalog.h"
#include "catalog/column_catalog.h"
#include "catalog/index_catalog.h"
#include "catalog/database_catalog.h"
#include "catalog/database_metrics_catalog.h"
#include "catalog/table_catalog.h"
#include "catalog/table_metrics_catalog.h"
#include "catalog/index_catalog.h"
#include "catalog/index_metrics_catalog.h"
#include "catalog/language_catalog.h"
#include "catalog/proc_catalog.h"
#include "catalog/query_metrics_catalog.h"
#include "catalog/settings_catalog.h"
#include "concurrency/transaction_manager_factory.h"
#include "catalog/table_catalog.h"
#include "catalog/table_metrics_catalog.h"
#include "catalog/trigger_catalog.h"
#include "catalog/proc_catalog.h"
#include "catalog/language_catalog.h"
#include "concurrency/transaction_manager_factory.h"
#include "function/date_functions.h"
#include "function/decimal_functions.h"
#include "function/string_functions.h"
@@ -1025,6 +1025,10 @@ void Catalog::InitializeFunctions() {
function::BuiltInFuncType{OperatorId::Extract,
function::DateFunctions::Extract},
txn);
AddBuiltinFunction(
"date_trunc", {type::TypeId::INTEGER, type::TypeId::TIMESTAMP},
type::TypeId::TIMESTAMP, internal_lang, "DateTrunc",
function::BuiltInFuncType{OperatorId::DateTrunc, NULL}, txn);
} catch (CatalogException &e) {
txn_manager.AbortTransaction(txn);
throw & e;
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// timestamp_functions_proxy.cpp
//
// Identification: src/codegen/proxy/timestamp_functions_proxy.cpp
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "function/timestamp_functions.h"
#include "codegen/proxy/timestamp_functions_proxy.h"
#include "codegen/proxy/type_builder.h"
namespace peloton {
namespace codegen {
DEFINE_METHOD(peloton::function, TimestampFunctions, DateTrunc);
} // namespace codegen
} // namespace peloton
@@ -12,11 +12,12 @@
#include "codegen/type/timestamp_type.h"
#include "codegen/value.h"
#include "codegen/proxy/timestamp_functions_proxy.h"
#include "codegen/proxy/values_runtime_proxy.h"
#include "codegen/type/boolean_type.h"
#include "codegen/type/date_type.h"
#include "codegen/type/integer_type.h"
#include "codegen/value.h"
#include "type/timestamp_type.h"
namespace peloton {
@@ -106,6 +107,28 @@ struct CompareTimestamp : public TypeSystem::Comparison {
}
};
// DateTrunc
struct DateTrunc : public TypeSystem::BinaryOperator {
bool SupportsTypes(const Type &left_type,
const Type &right_type) const override {
return left_type.GetSqlType() == Integer::Instance() &&
right_type.GetSqlType() == Timestamp::Instance();
}
Type ResultType(UNUSED_ATTRIBUTE const Type &left_type,
UNUSED_ATTRIBUTE const Type &right_type) const override {
return Type{Timestamp::Instance()};
}
Value DoWork(CodeGen &codegen, const Value &left, const Value &right,
UNUSED_ATTRIBUTE OnError on_error) const override {
PL_ASSERT(SupportsTypes(left.GetType(), right.GetType()));
llvm::Value *raw_ret = codegen.Call(TimestampFunctionsProxy::DateTrunc,
{left.GetValue(), right.GetLength()});
return Value{Timestamp::Instance(), raw_ret};
}
};
// The list of types a SQL timestamp type can be implicitly casted to
const std::vector<peloton::type::TypeId> kImplicitCastingTable = {
peloton::type::TypeId::DATE, peloton::type::TypeId::TIMESTAMP};
@@ -120,7 +143,10 @@ static std::vector<TypeSystem::ComparisonInfo> kComparisonTable = {
{kCompareTimestamp}};
static std::vector<TypeSystem::UnaryOpInfo> kUnaryOperatorTable = {};
static std::vector<TypeSystem::BinaryOpInfo> kBinaryOperatorTable = {};
static DateTrunc kDateTrunc;
static std::vector<TypeSystem::BinaryOpInfo> kBinaryOperatorTable = {
{OperatorId::DateTrunc, kDateTrunc}};
// Nary operations
static std::vector<TypeSystem::NaryOpInfo> kNaryOperatorTable = {};
@@ -131,8 +157,8 @@ static std::vector<TypeSystem::NaryOpInfo> kNaryOperatorTable = {};
Timestamp::Timestamp()
: SqlType(peloton::type::TypeId::TIMESTAMP),
type_system_(kImplicitCastingTable, kExplicitCastingTable,
kComparisonTable, kUnaryOperatorTable,
kBinaryOperatorTable, kNaryOperatorTable) {}
kComparisonTable, kUnaryOperatorTable, kBinaryOperatorTable,
kNaryOperatorTable) {}
Value Timestamp::GetMinValue(CodeGen &codegen) const {
auto *raw_val = codegen.Const64(peloton::type::PELOTON_TIMESTAMP_MIN);
@@ -163,4 +189,4 @@ llvm::Function *Timestamp::GetOutputFunction(
} // namespace type
} // namespace codegen
} // namespace peloton
} // namespace peloton
@@ -0,0 +1,127 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// timestamp_functions.cpp
//
// Identification: src/function/timestamp_functions.cpp
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "function/timestamp_functions.h"
#include <date/date.h>
#include <date/iso_week.h>
#include <inttypes.h>
#include "common/logger.h"
#include "type/limits.h"
#include "type/types.h"
#include "type/value.h"
#include "type/value_factory.h"
namespace peloton {
namespace function {
uint64_t TimestampFunctions::DateTrunc(uint32_t date_part_type,
uint64_t value) {
DatePartType date_part =
*reinterpret_cast<const DatePartType*>(&date_part_type);
uint64_t timestamp = value;
uint64_t result = 0;
if (timestamp == type::PELOTON_TIMESTAMP_NULL) {
return type::PELOTON_TIMESTAMP_NULL;
}
uint32_t micro = timestamp % 1000000;
timestamp /= 1000000;
uint32_t hour_min_sec = timestamp % 100000;
hour_min_sec /= 60;
uint16_t min = hour_min_sec % 60;
hour_min_sec /= 60;
uint16_t hour = hour_min_sec % 24;
timestamp /= 100000;
uint16_t year = timestamp % 10000;
timestamp /= 10000;
timestamp /= 27; // skip time zone
uint16_t day = timestamp % 32;
timestamp /= 32;
uint16_t month = timestamp;
uint8_t quarter = (month - 1) / 3 + 1;
date::year_month_day ymd = date::year_month_day{
date::year{year}, date::month{month}, date::day{day}};
iso_week::year_weeknum_weekday yww = iso_week::year_weeknum_weekday{ymd};
uint16_t dow = ((unsigned)yww.weekday()) == 7 ? 0 : (unsigned)yww.weekday();
switch (date_part) {
case DatePartType::CENTURY: {
result = ((32 + 1) * 27 * 10000 + year - year % 100) * 100000 * 1000000;
break;
}
case DatePartType::DAY: {
result = ((month * 32 + day) * 27 * 10000 + year) * 100000 * 1000000;
break;
}
case DatePartType::DECADE: {
result = ((32 + 1) * 27 * 10000 + year - year % 10) * 100000 * 1000000;
break;
}
case DatePartType::HOUR: {
result = (((month * 32 + day) * 27 * 10000 + year - year % 100) * 100000 +
hour * 86400) *
1000000;
break;
}
case DatePartType::MICROSECOND: {
result = value;
break;
}
case DatePartType::MILLENNIUM: {
result = ((32 + 1) * 27 * 10000 + year - year % 1000) * 100000 * 1000000;
break;
}
case DatePartType::MILLISECOND: {
result = value - micro % 1000;
break;
}
case DatePartType::MINUTE: {
result = value - micro - min * 1000000;
break;
}
case DatePartType::MONTH: {
result = ((month * 32 + 1) * 27 * 10000 + year) * 100000 * 1000000;
break;
}
case DatePartType::QUARTER: {
result = ((((quarter - 1) * 3 + 1) * 32 + 1) * 27 * 10000 + year) *
100000 * 1000000;
break;
}
case DatePartType::SECOND: {
result = value - micro;
break;
}
case DatePartType::WEEK: {
result =
((month * 32 + day - dow) * 27 * 10000 + year) * 100000 * 1000000;
break;
}
case DatePartType::YEAR: {
result = ((32 + 1) * 27 * 10000 + year) * 100000 * 1000000;
break;
}
default: { result = type::PELOTON_TIMESTAMP_NULL; }
};
return (result);
}
} // namespace expression
} // namespace peloton
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// timestamp_functions_proxy.h
//
// Identification: src/include/codegen/proxy/timestamp_functions_proxy.h
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include "codegen/proxy/proxy.h"
namespace peloton {
namespace codegen {
PROXY(TimestampFunctions) {
// Proxy everything in function::DateFunctions
DECLARE_METHOD(DateTrunc);
};
} // namespace codegen
} // namespace peloton
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// timestamp_functions.h
//
// Identification: src/include/function/timestamp_functions.h
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include <string>
#include <vector>
#include "common/logger.h"
#include "type/types.h"
#include "type/value.h"
namespace peloton {
namespace function {
class TimestampFunctions {
public:
// Truncate a timestamp to specified precision
// (1) The first argument selects to which precision to truncate the input
// value
// (2) The second argument is the length of the precision value (used to
// evaluate whether the value is NULL)
// (3) The third argument is the value expression of type timestamp
// (4) The fourth argument is the length of the timestamp value (used to
// evaluate whether the value is NULL)
// @return The Value returned is the value of type timestamp with
// all fields that are less significant than the selected one set to zero (or
// one, for day and month).
// TODO(lma): We do not support directly using date_trunc with a constant
// value from the SQL string. We don't need to change the implementation in
// this function here to support that, but we need the parser/binder to
// support explicit type casts. For example:
// SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
static uint64_t DateTrunc(uint32_t date_part_type, uint64_t value);
};
} // namespace function
} // namespace peloton
View
@@ -1042,6 +1042,7 @@ enum class OperatorId : uint32_t {
Sqrt,
Extract,
Floor,
DateTrunc,
// Add more operators here, before the last "Invalid" entry
Like,

0 comments on commit 319ad86

Please sign in to comment.