Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions extension/core_functions/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_AGGREGATE_FUNCTION(CovarPopFun),
DUCKDB_AGGREGATE_FUNCTION(CovarSampFun),
DUCKDB_SCALAR_FUNCTION(CurrentDatabaseFun),
DUCKDB_SCALAR_FUNCTION(CurrentDateFun),
DUCKDB_SCALAR_FUNCTION(CurrentQueryFun),
DUCKDB_SCALAR_FUNCTION(CurrentSchemaFun),
DUCKDB_SCALAR_FUNCTION(CurrentSchemasFun),
Expand Down Expand Up @@ -193,7 +192,6 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION_ALIAS(GenRandomUuidFun),
DUCKDB_SCALAR_FUNCTION_SET(GenerateSeriesFun),
DUCKDB_SCALAR_FUNCTION(GetBitFun),
DUCKDB_SCALAR_FUNCTION(CurrentTimeFun),
DUCKDB_SCALAR_FUNCTION(GetCurrentTimestampFun),
DUCKDB_SCALAR_FUNCTION_SET_ALIAS(GradeUpFun),
DUCKDB_SCALAR_FUNCTION_SET(GreatestFun),
Expand Down Expand Up @@ -371,7 +369,6 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION(ToTimestampFun),
DUCKDB_SCALAR_FUNCTION(ToWeeksFun),
DUCKDB_SCALAR_FUNCTION(ToYearsFun),
DUCKDB_SCALAR_FUNCTION_ALIAS(TodayFun),
DUCKDB_SCALAR_FUNCTION_ALIAS(TransactionTimestampFun),
DUCKDB_SCALAR_FUNCTION(TranslateFun),
DUCKDB_SCALAR_FUNCTION_SET(TrimFun),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ struct CenturyFun {
static ScalarFunctionSet GetFunctions();
};

struct CurrentDateFun {
static constexpr const char *Name = "current_date";
static constexpr const char *Parameters = "";
static constexpr const char *Description = "Returns the current date";
static constexpr const char *Example = "current_date()";

static ScalarFunction GetFunction();
};

struct TodayFun {
using ALIAS = CurrentDateFun;

static constexpr const char *Name = "today";
};

