Skip to content

Commit

Permalink
chore: Update vendored sources to hannes/duckdb-rfuns@d7c38b9
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed May 1, 2024
1 parent 06dfd8a commit 4875778
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/include/rfuns_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ ScalarFunctionSet base_r_lte();
ScalarFunctionSet base_r_gt();
ScalarFunctionSet base_r_gte();

ScalarFunctionSet base_r_is_na();
ScalarFunctionSet base_r_as_integer();

// sum
AggregateFunctionSet base_r_sum();
AggregateFunctionSet base_r_min();
AggregateFunctionSet base_r_max();

ScalarFunctionSet binary_dispatch(ScalarFunctionSet fn) ;

// is_na
ScalarFunctionSet base_r_is_na();

} // namespace rfuns

class RfunsExtension : public Extension {
Expand Down
88 changes: 88 additions & 0 deletions src/rfuns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,92 @@ ScalarFunctionSet base_r_add() {
} // namespace duckdb
#include "rfuns_extension.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/common/operator/double_cast_operator.hpp"

#include <math.h>
#include <climits>
#include <limits>

namespace duckdb {
namespace rfuns {

namespace {

template <typename T>
int32_t check_range(T value, ValidityMask &mask, idx_t idx) {
if (value > std::numeric_limits<int32_t>::max() || value < std::numeric_limits<int32_t>::min() ) {
mask.SetInvalid(idx);
}

return static_cast<int32_t>(value);
}

template <typename T>
int32_t cast(T input, ValidityMask &mask, idx_t idx) {
return static_cast<int32_t>(input);
}

template <>
int32_t cast<double>(double input, ValidityMask &mask, idx_t idx) {
if (isnan(input)) {
mask.SetInvalid(idx);
}
return check_range(input, mask, idx);
}

template <>
int32_t cast<string_t>(string_t input, ValidityMask &mask, idx_t idx) {
double result;
if (!TryDoubleCast<double>(input.GetData(), input.GetSize(), result, false)) {
mask.SetInvalid(idx);
}

return cast<double>(result, mask, idx);
}

template <>
int32_t cast<date_t>(date_t input, ValidityMask &mask, idx_t idx) {
return input.days;
}

template <>
int32_t cast<timestamp_t>(timestamp_t input, ValidityMask &mask, idx_t idx) {
return check_range(Timestamp::GetEpochSeconds(input), mask, idx);
}

template <LogicalTypeId TYPE>
ScalarFunction AsIntegerFunction() {
using physical_type = typename physical<TYPE>::type;

auto fun = [](DataChunk &args, ExpressionState &state, Vector &result) {
UnaryExecutor::ExecuteWithNulls<physical_type, int32_t>(
args.data[0], result, args.size(), cast<physical_type>
);
};
return ScalarFunction({TYPE}, LogicalType::INTEGER, fun);
}

}

ScalarFunctionSet base_r_as_integer() {
ScalarFunctionSet set("r_base::as.integer");

set.AddFunction(AsIntegerFunction<LogicalType::BOOLEAN>());
set.AddFunction(AsIntegerFunction<LogicalType::INTEGER>());
set.AddFunction(AsIntegerFunction<LogicalType::DOUBLE>());

set.AddFunction(AsIntegerFunction<LogicalType::VARCHAR>());

set.AddFunction(AsIntegerFunction<LogicalType::DATE>());
set.AddFunction(AsIntegerFunction<LogicalType::TIMESTAMP>());

return set;
}

}
}
#include "rfuns_extension.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"

#include <math.h>
#include <climits>
Expand Down Expand Up @@ -685,6 +771,8 @@ static void register_rfuns(DatabaseInstance &instance) {
register_binary(instance, base_r_gte());

ExtensionUtil::RegisterFunction(instance, base_r_is_na());
ExtensionUtil::RegisterFunction(instance, base_r_as_integer());

ExtensionUtil::RegisterFunction(instance, base_r_sum());
ExtensionUtil::RegisterFunction(instance, base_r_min());
ExtensionUtil::RegisterFunction(instance, base_r_max());
Expand Down

0 comments on commit 4875778

Please sign in to comment.