aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/ast/types.go
blob: 9bb48bc652fca7f1dccd9fb5b676ea34d401452d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package ast

import (
    "database/sql"
    "fmt"
    "math"
    "math/big"

    "github.com/dexon-foundation/decimal"

    "github.com/dexon-foundation/dexon/common"
    dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal"
    se "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
)

var (
    bigIntOne = big.NewInt(1)
)

// BoolValue represents a boolean value used by SQL three-valued logic.
type BoolValue uint8

// Define valid values for SQL boolean type. The zero value is invalid.
const (
    BoolValueTrue    BoolValue = 1
    BoolValueFalse   BoolValue = 2
    BoolValueUnknown BoolValue = 3
)

// DataTypeMajor defines type for high byte of DataType.
type DataTypeMajor uint8

// DataTypeMinor defines type for low byte of DataType.
type DataTypeMinor uint8

// DataType defines type for data type encoded.
type DataType uint16

// DataTypeMajor enums.
const (
    DataTypeMajorPending DataTypeMajor = iota
    DataTypeMajorSpecial
    DataTypeMajorBool
    DataTypeMajorAddress
    DataTypeMajorInt
    DataTypeMajorUint
    DataTypeMajorFixedBytes
    DataTypeMajorDynamicBytes
    DataTypeMajorFixed  DataTypeMajor = 0x10
    DataTypeMajorUfixed DataTypeMajor = 0x30
)

// DataTypeMinor enums.
const (
    DataTypeMinorDontCare       DataTypeMinor = 0x00
    DataTypeMinorSpecialNull    DataTypeMinor = 0x00
    DataTypeMinorSpecialAny     DataTypeMinor = 0x01
    DataTypeMinorSpecialDefault DataTypeMinor = 0x02
)

// Special data types which are commonly used.
const (
    DataTypePending DataType = (DataType(DataTypeMajorPending) << 8) | DataType(DataTypeMinorDontCare)
    DataTypeBad     DataType = math.MaxUint16
)

// Valid returns whether a BoolValue is valid.
func (v BoolValue) Valid() bool {
    return v-1 < 3
}

var boolValueStringMap = [3]string{
    BoolValueTrue - 1:    "TRUE",
    BoolValueFalse - 1:   "FALSE",
    BoolValueUnknown - 1: "UNKNOWN",
}

// String returns a string for printing a BoolValue.
func (v BoolValue) String() string {
    return boolValueStringMap[v-1]
}

var boolValueNullBoolMap = [3]sql.NullBool{
    BoolValueTrue - 1:    {Valid: true, Bool: true},
    BoolValueFalse - 1:   {Valid: true, Bool: false},
    BoolValueUnknown - 1: {Valid: false, Bool: false},
}

// NullBool converts a BoolValue to a sql.NullBool.
func (v BoolValue) NullBool() sql.NullBool {
    return boolValueNullBoolMap[v-1]
}

var boolValueAndTruthTable = [3][3]BoolValue{
    BoolValueTrue - 1: {
        BoolValueTrue - 1:    BoolValueTrue,
        BoolValueFalse - 1:   BoolValueFalse,
        BoolValueUnknown - 1: BoolValueUnknown,
    },
    BoolValueFalse - 1: {
        BoolValueTrue - 1:    BoolValueFalse,
        BoolValueFalse - 1:   BoolValueFalse,
        BoolValueUnknown - 1: BoolValueFalse,
    },
    BoolValueUnknown - 1: {
        BoolValueTrue - 1:    BoolValueUnknown,
        BoolValueFalse - 1:   BoolValueFalse,
        BoolValueUnknown - 1: BoolValueUnknown,
    },
}

// And returns v AND v2.
func (v BoolValue) And(v2 BoolValue) BoolValue {
    return boolValueAndTruthTable[v-1][v2-1]
}

var boolValueOrTruthTable = [3][3]BoolValue{
    BoolValueTrue - 1: {
        BoolValueTrue - 1:    BoolValueTrue,
        BoolValueFalse - 1:   BoolValueTrue,
        BoolValueUnknown - 1: BoolValueTrue,
    },
    BoolValueFalse - 1: {
        BoolValueTrue - 1:    BoolValueTrue,
        BoolValueFalse - 1:   BoolValueFalse,
        BoolValueUnknown - 1: BoolValueUnknown,
    },
    BoolValueUnknown - 1: {
        BoolValueTrue - 1:    BoolValueTrue,
        BoolValueFalse - 1:   BoolValueUnknown,
        BoolValueUnknown - 1: BoolValueUnknown,
    },
}

