An exact decimal is a ZuDecimal - #31
Merged
Merged
Conversation
The engine grew a decimal type: an integer of unscaled units and the
scale that says how large a unit is. This client had no way to receive
one and no way to send one, so a CAST to DECIMAL came back as something
else or not at all.
A class, because JavaScript has no exact number to be. A number is an
IEEE double and a tenth is not a binary fraction, so 0.1 + 0.2 is not
0.3 and a price read into one is not the price. A bigint is exact and
whole, which is half of what a decimal is. That is the same decision
ZuDate is, for the same reason: the runtime has no type for the value,
and inventing one that loses it would be worse than naming it.
The scale rides on the value rather than only on the column, because
CAST('1.20' AS DECIMAL(5, 2)) in a RETURN has no column to ask and
still has two places. ZuDecimal.parse reads the scale out of the text
it was given, so 1.20 and 1.2 are the same number written with
different care about how well it is known, and each prints back the way
it was written. ZuDecimal.of takes the pair for a caller who has one.
Both factories refuse what the carrier cannot hold, and they say which
limit was reached: 38 digits is the widest decimal here and the widest
DECIMAL(p, s) that may be declared. A NaN and an infinity are refused
at the call rather than turned into anything, because an exact number
is what a decimal is. The parse checks the width itself: the engine's
own parse takes a scale and does not compare the result against the
maximum, and 39 ones fit an i128.
Nothing on the way in is re-checked. Every ZuDecimal was built by a
factory that refused what a decimal cannot hold, or handed back by the
engine, so the two numbers read out of one are two this engine already
holds.
The column side needed nothing: a decimal column is a complex column
and rides the per-value path, and zu-arrow already writes Decimal128.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The engine grew a decimal type at
e617431: an integer of unscaled units and the scale that says how large a unit is. This client had no way to receive one and no way to send one, so aCASTtoDECIMALcame back as something else or not at all. The pin moves tocfdc70fand the type arrives with it.The shape
A class, because JavaScript has no exact number to be. A
numberis an IEEE double and a tenth is not a binary fraction, so0.1 + 0.2is not0.3and a price read into one is not the price. Abigintis exact and whole, which is half of what a decimal is. That is the same decisionZuDateis, for the same reason: the runtime has no type for the value, and inventing one that loses it would be worse than naming it.The scale rides on the value rather than only on the column it came from, because
CAST('1.20' AS DECIMAL(5, 2))in aRETURNhas no column to ask and still has two places.ZuDecimal.parsereads the scale out of the text it was given, so1.20and1.2are the same number written with different care about how well it is known, and each prints back the way it was written.ZuDecimal.of(120n, 2)takes the pair for a caller who already has one.toString()is the lossless spelling and is whatJSON.stringifywrites.toNumber()is the conversion offered rather than done.What is refused
Both factories refuse what the carrier cannot hold, and they say which limit was reached. 38 digits is the widest decimal here and the widest
DECIMAL(p, s)that may be declared. A NaN, an infinity and anything else that is not a number are refused at the call rather than turned into something, because an exact number is what a decimal is.The parse checks the width itself rather than trusting the engine's.
Decimal::parsetakes a scale and does not compare the result againstMAX_DIGITS, and 39 ones fit inside ani128, so a 39 digit decimal would otherwise have gone through. Filed against the engine separately.Nothing on the way in is re-checked. Every
ZuDecimalwas built by a factory that refused what a decimal cannot hold, or was handed back by the engine, so the two numbers read out of one are two this engine already holds.What needed nothing
The column side. A decimal column is a complex column and rides the per-value path, so the
kind()fallback already names it, andzu-arrowalready writesDecimal128.ZuAppendValueandZuFrameValueare untouched, since there is no decimal column type to append to.Checked
Run on a server rather than here.
npm run build:debug, thenbinding.d.ctscompared against whatnapi buildgenerated: identical, byte for bytenode --test test/values.test.mjs test/exports.test.mjs: 26 tests, 26 pass, 11 of them newnpm run check:types,npm run check:api,npm run check:package: green, withetc/zudb.api.mdupdated and committednode --test test/conformance*.test.mjs: 87 tests, 87 passOne test failed on that server and is not from this change:
progress.test.mjsasserts that a watch interval longer than the statement never fires, and the machine was loaded enough that the statement took 299 seconds.