Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
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
15 changes: 14 additions & 1 deletion cpp/src/arrow/compute/api_scalar.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,6 +158,9 @@ static auto kProjectOptionsType = GetFunctionOptionsType<ProjectOptions>(
DataMember("field_names", &ProjectOptions::field_names),
DataMember("field_nullability", &ProjectOptions::field_nullability),
DataMember("field_metadata", &ProjectOptions::field_metadata));
static auto kDayOfWeekOptionsType = GetFunctionOptionsType<DayOfWeekOptions>(
DataMember("one_based_numbering", &DayOfWeekOptions::one_based_numbering),
DataMember("week_start", &DayOfWeekOptions::week_start));
} // namespace
} // namespace internal

Expand DownExpand Up@@ -278,6 +281,12 @@ ProjectOptions::ProjectOptions(std::vector<std::string> n)
ProjectOptions::ProjectOptions() : ProjectOptions(std::vector<std::string>()) {}
constexpr char ProjectOptions::kTypeName[];

DayOfWeekOptions::DayOfWeekOptions(bool one_based_numbering, uint32_t week_start)
: FunctionOptions(internal::kDayOfWeekOptionsType),
one_based_numbering(one_based_numbering),
week_start(week_start) {}
constexpr char DayOfWeekOptions::kTypeName[];

namespace internal {
void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kArithmeticOptionsType));
Expand All@@ -296,6 +305,7 @@ void RegisterScalarOptions(FunctionRegistry* registry) {
DCHECK_OK(registry->AddFunctionOptionsType(kSliceOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kCompareOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kProjectOptionsType));
DCHECK_OK(registry->AddFunctionOptionsType(kDayOfWeekOptionsType));
}
} // namespace internal

Expand DownExpand Up@@ -458,7 +468,6 @@ Result<Datum> IfElse(const Datum& cond, const Datum& if_true, const Datum& if_fa
SCALAR_EAGER_UNARY(Year, "year")
SCALAR_EAGER_UNARY(Month, "month")
SCALAR_EAGER_UNARY(Day, "day")
SCALAR_EAGER_UNARY(DayOfWeek, "day_of_week")
SCALAR_EAGER_UNARY(DayOfYear, "day_of_year")
SCALAR_EAGER_UNARY(ISOYear, "iso_year")
SCALAR_EAGER_UNARY(ISOWeek, "iso_week")
Expand All@@ -472,5 +481,9 @@ SCALAR_EAGER_UNARY(Microsecond, "microsecond")
SCALAR_EAGER_UNARY(Nanosecond, "nanosecond")
SCALAR_EAGER_UNARY(Subsecond, "subsecond")

Result<Datum> DayOfWeek(const Datum& arg, DayOfWeekOptions options, ExecContext* ctx) {
return CallFunction("day_of_week", {arg}, &options, ctx);
}

} // namespace compute
} // namespace arrow
23 changes: 21 additions & 2 deletions cpp/src/arrow/compute/api_scalar.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -244,6 +244,18 @@ class ARROW_EXPORT ProjectOptions : public FunctionOptions {
std::vector<std::shared_ptr<const KeyValueMetadata>> field_metadata;
};

struct ARROW_EXPORT DayOfWeekOptions : public FunctionOptions {
public:
explicit DayOfWeekOptions(bool one_based_numbering = false, uint32_t week_start = 1);
constexpr static char const kTypeName[] = "DayOfWeekOptions";
static DayOfWeekOptions Defaults() { return DayOfWeekOptions{}; }

/// Number days from 1 if true and from 0 if false
bool one_based_numbering;
/// What day does the week start with (Monday=1, Sunday=7)
uint32_t week_start;
};

/// @}

/// \brief Get the absolute value of a value. Array values can be of arbitrary
Expand DownExpand Up@@ -713,15 +725,22 @@ ARROW_EXPORT
Result<Datum> Day(const Datum& values, ExecContext* ctx = NULLPTR);

