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

[CALCITE-2684] AssertionError on RexBuilder when creating BigDecimal RexLiteral (Ruben Quesada Lopez) #928

Closed
wants to merge 1 commit into from

Conversation

rubenada
Copy link
Contributor

The method RexBuilder#makeExactLiteral(java.math.BigDecimal) throws an AssertionError if the BigDecimal parameter has an unscaled value that overflows long.
Moreover, with the current implementation, it can be possible to have this AssertionError, even in the cases when the variable l would not be used at all (decimal number).
Provided unit tests (the last one will fail with the current implementation).

new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RexBuilder builder = new RexBuilder(typeFactory);
final RexNode literal1 = builder.makeExactLiteral(new BigDecimal("25"));
assertThat(((RexLiteral) literal1).getValueAs(BigDecimal.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please factor the common logic to assertBigDecimalLiteral(String inputText, String expectedOutputText)?

That would make test much more readable (it won't include repeated builder.makeExactLiteral...), and the failure message would be much easier to understand.

Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified.


private void assertBigDecimalLiteral(RexBuilder builder, String val) {
final RexNode literal = builder.makeExactLiteral(new BigDecimal(val));
assertThat(((RexLiteral) literal).getValueAs(BigDecimal.class).toString(), is(val));
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add message parameter to assertThat so it is clear WHAT is test testing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Message parameter included.


private void assertBigDecimalLiteral(RexBuilder builder, String val) {
final RexNode literal = builder.makeExactLiteral(new BigDecimal(val));
assertThat("Generated wrong BigDecimal RexLiteral",
Copy link
Contributor

Choose a reason for hiding this comment

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

"Generated wrong BigDecimal RexLiteral" does not clarify much. testBigDecimalLiteral clarifies much neither.

Exception message should provide clear (to a developer who never looked into the test method source) indication on why the test failed.

For instance:
assertThat("builder.makeExactLiteral(new BigDecimal(" + val + ")).toString()", ..., is(val))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I agree.
Code modified.


private void assertBigDecimalLiteral(RexBuilder builder, String val) {
final RexNode literal = builder.makeExactLiteral(new BigDecimal(val));
assertThat("builder.makeExactLiteral(new BigDecimal(" + val + ")).toString()",
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, there's .getValueAs(BigDecimal.class) as well.
Would you please add it to the message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

@vlsi
Copy link
Contributor

vlsi commented Nov 20, 2018

LGTM

@zinking
Copy link
Contributor

zinking commented Nov 21, 2018

changes look good, but can you squash the commits into 1 ?

@rubenada
Copy link
Contributor Author

Commits squashed into a single one

@@ -926,12 +926,11 @@ public RexLiteral makeLiteral(boolean b) {
public RexLiteral makeExactLiteral(BigDecimal bd) {
RelDataType relType;
int scale = bd.scale();
long l = bd.unscaledValue().longValue();
assert scale >= 0;
Copy link
Member

Choose a reason for hiding this comment

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

why is this required?

@asfgit asfgit closed this in 0ad68c3 Dec 4, 2018
F21 pushed a commit to F21/calcite that referenced this pull request Jan 3, 2019
…literal larger than 2^63 (Ruben Quesada Lopez)

Close apache#928

For [CALCITE-2701]:
Close apache#935
zhztheplayer pushed a commit to zhztheplayer/calcite that referenced this pull request Jan 12, 2019
…literal larger than 2^63 (Ruben Quesada Lopez)

Close apache#928

For [CALCITE-2701]:
Close apache#935
wangxlong pushed a commit to wangxlong/calcite that referenced this pull request Feb 13, 2020
…literal larger than 2^63 (Ruben Quesada Lopez)

Close apache#928

For [CALCITE-2701]:
Close apache#935
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants