Skip to content

Commit

Permalink
add some basic sql types
Browse files Browse the repository at this point in the history
copied some code from vitness
  • Loading branch information
chrislusf committed Oct 7, 2019
1 parent cf47f65 commit e26670c
Show file tree
Hide file tree
Showing 4 changed files with 613 additions and 0 deletions.
124 changes: 124 additions & 0 deletions weed/query/sqltypes/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package sqltypes

// copied from vitness

// Flag allows us to qualify types by their common properties.
type Flag int32

const (
Flag_NONE Flag = 0
Flag_ISINTEGRAL Flag = 256
Flag_ISUNSIGNED Flag = 512
Flag_ISFLOAT Flag = 1024
Flag_ISQUOTED Flag = 2048
Flag_ISTEXT Flag = 4096
Flag_ISBINARY Flag = 8192
)

var Flag_name = map[int32]string{
0: "NONE",
256: "ISINTEGRAL",
512: "ISUNSIGNED",
1024: "ISFLOAT",
2048: "ISQUOTED",
4096: "ISTEXT",
8192: "ISBINARY",
}
var Flag_value = map[string]int32{
"NONE": 0,
"ISINTEGRAL": 256,
"ISUNSIGNED": 512,
"ISFLOAT": 1024,
"ISQUOTED": 2048,
"ISTEXT": 4096,
"ISBINARY": 8192,
}

// Type defines the various supported data types in bind vars
// and query results.
type Type int32

const (
// NULL_TYPE specifies a NULL type.
Type_NULL_TYPE Type = 0
// INT8 specifies a TINYINT type.
// Properties: 1, IsNumber.
Type_INT8 Type = 257
// UINT8 specifies a TINYINT UNSIGNED type.
// Properties: 2, IsNumber, IsUnsigned.
Type_UINT8 Type = 770
// INT16 specifies a SMALLINT type.
// Properties: 3, IsNumber.
Type_INT16 Type = 259
// UINT16 specifies a SMALLINT UNSIGNED type.
// Properties: 4, IsNumber, IsUnsigned.
Type_UINT16 Type = 772
// INT24 specifies a MEDIUMINT type.
// Properties: 5, IsNumber.
Type_INT32 Type = 263
// UINT32 specifies a INTEGER UNSIGNED type.
// Properties: 8, IsNumber, IsUnsigned.
Type_UINT32 Type = 776
// INT64 specifies a BIGINT type.
// Properties: 9, IsNumber.
Type_INT64 Type = 265
// UINT64 specifies a BIGINT UNSIGNED type.
// Properties: 10, IsNumber, IsUnsigned.
Type_UINT64 Type = 778
// FLOAT32 specifies a FLOAT type.
// Properties: 11, IsFloat.
Type_FLOAT32 Type = 1035
// FLOAT64 specifies a DOUBLE or REAL type.
// Properties: 12, IsFloat.
Type_FLOAT64 Type = 1036
// TIMESTAMP specifies a TIMESTAMP type.
// Properties: 13, IsQuoted.
Type_TIMESTAMP Type = 2061
// DATE specifies a DATE type.
// Properties: 14, IsQuoted.
Type_DATE Type = 2062
// TIME specifies a TIME type.
// Properties: 15, IsQuoted.
Type_TIME Type = 2063
// DATETIME specifies a DATETIME type.
// Properties: 16, IsQuoted.
Type_DATETIME Type = 2064
// YEAR specifies a YEAR type.
// Properties: 17, IsNumber, IsUnsigned.
Type_YEAR Type = 785
// DECIMAL specifies a DECIMAL or NUMERIC type.
// Properties: 18, None.
Type_DECIMAL Type = 18
// TEXT specifies a TEXT type.
// Properties: 19, IsQuoted, IsText.
Type_TEXT Type = 6163
// BLOB specifies a BLOB type.
// Properties: 20, IsQuoted, IsBinary.
Type_BLOB Type = 10260
// VARCHAR specifies a VARCHAR type.
// Properties: 21, IsQuoted, IsText.
Type_VARCHAR Type = 6165
// VARBINARY specifies a VARBINARY type.
// Properties: 22, IsQuoted, IsBinary.
Type_VARBINARY Type = 10262
// CHAR specifies a CHAR type.
// Properties: 23, IsQuoted, IsText.
Type_CHAR Type = 6167
// BINARY specifies a BINARY type.
// Properties: 24, IsQuoted, IsBinary.
Type_BINARY Type = 10264
// BIT specifies a BIT type.
// Properties: 25, IsQuoted.
Type_BIT Type = 2073
// JSON specifies a JSON type.
// Properties: 30, IsQuoted.
Type_JSON Type = 2078
)

// BindVariable represents a single bind variable in a Query.
type BindVariable struct {
Type Type `protobuf:"varint,1,opt,name=type,enum=query.Type" json:"type,omitempty"`
Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
// values are set if type is TUPLE.
Values []*Value `protobuf:"bytes,3,rep,name=values" json:"values,omitempty"`
}
102 changes: 102 additions & 0 deletions weed/query/sqltypes/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package sqltypes

// These bit flags can be used to query on the
// common properties of types.
const (
flagIsIntegral = int(Flag_ISINTEGRAL)
flagIsUnsigned = int(Flag_ISUNSIGNED)
flagIsFloat = int(Flag_ISFLOAT)
flagIsQuoted = int(Flag_ISQUOTED)
flagIsText = int(Flag_ISTEXT)
flagIsBinary = int(Flag_ISBINARY)
)

// IsIntegral returns true if Type is an integral
// (signed/unsigned) that can be represented using
// up to 64 binary bits.
// If you have a Value object, use its member function.
func IsIntegral(t Type) bool {
return int(t)&flagIsIntegral == flagIsIntegral
}

// IsSigned returns true if Type is a signed integral.
// If you have a Value object, use its member function.
func IsSigned(t Type) bool {
return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral
}

// IsUnsigned returns true if Type is an unsigned integral.
// Caution: this is not the same as !IsSigned.
// If you have a Value object, use its member function.
func IsUnsigned(t Type) bool {
return int(t)&(flagIsIntegral|flagIsUnsigned) == flagIsIntegral|flagIsUnsigned
}

// IsFloat returns true is Type is a floating point.
// If you have a Value object, use its member function.
func IsFloat(t Type) bool {
return int(t)&flagIsFloat == flagIsFloat
}

// IsQuoted returns true if Type is a quoted text or binary.
// If you have a Value object, use its member function.
func IsQuoted(t Type) bool {
return (int(t)&flagIsQuoted == flagIsQuoted) && t != Bit
}

// IsText returns true if Type is a text.
// If you have a Value object, use its member function.
func IsText(t Type) bool {
return int(t)&flagIsText == flagIsText
}

// IsBinary returns true if Type is a binary.
// If you have a Value object, use its member function.
func IsBinary(t Type) bool {
return int(t)&flagIsBinary == flagIsBinary
}

// isNumber returns true if the type is any type of number.
func isNumber(t Type) bool {
return IsIntegral(t) || IsFloat(t) || t == Decimal
}

// IsTemporal returns true if Value is time type.
func IsTemporal(t Type) bool {
switch t {
case Timestamp, Date, Time, Datetime:
return true
}
return false
}

// Vitess data types. These are idiomatically
// named synonyms for the Type values.
const (
Null = Type_NULL_TYPE
Int8 = Type_INT8
Uint8 = Type_UINT8
Int16 = Type_INT16
Uint16 = Type_UINT16
Int32 = Type_INT32
Uint32 = Type_UINT32
Int64 = Type_INT64
Uint64 = Type_UINT64
Float32 = Type_FLOAT32
Float64 = Type_FLOAT64
Timestamp = Type_TIMESTAMP
Date = Type_DATE
Time = Type_TIME
Datetime = Type_DATETIME
Year = Type_YEAR
Decimal = Type_DECIMAL
Text = Type_TEXT
Blob = Type_BLOB
VarChar = Type_VARCHAR
VarBinary = Type_VARBINARY
Char = Type_CHAR
Binary = Type_BINARY
Bit = Type_BIT
TypeJSON = Type_JSON
)

30 changes: 30 additions & 0 deletions weed/query/sqltypes/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sqltypes

import (
"reflect"
"unsafe"
)

// BytesToString casts slice to string without copy
func BytesToString(b []byte) (s string) {
if len(b) == 0 {
return ""
}

bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{Data: bh.Data, Len: bh.Len}

return *(*string)(unsafe.Pointer(&sh))
}

// StringToBytes casts string to slice without copy
func StringToBytes(s string) []byte {
if len(s) == 0 {
return []byte{}
}

sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}

return *(*[]byte)(unsafe.Pointer(&bh))
}
Loading

0 comments on commit e26670c

Please sign in to comment.