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

Advanced rounding: Multiples and non-numeric values #5442

Open
philrz opened this issue Nov 6, 2024 · 0 comments
Open

Advanced rounding: Multiples and non-numeric values #5442

philrz opened this issue Nov 6, 2024 · 0 comments

Comments

@philrz
Copy link
Contributor

philrz commented Nov 6, 2024

tl;dr

A user asked in a recent community Slack thread:

is there an easy way to floor a duration? 18m2.43245343s => 18m2s

There's some ways to achieve this in the language currently, but Excel's MROUND and related functions seem like a nice template of ideas of how we might improve on this.

Details

At the time this issue is being filed, super is at commit. 92e3052.

FWIW, the specific result the user was seeking can be obtained in a couple ways, such as using the bucket function as suggested by @mattnibs:

$ super -version
Version: v1.18.0-121-g92e30525

$ echo '18m2.43245343s' | zq -z 'bucket(this, 1s)' -
18m2s

Before seeing that approach, I'd also come up with this hacky approach:

$ super -version
Version: v1.18.0-121-g92e30525

$ echo '18m2.43245343s' | super -z -c 'duration(regexp_replace(string(this), /\..*s$/, "s"))' -
18m2s

But for the general case, something more flexible is probably justified. Looking at what else is out there, Excel's MROUND has at least two improvements I could see adopting here.

  1. Excel's MROUND accepts time-based values, not just pure numbers. Right now our functions like round and floor only take numeric arguments, so for a duration value like the user's, the functions currently just return the same value unmodified. While bucket does take time & duration values, it behaves only in a "floor"-like manner, so a user that wants something "round"-like or "ceil"-like would need another approach.

  2. Excel's MROUND takes take an argument that can specify an arbitrary multiple by which to round/floor, whereas right now our round/floor/ceil functions only produce a result to the the appropriate nearest integer. bucket() can take an arbitrary duration value for its argument so it can produce a result like "the nearest 15-minute floor", but as previously mentioned, it can't produce a "round"-like or "ceil"-like result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant