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

Allow partial date parsing when simple datetime formatter is used #11386

Closed
Closed
Show file tree
Hide file tree
Changes from 6 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
16 changes: 14 additions & 2 deletions velox/docs/functions/spark/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ These functions support TIMESTAMP and DATE input types.
date formatter in lenient mode that is align with Spark legacy date parser behavior or
`Joda <https://www.joda.org/joda-time/>`_ date formatter depends on ``spark.legacy_date_formatter`` configuration.
Returns NULL for parsing error or NULL input. When `Simple` date formatter is used, null is returned for invalid
``dateFormat``; otherwise, exception is thrown. ::
``dateFormat``; otherwise, exception is thrown. The `Simple` date formatter also permits partial date parsing
which means that ``dateFormat`` can match only a part of ``string``. For example, if ``string`` is
2015-07-22 10:00:00, it can be parsed using ``dateFormat`` is yyyy-MM-dd because the parser does not require entire
input to be consumed. In contrast, the `Joda` date formatter performs strict checks to ensure that the
``dateFormat`` completely matches the ``string``. If there is any mismatch, exception is thrown. ::

SELECT get_timestamp('1970-01-01', 'yyyy-MM-dd); -- timestamp `1970-01-01`
SELECT get_timestamp('1970-01-01', 'yyyy-MM'); -- NULL (parsing error)
SELECT get_timestamp('1970-01-01', null); -- NULL
SELECT get_timestamp('2020-06-10', 'A'); -- (throws exception)
SELECT get_timestamp('2015-07-22 10:00:00', 'yyyy-MM-dd'); -- timestamp `2015-07-22` (for Simple date formatter)
SELECT get_timestamp('2015-07-22 10:00:00', 'yyyy-MM-dd'); -- (throws exception) (for Joda date formatter)

.. spark:function:: hour(timestamp) -> integer

Expand Down Expand Up @@ -311,7 +317,13 @@ These functions support TIMESTAMP and DATE input types.
`Datetime patterns for formatting and parsing
<https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html>`_.
Returns null if ``string`` does not match ``format`` or if ``format``
is invalid.
is invalid. The `Simple` date formatter permits partial date parsing
NEUpanning marked this conversation as resolved.
Show resolved Hide resolved
which means that ``format`` can match only a part of ``string``. For example,
if ``string`` is 2015-07-22 10:00:00, it can be parsed using ``format`` is
yyyy-MM-dd because the parser does not require entire input to be consumed.
In contrast, the `Joda` date formatter performs strict checks to ensure that the
``format`` completely matches the ``string``. If there is any mismatch,
exception is thrown.

.. function:: week_of_year(x) -> integer

Expand Down
5 changes: 3 additions & 2 deletions velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,9 @@ Expected<DateTimeResult> DateTimeFormatter::parse(
}
}

// Ensure all input was consumed.
if (cur < end) {
// Ensure all input was consumed if type_ is not simple datetime formatter.
if (type_ != DateTimeFormatterType::LENIENT_SIMPLE &&
type_ != DateTimeFormatterType::STRICT_SIMPLE && cur < end) {
return parseFail(input, cur, end);
}

Expand Down
9 changes: 9 additions & 0 deletions velox/functions/lib/tests/DateTimeFormatterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,4 +2441,13 @@ TEST_F(SimpleDateTimeFormatterTest, formatWeekOfMonth) {
}
}

TEST_F(SimpleDateTimeFormatterTest, parseUsingPartialInput) {
EXPECT_EQ(
fromTimestampString("2024-08-01"),
parseSimple("2024 08 01 5", "yyyy MM", true).timestamp);
EXPECT_EQ(
fromTimestampString("2024-08-01"),
parseSimple("2024 08 01 5", "yyyy MM", false).timestamp);
}

} // namespace facebook::velox::functions
Loading