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 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* A Spark function implementation for the Iceberg day transform.
*
* <p>Example usage: {@code SELECT system.days('source_col')}.
*
* <p>Alternate form: {@code SELECT system.day('source_col')}.
*/
public class DaysFunction extends UnaryUnboundFunction {
Copy link
Contributor Author

@wypoon wypoon Jan 28, 2025

Choose a reason for hiding this comment

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

@ebyhr if I understand you correctly, you also suggest introducing a constructor that takes a String name and then have name() return this name, right?
So new DaysFunction("day") would return "day" when its name() is called, while new DaysFunction("days") would return "days".
I think that introduces more complexity than it's worth.

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.

We don't want the function to be constructed with an arbitrary name. We really just want the name to be either "day" or "days" in this case. So we can have a constructor with a boolean.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* A Spark function implementation for the Iceberg hour transform.
*
* <p>Example usage: {@code SELECT system.hours('source_col')}.
*
* <p>Alternate form: {@code SELECT system.hour('source_col')}.
*/
public class HoursFunction extends UnaryUnboundFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* A Spark function implementation for the Iceberg month transform.
*
* <p>Example usage: {@code SELECT system.months('source_col')}.
*
* <p>Alternate form: {@code SELECT system.month('source_col')}.
*/
public class MonthsFunction extends UnaryUnboundFunction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,37 @@ public class SparkFunctions {

private SparkFunctions() {}

private static final UnboundFunction VERSION_FUNCTION = new IcebergVersionFunction();
private static final UnboundFunction YEAR_FUNCTION = new YearsFunction();
private static final UnboundFunction MONTH_FUNCTION = new MonthsFunction();
private static final UnboundFunction DAY_FUNCTION = new DaysFunction();
private static final UnboundFunction HOUR_FUNCTION = new HoursFunction();
private static final UnboundFunction BUCKET_FUNCTION = new BucketFunction();
private static final UnboundFunction TRUNCATE_FUNCTION = new TruncateFunction();

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", VERSION_FUNCTION)
.put("years", YEAR_FUNCTION)
.put("year", YEAR_FUNCTION)
.put("months", MONTH_FUNCTION)
.put("month", MONTH_FUNCTION)
.put("days", DAY_FUNCTION)
.put("day", DAY_FUNCTION)
.put("hours", HOUR_FUNCTION)
.put("hour", HOUR_FUNCTION)
.put("bucket", BUCKET_FUNCTION)
.put("truncate", TRUNCATE_FUNCTION)
.build();

private static final Map<Class<?>, UnboundFunction> CLASS_TO_FUNCTIONS =
ImmutableMap.of(
YearsFunction.class, new YearsFunction(),
MonthsFunction.class, new MonthsFunction(),
DaysFunction.class, new DaysFunction(),
HoursFunction.class, new HoursFunction(),
BucketFunction.class, new BucketFunction(),
TruncateFunction.class, new TruncateFunction());
YearsFunction.class, YEAR_FUNCTION,
MonthsFunction.class, MONTH_FUNCTION,
DaysFunction.class, DAY_FUNCTION,
HoursFunction.class, HOUR_FUNCTION,
BucketFunction.class, BUCKET_FUNCTION,
TruncateFunction.class, TRUNCATE_FUNCTION);

private static final List<String> FUNCTION_NAMES = ImmutableList.copyOf(FUNCTIONS.keySet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* A Spark function implementation for the Iceberg year transform.
*
* <p>Example usage: {@code SELECT system.years('source_col')}.
*
* <p>Alternate form: {@code SELECT system.year('source_col')}.
*/
public class YearsFunction extends UnaryUnboundFunction {

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