/// \brief DayOfWeek returns number of the day of the week value for each element of
/// `values`. Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.
/// `values`.
///
/// By default week starts on Monday denoted by 0 and ends on Sunday denoted
/// by 6. Start day of the week (Monday=1, Sunday=7) and numbering base (0 or 1) can be
/// set using DayOfWeekOptions
///
/// \param[in] values input to extract number of the day of the week from
/// \param[in] options for setting start of the week and day numbering
/// \param[in] ctx the function execution context, optional
/// \return the resulting datum
///
/// \since 5.0.0
/// \note API not yet finalized
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values, ExecContext* ctx = NULLPTR);
ARROW_EXPORT Result<Datum> DayOfWeek(const Datum& values,
DayOfWeekOptions options = DayOfWeekOptions(),
ExecContext* ctx = NULLPTR);

/// \brief DayOfYear returns number of day of the year for each element of `values`.
/// January 1st maps to day number 1, February 1st to 32, etc.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/function_test.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ TEST(FunctionOptions, Equality) {
options.emplace_back(new ProjectOptions({"col1"}, {false}, {}));
options.emplace_back(
new ProjectOptions({"col1"}, {false}, {key_value_metadata({{"key", "val"}})}));
options.emplace_back(new DayOfWeekOptions(false, 1));
options.emplace_back(new CastOptions(CastOptions::Safe(boolean())));
options.emplace_back(new CastOptions(CastOptions::Unsafe(int64())));
options.emplace_back(new FilterOptions());
Expand Down
97 changes: 88 additions & 9 deletions cpp/src/arrow/compute/kernels/scalar_temporal.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@
// under the License.

#include "arrow/builder.h"
#include "arrow/compute/api_scalar.h"
#include "arrow/compute/kernels/common.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/time.h"
Expand DownExpand Up@@ -48,6 +49,8 @@ using arrow_vendored::date::literals::thu;
using internal::applicator::ScalarUnaryNotNull;
using internal::applicator::SimpleUnary;

using DayOfWeekState = OptionsWrapper<DayOfWeekOptions>;

const std::string& GetInputTimezone(const Datum& datum) {
return checked_cast<const TimestampType&>(*datum.type()).timezone();
}
Expand DownExpand Up@@ -80,6 +83,25 @@ struct TemporalComponentExtract {
}
};

template <typename Op, typename OutType>
struct DayOfWeekExec {
using OutValue = typename internal::GetOutputType<OutType>::T;

static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
const DayOfWeekOptions& options = DayOfWeekState::Get(ctx);
if (options.week_start < 1 || 7 < options.week_start) {
return Status::Invalid(
"week_start must follow ISO convention (Monday=1, Sunday=7). Got week_start=",
options.week_start);
}

RETURN_NOT_OK(TemporalComponentExtractCheckTimezone(batch.values[0]));
applicator::ScalarUnaryNotNullStateful<OutType, TimestampType, Op> kernel{
Op(options)};
return kernel.Exec(ctx, batch, out);
}
};

// ----------------------------------------------------------------------
// Extract year from timestamp

