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

Add int64 support #331

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions aggregates/average.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var AverageOverloads = []physical.AggregateDescriptor{
OutputType: octosql.Int,
Prototype: NewAverageIntPrototype(),
},
{
ArgumentType: octosql.Int64,
OutputType: octosql.Int64,
Prototype: NewAverageInt64Prototype(),
},
{
ArgumentType: octosql.Float,
OutputType: octosql.Float,
Expand Down Expand Up @@ -49,6 +54,29 @@ func (c *AverageInt) Trigger() octosql.Value {
return octosql.NewInt(c.sum.Trigger().Int / c.count.Trigger().Int)
}

type AverageInt64 struct {
sum SumInt64
count Count
}

func NewAverageInt64Prototype() func() nodes.Aggregate {
return func() nodes.Aggregate {
return &AverageInt64{
sum: SumInt64{},
count: Count{},
}
}
}

func (c *AverageInt64) Add(retraction bool, value octosql.Value) bool {
c.sum.Add(retraction, value)
return c.count.Add(retraction, value)
}

func (c *AverageInt64) Trigger() octosql.Value {
return octosql.NewInt64(c.sum.Trigger().Int64 / int64(c.count.Trigger().Int))
}

type AverageFloat struct {
sum SumFloat
count Count
Expand Down
5 changes: 5 additions & 0 deletions aggregates/max.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var MaxOverloads = []physical.AggregateDescriptor{
OutputType: octosql.Int,
Prototype: NewMaxPrototype(),
},
{
ArgumentType: octosql.Int64,
OutputType: octosql.Int64,
Prototype: NewMaxPrototype(),
},
{
ArgumentType: octosql.Float,
OutputType: octosql.Float,
Expand Down
5 changes: 5 additions & 0 deletions aggregates/min.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var MinOverloads = []physical.AggregateDescriptor{
OutputType: octosql.Int,
Prototype: NewMinPrototype(),
},
{
ArgumentType: octosql.Int64,
OutputType: octosql.Int64,
Prototype: NewMinPrototype(),
},
{
ArgumentType: octosql.Float,
OutputType: octosql.Float,
Expand Down
30 changes: 30 additions & 0 deletions aggregates/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var SumOverloads = []physical.AggregateDescriptor{
OutputType: octosql.Int,
Prototype: NewSumIntPrototype(),
},
{
ArgumentType: octosql.Int64,
OutputType: octosql.Int64,
Prototype: NewSumInt64Prototype(),
},
{
ArgumentType: octosql.Float,
OutputType: octosql.Float,
Expand Down Expand Up @@ -51,6 +56,31 @@ func (c *SumInt) Trigger() octosql.Value {
return octosql.NewInt(c.sum)
}

type SumInt64 struct {
sum int64
}

func NewSumInt64Prototype() func() nodes.Aggregate {
return func() nodes.Aggregate {
return &SumInt64{
sum: 0,
}
}
}

func (c *SumInt64) Add(retraction bool, value octosql.Value) bool {
if !retraction {
c.sum += value.Int64
} else {
c.sum -= value.Int64
}
return c.sum == 0
}

func (c *SumInt64) Trigger() octosql.Value {
return octosql.NewInt64(c.sum)
}

type SumFloat struct {
sum float64
}
Expand Down
2 changes: 1 addition & 1 deletion datasources/csv/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (d *DatasourceExecuting) Run(ctx ExecutionContext, produce ProduceFn, metaS
if octosql.Int.Is(d.fields[i].Type) == octosql.TypeRelationIs {
integer, err := fastfloat.ParseInt64(str)
if err == nil {
values[i] = octosql.NewInt(int(integer))
values[i] = octosql.NewInt64(integer)
continue
}
}
Expand Down
2 changes: 1 addition & 1 deletion datasources/parquet/reconstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func assignValue(dst *octosql.Value, src parquet.Value) error {
case parquet.Int32:
*dst = octosql.NewInt(int(src.Int32()))
case parquet.Int64:
*dst = octosql.NewInt(int(src.Int64()))
*dst = octosql.NewInt64(src.Int64())
case parquet.Int96:
*dst = octosql.NewString(src.Int96().String())
case parquet.Float:
Expand Down
131 changes: 131 additions & 0 deletions functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewInt(values[0].Int + values[1].Int), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64, octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(values[0].Int64 + values[1].Int64), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float, octosql.Float},
OutputType: octosql.Float,
Expand Down Expand Up @@ -223,6 +231,22 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewInt(-values[0].Int), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64, octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(values[0].Int64 - values[1].Int64), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(-values[0].Int64), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float, octosql.Float},
OutputType: octosql.Float,
Expand Down Expand Up @@ -275,6 +299,14 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewInt(values[0].Int * values[1].Int), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64, octosql.Int64},
OutputType: octosql.Int,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(values[0].Int64 * values[1].Int64), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float, octosql.Float},
OutputType: octosql.Float,
Expand Down Expand Up @@ -327,6 +359,14 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewInt(values[0].Int / values[1].Int), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64, octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(values[0].Int64 / values[1].Int64), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float, octosql.Float},
OutputType: octosql.Float,
Expand Down Expand Up @@ -368,6 +408,17 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewInt(values[0].Int * -1), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
if values[0].Int64 > 0 {
return values[0], nil
}
return octosql.NewInt64(values[0].Int64 * -1), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float},
OutputType: octosql.Float,
Expand Down Expand Up @@ -910,6 +961,14 @@ func FunctionMap() map[string]physical.FunctionDetails {
return values[0], nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64},
OutputType: octosql.Int,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt(int(values[0].Int64)), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Boolean},
OutputType: octosql.Int,
Expand Down Expand Up @@ -953,6 +1012,70 @@ func FunctionMap() map[string]physical.FunctionDetails {
},
},
},
"int64": {
Description: "Converts the argument to an int64.",
Descriptors: []physical.FunctionDescriptor{
{
// This case will catch any types which may be int at the start of non-exact matching.
// So the int function can be used as a type cast.
ArgumentTypes: []octosql.Type{octosql.Int},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(int64(values[0].Int)), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return values[0], nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Boolean},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
if values[0].Boolean {
return octosql.NewInt64(1), nil
} else {
return octosql.NewInt64(0), nil
}
},
},
{
ArgumentTypes: []octosql.Type{octosql.Float},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(int64(values[0].Float)), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.String},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
n, err := strconv.ParseInt(values[0].Str, 10, 64)
if err != nil {
log.Printf("couldn't parse string '%s' as int64: %s", values[0].Str, err)
return octosql.NewNull(), nil
}
return octosql.NewInt64(n), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Duration},
OutputType: octosql.Int64,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewInt64(int64(values[0].Duration)), nil
},
},
},
},
"float": {
Description: "Converts the argument to an float.",
Descriptors: []physical.FunctionDescriptor{
Expand All @@ -974,6 +1097,14 @@ func FunctionMap() map[string]physical.FunctionDetails {
return octosql.NewFloat(float64(values[0].Int)), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.Int64},
OutputType: octosql.Float,
Strict: true,
Function: func(values []octosql.Value) (octosql.Value, error) {
return octosql.NewFloat(float64(values[0].Int64)), nil
},
},
{
ArgumentTypes: []octosql.Type{octosql.String},
OutputType: octosql.Float,
Expand Down
4 changes: 4 additions & 0 deletions octosql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
TypeIDTuple
TypeIDUnion
TypeIDAny // TODO: Remove this type?
TypeIDInt64
)

func (t TypeID) String() string {
Expand All @@ -29,6 +30,8 @@ func (t TypeID) String() string {
return "Null"
case TypeIDInt:
return "Int"
case TypeIDInt64:
return "Int64"
case TypeIDFloat:
return "Float"
case TypeIDBoolean:
Expand Down Expand Up @@ -228,6 +231,7 @@ func (t Type) String() string {
var (
Null = Type{TypeID: TypeIDNull}
Int = Type{TypeID: TypeIDInt}
Int64 = Type{TypeID: TypeIDInt64}
Float = Type{TypeID: TypeIDFloat}
Boolean = Type{TypeID: TypeIDBoolean}
String = Type{TypeID: TypeIDString}
Expand Down
Loading