Skip to content

Commit

Permalink
schema: tuple support
Browse files Browse the repository at this point in the history
We introduce a TupleType that handles generation of data and
type specific CQL for select clauses and create statements.
The simple types are all bundled in specific instances of
a new type SimpleType.
  • Loading branch information
Henrik Johansson committed Apr 10, 2019
1 parent f52e994 commit 404bad5
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 147 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Tuple support added for simple columns.
- Added version info printing using '--version' program argument.
- CQL `INSERT JSON` statement support.

Expand Down
124 changes: 6 additions & 118 deletions datautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ package gemini
import (
"encoding/base64"
"fmt"
"math/big"
"math/rand"
"net"
"strconv"
"strings"
"time"

"github.com/gocql/gocql"
"gopkg.in/inf.v0"

"github.com/segmentio/ksuid"
)

Expand Down Expand Up @@ -127,120 +122,13 @@ func randIpV4Address(v, pos int) string {
return strings.Join(blocks, ".")
}

func genValue(columnType string, p *PartitionRange) interface{} {
switch columnType {
case "ascii", "blob", "text", "varchar":
return randStringWithTime(nonEmptyRandIntRange(p.Max, p.Max, 10), randTime())
case "bigint":
return rand.Int63()
case "boolean":
return rand.Int()%2 == 0
case "date":
return randDate()
case "time", "timestamp":
return randTime()
case "decimal":
return inf.NewDec(randInt64Range(int64(p.Min), int64(p.Max)), 3)
case "double":
return randFloat64Range(float64(p.Min), float64(p.Max))
case "duration":
return (time.Minute * time.Duration(randIntRange(p.Min, p.Max))).String()
case "float":
return randFloat32Range(float32(p.Min), float32(p.Max))
case "inet":
return net.ParseIP(randIpV4Address(rand.Intn(255), 2))
case "int":
return nonEmptyRandIntRange(p.Min, p.Max, 10)
case "smallint":
return int16(nonEmptyRandIntRange(p.Min, p.Max, 10))
case "timeuuid", "uuid":
r := gocql.UUIDFromTime(randTime())
return r.String()
case "tinyint":
return int8(nonEmptyRandIntRange(p.Min, p.Max, 10))
case "varint":
return big.NewInt(randInt64Range(int64(p.Min), int64(p.Max)))
default:
panic(fmt.Sprintf("generate value: not supported type %s", columnType))
}
func appendValue(columnType Type, p *PartitionRange, values []interface{}) []interface{} {
return append(values, columnType.GenValue(p)...)
}

func appendValue(columnType string, p *PartitionRange, values []interface{}) []interface{} {
return append(values, genValue(columnType, p))
}

func appendValueRange(columnType string, p *PartitionRange, values []interface{}) []interface{} {
switch columnType {
case "ascii", "blob", "text", "varchar":
startTime := randTime()
start := nonEmptyRandIntRange(p.Min, p.Max, 10)
end := start + nonEmptyRandIntRange(p.Min, p.Max, 10)
values = append(values, nonEmptyRandStringWithTime(start, startTime))
values = append(values, nonEmptyRandStringWithTime(end, randTimeNewer(startTime)))
case "bigint":
start := nonEmptyRandInt64Range(int64(p.Min), int64(p.Max), 10)
end := start + nonEmptyRandInt64Range(int64(p.Min), int64(p.Max), 10)
values = append(values, start)
values = append(values, end)
case "date", "time", "timestamp":
start := randTime()
end := randTimeNewer(start)
values = append(values, start)
values = append(values, end)
case "decimal":
start := nonEmptyRandInt64Range(int64(p.Min), int64(p.Max), 10)
end := start + nonEmptyRandInt64Range(int64(p.Min), int64(p.Max), 10)
values = append(values, inf.NewDec(start, 3))
values = append(values, inf.NewDec(end, 3))
case "double":
start := nonEmptyRandFloat64Range(float64(p.Min), float64(p.Max), 10)
end := start + nonEmptyRandFloat64Range(float64(p.Min), float64(p.Max), 10)
values = append(values, start)
values = append(values, end)
case "duration":
start := time.Minute * time.Duration(nonEmptyRandIntRange(p.Min, p.Max, 10))
end := start + time.Minute*time.Duration(nonEmptyRandIntRange(p.Min, p.Max, 10))
values = append(values, start)
values = append(values, end)
case "float":
start := nonEmptyRandFloat32Range(float32(p.Min), float32(p.Max), 10)
end := start + nonEmptyRandFloat32Range(float32(p.Min), float32(p.Max), 10)
values = append(values, start)
values = append(values, end)
case "inet":
start := randIpV4Address(0, 3)
end := randIpV4Address(255, 3)
values = append(values, net.ParseIP(start))
values = append(values, net.ParseIP(end))
case "int":
start := nonEmptyRandIntRange(p.Min, p.Max, 10)
end := start + nonEmptyRandIntRange(p.Min, p.Max, 10)
values = append(values, start)
values = append(values, end)
case "smallint":
start := int16(nonEmptyRandIntRange(p.Min, p.Max, 10))
end := start + int16(nonEmptyRandIntRange(p.Min, p.Max, 10))
values = append(values, start)
values = append(values, end)
case "timeuuid", "uuid":
start := randTime()
end := randTimeNewer(start)
values = append(values, gocql.UUIDFromTime(start).String())
values = append(values, gocql.UUIDFromTime(end).String())
case "tinyint":
start := int8(nonEmptyRandIntRange(p.Min, p.Max, 10))
end := start + int8(nonEmptyRandIntRange(p.Min, p.Max, 10))
values = append(values, start)
values = append(values, end)
case "varint":
end := &big.Int{}
start := big.NewInt(randInt64Range(int64(p.Min), int64(p.Max)))
end.Set(start)
end = end.Add(start, big.NewInt(randInt64Range(int64(p.Min), int64(p.Max))))
values = append(values, start)
values = append(values, end)
default:
panic(fmt.Sprintf("generate value range: not supported type %s", columnType))
}
func appendValueRange(columnType Type, p *PartitionRange, values []interface{}) []interface{} {
left, right := columnType.GenValueRange(p)
values = append(values, left...)
values = append(values, right...)
return values
}
Loading

0 comments on commit 404bad5

Please sign in to comment.