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

CASSANDRA-19546: Add to_human_size and to_human_duration function #3741

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

chengw-netflix
Copy link
Contributor

Thanks for sending a pull request! Here are some tips if you're new here:

  • Ensure you have added or run the appropriate tests for your PR.
  • Be sure to keep the PR description updated to reflect all changes.
  • Write your PR title to summarize what this PR proposes.
  • If possible, provide a concise example to reproduce the issue for a faster review.
  • Read our contributor guidelines
  • If you're making a documentation change, see our guide to documentation contribution

Commit messages should follow the following format:

<One sentence description, usually Jira title or CHANGES.txt summary>

<Optional lengthier description (context on patch)>

patch by <Authors>; reviewed by <Reviewers> for CASSANDRA-#####

Co-authored-by: Name1 <email1>
Co-authored-by: Name2 <email2>

The Cassandra Jira


==== format_bytes

This function looks at values in a column as if it was in bytes, and it will convert it to whatever a user pleases. There are three ways how to call this function
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: final .

)
----

Imagine that we wanted to look at `val` values as if they were in mebibytes. We would like to have more human-friendly output in order to not visually divide the values by 1024 in order to get them in respective bigger units. The following function call may take just a column itself as an argument, and it will
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL what a mebibyte is :-)
https://simple.wikipedia.org/wiki/Mebibyte

*/
public static class FormatTimeFct extends NativeScalarFunction
{
private static final String FUNCTION_NAME = "format_time";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be more specific with the naming and have it like human_format_time?


long value = getValue(arguments);

if (value < 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we don't want to support negative times? (as in before now?)

sourceUnit = MILLISECONDS;
targetUnitAsString = arguments.get(1);
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also check for arguments.size() > 3? I think we should also return an `InvalidRequestException on that case instead of silently ignoring the extra args.

else if (targetUnit == DAYS)
return x(duration::toDays);
else
throw new InvalidRequestException("unsupported target unit " + targetUnit);
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes me wonder, should we move the validate unit calls inside this method to avoid having to call them from the main execute method?

*/
public static class FormatBytesFct extends NativeScalarFunction
{
private static final String FUNCTION_NAME = "format_bytes";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be more specific with the naming and have it like human_format_bytes?

sourceUnit = BYTES;
targetUnit = validateUnit(arguments.get(1));
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as before: Should we also check for arguments.size() > 3? I think we should also return an InvalidRequestException on that case instead of silently ignoring the extra args.

else if (targetUnit == GIBIBYTES)
return sourceUnit.toGibibytes(valueToConvert);
else
throw new InvalidRequestException("unsupported target unit " + targetUnit);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as before: This makes me wonder, should we move the validate unit calls inside this method to avoid having to call them from the main execute method?

import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class FormatBytesFctTest extends CQLTester
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests look like could benefit from the usage of QuickTheories. There are other tests in the code base using it. Maybe it is worth taking a look to increase coverage against unexpected inputs.

https://github.com/quicktheories/QuickTheories

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