diff --git a/QueryEngine/Execute.cpp b/QueryEngine/Execute.cpp index af2d42036a..d597729470 100644 --- a/QueryEngine/Execute.cpp +++ b/QueryEngine/Execute.cpp @@ -125,6 +125,7 @@ bool g_enable_smem_non_grouped_agg{ // non-grouped aggregates bool g_is_test_env{false}; // operating under a unit test environment. Currently only // limits the allocation for the output buffer arena +bool g_monday_first_weekday{false}; size_t g_approx_quantile_buffer{1000}; size_t g_approx_quantile_centroids{300}; diff --git a/QueryEngine/ExtractFromTime.cpp b/QueryEngine/ExtractFromTime.cpp index 7a188812d0..2d49dded8c 100644 --- a/QueryEngine/ExtractFromTime.cpp +++ b/QueryEngine/ExtractFromTime.cpp @@ -72,7 +72,7 @@ extern "C" ALWAYS_INLINE DEVICE int64_t extract_nanosecond(const int64_t lcltime // First day of epoch is Thursday, so + 4 to have Sunday=0. extern "C" ALWAYS_INLINE DEVICE int64_t extract_dow(const int64_t lcltime) { int64_t const days_past_epoch = floor_div(lcltime, kSecsPerDay); - return unsigned_mod(days_past_epoch + 4, kDaysPerWeek); + return unsigned_mod(days_past_epoch + 4 - g_monday_first_weekday, kDaysPerWeek); } extern "C" ALWAYS_INLINE DEVICE int64_t extract_quarterday(const int64_t lcltime) { diff --git a/Tests/ExecuteTest.cpp b/Tests/ExecuteTest.cpp index 0611042d7e..23b7ef83a5 100644 --- a/Tests/ExecuteTest.cpp +++ b/Tests/ExecuteTest.cpp @@ -4903,6 +4903,31 @@ TEST(Select, Time) { "test WHERE (m >= TIMESTAMP(3) '1970-01-01 " "00:00:00.000') GROUP BY key0 ORDER BY key0 LIMIT 1;", dt))); + + // test first weekday option + bool prev_monday_first_weekday = g_monday_first_weekday; + g_monday_first_weekday = true; + // Monday + ASSERT_EQ(0, + v(run_simple_agg("SELECT EXTRACT(DOW FROM CAST('2008-03-03 " + "20:00:11' AS TIMESTAMP)) FROM test limit 1;", + dt))); + // Wednesday + ASSERT_EQ(2, + v(run_simple_agg("SELECT EXTRACT(DOW FROM CAST('2021-01-27 " + "20:15:12' AS TIMESTAMP)) FROM test limit 1;", + dt))); + // Saturday + ASSERT_EQ(5, + v(run_simple_agg("SELECT EXTRACT(DOW FROM CAST('2020-11-14 " + "14:47:12' AS TIMESTAMP)) FROM test limit 1;", + dt))); + // Sunday + ASSERT_EQ(6, + v(run_simple_agg("SELECT EXTRACT(DOW FROM CAST('2020-09-27 " + "21:30:12' AS TIMESTAMP)) FROM test limit 1;", + dt))); + g_monday_first_weekday = prev_monday_first_weekday; } } diff --git a/ThriftHandler/CommandLineOptions.cpp b/ThriftHandler/CommandLineOptions.cpp index 2546d6a236..cf91c212d5 100644 --- a/ThriftHandler/CommandLineOptions.cpp +++ b/ThriftHandler/CommandLineOptions.cpp @@ -265,6 +265,11 @@ void CommandLineOptions::fillOptions() { ->default_value(g_null_div_by_zero) ->implicit_value(true), "Return null on division by zero instead of throwing an exception."); + help_desc.add_options()("monday-first-weekday", + po::value(&g_monday_first_weekday) + ->default_value(g_monday_first_weekday) + ->implicit_value(true), + "Set Monday as first day of week instead of Sunday"); help_desc.add_options()( "num-reader-threads", po::value(&num_reader_threads)->default_value(num_reader_threads), diff --git a/ThriftHandler/CommandLineOptions.h b/ThriftHandler/CommandLineOptions.h index 7878450f69..0900b3c9ea 100644 --- a/ThriftHandler/CommandLineOptions.h +++ b/ThriftHandler/CommandLineOptions.h @@ -179,6 +179,7 @@ extern size_t g_gpu_smem_threshold; extern bool g_enable_smem_non_grouped_agg; extern bool g_enable_smem_grouped_non_count_agg; extern bool g_use_estimator_result_cache; +extern bool g_monday_first_weekday; extern int64_t g_omni_kafka_seek; extern size_t g_leaf_count;