struct DateDiffFun {
static constexpr const char *Name = "date_diff";
static constexpr const char *Parameters = "part,startdate,enddate";
Expand Down Expand Up @@ -198,15 +183,6 @@ struct EpochNsFun {
static ScalarFunctionSet GetFunctions();
};

struct CurrentTimeFun {
static constexpr const char *Name = "get_current_time";
static constexpr const char *Parameters = "";
static constexpr const char *Description = "Returns the current time";
static constexpr const char *Example = "get_current_time()";

static ScalarFunction GetFunction();
};

struct EraFun {
static constexpr const char *Name = "era";
static constexpr const char *Parameters = "ts";
Expand Down
37 changes: 0 additions & 37 deletions extension/core_functions/scalar/date/current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,4 @@ ScalarFunction GetCurrentTimestampFun::GetFunction() {
return current_timestamp;
}

static unique_ptr<Expression> CurrentTimeExpr(FunctionBindExpressionInput &input) {
auto timestamp = GetCurrentTimestampFun::GetFunction();
timestamp.name = GetCurrentTimestampFun::Name;

vector<unique_ptr<Expression>> args;

auto func = make_uniq_base<Expression, BoundFunctionExpression>(LogicalType::TIMESTAMP_TZ, timestamp,
std::move(args), nullptr);

return BoundCastExpression::AddCastToType(input.context, std::move(func), LogicalType::TIME_TZ);
}

static unique_ptr<Expression> CurrentDateExpr(FunctionBindExpressionInput &input) {
auto timestamp = GetCurrentTimestampFun::GetFunction();
timestamp.name = GetCurrentTimestampFun::Name;

vector<unique_ptr<Expression>> args;

auto func = make_uniq_base<Expression, BoundFunctionExpression>(LogicalType::TIMESTAMP_TZ, timestamp,
std::move(args), nullptr);
return BoundCastExpression::AddCastToType(input.context, std::move(func), LogicalType::DATE);
}

ScalarFunction CurrentTimeFun::GetFunction() {
ScalarFunction current_time({}, LogicalType::TIME_TZ, nullptr);
current_time.bind_expression = CurrentTimeExpr;
current_time.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_time;
}

ScalarFunction CurrentDateFun::GetFunction() {
ScalarFunction current_date({}, LogicalType::DATE, nullptr);
current_date.bind_expression = CurrentDateExpr;
current_date.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_date;
}

} // namespace duckdb
14 changes: 0 additions & 14 deletions extension/core_functions/scalar/date/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
"example": "century(timestamp '2021-08-03 11:59:44.123456')",
"type": "scalar_function_set"
},
{
"name": "current_date",
"description": "Returns the current date",
"example": "current_date()",
"type": "scalar_function",
"aliases": ["today"]
},
{
"name": "date_diff",
"parameters": "part,startdate,enddate",
Expand Down Expand Up @@ -126,13 +119,6 @@
"example": "epoch_ns(timestamp '2021-08-03 11:59:44.123456')",
"type": "scalar_function_set"
},
{
"struct": "CurrentTimeFun",
"name": "get_current_time",
"description": "Returns the current time",
"example": "get_current_time()",
"type": "scalar_function"
},
{
"name": "era",
"parameters": "ts",
Expand Down
1 change: 1 addition & 0 deletions extension/icu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_subdirectory(third_party)
set(ICU_EXTENSION_FILES
${ICU_LIBRARY_FILES}
icu_extension.cpp
icu-current.cpp
icu-dateadd.cpp
icu-datefunc.cpp
icu-datepart.cpp
Expand Down
63 changes: 63 additions & 0 deletions extension/icu/icu-current.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "include/icu-dateadd.hpp"

#include "duckdb/main/extension_util.hpp"
#include "duckdb/common/types/time.hpp"
#include "duckdb/common/types/timestamp.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/transaction/meta_transaction.hpp"
#include "include/icu-current.hpp"
#include "include/icu-casts.hpp"

namespace duckdb {

static timestamp_t GetTransactionTimestamp(ExpressionState &state) {
return MetaTransaction::Get(state.GetContext()).start_timestamp;
}

static void CurrentTimeFunction(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 0);
auto instant = GetTransactionTimestamp(state);
ICUDateFunc::BindData data(state.GetContext());

dtime_tz_t result_time(dtime_t(0), 0);
ICUToTimeTZ::ToTimeTZ(data.calendar.get(), instant, result_time);
auto val = Value::TIMETZ(result_time);
result.Reference(val);
}

static void CurrentDateFunction(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 0);
auto instant = GetTransactionTimestamp(state);

auto val = Value::DATE(ICUMakeDate::ToDate(state.GetContext(), instant));
result.Reference(val);
}

ScalarFunction GetCurrentTimeFun() {
ScalarFunction current_time({}, LogicalType::TIME_TZ, CurrentTimeFunction);
current_time.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_time;
}

ScalarFunction GetCurrentDateFun() {
ScalarFunction current_date({}, LogicalType::DATE, CurrentDateFunction);
current_date.stability = FunctionStability::CONSISTENT_WITHIN_QUERY;
return current_date;
}

void RegisterICUCurrentFunctions(DatabaseInstance &db) {
// temporal + interval
ScalarFunctionSet current_time("get_current_time");
current_time.AddFunction(GetCurrentTimeFun());
ExtensionUtil::RegisterFunction(db, current_time);

ScalarFunctionSet current_date("current_date");
current_date.AddFunction(GetCurrentDateFun());
ExtensionUtil::RegisterFunction(db, current_date);

current_date.name = "today";
ExtensionUtil::RegisterFunction(db, current_date);
}

} // namespace duckdb
82 changes: 43 additions & 39 deletions extension/icu/icu-makedate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,66 @@
#include "duckdb/function/cast/cast_function_set.hpp"
#include "duckdb/main/extension_util.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "include/icu-casts.hpp"
#include "include/icu-datefunc.hpp"
#include "include/icu-datetrunc.hpp"

