-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat: add PARSE_DATE and FORMAT_DATE functions #7733
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.KsqlFunctionException; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@UdfDescription( | ||
name = "format_date", | ||
category = FunctionCategory.DATE_TIME, | ||
author = KsqlConstants.CONFLUENT_AUTHOR, | ||
description = "Converts a DATE value to a string" | ||
+ " using the given format pattern. The format pattern should be" | ||
+ " in the format expected by java.time.format.DateTimeFormatter." | ||
) | ||
public class FormatDate { | ||
|
||
private final LoadingCache<String, DateTimeFormatter> formatters = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(1000) | ||
.build(CacheLoader.from(DateTimeFormatter::ofPattern)); | ||
|
||
@Udf(description = "Converts the number of days since 1970-01-01 00:00:00 UTC/GMT to a date " | ||
+ "string using the given format pattern. The format pattern should be in the format" | ||
+ " expected by java.time.format.DateTimeFormatter") | ||
public String formatDate( | ||
@UdfParameter( | ||
description = "The date to convert") final Date date, | ||
@UdfParameter( | ||
description = "The format pattern should be in the format expected by" | ||
+ " java.time.format.DateTimeFormatter.") final String formatPattern) { | ||
if (date == null) { | ||
return null; | ||
} | ||
try { | ||
final DateTimeFormatter formatter = formatters.get(formatPattern); | ||
return LocalDate.ofEpochDay(TimeUnit.MILLISECONDS.toDays(date.getTime())).format(formatter); | ||
} catch (final ExecutionException | RuntimeException e) { | ||
throw new KsqlFunctionException("Failed to format date " + date | ||
+ " with formatter '" + formatPattern | ||
+ "': " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.KsqlFunctionException; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@UdfDescription( | ||
name = "parse_date", | ||
category = FunctionCategory.DATE_TIME, | ||
author = KsqlConstants.CONFLUENT_AUTHOR, | ||
description = "Converts a string representation of a date in the given format" | ||
+ " into a DATE value. The format pattern should be in the format expected by" | ||
+ " java.time.format.DateTimeFormatter" | ||
) | ||
public class ParseDate { | ||
|
||
private final LoadingCache<String, DateTimeFormatter> formatters = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(1000) | ||
.build(CacheLoader.from(DateTimeFormatter::ofPattern)); | ||
|
||
@Udf(description = "Converts a string representation of a date in the given format" | ||
+ " into a DATE value.") | ||
public Date parseDate( | ||
@UdfParameter( | ||
description = "The string representation of a date.") final String formattedDate, | ||
@UdfParameter( | ||
description = "The format pattern should be in the format expected by" | ||
+ " java.time.format.DateTimeFormatter.") final String formatPattern) { | ||
try { | ||
|
||
final TemporalAccessor ta = formatters.get(formatPattern).parse(formattedDate); | ||
final Optional<ChronoField> timeField = Arrays.stream(ChronoField.values()) | ||
.filter(field -> field.isTimeBased()) | ||
.filter(field -> ta.isSupported(field)) | ||
.findFirst(); | ||
|
||
if (timeField.isPresent()) { | ||
throw new KsqlFunctionException("Date format contains time field."); | ||
} | ||
|
||
return new Date( | ||
TimeUnit.DAYS.toMillis(LocalDate.from(ta).toEpochDay())); | ||
} catch (final ExecutionException | RuntimeException e) { | ||
throw new KsqlFunctionException("Failed to parse date '" + formattedDate | ||
+ "' with formatter '" + formatPattern | ||
+ "': " + e.getMessage(), e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||
/* | ||||
* Copyright 2021 Confluent Inc. | ||||
* | ||||
* Licensed under the Confluent Community License (the "License"); you may not use | ||||
* this file except in compliance with the License. You may obtain a copy of the | ||||
* License at | ||||
* | ||||
* http://www.confluent.io/confluent-community-license | ||||
* | ||||
* Unless required by applicable law or agreed to in writing, software | ||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||||
* specific language governing permissions and limitations under the License. | ||||
*/ | ||||
|
||||
package io.confluent.ksql.function.udf.datetime; | ||||
|
||||
import static org.hamcrest.CoreMatchers.is; | ||||
import static org.hamcrest.MatcherAssert.assertThat; | ||||
import static org.hamcrest.Matchers.containsString; | ||||
import static org.junit.Assert.assertThrows; | ||||
import static org.junit.Assert.fail; | ||||
|
||||
import io.confluent.ksql.function.KsqlFunctionException; | ||||
import java.sql.Date; | ||||
import java.util.stream.IntStream; | ||||
import org.junit.Before; | ||||
import org.junit.Test; | ||||
|
||||
public class FormatDateTest { | ||||
|
||||
private FormatDate udf; | ||||
|
||||
@Before | ||||
public void setUp() { | ||||
udf = new FormatDate(); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldConvertDateToString() { | ||||
// When: | ||||
final String result = udf.formatDate(Date.valueOf("2014-11-09"), "yyyy-MM-dd"); | ||||
|
||||
// Then: | ||||
assertThat(result, is("2014-11-09")); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldThrowOnUnsupportedFields() { | ||||
// When: | ||||
final Exception e = assertThrows( | ||||
KsqlFunctionException.class, | ||||
() -> udf.formatDate(Date.valueOf("2014-11-09"), "yyyy-MM-dd HH:mm")); | ||||
|
||||
// Then: | ||||
assertThat(e.getMessage(), is("Failed to format date 2014-11-09 with formatter 'yyyy-MM-dd HH:mm': Unsupported field: HourOfDay")); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldRoundTripWithStringToDate() { | ||||
final String format = "dd/MM/yyyy'Freya'"; | ||||
final ParseDate parseDate = new ParseDate(); | ||||
IntStream.range(-10_000, 20_000) | ||||
.parallel() | ||||
.forEach(idx -> { | ||||
final String result = udf.formatDate(new Date(idx * 86400000L), format); | ||||
final Date date = parseDate.parseDate(result, format); | ||||
assertThat(date.getTime(), is(idx * 86400000L)); | ||||
}); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldSupportEmbeddedChars() { | ||||
// When: | ||||
final Object result = udf.formatDate(Date.valueOf("2014-11-09"), "yyyy-dd-MM'Fred'"); | ||||
|
||||
// Then: | ||||
assertThat(result, is("2014-09-11Fred")); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this. Should we support embedded chars in the final date string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was supported in the old DateToString function: ksql/ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/datetime/DateToStringTest.java Line 61 in cd1a988
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just tested Mysql and it supports this too. Perhaps it is allowed in order to write the format you want, i.e. dates |
||||
} | ||||
|
||||
@Test | ||||
public void shouldThrowIfFormatInvalid() { | ||||
// When: | ||||
final Exception e = assertThrows( | ||||
KsqlFunctionException.class, | ||||
() -> udf.formatDate(Date.valueOf("2014-11-09"), "invalid") | ||||
); | ||||
|
||||
// Then: | ||||
assertThat(e.getMessage(), containsString("Failed to format date 2014-11-09 with formatter 'invalid'")); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldByThreadSafeAndWorkWithManyDifferentFormatters() { | ||||
IntStream.range(0, 10_000) | ||||
.parallel() | ||||
.forEach(idx -> { | ||||
try { | ||||
final String pattern = "yyyy-MM-dd'X" + idx + "'"; | ||||
final String result = udf.formatDate(Date.valueOf("2021-05-18"), pattern); | ||||
assertThat(result, is("2021-05-18X" + idx)); | ||||
} catch (final Exception e) { | ||||
fail(e.getMessage()); | ||||
} | ||||
}); | ||||
} | ||||
|
||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what if
formatPattern
contains time characters? Would those be part of the final String?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test for this.