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

added tests for decimal #790

Merged
merged 5 commits into from
Jun 4, 2024
Merged

added tests for decimal #790

merged 5 commits into from
Jun 4, 2024

Conversation

brennanjl
Copy link
Collaborator

This adds more tests for decimal and properly handles return types from sum, fixing a bug noted by Truflation trufnetwork/node#295.

It also fixes a variety of other small bugs, such as failure to catch runtime errors for calculations done on numeric array types, and not enforcing precision and scale on final results returned to the client.

switch {
case args[0].EqualsStrict(types.IntType):
retType = decimal1000.Copy()
case args[0].Name == types.DecimalStr:
Copy link
Member

Choose a reason for hiding this comment

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

I guess you're not using EqualsStrict because the metadata (precision and scale) isn't relevant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Exactly. Regardless of what the precision and scale is, if it is any sort of decimal we want to set it to precision 1000

retType = decimal1000.Copy()
case args[0].Name == types.DecimalStr:
retType = args[0].Copy()
retType.Metadata[0] = 1000 // max precision
Copy link
Member

Choose a reason for hiding this comment

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

Is there any need to modify scale? Error if not already 0? I'm just guessing about the assumptions here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so. In postgres, sum will always maintain the input's scale. If we were multiplying or dividing, I would be a bit more concerned about scale, but for sum it really seems like we should just give as much precision as possible and keep the scale the same

Comment on lines 585 to 586
case args[0].EqualsStrict(types.UnknownType):
retType = types.UnknownType.Copy()
Copy link
Member

Choose a reason for hiding this comment

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

EqualsStrict has this:

	// if unknown, return true. unknown is a special case used
	// internally when type checking is disabled.
	if c.Name == unknownStr || other.Name == unknownStr {
		return true
	}

So this can be true with any type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh jeez, that's a good catch. Will fix this

retType.Metadata[0] = 1000 // max precision
case args[0].EqualsStrict(types.Uint256Type):
retType = decimal1000.Copy()
case args[0].EqualsStrict(types.UnknownType):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you give an example what's a unknown numeric type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

unknown is a special case that can be used for any type. I refactored this part though, as noted by Jon's bug above.

// decimal1000 is a decimal type with a precision of 1000.
var decimal1000 *types.DataType

func init() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel we should always put init at the very top, but I couldn't find a guide for this. nbd

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I thought about it, but honestly I think it's more clear next to the decimal since it is what we are initializing.

Copy link
Contributor

@Yaiba Yaiba left a comment

Choose a reason for hiding this comment

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

LGTM

@brennanjl brennanjl merged commit 20c53ed into main Jun 4, 2024
2 checks passed
@brennanjl brennanjl deleted the decimal-tests branch June 4, 2024 16:30
@jchappelow jchappelow added this to the v0.8.0 milestone Jun 7, 2024
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