-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
all: inf.dec -> apd.Decimal #13551
all: inf.dec -> apd.Decimal #13551
Conversation
Looks good overall, although I still have an open concern about the default precision in string representations. I did not review the changes to the Reviewed 38 of 42 files at r1, 1 of 1 files at r2. pkg/sql/parser/aggregate_builtins.go, line 308 at r1 (raw file):
Is division by zero the only possible error returned by pkg/sql/parser/aggregate_builtins.go, line 556 at r1 (raw file):
What error can that be? (Please clarify in "TODO" message). pkg/sql/parser/aggregate_builtins.go, line 594 at r1 (raw file):
ditto pkg/sql/parser/aggregate_builtins.go, line 754 at r1 (raw file):
ditto pkg/sql/parser/aggregate_builtins.go, line 765 at r1 (raw file):
ditto pkg/sql/parser/aggregate_builtins.go, line 798 at r1 (raw file):
same as sum above, this value can never be negative. pkg/sql/parser/builtins.go, line 1255 at r1 (raw file):
In the interim, please add a phrase in the pkg/sql/parser/builtins.go, line 1307 at r1 (raw file):
It's the other way around: the other two must be extended to check their argument and return an error when it's invalid. pkg/sql/parser/eval_test.go, line 66 at r1 (raw file):
See my comments about pkg/sql/testdata/aggregate, line 475 at r1 (raw file):
Same question as for pkg/sql/testdata/alias_types, line 59 at r1 (raw file):
I'm not sure about this. Is this the precision we want for pkg/sql/testdata/builtin_function, line 663 at r1 (raw file):
I checked with Oracle and mssql too. Both use half-up rounding. Please add a note to this effect ("it's rather common ...") both here and in the built-in definition for round(). pkg/sql/testdata/window, line 50 at r1 (raw file):
Same question as for Comments from Reviewable |
I like how this is turning out so far! I'm working my way through all the changes and saving the encoding stuff for last. I mirror @knz's concern about the string representation, as well as a few minor issues about our use of APD and the interface it exposes (ie. questions about Reviewed 32 of 42 files at r1. glide.lock, line 407 at r1 (raw file):
Can we remove this yet? pkg/sql/parser/aggregate_builtins.go, line 308 at r1 (raw file): Previously, knz (kena) wrote…
Remove the error or the comment? We should definitely prefer pkg/sql/parser/aggregate_builtins.go, line 556 at r1 (raw file):
pkg/sql/parser/aggregate_builtins.go, line 765 at r1 (raw file):
ditto to panicing for now instead of avoiding the check. pkg/sql/parser/builtins.go, line 45 at r1 (raw file):
move this guy up pkg/sql/parser/builtins.go, line 1240 at r1 (raw file):
How does pkg/sql/parser/builtins.go, line 1336 at r1 (raw file):
Trunc seems like a good addition to the library itself. Also, why not make pkg/sql/parser/datum.go, line 491 at r1 (raw file):
Didn't we add a pkg/sql/parser/datum.go, line 556 at r1 (raw file):
Consider adding pkg/sql/parser/normalize.go, line 689 at r1 (raw file):
Do we still need this? pkg/sql/pgwire/types.go, line 542 at r1 (raw file):
Same point about pkg/sql/pgwire/types_test.go, line 261 at r1 (raw file):
What do these Decimal benchmarks say? pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, knz (kena) wrote…
pkg/util/encoding/decimal_test.go, line 485 at r1 (raw file):
Could you include all of these benchmarks in the commit message? Comments from Reviewable |
Reviewed 10 of 42 files at r1, 1 of 1 files at r2. pkg/sql/testdata/decimal, line 3 at r2 (raw file):
Let's make this comment more timeless. Add some context about how inf.Dec used to be the decimal library we used. pkg/util/encoding/decimal_test.go, line 72 at r1 (raw file):
pkg/util/encoding/encoding_test.go, line 885 at r1 (raw file):
Is this just Comments from Reviewable |
pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
In this case, since the input is just 16 it seems fine to me. It would probably make sense to preserve at least as many decimals as the input. Meaning By the way, preserving the number of decimals doesn't really work across encoding/decoding cycles (#13384). Maybe it's better to just always remove trailing 0s in the interim, as we would at least be consistent. Comments from Reviewable |
Updated with some perf improvements and benchmarks. Looking into why some decoding benchmarks are slower. Review status: 25 of 43 files reviewed at latest revision, 28 unresolved discussions. glide.lock, line 407 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 308 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
All decimal operations can error for a variety of reasons when setting exponents including overflow and underflow errors. Quo can further error when the precision is 0 or too high (guaranteed not to happen here though) and on divide by zero. I've updated the comment to be more general though. pkg/sql/parser/aggregate_builtins.go, line 556 at r1 (raw file): Previously, knz (kena) wrote…
In this case overflow or adding two decimals of ridiculously differing exponents (unlikely but possible), and all of the various set exponent errors alluded above. Documenting each possible error is probably not practical because there are so many kinds that it's easy to miss one and difficult to keep accurate. pkg/sql/parser/builtins.go, line 45 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/parser/builtins.go, line 1240 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Round doesn't care about the exponent, just the number of digits. Quantize puts its value in terms of the exponent of the second argument. pkg/sql/parser/builtins.go, line 1255 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 1307 at r1 (raw file): Previously, knz (kena) wrote…
ln and log perform those checks themselves. The check here is not needed, except to produce a specific error message (apd returns the same error, just with different text). The TODO is there because not all the decimal function display errors in exactly the same way, but they are always handled. pkg/sql/parser/builtins.go, line 1336 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
The ToIntegral method does this, but requires the rounding mode to be always down to get the same results as truncate. The modf method here is just an optimization because we know it's safe. I've added a todo to move this method to apd. pkg/sql/parser/datum.go, line 491 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
There's a SetString on pkg/sql/parser/datum.go, line 556 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Filed an issue for this. pkg/sql/parser/normalize.go, line 689 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Removed. pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
This is tricky because internally apd knows this result is exact, meaning the trailing zeros are not needed, so it drops them. Unclear what to do here, but I'm inclined to leave it. pkg/sql/testdata/builtin_function, line 663 at r1 (raw file): Previously, knz (kena) wrote…
Both oracle and mssql use half-up for decimals and banker for floats? Just trying to make sure I get the docs correct. pkg/sql/testdata/decimal, line 3 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/util/encoding/decimal_test.go, line 72 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Yes, this block has been removed. pkg/util/encoding/encoding_test.go, line 885 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. Comments from Reviewable |
I found the slowdown in those functions and fixed it. Pretty much everything's better now: faster or less allocs. Review status: 25 of 43 files reviewed at latest revision, 28 unresolved discussions. Comments from Reviewable |
Reviewed 18 of 18 files at r3. pkg/sql/parser/aggregate_builtins.go, line 308 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
Please insert a reference to issue #13640 in that TODO thanks. pkg/sql/parser/aggregate_builtins.go, line 556 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
Ok, please refer to issue #13640 here instead then. pkg/sql/parser/builtins.go, line 45 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
I don't see anything changed. pkg/sql/parser/builtins.go, line 1255 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
It's not exactly what I meant. Doc readers are unlikely to understand this because nowhere else in our docs we explain what an "int32" is. I suggest to go for: "If Also that said, now I can see two additional issues popping up:
pkg/sql/parser/builtins.go, line 1307 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
Oh I trust that I mean we can either do one or the other, but they indeed need to be consistent. Your call: either address this now or leave a TODO both here and also in the log functions. pkg/sql/parser/builtins.go, line 1336 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
Mention cockroachdb/apd#24 in the TODO. pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
So the issue I see is not whether Hence the question, is there a mathematician in the room? pkg/sql/testdata/builtin_function, line 663 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
I hadn't checked what they do for floats. Also, I made a mistake; and your comment is not saying the same thing as your code. See below. So I went for a second inspection of the docs:
Both MySQL and Oracle says explicitly that float gets a different treatment. MySQL says explicitly they use the underlying math lib, which happens to do round to nearest even; Oracle says round to nearest even always. I think the case is rather clear: half away from zero for decimal seems to be nearly unanimous. Also for compat with pg we're good to use the math lib for float. pkg/util/encoding/decimal.go, line 622 at r3 (raw file):
How did you choose this constant 10000? Comments from Reviewable |
pkg/util/encoding/decimal.go, line 622 at r3 (raw file): Previously, knz (kena) wrote…
I suggested it in similar code in cockroachdb/apd#20.
Of course for such small numbers one would find this empirically, and multiple steps (e.g. Comments from Reviewable |
pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, knz (kena) wrote…
My vote is to let this in now (with trimming zeros) and we can have a discussion and plan the necessary work in #13384. My motivation is a bit selfish, because I am having trouble with logic tests with DistSQL because depending on how the decimals travel over the wire they can lose trailing 0s. There is no way to make this better with DistSQL until we have an encoding that preserves the 0s. Comments from Reviewable |
Review status: 41 of 43 files reviewed at latest revision, 29 unresolved discussions, all commit checks successful. pkg/sql/parser/aggregate_builtins.go, line 556 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 594 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 754 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 765 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 765 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/parser/aggregate_builtins.go, line 798 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 45 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 1255 at r1 (raw file): Previously, knz (kena) wrote…
pkg/sql/parser/builtins.go, line 1307 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 1336 at r1 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/testdata/alias_types, line 59 at r1 (raw file):
So internal calculations will retain zeros nearly all of the time. (Anything written/read from disk will not have trailing zeros retained, though, because that's how the encoding package works. If we want to change that it'd be in another PR.) @knz is this fine? I think you are correct in general, but sqrt and cbrt are bad examples. pkg/sql/testdata/builtin_function, line 663 at r1 (raw file): Previously, knz (kena) wrote…
"half up" as specified by apd doesn't care about sign, so it's the same as "half away from zero" if you care about sign. I think we are talking about the same thing, I was just using a different definition. I've added docs and comments about this. Comments from Reviewable |
Reviewed 4 of 4 files at r4. pkg/sql/parser/builtins.go, line 1255 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
ok, excellent pkg/sql/parser/builtins.go, line 1241 at r4 (raw file):
While the reference to "Banker's rounding" above is sufficient for a reader to find something online to explain, the sentence here is too poor in details to understand what's really going on. What about: "Rounds pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
I think I'm ok with this for now, but please clarify (and add a test for) what is intended to happen if the result of sqrt/cbrt is not exact. pkg/util/encoding/decimal.go, line 622 at r3 (raw file): Previously, RaduBerinde wrote…
Oh, this is fine. But the same sentence that you wrote above would be nice in a comment in the code here. Comments from Reviewable |
Review status: all files reviewed at latest revision, 22 unresolved discussions, some commit checks failed. pkg/sql/parser/builtins.go, line 1241 at r4 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/testdata/alias_types, line 59 at r1 (raw file): Previously, knz (kena) wrote…
There is such an example in pkg/util/encoding/decimal.go, line 622 at r3 (raw file): Previously, knz (kena) wrote…
Added this as a comment. Comments from Reviewable |
Reviewed 6 of 18 files at r3, 2 of 4 files at r4, 14 of 14 files at r5. pkg/sql/parser/datum.go, line 491 at r1 (raw file): Previously, mjibson (Matt Jibson) wrote…
Why don't we add one to
Comments from Reviewable |
Perhaps you could add "Fixes #13051" to the PR description (and add a test?) |
Done. Review status: 32 of 43 files reviewed at latest revision, 13 unresolved discussions. pkg/sql/parser/datum.go, line 491 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. Comments from Reviewable |
Reviewed 10 of 14 files at r5, 11 of 11 files at r6. pkg/sql/testdata/decimal, line 55 at r6 (raw file):
nit: I usually put the issue number that motivates a regression test in the comment, so that later the person curious about why this is there can check the reason out. Comments from Reviewable |
Reviewed 1 of 4 files at r4, 1 of 11 files at r6. Comments from Reviewable |
Differences: - decimal rounding is now consistent with postgres (note that it is different than float rounding). The util/decimal package has been completely removed. There were some non-inf uses of it to compute 10^X, but those have been hard coded or removed. Fixes #13051 name old time/op new time/op delta EncodeDecimalSmall-12 302ns ± 1% 307ns ± 0% +1.72% (p=0.016 n=5+4) DecodeDecimalSmall-12 400ns ± 1% 400ns ± 0% ~ (p=0.595 n=5+5) EncodeDecimalMedium-12 290ns ± 1% 298ns ± 1% +2.76% (p=0.008 n=5+5) DecodeDecimalMedium-12 392ns ± 0% 398ns ± 1% +1.48% (p=0.008 n=5+5) EncodeDecimalLarge-12 300ns ± 1% 308ns ± 0% +2.47% (p=0.008 n=5+5) DecodeDecimalLarge-12 401ns ± 1% 402ns ± 0% ~ (p=0.587 n=5+5) PeekLengthDecimal-12 35.1ns ± 0% 35.9ns ± 0% +2.28% (p=0.008 n=5+5) NonsortingEncodeDecimal-12 165ns ± 3% 116ns ± 1% -30.02% (p=0.008 n=5+5) NonsortingDecodeDecimal-12 147ns ± 2% 143ns ± 1% -2.72% (p=0.008 n=5+5) EncodeDecimalValue-12 164ns ± 4% 108ns ± 1% -33.98% (p=0.008 n=5+5) DecodeDecimalValue-12 167ns ± 1% 172ns ± 2% +3.12% (p=0.016 n=5+5) WriteTextDecimal-12 508ns ± 0% 517ns ± 5% ~ (p=0.611 n=5+5) WriteBinaryDecimal-12 509ns ± 0% 509ns ± 1% ~ (p=0.778 n=4+5) DecodeBinaryDecimal-12 3.78µs ± 1% 3.82µs ± 1% ~ (p=0.111 n=5+5) name old alloc/op new alloc/op delta EncodeDecimalSmall-12 74.0B ± 0% 74.0B ± 0% ~ (all equal) DecodeDecimalSmall-12 128B ± 0% 128B ± 0% ~ (all equal) EncodeDecimalMedium-12 74.0B ± 0% 74.0B ± 0% ~ (all equal) DecodeDecimalMedium-12 128B ± 0% 128B ± 0% ~ (all equal) EncodeDecimalLarge-12 74.0B ± 0% 74.0B ± 0% ~ (all equal) DecodeDecimalLarge-12 128B ± 0% 128B ± 0% ~ (all equal) PeekLengthDecimal-12 0.00B 0.00B ~ (all equal) NonsortingEncodeDecimal-12 23.4B ± 3% 23.0B ± 0% ~ (p=0.333 n=5+4) NonsortingDecodeDecimal-12 96.0B ± 0% 96.0B ± 0% ~ (all equal) EncodeDecimalValue-12 7.00B ± 0% 7.60B ± 8% ~ (p=0.238 n=4+5) DecodeDecimalValue-12 96.0B ± 0% 96.0B ± 0% ~ (all equal) WriteTextDecimal-12 288B ± 0% 240B ± 0% -16.67% (p=0.008 n=5+5) WriteBinaryDecimal-12 288B ± 0% 240B ± 0% -16.67% (p=0.008 n=5+5) DecodeBinaryDecimal-12 464B ± 0% 464B ± 0% ~ (all equal) name old allocs/op new allocs/op delta EncodeDecimalSmall-12 2.00 ± 0% 2.00 ± 0% ~ (all equal) DecodeDecimalSmall-12 3.00 ± 0% 3.00 ± 0% ~ (all equal) EncodeDecimalMedium-12 2.00 ± 0% 2.00 ± 0% ~ (all equal) DecodeDecimalMedium-12 3.00 ± 0% 3.00 ± 0% ~ (all equal) EncodeDecimalLarge-12 2.00 ± 0% 2.00 ± 0% ~ (all equal) DecodeDecimalLarge-12 3.00 ± 0% 3.00 ± 0% ~ (all equal) PeekLengthDecimal-12 0.00 0.00 ~ (all equal) NonsortingEncodeDecimal-12 0.00 0.00 ~ (all equal) NonsortingDecodeDecimal-12 2.00 ± 0% 2.00 ± 0% ~ (all equal) EncodeDecimalValue-12 0.00 0.00 ~ (all equal) DecodeDecimalValue-12 2.00 ± 0% 2.00 ± 0% ~ (all equal) WriteTextDecimal-12 6.00 ± 0% 5.00 ± 0% -16.67% (p=0.008 n=5+5) WriteBinaryDecimal-12 6.00 ± 0% 5.00 ± 0% -16.67% (p=0.008 n=5+5) DecodeBinaryDecimal-12 21.0 ± 0% 21.0 ± 0% ~ (all equal)
Review status: 42 of 43 files reviewed at latest revision, 10 unresolved discussions. pkg/sql/testdata/decimal, line 55 at r6 (raw file): Previously, knz (kena) wrote…
Done. Comments from Reviewable |
Notes for docs:
this changes/explains the behavior for ROUND. See the discussion around here: https://reviewable.io/reviews/cockroachdb/cockroach/13551#-KcwrN33JECvMtotq8K2:-KdAZqtbFIOrYhpom-Gc:bq7n2cp
There's a table in there which compares what other databases do. May or may not be usable.
we have something uncanny going on for LOG/LN. Consider checking out issue sql: float log and sqrt are inconsistent in their error handling #13642 for details.
This change is