Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Add helper function to return the number of days since epoch for a week-of-month date #10604
Add helper function to return the number of days since epoch for a week-of-month date #10604
Changes from 13 commits
6104fc6
1d4667b
e4b2c78
b4504f8
e9be740
a4ff46c
555e1d9
83301ab
e718074
9ce7750
216a000
e850388
cb45539
a98fedd
6135991
b5f3be9
3165392
d3f2657
0e9412c
7a52b45
7d72017
3e2fb0c
0fcb111
0d29272
9267d21
935bab1
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
According to the discussion at https://github.com/facebookincubator/velox/pull/10511/files#r1686566980, true result does not always indicate a valid date.
With current implementation, for some invalid date, we return user error status while for the others we return a int64_t result in the
daysSinceEpochFromWeekOfMonthDate
function, which seems to be confusing.I notice the method
isValidDate
provides an accurate evaluation for valid date. Does it make sense to use that?https://github.com/facebookincubator/velox/blob/main/velox/type/TimestampConversion.cpp#L518-L529
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.
You are right. We should remove the validation for week of month. According to SimpleDateFormat's doc the range of
day
is [1,7] soisValidDate
isn't suitable for this.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.
Done. See e718074
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.
Could you clarify the purpose of
isValidWeekOfMonthDate
? Is it to follow certain rule defined in SimpleDateFormat to check if a date is valid, and what are the rules? Perhaps add some comments for this function.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 want to do validation follows SimpleDateFormat. However, SimpleDateFormat does not provide clear rules or relevant documentation. For example, document states
WEEK_OF_MONTH
field range is from 0 to 6, , but in my tests, a value of 99 is considered valid.(https://github.com/facebookincubator/velox/pull/10511/files#r1686566980)Another example is the valid range for the
year
field, which is not documented.I tried to determine the valid range through testing, but it is too costly and not reliable enough. I wonder how to properly validate dates in Velox. Could you give me some advice?
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.
@rui-mo I don't believe this functionality is needed for Presto functions. Hence, we are free to define semantics in any "sensible" way. Assuming we need these semantics to match Spark, we can use Spark semantics.
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.
Thanks for the pointer. Understood.
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.
@mbasmanova @rui-mo According to this discussion, Spark date validation rules in document don't match with the test results. Which one should I follow? Thanks.
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.
@NEUpanning I assume we need to follow the actual behavior of Spark. How do you think?
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.
@rui-mo I agree with you. I will change the part of date validation for following the actual behavior of Spark.
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.
This doesn't seem to match the documentation, which suggests the valid range is fixed [1, 7].
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.
Same as above. The documentation is not clear. Thanks.
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 believe I asked this earlier, but I don't think I saw an answer, hence, asking again. What happens if weekOfMonth is 5, but month has only 4 weeks (Feb)? Similarly, what happens if weekOfMonth is 5 and dayOfWeek is 7 (no month has 35 days)?
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.
In Java's SimpleDateFormat, "invalid" weekOfMonth and dayOfWeek etc. are allowed. If the date falls outside the valid range, the week or day from the preceding or following month's will be used. For example, if weekOfMonth is 5 but the current month only has 4 weeks (such as February), the first week of March will be considered as the 5th week of February. Similarly, if weekOfMonth is 5 and dayOfWeek is 7, the corresponding day from the next month will be used.
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.
Thank you for clarifying. Let's update method comments to explain this as it is not obvious. Do we already have tests for these cases? If not, let's make sure to add.
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 added this comments for clarifying and two tests for cases that days in previous/next month.
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'm seeing that DATE type is based on INTEGER type, hence, allows only int32_t number of days since epoch. If that's the case, then, perhaps, daysSinceEpochFromDate should return int32_t, not int64_t.
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.
This PR is intended for date parsing, and the DateTimeFormatter::parse method returns a Timestamp. A Timestamp is based on int64_t seconds and can represent up to 106,751,991,167 days, which is calculated as (2^63 - 1) / 1000 / 86400. This value is greater than the maximum value of an int32_t. Therefore, I think that daysSinceEpochFromDate should return an int64_t.
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.
extractISODayOfTheWeek
accepts an int32_t value while the input is of int64_t type. Shall we check for overflow?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 notice
extractISODayOfTheWeek
casts input to int64_t. How about changing parameter to int64_t type instead of int32_t typeThere 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.
Looks like the cast is to avoid overflow in addition and negating. If we change it as int64_t, overflow needs to considered as well by casting to a larger type.
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.
Thanks for your explanation. I see
daysSinceEpochFromWeekDate
function also calls this funtion with a int64_t argument. I think we changeextractISODayOfTheWeek
parameter to int64_t and cast it to int128_t when do calculate would be better. How do you think?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 if there is any limitation for year? If the days cannot exceed INT32_MAX, it would be safe to call it.
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.
The max year is 292278994 so the days can be (292278994-1970)*365=106,681,113,760 that is bigger than 2,147,483,647(INT32_MAX).
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.
Looks fine to me. @mbasmanova What are your thoughts? Thanks.
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.
typo? The range doesn't seem to allow negative values or zero.
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.
Not a typo. The min value of year in non-lenient mode is 1. Here is Java implementation:
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.
@NEUpanning Got it. In this case "e.g: 1996, -2000." needs updating since -2000 is not allowed.
It might be helpful to re-iterate that dates before Jan 1, 1970 are not supported in non-lenient mode.
I find it hard to say / write non-lenient. Would it make sense to flip the boolean to 'strict' mode?
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.
@mbasmanova I assume using
non-lenient
is clearer because it indicates we only have tow modes. If usingstrict
, readers might wonder if there is abalanced
ormoderate
mode.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'm not sure I follow. I was suggesting to consider replacing 'lenient' boolean with 'strict'.
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.
Spark uses the term 'lenient', and this function aligns with Spark. See link. Therefore, I think 'lenient' would be more appropriate.
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.
@NEUpanning I assume this suggestion is about replacing 'non-lenient' with 'strict' which makes it more straightforward to understand. The 'lenient' will stay unchanged.
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.
@rui-mo
This is my concern. If this is not a problem or rarely happens, I'd like to replace it.
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.
@NEUpanning Since it has been clarified in the comment that there are only two modes, so I think it is fine. Thanks.
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.
Thanks for your suggestion. I've replaced it.
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.
The implementation imposes limits on the 'year' value. Would you document these?
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.
Thanks. Done.