Expand DownExpand Up@@ -118,16 +140,30 @@ struct Day {

// ----------------------------------------------------------------------
// Extract day of week from timestamp
//
// By default week starts on Monday represented by 0 and ends on Sunday represented
// by 6. Start day of the week (Monday=1, Sunday=7) and numbering start (0 or 1) can be
// set using DayOfWeekOptions

template <typename Duration>
struct DayOfWeek {
explicit DayOfWeek(const DayOfWeekOptions& options) {
for (int i = 0; i < 7; i++) {
lookup_table[i] = i + 8 - options.week_start;
lookup_table[i] = (lookup_table[i] > 6) ? lookup_table[i] - 7 : lookup_table[i];
lookup_table[i] += options.one_based_numbering;
}
}

template <typename T, typename Arg0>
static T Call(KernelContext*, Arg0 arg, Status*) {
return static_cast<T>(
weekday(year_month_day(floor<days>(sys_time<Duration>(Duration{arg}))))
.iso_encoding() -
1);
T Call(KernelContext*, Arg0 arg, Status*) const {
const auto wd = arrow_vendored::date::year_month_weekday(
floor<days>(sys_time<Duration>(Duration{arg})))
.weekday()
.iso_encoding();
return lookup_table[wd - 1];
}
std::array<int64_t, 7> lookup_table;
};

// ----------------------------------------------------------------------
Expand DownExpand Up@@ -398,6 +434,42 @@ std::shared_ptr<ScalarFunction> MakeTemporal(std::string name, const FunctionDoc
return func;
}

template <template <typename...> class Op, typename OutType>
std::shared_ptr<ScalarFunction> MakeTemporalWithOptions(
std::string name, const FunctionDoc* doc, const DayOfWeekOptions& default_options,
KernelInit init) {
const auto& out_type = TypeTraits<OutType>::type_singleton();
auto func =
std::make_shared<ScalarFunction>(name, Arity::Unary(), doc, &default_options);

for (auto unit : internal::AllTimeUnits()) {
InputType in_type{match::TimestampTypeUnit(unit)};
switch (unit) {
case TimeUnit::SECOND: {
auto exec = DayOfWeekExec<Op<std::chrono::seconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MILLI: {
auto exec = DayOfWeekExec<Op<std::chrono::milliseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::MICRO: {
auto exec = DayOfWeekExec<Op<std::chrono::microseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
case TimeUnit::NANO: {
auto exec = DayOfWeekExec<Op<std::chrono::nanoseconds>, OutType>::Exec;
DCHECK_OK(func->AddKernel({in_type}, out_type, std::move(exec), init));
break;
}
}
}
return func;
}

template <template <typename...> class Op>
std::shared_ptr<ScalarFunction> MakeStructTemporal(std::string name,
const FunctionDoc* doc) {
Expand DownExpand Up@@ -451,9 +523,14 @@ const FunctionDoc day_doc{

const FunctionDoc day_of_week_doc{
"Extract day of the week number",
("Week starts on Monday denoted by 0 and ends on Sunday denoted by 6.\n"
("By default, the week starts on Monday represented by 0 and ends on Sunday "
"represented by 6.\n"
"DayOfWeekOptions.week_start can be used to set another starting day using ISO "
Comment thread
jorisvandenbossche marked this conversation as resolved.
Outdated
"convention (Monday=1, Sunday=7). Day numbering can start with 0 or 1 using "
"DayOfWeekOptions.one_based_numbering parameter.\n"
"Returns an error if timestamp has a defined timezone. Null values return null."),
{"values"}};
{"values"},
"DayOfWeekOptions"};

const FunctionDoc day_of_year_doc{
"Extract number of day of year",
Expand DownExpand Up@@ -537,7 +614,9 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto day = MakeTemporal<Day, Int64Type>("day", &year_doc);
DCHECK_OK(registry->AddFunction(std::move(day)));

auto day_of_week = MakeTemporal<DayOfWeek, Int64Type>("day_of_week", &day_of_week_doc);
static auto default_day_of_week_options = DayOfWeekOptions::Defaults();
auto day_of_week = MakeTemporalWithOptions<DayOfWeek, Int64Type>(
"day_of_week", &day_of_week_doc, default_day_of_week_options, DayOfWeekState::Init);
DCHECK_OK(registry->AddFunction(std::move(day_of_week)));

auto day_of_year = MakeTemporal<DayOfYear, Int64Type>("day_of_year", &day_of_year_doc);
Expand All@@ -561,7 +640,7 @@ void RegisterScalarTemporal(FunctionRegistry* registry) {
auto minute = MakeTemporal<Minute, Int64Type>("minute", &minute_doc);
DCHECK_OK(registry->AddFunction(std::move(minute)));

auto second = MakeTemporal<Second, DoubleType>("second", &second_doc);
auto second = MakeTemporal<Second, Int64Type>("second", &second_doc);
DCHECK_OK(registry->AddFunction(std::move(second)));

auto millisecond =
Expand Down
Loading