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

API-7308 sparkql date function support for day_of_week, day_of_year, and weekdays. #71

Merged
merged 9 commits into from
May 6, 2021

Conversation

wlmcewen
Copy link
Contributor

@wlmcewen wlmcewen commented May 5, 2021

When I brought up this feature in grooming, my understanding of the business need here was simply that the queries needed to know if a date was a specific day of the week (e.g. no Tuesday's allowed). However, it seems the more needed feature was actually to articulate a query using a range of weekdays from a date. The specific use case that just came in could still have been accomplished with the original functions, but it would have been a nasty db query:

(day_of_week(now()) Eq 5 And ShowingDate Le days(16) Or
day_of_week(now()) Eq 6 And ShowingDate Le days(15) Or
ShowingDate Le days(14))

Would now be:

ShowingDate Le weekdays(10)

I steered outside the original estimate to add in this brain teaser of a problem. I believe I've tested the bounds fairly well, but I'm hoping you all would nitpick my approach, or point out some logical or testing holes.

I've also included some project cleanup and rubocop'd the files I touched. These should be in separate commits, so focus on: e5be999 + ac7a0b8. Link for the shift+click struggle bus

One last note: Marcy and I did discuss support for some sort of automatic exclusion for holidays. This seems like a much bigger feature than what we have discussed so far, and we've managed to push back on that as a need for now. This is why I explicitly used the function weekdays() even though the ideal feature would amount to a function for business_days(). I'm booting that idea down the road as far as I can send it.

assert_equal '2012-10-27', value[:value]
end

test 'weekdays()' do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main test I was concerned about.

:value => "'#{string.to_s.upcase}'"
}
end
def weekdays(number_of_days)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And here is the new function. It turned out rather well, just don't look at my first implementation 🤣

today = current_date
weekend_start = today.saturday? || today.sunday?
direction = number_of_days.positive? ? 1 : -1
weeks = (number_of_days / 5.0).to_i
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shout out to Mr. Lobdell who helped me realize -4/5 == -1. This is the workaround I came up with to drop the remainder.

[-6, '2012-10-12'],
[-5, '2012-10-15'],
[-1, '2012-10-19'],
[0, '2012-10-22'],
Copy link
Contributor Author

@wlmcewen wlmcewen May 5, 2021

Choose a reason for hiding this comment

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

Oh right, I did make an opinionated decision here on what to do with zero on a weekend. My take is to move it to the following Monday. I've compared this with some online calculators, and they just didn't support zero, which I would be OK with as well.

@codygustafson
Copy link
Contributor

codygustafson commented May 5, 2021

I'm having a hard time following with the rubocop changes here. From the description, what I had imagined was something like:

(day_of_week(ShowingDate) Eq 5 or day_of_week(ShowingDate) Eq 6) And ShowDate Bt ...

I think I'm missing some context here, is all.

How does this:

(day_of_week(now()) Eq 5 And ShowingDate Le days(16) Or
day_of_week(now()) Eq 6 And ShowingDate Le days(15) Or
ShowingDate Le days(14))

get reduced to this?

ShowingDate Le days(14)

Doh, I should have read the next sentence before continuing to try and decipher the code

I've also included some project cleanup and rubocop'd the files I touched. These should be in separate commits, so focus on

Looking now.

@wlmcewen
Copy link
Contributor Author

wlmcewen commented May 5, 2021

get reduced to this?
ShowingDate Le days(14)

Sorry, that was some bad copy-pasta from my conversations with Marcy. I've corrected my summary to use the weekdays() function instead. Seems you're starting to see the purpose here. 👍

@codygustafson
Copy link
Contributor

Alright, I think I've gotten the gist here. I do like the shortening of the query for the client. My main concern is, the same with the days() function. That is, you can't decide things like timezone differences and such. But for comparing dates, not times, it should work great.

Gemfile Outdated Show resolved Hide resolved
@wlmcewen
Copy link
Contributor Author

wlmcewen commented May 6, 2021

@joshcom and @codygustafson, I needed to make one last change here, as I forgot that sparkql doesn't support underscores for field or function names. I could add that in, but thought renaming my functions to be consistent with what's already here may be reasonable. Does 'dayofyear' or 'doy' and 'dayofweek' or 'dow' fit the bill?

@codygustafson
Copy link
Contributor

I would vote for dayofyear but I'll let @joshcom speak up.

@joshcom
Copy link
Contributor

joshcom commented May 6, 2021

dayofyear/dayofweek is more consistent, when compared to other functions (e.g. toupper). Let's go with those.

@wlmcewen
Copy link
Contributor Author

wlmcewen commented May 6, 2021

Thanks guys, I'll update that and merge this on through!

@wlmcewen wlmcewen merged commit 5cc54cf into sparkapi:master May 6, 2021
@wlmcewen wlmcewen deleted the API-7308 branch May 6, 2021 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants