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

V0.10 sql #1070

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/common/display/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func Example_respTxQuery_json() {
// },
// "body": {
// "desc": "This is a test transaction for cli",
// "payload": "AAH4Vrg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyzs3MxoR0ZXh0gMSDZm9v",
// "payload": "AAH4V7g5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyz87Nx4R0ZXh0gMDEg2Zvbw==",
// "type": "execute",
// "fee": "100",
// "nonce": 10,
Expand Down Expand Up @@ -194,7 +194,7 @@ func Example_respTxQuery_WithRaw_json() {
// },
// "body": {
// "desc": "This is a test transaction for cli",
// "payload": "AAH4Vrg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyzs3MxoR0ZXh0gMSDZm9v",
// "payload": "AAH4V7g5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyz87Nx4R0ZXh0gMDEg2Zvbw==",
// "type": "execute",
// "fee": "100",
// "nonce": 10,
Expand All @@ -209,8 +209,8 @@ func Example_respTxQuery_WithRaw_json() {
// "gas_used": 10,
// "gas_wanted": 10
// },
// "raw": "0001f8eaf850b841cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758008c736563703235366b315f6570f88ea25468697320697320612074657374207472616e73616374696f6e20666f7220636c69b85a0001f856b8397866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064348b6372656174655f75736572cecdccc6847465787480c483666f6f8765786563757465640a846173646686636f6e63617480",
// "warning": "HASH MISMATCH: requested 31303234; received f866b4251d21552de1bc5b819a4b563a540146954e956e8150163574ce5325ac"
// "raw": "0001f8ebf850b841cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758008c736563703235366b315f6570f88fa25468697320697320612074657374207472616e73616374696f6e20666f7220636c69b85b0001f857b8397866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064348b6372656174655f75736572cfcecdc7847465787480c0c483666f6f8765786563757465640a846173646686636f6e63617480",
// "warning": "HASH MISMATCH: requested 31303234; received 3a05fc591b97c720a3c67852807f3df5a27ab51d41fca03b86c92af9a1915c4c"
// },
// "error": ""
// }
Expand All @@ -224,7 +224,7 @@ func Test_TxHashAndExecResponse(t *testing.T) {
Hash: hash,
QueryResp: &RespTxQuery{Msg: qr},
}
expectJson := `{"tx_hash":"0102030405","exec_result":{"hash":"0102030405","height":10,"tx":{"signature":{"sig":"yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=","type":"secp256k1_ep"},"body":{"desc":"This is a test transaction for cli","payload":"AAH4Vrg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyzs3MxoR0ZXh0gMSDZm9v","type":"execute","fee":"100","nonce":10,"chain_id":"asdf"},"serialization":"concat","sender":""},"tx_result":{"code":0,"log":"This is log","gas_used":10,"gas_wanted":10}}}`
expectJson := "{\"tx_hash\":\"0102030405\",\"exec_result\":{\"hash\":\"0102030405\",\"height\":10,\"tx\":{\"signature\":{\"sig\":\"yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=\",\"type\":\"secp256k1_ep\"},\"body\":{\"desc\":\"This is a test transaction for cli\",\"payload\":\"AAH4V7g5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2Vyz87Nx4R0ZXh0gMDEg2Zvbw==\",\"type\":\"execute\",\"fee\":\"100\",\"nonce\":10,\"chain_id\":\"asdf\"},\"serialization\":\"concat\",\"sender\":\"\"},\"tx_result\":{\"code\":0,\"log\":\"This is log\",\"gas_used\":10,\"gas_wanted\":10}}}"
expectText := "TxHash: 0102030405\nStatus: success\nHeight: 10\nLog: This is log"

outText, err := resp.MarshalText()
Expand Down
14 changes: 14 additions & 0 deletions core/types/decimal/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ func (d *Decimal) Float64() (float64, error) {
return d.dec.Float64()
}

// Copy returns a copy of the decimal.
func (d *Decimal) Copy() *Decimal {
return &Decimal{
dec: apd.Decimal{
Coeff: *apd.NewBigInt(d.dec.Coeff.Int64()),
Form: d.dec.Form,
Negative: d.dec.Negative,
Exponent: d.dec.Exponent,
},
scale: d.scale,
precision: d.precision,
}
}

var _ json.Marshaler = Decimal{}
var _ json.Marshaler = (*Decimal)(nil)

Expand Down
38 changes: 22 additions & 16 deletions core/types/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ type DataType struct {
// IsArray is true if the type is an array.
IsArray bool `json:"is_array"`
// Metadata is the metadata of the type.
Metadata [2]uint16 `json:"metadata"`
Metadata *[2]uint16 `json:"metadata" rlp:"optional"`
}

// String returns the string representation of the type.
Expand All @@ -1121,8 +1121,6 @@ func (c *DataType) String() string {
return str.String()
}

var ZeroMetadata = [2]uint16{}

// PGString returns the string representation of the type in Postgres.
func (c *DataType) PGString() (string, error) {
var scalar string
Expand All @@ -1140,11 +1138,11 @@ func (c *DataType) PGString() (string, error) {
case uint256Str:
scalar = "UINT256"
case DecimalStr:
if c.Metadata == ZeroMetadata {
return "", fmt.Errorf("decimal type must have metadata")
if c.Metadata == nil {
scalar = "NUMERIC"
} else {
scalar = fmt.Sprintf("NUMERIC(%d,%d)", c.Metadata[0], c.Metadata[1])
}

scalar = fmt.Sprintf("NUMERIC(%d,%d)", c.Metadata[0], c.Metadata[1])
case nullStr:
return "", fmt.Errorf("cannot have null column type")
case unknownStr:
Expand All @@ -1164,14 +1162,15 @@ func (c *DataType) Clean() error {
c.Name = strings.ToLower(c.Name)
switch c.Name {
case intStr, textStr, boolStr, blobStr, uuidStr, uint256Str: // ok
if c.Metadata != ZeroMetadata {
if c.Metadata != nil {
return fmt.Errorf("type %s cannot have metadata", c.Name)
}

return nil
case DecimalStr:
if c.Metadata == ZeroMetadata {
return fmt.Errorf("decimal type must have metadata")
if c.Metadata == nil {
// numeric can have unspecified precision and scale
return nil
}

err := decimal.CheckPrecisionAndScale(c.Metadata[0], c.Metadata[1])
Expand All @@ -1185,7 +1184,7 @@ func (c *DataType) Clean() error {
return fmt.Errorf("type %s cannot be an array", c.Name)
}

if c.Metadata != ZeroMetadata {
if c.Metadata != nil {
return fmt.Errorf("type %s cannot have metadata", c.Name)
}

Expand Down Expand Up @@ -1219,10 +1218,10 @@ func (c *DataType) EqualsStrict(other *DataType) bool {
return false
}

if (c.Metadata == ZeroMetadata) != (other.Metadata == ZeroMetadata) {
if (c.Metadata == nil) != (other.Metadata == nil) {
return false
}
if c.Metadata != ZeroMetadata {
if c.Metadata != nil {
if c.Metadata[0] != other.Metadata[0] || c.Metadata[1] != other.Metadata[1] {
return false
}
Expand Down Expand Up @@ -1275,8 +1274,8 @@ var (
// For type detection, users should prefer compare a datatype
// name with the DecimalStr constant.
DecimalType = &DataType{
Name: DecimalStr,
Metadata: [2]uint16{1, 0}, // the minimum precision and scale
Name: DecimalStr,
// unspecified precision and scale
}
DecimalArrayType = ArrayType(DecimalType)
Uint256Type = &DataType{
Expand All @@ -1292,6 +1291,11 @@ var (
UnknownType = &DataType{
Name: unknownStr,
}
// RecordType is a special type used internally to represent
// an record with referenceable fields.
RecordType = &DataType{
Name: "record",
}
)

// ArrayType creates an array type of the given type.
Expand Down Expand Up @@ -1327,8 +1331,10 @@ func NewDecimalType(precision, scale uint16) (*DataType, error) {
return nil, err
}

met := [2]uint16{precision, scale}

return &DataType{
Name: DecimalStr,
Metadata: [2]uint16{precision, scale},
Metadata: &met,
}, nil
}
2 changes: 1 addition & 1 deletion core/types/transactions/payload_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type DataType struct {
// IsArray is true if the type is an array.
IsArray bool
// Metadata is the metadata of the type.
Metadata [2]uint16 `rlp:"optional"`
Metadata *[2]uint16 `rlp:"nil"`
}

// ForeignProcedure is a foreign procedure call in a database
Expand Down
2 changes: 1 addition & 1 deletion core/types/transactions/payloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func UnmarshalPayload(payloadType PayloadType, payload []byte) (Payload, error)
}

t := reflect.TypeOf(prototype).Elem()
elem := reflect.New(t) // reflect.Type => reflect.Value
elem := reflect.New(t) // reflect.Type => reflect.Param
instance := elem.Interface() // reflect.Type => any

err := serialize.Decode(payload, instance)
Expand Down
4 changes: 2 additions & 2 deletions core/types/transactions/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestTransaction_Sign(t *testing.T) {

ethPersonalSigner := auth.EthPersonalSigner{Key: *secp256k1PrivateKey}

expectPersonalSignConcatSigHex := "e09459d0dc078f12bb176da6ec52764ac457e322644f4031a6c498979795eff16163edbfe2c68ba60e25d6a76a283f63662245555caecf68889fbfad786ae52801"
expectPersonalSignConcatSigHex := "2124b683f153a158f9122fcce4887ad5109ece0137d49fd791e242bbf29eeae343feee21c6989ac5c570c2bdf7b8159d630bfa565cc46f3f6d3e78add943fba700"
expectPersonalSignConcatSigBytes, _ := hex.DecodeString(expectPersonalSignConcatSigHex)
expectPersonalSignConcatSig := &auth.Signature{
Signature: expectPersonalSignConcatSigBytes,
Expand Down Expand Up @@ -304,7 +304,7 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
mst: transactions.SignedMsgConcat,
description: defaultDescription,
},
wantMsg: "4279207369676e696e672074686973206d6573736167652c20796f75276c6c2072657665616c20796f75722078787820746f207a7a7a0a0a5061796c6f6164547970653a20657865637574650a5061796c6f61644469676573743a20323038623838653133656336313866313836376564333534366131343861656335633835316631310a4665653a203130300a4e6f6e63653a20310a0a4b77696c20436861696e2049443a2030303030303030303030300a",
wantMsg: "4279207369676e696e672074686973206d6573736167652c20796f75276c6c2072657665616c20796f75722078787820746f207a7a7a0a0a5061796c6f6164547970653a20657865637574650a5061796c6f61644469676573743a20636666396361646362336433306666386130366635363265623234313034306539363033636537640a4665653a203130300a4e6f6e63653a20310a0a4b77696c20436861696e2049443a2030303030303030303030300a",
wantErr: false,
},
}
Expand Down
61 changes: 61 additions & 0 deletions core/types/uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,67 @@ func (u *Uint256) Clone() *Uint256 {
return &v
}

func (u *Uint256) Add(v *Uint256) *Uint256 {
z := uint256.NewInt(0)

return &Uint256{
base: *z.Add(&u.base, &v.base),
}
}

func (u *Uint256) Sub(v *Uint256) (*Uint256, error) {
z := uint256.NewInt(0)
res, overflow := z.SubOverflow(&u.base, &v.base)
if overflow {
return nil, fmt.Errorf("overflow")
}

return &Uint256{
base: *res,
}, nil
}

func (u *Uint256) Mul(v *Uint256) (*Uint256, error) {
z := uint256.NewInt(0)

res, overflow := z.MulOverflow(&u.base, &v.base)
if overflow {
return nil, fmt.Errorf("overflow")
}

return &Uint256{
base: *res,
}, nil
}

func (u *Uint256) Div(v *Uint256) *Uint256 {
z := uint256.NewInt(0)

return &Uint256{
base: *z.Div(&u.base, &v.base),
}
}

func (u *Uint256) DivMod(v *Uint256) (*Uint256, *Uint256) {
z := uint256.NewInt(0)
mod := uint256.NewInt(0)
z.DivMod(&u.base, &v.base, mod)

return &Uint256{
base: *z,
}, &Uint256{
base: *mod,
}
}

func (u *Uint256) Mod(v *Uint256) *Uint256 {
z := uint256.NewInt(0)

return &Uint256{
base: *z.Mod(&u.base, &v.base),
}
}

func (u *Uint256) Cmp(v *Uint256) int {
return u.base.Cmp(&v.base)
}
Expand Down
31 changes: 31 additions & 0 deletions core/types/uint256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,35 @@ func TestUint256JSONRoundTrip(t *testing.T) {

assert.Equal(t, original.String(), unmarshaled.String())
}

}

func Test_Uint256Math(t *testing.T) {
// simply testing that the base number is not modified
a, err := Uint256FromString("500")
require.NoError(t, err)

b, err := Uint256FromString("10000000000")
require.NoError(t, err)

c := a.Add(b)
require.Equal(t, "10000000500", c.String())
require.Equal(t, "500", a.String())
require.Equal(t, "10000000000", b.String())

// go underflow
_, err = a.Sub(b)
require.Error(t, err)

// div without mod
d, err := Uint256FromString("498")
require.NoError(t, err)

e := a.Div(d)
require.Equal(t, "1", e.String())

// div mod
f, g := a.DivMod(d)
require.Equal(t, "1", f.String())
require.Equal(t, "2", g.String())
}
5 changes: 2 additions & 3 deletions internal/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"sync"
"sync/atomic"

abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/kwilteam/kwil-db/common"
"github.com/kwilteam/kwil-db/common/chain"
"github.com/kwilteam/kwil-db/common/chain/forks"
Expand All @@ -35,9 +37,6 @@ import (
"github.com/kwilteam/kwil-db/internal/txapp"
"github.com/kwilteam/kwil-db/internal/version"
"github.com/kwilteam/kwil-db/parse"

abciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/tmhash"
"go.uber.org/zap"
)

Expand Down
Loading