#include <cmath>

namespace duckdb {

struct ICUMakeDate : public ICUDateFunc {
static inline date_t Operation(icu::Calendar *calendar, timestamp_t instant) {
if (!Timestamp::IsFinite(instant)) {
return Timestamp::GetDate(instant);
}

// Extract the time zone parts
SetTime(calendar, instant);
const auto era = ExtractField(calendar, UCAL_ERA);
const auto year = ExtractField(calendar, UCAL_YEAR);
const auto mm = ExtractField(calendar, UCAL_MONTH) + 1;
const auto dd = ExtractField(calendar, UCAL_DATE);

const auto yyyy = era ? year : (-year + 1);
date_t result;
if (!Date::TryFromDate(yyyy, mm, dd, result)) {
throw ConversionException("Unable to convert TIMESTAMPTZ to DATE");
}
date_t ICUMakeDate::Operation(icu::Calendar *calendar, timestamp_t instant) {
if (!Timestamp::IsFinite(instant)) {
return Timestamp::GetDate(instant);
}

return result;
// Extract the time zone parts
SetTime(calendar, instant);
const auto era = ExtractField(calendar, UCAL_ERA);
const auto year = ExtractField(calendar, UCAL_YEAR);
const auto mm = ExtractField(calendar, UCAL_MONTH) + 1;
const auto dd = ExtractField(calendar, UCAL_DATE);

const auto yyyy = era ? year : (-year + 1);
date_t result;
if (!Date::TryFromDate(yyyy, mm, dd, result)) {
throw ConversionException("Unable to convert TIMESTAMPTZ to DATE");
}

static bool CastToDate(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
auto &cast_data = parameters.cast_data->Cast<CastData>();
auto &info = cast_data.info->Cast<BindData>();
CalendarPtr calendar(info.calendar->clone());
return result;
}

UnaryExecutor::Execute<timestamp_t, date_t>(
source, result, count, [&](timestamp_t input) { return Operation(calendar.get(), input); });
return true;
}
date_t ICUMakeDate::ToDate(ClientContext &context, timestamp_t instant) {
ICUDateFunc::BindData data(context);
return Operation(data.calendar.get(), instant);
}

static BoundCastInfo BindCastToDate(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
if (!input.context) {
throw InternalException("Missing context for TIMESTAMPTZ to DATE cast.");
}
bool ICUMakeDate::CastToDate(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
auto &cast_data = parameters.cast_data->Cast<CastData>();
auto &info = cast_data.info->Cast<BindData>();
CalendarPtr calendar(info.calendar->clone());

auto cast_data = make_uniq<CastData>(make_uniq<BindData>(*input.context));
UnaryExecutor::Execute<timestamp_t, date_t>(source, result, count,
[&](timestamp_t input) { return Operation(calendar.get(), input); });
return true;
}

return BoundCastInfo(CastToDate, std::move(cast_data));
BoundCastInfo ICUMakeDate::BindCastToDate(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
if (!input.context) {
throw InternalException("Missing context for TIMESTAMPTZ to DATE cast.");
}

static void AddCasts(DatabaseInstance &db) {
auto &config = DBConfig::GetConfig(db);
auto &casts = config.GetCastFunctions();
auto cast_data = make_uniq<CastData>(make_uniq<BindData>(*input.context));

casts.RegisterCastFunction(LogicalType::TIMESTAMP_TZ, LogicalType::DATE, BindCastToDate);
}
};
return BoundCastInfo(CastToDate, std::move(cast_data));
}

void ICUMakeDate::AddCasts(DatabaseInstance &db) {
auto &config = DBConfig::GetConfig(db);
auto &casts = config.GetCastFunctions();

casts.RegisterCastFunction(LogicalType::TIMESTAMP_TZ, LogicalType::DATE, BindCastToDate);
}

struct ICUMakeTimestampTZFunc : public ICUDateFunc {
template <typename T>
Expand Down
Loading
Loading