// Or returns v OR v2.
func (v BoolValue) Or(v2 BoolValue) BoolValue {
    return boolValueOrTruthTable[v-1][v2-1]
}

var boolValueNotTruthTable = [3]BoolValue{
    BoolValueTrue - 1:    BoolValueFalse,
    BoolValueFalse - 1:   BoolValueTrue,
    BoolValueUnknown - 1: BoolValueUnknown,
}

// Not returns NOT v.
func (v BoolValue) Not() BoolValue {
    return boolValueNotTruthTable[v-1]
}

// DecomposeDataType to major and minor part with given data type.
func DecomposeDataType(t DataType) (DataTypeMajor, DataTypeMinor) {
    return DataTypeMajor(t >> 8), DataTypeMinor(t & 0xff)
}

// ComposeDataType to concrete type with major and minor part.
func ComposeDataType(major DataTypeMajor, minor DataTypeMinor) DataType {
    return (DataType(major) << 8) | DataType(minor)
}

// IsFixedRange checks if major is in range of DataTypeMajorFixed.
func (d DataTypeMajor) IsFixedRange() bool {
    return d >= DataTypeMajorFixed && d-DataTypeMajorFixed <= 0x1f
}

// IsUfixedRange checks if major is in range of DataTypeMajorUfixed.
func (d DataTypeMajor) IsUfixedRange() bool {
    return d >= DataTypeMajorUfixed && d-DataTypeMajorUfixed <= 0x1f
}

// Size return the bytes of the data type occupied.
func (dt DataType) Size() uint8 {
    major, minor := DecomposeDataType(dt)
    if major.IsFixedRange() {
        return uint8(major - DataTypeMajorFixed + 1)
    }
    if major.IsUfixedRange() {
        return uint8(major - DataTypeMajorUfixed + 1)
    }
    switch major {
    case DataTypeMajorBool:
        return 1
    case DataTypeMajorDynamicBytes:
        return common.HashLength
    case DataTypeMajorAddress:
        return common.AddressLength
    case DataTypeMajorInt, DataTypeMajorUint, DataTypeMajorFixedBytes:
        return uint8(minor + 1)
    default:
        panic(fmt.Sprintf("unknown data type %v", dt))
    }
}

// GetNode constructs an AST node from a data type.
func (dt DataType) GetNode() TypeNode {
    major, minor := DecomposeDataType(dt)
    switch major {
    case DataTypeMajorBool:
        return &BoolTypeNode{}
    case DataTypeMajorAddress:
        return &AddressTypeNode{}
    case DataTypeMajorInt:
        if minor <= 0x1f {
            size := (uint32(minor) + 1) * 8
            return &IntTypeNode{Unsigned: false, Size: size}
        }
    case DataTypeMajorUint:
        if minor <= 0x1f {
            size := (uint32(minor) + 1) * 8
            return &IntTypeNode{Unsigned: true, Size: size}
        }
    case DataTypeMajorFixedBytes:
        if minor <= 0x1f {
            size := uint32(minor) + 1
            return &FixedBytesTypeNode{Size: size}
        }
    case DataTypeMajorDynamicBytes:
        return &DynamicBytesTypeNode{}
    }
    switch {
    case major.IsFixedRange():
        if minor <= 80 {
            size := (uint32(major-DataTypeMajorFixed) + 1) * 8
            fractionalDigits := uint32(minor)
            return &FixedTypeNode{
                Unsigned:         false,
                Size:             size,
                FractionalDigits: fractionalDigits,
            }
        }
    case major.IsUfixedRange():
        if minor <= 80 {
            size := (uint32(major-DataTypeMajorUfixed) + 1) * 8
            fractionalDigits := uint32(minor)
            return &FixedTypeNode{
                Unsigned:         true,
                Size:             size,
                FractionalDigits: fractionalDigits,
            }
        }
    }
    return nil
}

type decimalMinMaxPair struct {
    Min, Max decimal.Decimal
}

