Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spark: Support singular form of years, months, days, and hours functions #12117

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ public class SparkFunctions {
private SparkFunctions() {}

private static final Map<String, UnboundFunction> FUNCTIONS =
ImmutableMap.of(
"iceberg_version", new IcebergVersionFunction(),
"years", new YearsFunction(),
"months", new MonthsFunction(),
"days", new DaysFunction(),
"hours", new HoursFunction(),
"bucket", new BucketFunction(),
"truncate", new TruncateFunction());
new ImmutableMap.Builder<String, UnboundFunction>()
.put("iceberg_version", new IcebergVersionFunction())
.put("years", new YearsFunction())
.put("year", new YearsFunction())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could extract a constant instead of initializing two instances.
(This comment might be conflicted with my another comment in TestSparkDaysFunction)

.put("months", new MonthsFunction())
.put("month", new MonthsFunction())
.put("days", new DaysFunction())
.put("day", new DaysFunction())
.put("hours", new HoursFunction())
.put("hour", new HoursFunction())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These function classes contain examples with plural styles in javadoc. Can we add another example or just update them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Let me update the javadoc of those functions to add examples with the singular form.
The functions also have name() methods that return the plural form. I'm not sure if we should change the name though. For now, my thinking is to leave the name alone, but just to support using the singular form (and documenting the use in javadoc). If there is a doc page that should be updated, I'll be happy to update it if you let me know.

.put("bucket", new BucketFunction())
.put("truncate", new TruncateFunction())
.build();

private static final Map<Class<?>, UnboundFunction> CLASS_TO_FUNCTIONS =
ImmutableMap.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public void testDates() {
assertThat(scalarSql("SELECT system.days(date('2017-12-01'))"))
.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.day(date('2017-12-01'))"))
Copy link
Contributor

@ebyhr ebyhr Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also update negative test cases? e.g. testWrongNumberOfArguments
The error message always uses plural names because function name is hard-coded in name() and canonicalName() methods in each function class. We may want to add a constructor taking the function name to report the correct function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't repeat all the test cases, as it does not seem necessary. I just added a sample of each (posititive) case with the singular form to demonstrate that the form can be used.

Copy link
Contributor

@ebyhr ebyhr Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It hides the fact that these functions can throw different function names which users specified. By the way, I'm not requesting repeating all the test cases.

Copy link
Contributor Author

@wypoon wypoon Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see what you're getting at.
I decided to add constructors to the functions so that we know what form (singular or plural) is being used; plural is the default to preserve existing behavior.

.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.days(date('1970-01-01'))"))
.as("Expected to produce 1970-01-01")
.isEqualTo(Date.valueOf("1970-01-01"));
Expand All @@ -53,6 +56,9 @@ public void testTimestamps() {
assertThat(scalarSql("SELECT system.days(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.day(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.days(TIMESTAMP '1970-01-01 00:00:01.000001 UTC+00:00')"))
.as("Expected to produce 1970-01-01")
.isEqualTo(Date.valueOf("1970-01-01"));
Expand All @@ -67,6 +73,9 @@ public void testTimestampNtz() {
assertThat(scalarSql("SELECT system.days(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.day(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 2017-12-01")
.isEqualTo(Date.valueOf("2017-12-01"));
assertThat(scalarSql("SELECT system.days(TIMESTAMP_NTZ '1970-01-01 00:00:01.000001 UTC')"))
.as("Expected to produce 1970-01-01")
.isEqualTo(Date.valueOf("1970-01-01"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public void testTimestamps() {
assertThat(scalarSql("SELECT system.hours(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 17501 * 24 + 10")
.isEqualTo(420034);
assertThat(scalarSql("SELECT system.hour(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 17501 * 24 + 10")
.isEqualTo(420034);
assertThat(scalarSql("SELECT system.hours(TIMESTAMP '1970-01-01 00:00:01.000001 UTC+00:00')"))
.as("Expected to produce 0 * 24 + 0 = 0")
.isEqualTo(0);
Expand All @@ -52,6 +55,9 @@ public void testTimestampsNtz() {
assertThat(scalarSql("SELECT system.hours(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 17501 * 24 + 10")
.isEqualTo(420034);
assertThat(scalarSql("SELECT system.hour(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 17501 * 24 + 10")
.isEqualTo(420034);
assertThat(scalarSql("SELECT system.hours(TIMESTAMP_NTZ '1970-01-01 00:00:01.000001 UTC')"))
.as("Expected to produce 0 * 24 + 0 = 0")
.isEqualTo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public void testDates() {
assertThat(scalarSql("SELECT system.months(date('2017-12-01'))"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.month(date('2017-12-01'))"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.months(date('1970-01-01'))"))
.as("Expected to produce 0 * 12 + 0 = 0")
.isEqualTo(0);
Expand All @@ -53,6 +56,9 @@ public void testTimestamps() {
assertThat(scalarSql("SELECT system.months(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.month(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.months(TIMESTAMP '1970-01-01 00:00:01.000001 UTC+00:00')"))
.as("Expected to produce 0 * 12 + 0 = 0")
.isEqualTo(0);
Expand All @@ -67,6 +73,9 @@ public void testTimestampNtz() {
assertThat(scalarSql("SELECT system.months(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.month(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 47 * 12 + 11 = 575")
.isEqualTo(575);
assertThat(scalarSql("SELECT system.months(TIMESTAMP_NTZ '1970-01-01 00:00:01.000001 UTC')"))
.as("Expected to produce 0 * 12 + 0 = 0")
.isEqualTo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public void testDates() {
assertThat(scalarSql("SELECT system.years(date('2017-12-01'))"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.year(date('2017-12-01'))"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.years(date('1970-01-01'))"))
.as("Expected to produce 1970 - 1970 = 0")
.isEqualTo(0);
Expand All @@ -53,6 +56,9 @@ public void testTimestamps() {
assertThat(scalarSql("SELECT system.years(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.year(TIMESTAMP '2017-12-01 10:12:55.038194 UTC+00:00')"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.years(TIMESTAMP '1970-01-01 00:00:01.000001 UTC+00:00')"))
.as("Expected to produce 1970 - 1970 = 0")
.isEqualTo(0);
Expand All @@ -67,6 +73,9 @@ public void testTimestampNtz() {
assertThat(scalarSql("SELECT system.years(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.year(TIMESTAMP_NTZ '2017-12-01 10:12:55.038194 UTC')"))
.as("Expected to produce 2017 - 1970 = 47")
.isEqualTo(47);
assertThat(scalarSql("SELECT system.years(TIMESTAMP_NTZ '1970-01-01 00:00:01.000001 UTC')"))
.as("Expected to produce 1970 - 1970 = 0")
.isEqualTo(0);
Expand Down