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

feat(sql): add format_price function for finance #4960

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

thalesreisbr
Copy link

Implements format_price to #4620

@CLAassistant
Copy link

CLAassistant commented Sep 16, 2024

CLA assistant check
All committers have signed the CLA.

@thalesreisbr
Copy link
Author

Hi @nwoolmer,

I'm new on open source, this is my first issue.

I'd like to help in more issues.

I implement the format_price to #4620, But in the documentation https://www.cmegroup.com/education/courses/introduction-to-treasuries/calculating-us-treasury-pricing.html can have the third digit in fraction part ( The Default for US Treasury is quarter of 32nd of a point). But you didn't put that in the signature described in #4620, isn't that expected?

@glasstiger
Copy link
Contributor

glasstiger commented Sep 17, 2024

Hi @nwoolmer,

I'm new on open source, this is my first issue.

I'd like to help in more issues.

I implement the format_price to #4620, But in the documentation https://www.cmegroup.com/education/courses/introduction-to-treasuries/calculating-us-treasury-pricing.html can have the third digit in fraction part ( The Default for US Treasury is quarter of 32nd of a point). But you didn't put that in the signature described in #4620, isn't that expected?

Hi @thalesreisbr, thank you for the contribution, and this is a very good question.
What you see in the issue description, is just an example.
The implementation should be able to parse the 3rd digit too, i.e. should support quarter and half of 32nd of a point too.

return new FormatPrice(args.getQuick(0), args.getQuick(1));
}

private static class FormatPrice extends StrFunction implements BinaryFunction {
Copy link
Member

Choose a reason for hiding this comment

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

it is better to have a VARCHAR function. This is a UTF8 encoded string, that avoids UTF16-to-UTF8 reencoding on the wire.

double fractionalPart = decimalPrice - wholePart;
int fractionalTicks = (int) Math.round(fractionalPart * tickSize);

return String.format("%d-%02d", wholePart, fractionalTicks);
Copy link
Member

Choose a reason for hiding this comment

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

while this is a conventional Java way to format primitive numerics as a string, we are trying to avoid this method across our codebase to avoid generating string objects on the critical path.

Instead, please create a sink class member of Utf8StringSink type and print primitive values into that sink.

null values should not be printed at all, thus resulting in an empty sink. Hope this makes sense?

public class FormatPriceFunctionFactoryTest extends AbstractFunctionFactoryTest {

@Test
public void testFormatPriceHappyPath() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

could you add tests that produce multi-row result set? for example

select format_price(rnd_double() * 100, 16) from long_sequence(50) order by 1

that would also sort the values of your function to a more complete exercise.

assertQuery("format_price\n100-16\n", "select format_price(100.5, 32)");
assertQuery("format_price\n101-00\n", "select format_price(101.0, 32)");
assertQuery("format_price\n101-00\n", "select format_price(101, 32)");
assertQuery("format_price\n99-12\n", "select format_price(99.375, 32)");
Copy link
Member

Choose a reason for hiding this comment

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

please add tests that exercise different values of the tick size

}

private static class FormatPrice extends StrFunction implements BinaryFunction {
private final Function decimal_form;
Copy link
Member

Choose a reason for hiding this comment

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

in Java we use camelCase variable names, e.g. decimalForm

@nwoolmer
Copy link
Contributor

Hey @thalesreisbr , are you still interested in this or would you like us to sort it out?

@devkhishan
Copy link

Hey @thalesreisbr , are you still interested in this or would you like us to sort it out?

Hi @nwoolmer, I would like to take this issue if possible

@nwoolmer
Copy link
Contributor

nwoolmer commented Dec 1, 2024

Let me get back to you on Monday!

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.

6 participants