var decimalMinMaxMap = func() map[DataType]decimalMinMaxPair {
    m := make(map[DataType]decimalMinMaxPair)

    // bool
    {
        dt := ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
        m[dt] = decimalMinMaxPair{Min: dec.False, Max: dec.True}
    }

    // address
    {
        dt := ComposeDataType(DataTypeMajorAddress, DataTypeMinorDontCare)
        bigMax := new(big.Int)
        bigMax.Lsh(bigIntOne, common.AddressLength*8)
        bigMax.Sub(bigMax, bigIntOne)
        max := decimal.NewFromBigInt(bigMax, 0)
        m[dt] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
    }

    // uint, bytes{X}
    for i := uint(0); i <= 0x1f; i++ {
        dtUint := ComposeDataType(DataTypeMajorUint, DataTypeMinor(i))
        dtBytes := ComposeDataType(DataTypeMajorFixedBytes, DataTypeMinor(i))
        bigMax := new(big.Int)
        bigMax.Lsh(bigIntOne, (i+1)*8)
        bigMax.Sub(bigMax, bigIntOne)
        max := decimal.NewFromBigInt(bigMax, 0)
        m[dtUint] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
        m[dtBytes] = decimalMinMaxPair{Min: decimal.Zero, Max: max}
    }

    // int
    for i := uint(0); i <= 0x1f; i++ {
        dt := ComposeDataType(DataTypeMajorInt, DataTypeMinor(i))
        bigMax := new(big.Int)
        bigMax.Lsh(bigIntOne, (i+1)*8-1)
        bigMin := new(big.Int)
        bigMin.Neg(bigMax)
        bigMax.Sub(bigMax, bigIntOne)
        min := decimal.NewFromBigInt(bigMin, 0)
        max := decimal.NewFromBigInt(bigMax, 0)
        m[dt] = decimalMinMaxPair{Min: min, Max: max}
    }

    return m
}()

// GetMinMax returns min, max pair according to given data type.
func (dt DataType) GetMinMax() (decimal.Decimal, decimal.Decimal, bool) {
    var (
        pair decimalMinMaxPair
        ok   bool
    )
    pair, ok = decimalMinMaxMap[dt]
    return pair.Min, pair.Max, ok
}

func decimalToBig(d decimal.Decimal) *big.Int {
    d = d.Rescale(0)
    return d.Coefficient()
}

// Don't handle overflow here.
func decimalEncode(size int, d decimal.Decimal) []byte {
    ret := make([]byte, size)
    s := d.Sign()
    if s == 0 {
        return ret
    }

    b := decimalToBig(d)

    if s > 0 {
        bs := b.Bytes()
        copy(ret[size-len(bs):], bs)
        return ret
    }

    b.Add(b, bigIntOne)
    bs := b.Bytes()
    copy(ret[size-len(bs):], bs)
    for idx := range ret {
        ret[idx] = ^ret[idx]
    }
    return ret
}

// Don't handle overflow here.
func decimalDecode(signed bool, bs []byte) decimal.Decimal {
    neg := false
    if signed && (bs[0]&0x80 != 0) {
        newbs := make([]byte, 0, len(bs))
        bs = append(newbs, bs...)
        neg = true
        for idx := range bs {
            bs[idx] = ^bs[idx]
        }
    }

    b := new(big.Int).SetBytes(bs)

    if neg {
        b.Add(b, bigIntOne)
        b.Neg(b)
    }

    return decimal.NewFromBigInt(b, 0)
}

// DecimalEncode encodes decimal to bytes depend on data type.
func DecimalEncode(dt DataType, d decimal.Decimal) ([]byte, error) {
    major, minor := DecomposeDataType(dt)
    switch major {
    case DataTypeMajorInt,
        DataTypeMajorUint:
        return decimalEncode(int(minor)+1, d), nil
    }
    switch {
    case major.IsFixedRange():
        return decimalEncode(
            int(major-DataTypeMajorFixed)+1,
            d.Shift(int32(minor))), nil
    case major.IsUfixedRange():
        return decimalEncode(
            int(major-DataTypeMajorUfixed)+1,
            d.Shift(int32(minor))), nil
    }

    return nil, se.ErrorCodeDecimalEncode
}

// DecimalDecode decodes decimal from bytes.
func DecimalDecode(dt DataType, b []byte) (decimal.Decimal, error) {
    major, minor := DecomposeDataType(dt)
    switch major {
    case DataTypeMajorInt:
        return decimalDecode(true, b), nil
    case DataTypeMajorUint:
        return decimalDecode(false, b), nil
    }
    switch {
    case major.IsFixedRange():
        return decimalDecode(true, b).Shift(-int32(minor)), nil
    case major.IsUfixedRange():
        return decimalDecode(false, b).Shift(-int32(minor)), nil
    }

    return decimal.Zero, se.ErrorCodeDecimalDecode
}

// DecimalToUint64 convert decimal to uint64.
// Negative case will return error, and decimal part will be trancated.
func DecimalToUint64(d decimal.Decimal) (uint64, error) {
    s := d.Sign()
    if s == 0 {
        return 0, nil
    }
    if s < 0 {
        return 0, se.ErrorCodeNegDecimalToUint64
    }
    return decimalToBig(d).Uint64(), nil
}