Documentation
¶
Index ¶
- Constants
- Variables
- func AddDate(year, month, day int64, ot gotime.Time) (nt gotime.Time, _ error)
- func CheckFsp(fsp int) (int, error)
- func CheckVectorDimValid(dim int) error
- func DateDiff(startTime, endTime CoreTime) int
- func DecimalAdd(from1, from2, to *MyDecimal) error
- func DecimalBinSize(precision, frac int) (int, error)
- func DecimalDiv(from1, from2, to *MyDecimal, fracIncr int) error
- func DecimalLength2Precision(length int, scale int, hasUnsignedFlag bool) int
- func DecimalMod(from1, from2, to *MyDecimal) error
- func DecimalMul(from1, from2, to *MyDecimal) error
- func DecimalPeak(b []byte) (int, error)
- func DecimalSub(from1, from2, to *MyDecimal) error
- func GetLastDay(year, month int) int
- func GetMaxFloat(flen int, decimal int) float64
- func ParseFrac(s string, fsp int) (v int, overflow bool, err error)
- func PeekBytesAsJSON(b []byte) (n int, err error)
- func PeekBytesAsVectorFloat32(b []byte) (n int, err error)
- func Precision2LengthNoTruncation(length int, scale int, hasUnsignedFlag bool) int
- func Round(f float64, dec int) float64
- func RoundFloat(f float64) float64
- func Truncate(f float64, dec int) float64
- func TruncateFloat(f float64, flen int, decimal int) (float64, error)
- func TruncateFloatToString(f float64, dec int) string
- type BinaryJSON
- func (bj BinaryJSON) ArrayGetElem(idx int) BinaryJSON
- func (bj BinaryJSON) Copy() BinaryJSON
- func (bj BinaryJSON) GetDuration() Duration
- func (bj BinaryJSON) GetElemCount() int
- func (bj BinaryJSON) GetElemDepth() int
- func (bj BinaryJSON) GetFloat64() float64
- func (bj BinaryJSON) GetInt64() int64
- func (bj BinaryJSON) GetKeys() BinaryJSON
- func (bj BinaryJSON) GetOpaque() Opaque
- func (bj BinaryJSON) GetOpaqueFieldType() byte
- func (bj BinaryJSON) GetString() []byte
- func (bj BinaryJSON) GetTime() Timedeprecated
- func (bj BinaryJSON) GetTimeWithFsp(fsp int) Time
- func (bj BinaryJSON) GetUint64() uint64
- func (bj BinaryJSON) GetValue() any
- func (bj BinaryJSON) HashValue(buf []byte) []byte
- func (bj BinaryJSON) MarshalJSON() ([]byte, error)
- func (bj BinaryJSON) String() string
- func (bj *BinaryJSON) UnmarshalJSON(data []byte) error
- type BinaryLiteral
- type CoreTime
- func (t CoreTime) AdjustedGoTime(loc *gotime.Location) (gotime.Time, error)
- func (t CoreTime) Day() int
- func (t CoreTime) GoTime(loc *gotime.Location) (gotime.Time, error)
- func (t CoreTime) Hour() int
- func (t CoreTime) IsLeapYear() bool
- func (t CoreTime) Microsecond() int
- func (t CoreTime) Minute() int
- func (t CoreTime) Month() int
- func (t CoreTime) Second() int
- func (t CoreTime) String() string
- func (t CoreTime) Week(mode int) int
- func (t CoreTime) Weekday() gotime.Weekday
- func (t CoreTime) Year() int
- func (t CoreTime) YearDay() int
- func (t CoreTime) YearWeek(mode int) (year int, week int)
- type Datum
- func (d *Datum) GetBinaryLiteral() BinaryLiteral
- func (d *Datum) GetBytes() []byte
- func (d *Datum) GetFloat32() float32
- func (d *Datum) GetFloat64() float64
- func (d *Datum) GetInt64() int64
- func (d *Datum) GetInterface() any
- func (d *Datum) GetMysqlDecimal() *MyDecimal
- func (d *Datum) GetMysqlDuration() Duration
- func (d *Datum) GetMysqlEnum() Enum
- func (d *Datum) GetMysqlJSON() BinaryJSON
- func (d *Datum) GetMysqlSet() Set
- func (d *Datum) GetMysqlTime() Time
- func (d *Datum) GetString() string
- func (d *Datum) GetUint64() uint64
- func (d *Datum) GetValue() any
- func (d *Datum) GetVectorFloat32() VectorFloat32
- func (d *Datum) Kind() byte
- func (d *Datum) SetBytes(b []byte)
- func (d *Datum) SetFloat64(f float64)
- func (d *Datum) SetFrac(frac int)
- func (d *Datum) SetInt64(i int64)
- func (d *Datum) SetLength(l int)
- func (d *Datum) SetMysqlDecimal(b *MyDecimal)
- func (d *Datum) SetMysqlDuration(b Duration)
- func (d *Datum) SetMysqlJSON(b BinaryJSON)
- func (d *Datum) SetUint64(i uint64)
- func (d *Datum) SetVectorFloat32(vec VectorFloat32)
- func (d *Datum) ToString() (string, error)
- type Duration
- type Enum
- type JSONModifyType
- type JSONTypeCode
- type MyDecimal
- func (d *MyDecimal) Compare(to *MyDecimal) int
- func (d *MyDecimal) FromBin(bin []byte, precision, frac int) (binSize int, err error)
- func (d *MyDecimal) FromFloat64(f float64) error
- func (d *MyDecimal) FromInt(val int64) *MyDecimal
- func (d *MyDecimal) FromString(str []byte) error
- func (d *MyDecimal) FromUint(val uint64) *MyDecimal
- func (d *MyDecimal) GetDigitsFrac() int8
- func (d *MyDecimal) GetDigitsInt() int8
- func (d *MyDecimal) IsNegative() bool
- func (d *MyDecimal) IsZero() bool
- func (d *MyDecimal) MarshalJSON() ([]byte, error)
- func (d *MyDecimal) PrecisionAndFrac() (precision, frac int)
- func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err error)
- func (d *MyDecimal) Shift(shift int) error
- func (d *MyDecimal) String() string
- func (d *MyDecimal) ToBin(precision, frac int) ([]byte, error)
- func (d *MyDecimal) ToFloat64() (f float64, err error)
- func (d *MyDecimal) ToHashKey() ([]byte, error)
- func (d *MyDecimal) ToInt() (int64, error)
- func (d *MyDecimal) ToString() (str []byte)
- func (d *MyDecimal) ToUint() (uint64, error)
- func (d *MyDecimal) UnmarshalJSON(data []byte) error
- func (d *MyDecimal) WriteBin(precision, frac int, buf []byte) ([]byte, error)
- type Opaque
- type RoundMode
- type Set
- type Time
- type VectorFloat32
- func CreateVectorFloat32(vector []float32) (VectorFloat32, error)
- func InitVectorFloat32(dims int) VectorFloat32
- func MustCreateVectorFloat32(v []float32) VectorFloat32
- func ParseVectorFloat32(s string) (VectorFloat32, error)
- func ZeroCopyDeserializeVectorFloat32(b []byte) (VectorFloat32, []byte, error)
- func (v VectorFloat32) CheckDimsFitColumn(expectedFlen int) error
- func (v VectorFloat32) Clone() VectorFloat32
- func (v VectorFloat32) Elements() []float32
- func (v VectorFloat32) EstimatedMemUsage() int
- func (v VectorFloat32) IsZeroValue() bool
- func (v VectorFloat32) Len() int
- func (v VectorFloat32) SerializeTo(b []byte) []byte
- func (v VectorFloat32) SerializedSize() int
- func (v VectorFloat32) String() string
- func (v VectorFloat32) TruncatedString() string
- func (v VectorFloat32) ZeroCopySerialize() []byte
Constants ¶
const ( KindNull byte = 0 KindInt64 byte = 1 KindUint64 byte = 2 KindFloat32 byte = 3 KindFloat64 byte = 4 KindString byte = 5 KindBytes byte = 6 KindBinaryLiteral byte = 7 // Used for BIT / HEX literals. KindMysqlDecimal byte = 8 KindMysqlDuration byte = 9 KindMysqlEnum byte = 10 KindMysqlBit byte = 11 // Used for BIT table column values. KindMysqlSet byte = 12 KindMysqlTime byte = 13 KindInterface byte = 14 KindMinNotNull byte = 15 KindMaxValue byte = 16 KindRaw byte = 17 KindMysqlJSON byte = 18 KindVectorFloat32 byte = 19 )
Kind constants.
const ( DateTimeStr = "datetime" DateStr = "date" TimeStr = "time" TimestampStr = "timestamp" )
const strings for ErrWrongValue
const ( // UnspecifiedFsp is the unspecified fractional seconds part. UnspecifiedFsp = -1 // MaxFsp is the maximum digit of fractional seconds part. MaxFsp = 6 // MinFsp is the minimum digit of fractional seconds part. MinFsp = 0 // DefaultFsp is the default digit of fractional seconds part. // MySQL use 0 as the default Fsp. DefaultFsp = 0 )
const ( // JSONLiteralNil represents JSON null. JSONLiteralNil byte = 0x00 // JSONLiteralTrue represents JSON true. JSONLiteralTrue byte = 0x01 // JSONLiteralFalse represents JSON false. JSONLiteralFalse byte = 0x02 )
const ( // 'all': 1 if all paths exist within the document, 0 otherwise. JSONContainsPathAll = "all" // 'one': 1 if at least one path exists within the document, 0 otherwise. JSONContainsPathOne = "one" )
json_contains_path function type choices See: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-contains-path
const MyDecimalStructSize = 40
MyDecimalStructSize is the struct size of MyDecimal.
const (
UnspecifiedLength = -1
)
UnspecifiedLength is unspecified length.
Variables ¶
var ( // ErrTruncated is returned when data has been truncated during conversion. ErrTruncated = dbterror.ClassTypes.NewStd(mysql.WarnDataTruncated) // ErrOverflow is returned when data is out of range for a field type. ErrOverflow = dbterror.ClassTypes.NewStd(mysql.ErrDataOutOfRange) // ErrDivByZero is return when do division by 0. ErrDivByZero = dbterror.ClassTypes.NewStd(mysql.ErrDivisionByZero) // ErrBadNumber is return when parsing an invalid binary decimal number. ErrBadNumber = dbterror.ClassTypes.NewStd(mysql.ErrBadNumber) // ErrDatetimeFunctionOverflow is returned when the calculation in datetime function cause overflow. ErrDatetimeFunctionOverflow = dbterror.ClassTypes.NewStd(mysql.ErrDatetimeFunctionOverflow) // ErrTruncatedWrongVal is returned when data has been truncated during conversion. ErrTruncatedWrongVal = dbterror.ClassTypes.NewStd(mysql.ErrTruncatedWrongValue) // ErrWrongValue is returned when the input value is in wrong format. ErrWrongValue = dbterror.ClassTypes.NewStdErr(mysql.ErrTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrWrongValue]) )
var ( // ErrJSONDocumentTooDeep means that json's depth is too deep. ErrJSONDocumentTooDeep = dbterror.ClassJSON.NewStd(mysql.ErrJSONDocumentTooDeep) // ErrJSONObjectKeyTooLong means JSON object with key length >= 65536 which is not yet supported. ErrJSONObjectKeyTooLong = dbterror.ClassTypes.NewStdErr(mysql.ErrJSONObjectKeyTooLong, mysql.MySQLErrName[mysql.ErrJSONObjectKeyTooLong]) )
var ZeroCoreTime = CoreTime(0)
ZeroCoreTime is the zero value for TimeInternal type.
var ( // ZeroCoreTime is the zero value for Time type. ZeroTime = Time{} )
Zero values for different types.
var ZeroVectorFloat32 = InitVectorFloat32(0)
ZeroVectorFloat32 is a zero value of VectorFloat32.
Functions ¶
func AddDate ¶
AddDate fix gap between mysql and golang api When we execute select date_add('2018-01-31',interval 1 month) in mysql we got 2018-02-28 but in tidb we got 2018-03-03. Dig it and we found it's caused by golang api time.Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time , it says October 32 converts to November 1 ,it conflicts with mysql. See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add
func CheckVectorDimValid ¶
CheckVectorDimValid checks if the vector's dimension is valid.
func DecimalAdd ¶
DecimalAdd adds two decimals, sets the result to 'to'. Note: DO NOT use `from1` or `from2` as `to` since the metadata of `to` may be changed during evaluating.
func DecimalBinSize ¶
DecimalBinSize returns the size of array to hold a binary representation of a decimal.
func DecimalDiv ¶
DecimalDiv does division of two decimals.
from1 - dividend from2 - divisor to - quotient fracIncr - increment of fraction
func DecimalLength2Precision ¶
DecimalLength2Precision gets the precision.
func DecimalMod ¶
DecimalMod does modulus of two decimals.
from1 - dividend
from2 - divisor
to - modulus
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
NOTES
see do_div_mod()
DESCRIPTION
the modulus R in R = M mod N
is defined as
0 <= |R| < |M|
sign R == sign M
R = M - k*N, where k is integer
thus, there's no requirement for M or N to be integers
func DecimalMul ¶
DecimalMul multiplies two decimals.
from1, from2 - factors
to - product
RETURN VALUE
E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
NOTES
in this implementation, with wordSize=4 we have digitsPerWord=9,
and 63-digit number will take only 7 words (basically a 7-digit
"base 999999999" number). Thus there's no need in fast multiplication
algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
method.
XXX if this library is to be used with huge numbers of thousands of
digits, fast multiplication must be implemented.
func DecimalPeak ¶
DecimalPeak returns the length of the encoded decimal.
func DecimalSub ¶
DecimalSub subs one decimal from another, sets the result to 'to'.
func GetMaxFloat ¶
GetMaxFloat gets the max float for given flen and decimal.
func ParseFrac ¶
ParseFrac parses the input string according to fsp, returns the microsecond, and also a bool value to indice overflow. eg: "999" fsp=2 will overflow.
func PeekBytesAsJSON ¶
PeekBytesAsJSON trys to peek some bytes from b, until we can deserialize a JSON from those bytes.
func PeekBytesAsVectorFloat32 ¶
PeekBytesAsVectorFloat32 trys to peek some bytes from b, until we can deserialize a VectorFloat32 from those bytes.
func Precision2LengthNoTruncation ¶
Precision2LengthNoTruncation gets the length.
func Round ¶
Round rounds the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero.
func RoundFloat ¶
RoundFloat rounds float val to the nearest even integer value with float64 format, like MySQL Round function. RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html so rounding use "round to nearest even". e.g, 1.5 -> 2, -1.5 -> -2.
func Truncate ¶
Truncate truncates the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero.
func TruncateFloat ¶
TruncateFloat tries to truncate f. If the result exceeds the max/min float that flen/decimal allowed, returns the max/min float allowed.
func TruncateFloatToString ¶
TruncateFloatToString is used to truncate float to string where dec is the number of digits after the decimal point.
Types ¶
type BinaryJSON ¶
type BinaryJSON struct {
TypeCode JSONTypeCode
Value []byte
}
BinaryJSON represents a binary encoded JSON object. It can be randomly accessed without deserialization.
func CreateBinaryJSON ¶
func CreateBinaryJSON(in any) BinaryJSON
CreateBinaryJSON creates a BinaryJSON from interface.
func CreateBinaryJSONWithCheck ¶
func CreateBinaryJSONWithCheck(in any) (BinaryJSON, error)
CreateBinaryJSONWithCheck creates a BinaryJSON from interface with error check.
func (BinaryJSON) ArrayGetElem ¶
func (bj BinaryJSON) ArrayGetElem(idx int) BinaryJSON
ArrayGetElem gets the element of the index `idx`.
func (BinaryJSON) GetDuration ¶
func (bj BinaryJSON) GetDuration() Duration
GetDuration gets the duration value
func (BinaryJSON) GetElemCount ¶
func (bj BinaryJSON) GetElemCount() int
GetElemCount gets the count of Object or Array.
func (BinaryJSON) GetElemDepth ¶
func (bj BinaryJSON) GetElemDepth() int
GetElemDepth for JSON_DEPTH Returns the maximum depth of a JSON document rules referenced by MySQL JSON_DEPTH function [https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html#function_json-depth] 1) An empty array, empty object, or scalar value has depth 1. 2) A nonempty array containing only elements of depth 1 or nonempty object containing only member values of depth 1 has depth 2. 3) Otherwise, a JSON document has depth greater than 2. e.g. depth of '{}', '[]', 'true': 1 e.g. depth of '[10, 20]', '[[], {}]': 2 e.g. depth of '[10, {"a": 20}]': 3
func (BinaryJSON) GetFloat64 ¶
func (bj BinaryJSON) GetFloat64() float64
GetFloat64 gets the float64 value.
func (BinaryJSON) GetKeys ¶
func (bj BinaryJSON) GetKeys() BinaryJSON
GetKeys gets the keys of the object
func (BinaryJSON) GetOpaque ¶
func (bj BinaryJSON) GetOpaque() Opaque
GetOpaque gets the opaque value
func (BinaryJSON) GetOpaqueFieldType ¶
func (bj BinaryJSON) GetOpaqueFieldType() byte
GetOpaqueFieldType returns the type of opaque value
func (BinaryJSON) GetString ¶
func (bj BinaryJSON) GetString() []byte
GetString gets the string value.
func (BinaryJSON) GetTime
deprecated
func (bj BinaryJSON) GetTime() Time
GetTime gets the time value with default fsp
Deprecated: use GetTimeWithFsp instead. The `BinaryJSON` doesn't contain the fsp information, so the caller should always provide the fsp.
func (BinaryJSON) GetTimeWithFsp ¶
func (bj BinaryJSON) GetTimeWithFsp(fsp int) Time
GetTimeWithFsp gets the time value with given fsp
func (BinaryJSON) GetUint64 ¶
func (bj BinaryJSON) GetUint64() uint64
GetUint64 gets the uint64 value.
func (BinaryJSON) GetValue ¶
func (bj BinaryJSON) GetValue() any
GetValue return the primitive value of the JSON.
func (BinaryJSON) HashValue ¶
func (bj BinaryJSON) HashValue(buf []byte) []byte
HashValue converts certain JSON values for aggregate comparisons. For example int64(3) == float64(3.0) Other than the numeric condition, this function has to construct a bidirectional map between hash value and the original representation
func (BinaryJSON) MarshalJSON ¶
func (bj BinaryJSON) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (BinaryJSON) String ¶
func (bj BinaryJSON) String() string
String implements fmt.Stringer interface.
func (*BinaryJSON) UnmarshalJSON ¶
func (bj *BinaryJSON) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
type BinaryLiteral ¶
type BinaryLiteral []byte
BinaryLiteral is the internal type for storing bit / hex literal type.
func (BinaryLiteral) ToString ¶
func (b BinaryLiteral) ToString() string
ToString returns the string representation for the literal.
type CoreTime ¶
type CoreTime uint64
CoreTime is the internal struct type for Time.
func (CoreTime) AdjustedGoTime ¶
AdjustedGoTime converts Time to GoTime and adjust for invalid DST times like during the DST change with increased offset, normally moving to Daylight Saving Time. see https://github.com/pingcap/tidb/issues/28739
func (CoreTime) IsLeapYear ¶
IsLeapYear returns if it's leap year.
func (CoreTime) Microsecond ¶
Microsecond returns the microsecond value.
type Datum ¶
type Datum struct {
// contains filtered or unexported fields
}
Datum is a data box holds different kind of data. It has better performance and is easier to use than `interface{}`.
func (*Datum) GetBinaryLiteral ¶
func (d *Datum) GetBinaryLiteral() BinaryLiteral
GetBinaryLiteral gets Bit value
func (*Datum) GetMysqlDecimal ¶
GetMysqlDecimal gets decimal value
func (*Datum) GetMysqlDuration ¶
GetMysqlDuration gets Duration value
func (*Datum) GetMysqlJSON ¶
func (d *Datum) GetMysqlJSON() BinaryJSON
GetMysqlJSON gets json.BinaryJSON value
func (*Datum) GetVectorFloat32 ¶
func (d *Datum) GetVectorFloat32() VectorFloat32
GetVectorFloat32 gets VectorFloat32 value
func (*Datum) SetMysqlDecimal ¶
SetMysqlDecimal sets decimal value
func (*Datum) SetMysqlDuration ¶
SetMysqlDuration sets Duration value
func (*Datum) SetMysqlJSON ¶
func (d *Datum) SetMysqlJSON(b BinaryJSON)
SetMysqlJSON sets json.BinaryJSON value
func (*Datum) SetVectorFloat32 ¶
func (d *Datum) SetVectorFloat32(vec VectorFloat32)
SetVectorFloat32 sets VectorFloat32 value
type Duration ¶
type Duration struct {
gotime.Duration
// Fsp is short for Fractional Seconds Precision.
// See http://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html
Fsp int
}
Duration is the type for MySQL TIME type.
type JSONModifyType ¶
type JSONModifyType byte
JSONModifyType is for modify a JSON. There are three valid values: JSONModifyInsert, JSONModifyReplace and JSONModifySet.
const ( // JSONModifyInsert is for insert a new element into a JSON. // If an old elemList exists, it would NOT replace it. JSONModifyInsert JSONModifyType = 0x01 // JSONModifyReplace is for replace an old elemList from a JSON. // If no elemList exists, it would NOT insert it. JSONModifyReplace JSONModifyType = 0x02 // JSONModifySet = JSONModifyInsert | JSONModifyReplace JSONModifySet JSONModifyType = 0x03 )
type JSONTypeCode ¶
type JSONTypeCode = byte
JSONTypeCode indicates JSON type.
const ( // JSONTypeCodeObject indicates the JSON is an object. JSONTypeCodeObject JSONTypeCode = 0x01 // JSONTypeCodeArray indicates the JSON is an array. JSONTypeCodeArray JSONTypeCode = 0x03 // JSONTypeCodeLiteral indicates the JSON is a literal. JSONTypeCodeLiteral JSONTypeCode = 0x04 // JSONTypeCodeInt64 indicates the JSON is a signed integer. JSONTypeCodeInt64 JSONTypeCode = 0x09 // JSONTypeCodeUint64 indicates the JSON is a unsigned integer. JSONTypeCodeUint64 JSONTypeCode = 0x0a // JSONTypeCodeFloat64 indicates the JSON is a double float number. JSONTypeCodeFloat64 JSONTypeCode = 0x0b // JSONTypeCodeString indicates the JSON is a string. JSONTypeCodeString JSONTypeCode = 0x0c // JSONTypeCodeOpaque indicates the JSON is a opaque JSONTypeCodeOpaque JSONTypeCode = 0x0d // JSONTypeCodeDate indicates the JSON is a opaque JSONTypeCodeDate JSONTypeCode = 0x0e // JSONTypeCodeDatetime indicates the JSON is a opaque JSONTypeCodeDatetime JSONTypeCode = 0x0f // JSONTypeCodeTimestamp indicates the JSON is a opaque JSONTypeCodeTimestamp JSONTypeCode = 0x10 // JSONTypeCodeDuration indicates the JSON is a opaque JSONTypeCodeDuration JSONTypeCode = 0x11 )
type MyDecimal ¶
type MyDecimal struct {
// contains filtered or unexported fields
}
MyDecimal represents a decimal value.
func NewDecFromFloatForTest ¶
NewDecFromFloatForTest creates a MyDecimal from float, as it returns no error, it should only be used in test.
func NewDecFromInt ¶
NewDecFromInt creates a MyDecimal from int.
func NewDecFromStringForTest ¶
NewDecFromStringForTest creates a MyDecimal from string, as it returns no error, it should only be used in test.
func NewDecFromUint ¶
NewDecFromUint creates a MyDecimal from uint.
func NewMaxOrMinDec ¶
NewMaxOrMinDec returns the max or min value decimal for given precision and fraction.
func (*MyDecimal) FromFloat64 ¶
FromFloat64 creates a decimal from float64 value.
func (*MyDecimal) FromString ¶
FromString parses decimal from string.
func (*MyDecimal) GetDigitsFrac ¶
GetDigitsFrac returns the digitsFrac.
func (*MyDecimal) GetDigitsInt ¶
GetDigitsInt returns the digitsInt.
func (*MyDecimal) IsNegative ¶
IsNegative returns whether a decimal is negative.
func (*MyDecimal) MarshalJSON ¶
MarshalJSON implements Marshaler.MarshalJSON interface.
func (*MyDecimal) PrecisionAndFrac ¶
PrecisionAndFrac returns the internal precision and frac number.
func (*MyDecimal) Round ¶
Round rounds the decimal to "frac" digits.
to - result buffer. d == to is allowed frac - to what position after fraction point to round. can be negative! roundMode - round to nearest even or truncate ModeHalfUp rounds normally. ModeTruncate just truncates the decimal.
NOTES
frac can be negative ! one TRUNCATED error (line XXX below) isn't treated very logical :(
RETURN VALUE
nil/ErrTruncated/ErrOverflow
func (*MyDecimal) Shift ¶
Shift shifts decimal digits in given number (with rounding if it need), shift > 0 means shift to left shift, shift < 0 means right shift. In fact it is multiplying on 10^shift.
RETURN
eDecOK OK eDecOverflow operation lead to overflow, number is untoched eDecTruncated number was rounded to fit into buffer
func (*MyDecimal) ToBin ¶
ToBin converts decimal to its binary fixed-length representation two representations of the same length can be compared with memcmp with the correct -1/0/+1 result
PARAMS
precision/frac - if precision is 0, internal value of the decimal will be used,
then the encoded value is not memory comparable.
NOTE
the buffer is assumed to be of the size DecimalBinSize(precision, frac)
RETURN VALUE
bin - binary value
errCode - eDecOK/eDecTruncate/eDecOverflow
DESCRIPTION
for storage decimal numbers are converted to the "binary" format.
This format has the following properties:
1. length of the binary representation depends on the {precision, frac}
as provided by the caller and NOT on the digitsInt/digitsFrac of the decimal to
convert.
2. binary representations of the same {precision, frac} can be compared
with memcmp - with the same result as DecimalCompare() of the original
decimals (not taking into account possible precision loss during
conversion).
This binary format is as follows:
1. First the number is converted to have a requested precision and frac.
2. Every full digitsPerWord digits of digitsInt part are stored in 4 bytes
as is
3. The first digitsInt % digitesPerWord digits are stored in the reduced
number of bytes (enough bytes to store this number of digits -
see dig2bytes)
4. same for frac - full word are stored as is,
the last frac % digitsPerWord digits - in the reduced number of bytes.
5. If the number is negative - every byte is inversed.
5. The very first bit of the resulting byte array is inverted (because
memcmp compares unsigned bytes, see property 2 above)
Example:
1234567890.1234
internally is represented as 3 words
1 234567890 123400000
(assuming we want a binary representation with precision=14, frac=4)
in hex it's
00-00-00-01 0D-FB-38-D2 07-5A-EF-40
now, middle word is full - it stores 9 decimal digits. It goes
into binary representation as is:
........... 0D-FB-38-D2 ............
First word has only one decimal digit. We can store one digit in
one byte, no need to waste four:
01 0D-FB-38-D2 ............
now, last word. It's 123400000. We can store 1234 in two bytes:
01 0D-FB-38-D2 04-D2
So, we've packed 12 bytes number in 7 bytes.
And now we invert the highest bit to get the final result:
81 0D FB 38 D2 04 D2
And for -1234567890.1234 it would be
7E F2 04 C7 2D FB 2D
func (*MyDecimal) ToHashKey ¶
ToHashKey removes the leading and trailing zeros and generates a hash key. Two Decimals dec0 and dec1 with different fraction will generate the same hash keys if dec0.Compare(dec1) == 0.
func (*MyDecimal) ToString ¶
ToString converts decimal to its printable string representation without rounding.
RETURN VALUE
str - result string
errCode - eDecOK/eDecTruncate/eDecOverflow
func (*MyDecimal) UnmarshalJSON ¶
UnmarshalJSON implements Unmarshaler.UnmarshalJSON interface.
type Opaque ¶
type Opaque struct {
// TypeCode is the same with database type code
TypeCode byte
// Buf is the underlying bytes of the data
Buf []byte
}
Opaque represents a raw binary type
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
Time is the struct for handling datetime, timestamp and date.
type VectorFloat32 ¶
type VectorFloat32 struct {
// contains filtered or unexported fields
}
VectorFloat32 represents a vector of float32.
Memory Format: 4 byte - Length 4 byte * N - Data in Float32
Normally, the data layout in storage (i.e. after serialization) is identical to the memory layout. However, in BigEndian machines, we have BigEndian in memory and always have LittleEndian in storage or during data exchange.
func CreateVectorFloat32 ¶
func CreateVectorFloat32(vector []float32) (VectorFloat32, error)
CreateVectorFloat32 creates a VectorFloat32. Returns error if there are invalid values like NaN and Inf.
func InitVectorFloat32 ¶
func InitVectorFloat32(dims int) VectorFloat32
InitVectorFloat32 initializes a vector with the given dimension. The values are initialized to zero.
func MustCreateVectorFloat32 ¶
func MustCreateVectorFloat32(v []float32) VectorFloat32
MustCreateVectorFloat32 creates a VectorFloat32. Panics if there are invalid values like NaN and Inf.
func ParseVectorFloat32 ¶
func ParseVectorFloat32(s string) (VectorFloat32, error)
ParseVectorFloat32 parses a string into a vector.
func ZeroCopyDeserializeVectorFloat32 ¶
func ZeroCopyDeserializeVectorFloat32(b []byte) (VectorFloat32, []byte, error)
ZeroCopyDeserializeVectorFloat32 deserializes the byte slice into a vector, without memory copy. Note: b must not be mutated, because this function does zero copy.
func (VectorFloat32) CheckDimsFitColumn ¶
func (v VectorFloat32) CheckDimsFitColumn(expectedFlen int) error
CheckDimsFitColumn checks if the vector has the expected dimension, which is defined by the column type or cast type.
func (VectorFloat32) Clone ¶
func (v VectorFloat32) Clone() VectorFloat32
Clone returns a deep copy of the vector.
func (VectorFloat32) Elements ¶
func (v VectorFloat32) Elements() []float32
Elements returns a mutable typed slice of the elements.
func (VectorFloat32) EstimatedMemUsage ¶
func (v VectorFloat32) EstimatedMemUsage() int
EstimatedMemUsage returns the estimated memory usage.
func (VectorFloat32) IsZeroValue ¶
func (v VectorFloat32) IsZeroValue() bool
IsZeroValue returns true if the vector is a zero value (which length is zero).
func (VectorFloat32) Len ¶
func (v VectorFloat32) Len() int
Len returns the length (dimension) of the vector.
func (VectorFloat32) SerializeTo ¶
func (v VectorFloat32) SerializeTo(b []byte) []byte
SerializeTo serializes the vector into the byte slice.
func (VectorFloat32) SerializedSize ¶
func (v VectorFloat32) SerializedSize() int
SerializedSize returns the size of the serialized data.
func (VectorFloat32) String ¶
func (v VectorFloat32) String() string
String returns a string representation of the vector, which can be parsed later.
func (VectorFloat32) TruncatedString ¶
func (v VectorFloat32) TruncatedString() string
TruncatedString prints the vector in a truncated form, which is useful for outputting in logs or EXPLAIN statements.
func (VectorFloat32) ZeroCopySerialize ¶
func (v VectorFloat32) ZeroCopySerialize() []byte
ZeroCopySerialize serializes the vector into a new byte slice, without memory copy.