forked from substrait-io/substrait-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
163 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/substrait-io/substrait-go/proto" | ||
) | ||
|
||
// IntervalDayType this is used to represent a type of interval day. | ||
type IntervalDayType struct { | ||
Precision TimePrecision | ||
TypeVariationRef uint32 | ||
Nullability Nullability | ||
} | ||
|
||
func (m *IntervalDayType) WithTypeVariationRef(typeVariationRef uint32) *IntervalDayType { | ||
m.TypeVariationRef = typeVariationRef | ||
return m | ||
} | ||
|
||
func (m *IntervalDayType) GetPrecisionProtoVal() int32 { | ||
return m.Precision.ToProtoVal() | ||
} | ||
|
||
func (*IntervalDayType) isRootRef() {} | ||
func (m *IntervalDayType) WithNullability(n Nullability) Type { | ||
m.Nullability = n | ||
return m | ||
} | ||
|
||
func (m *IntervalDayType) WithPrecision(precision TimePrecision) *IntervalDayType { | ||
m.Precision = precision | ||
return m | ||
} | ||
|
||
func (m *IntervalDayType) GetType() Type { return m } | ||
func (m *IntervalDayType) GetNullability() Nullability { return m.Nullability } | ||
func (m *IntervalDayType) GetTypeVariationReference() uint32 { return m.TypeVariationRef } | ||
func (m *IntervalDayType) Equals(rhs Type) bool { | ||
if o, ok := rhs.(*IntervalDayType); ok { | ||
return *o == *m | ||
} | ||
return false | ||
} | ||
|
||
func (m *IntervalDayType) ToProtoFuncArg() *proto.FunctionArgument { | ||
return &proto.FunctionArgument{ | ||
ArgType: &proto.FunctionArgument_Type{Type: m.ToProto()}, | ||
} | ||
} | ||
|
||
func (m *IntervalDayType) ToProto() *proto.Type { | ||
precisionVal := m.Precision.ToProtoVal() | ||
return &proto.Type{Kind: &proto.Type_IntervalDay_{ | ||
IntervalDay: &proto.Type_IntervalDay{ | ||
Precision: &precisionVal, | ||
Nullability: m.Nullability, | ||
TypeVariationReference: m.TypeVariationRef}}} | ||
} | ||
|
||
func (*IntervalDayType) ShortString() string { return "iday" } | ||
func (m *IntervalDayType) String() string { | ||
return fmt.Sprintf("interval_day%s<%d>", strNullable(m), | ||
m.Precision.ToProtoVal()) | ||
} | ||
|
||
func (m *IntervalDayType) ParameterString() string { | ||
return fmt.Sprintf("%d", m.Precision.ToProtoVal()) | ||
} | ||
|
||
func (s *IntervalDayType) BaseString() string { | ||
return "interval_day" | ||
} | ||
|
||
func (m *IntervalDayType) GetPrecision() TimePrecision { | ||
return m.Precision | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/substrait-io/substrait-go/proto" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
) | ||
|
||
func TestIntervalDayType(t *testing.T) { | ||
allPossibleTimePrecision := []TimePrecision{PrecisionSeconds, PrecisionDeciSeconds, PrecisionCentiSeconds, PrecisionMilliSeconds, | ||
PrecisionEMinus4Seconds, PrecisionEMinus5Seconds, PrecisionMicroSeconds, PrecisionEMinus7Seconds, PrecisionEMinus8Seconds, PrecisionNanoSeconds} | ||
allPossibleNullability := []Nullability{NullabilityUnspecified, NullabilityNullable, NullabilityRequired} | ||
|
||
for _, precision := range allPossibleTimePrecision { | ||
for _, nullability := range allPossibleNullability { | ||
expectedIntervalDayType := &IntervalDayType{Precision: precision, Nullability: nullability} | ||
expectedFormatString := fmt.Sprintf("%s<%d>", strNullable(expectedIntervalDayType), precision.ToProtoVal()) | ||
// verify IntervalDayType | ||
createdIntervalDayTypeIfc := (&IntervalDayType{Precision: precision}).WithNullability(nullability) | ||
createdIntervalDayType := createdIntervalDayTypeIfc.(*IntervalDayType) | ||
assert.True(t, createdIntervalDayType.Equals(expectedIntervalDayType)) | ||
assert.Equal(t, expectedProtoValMap[precision], createdIntervalDayType.GetPrecisionProtoVal()) | ||
assert.Equal(t, nullability, createdIntervalDayType.GetNullability()) | ||
assert.Zero(t, createdIntervalDayType.GetTypeVariationReference()) | ||
assert.Equal(t, fmt.Sprintf("interval_day%s", expectedFormatString), createdIntervalDayType.String()) | ||
assert.Equal(t, "iday", createdIntervalDayType.ShortString()) | ||
assertIntervalDayTypeProto(t, precision, nullability, createdIntervalDayType) | ||
} | ||
} | ||
} | ||
|
||
func assertIntervalDayTypeProto(t *testing.T, expectedPrecision TimePrecision, expectedNullability Nullability, | ||
toVerifyType *IntervalDayType) { | ||
|
||
expectedPrecisionProtoVal := expectedPrecision.ToProtoVal() | ||
expectedTypeProto := &proto.Type{Kind: &proto.Type_IntervalDay_{ | ||
IntervalDay: &proto.Type_IntervalDay{ | ||
Precision: &expectedPrecisionProtoVal, | ||
Nullability: expectedNullability, | ||
}, | ||
}} | ||
if diff := cmp.Diff(toVerifyType.ToProto(), expectedTypeProto, protocmp.Transform()); diff != "" { | ||
t.Errorf("IntervalDayType proto didn't match, diff:\n%v", diff) | ||
} | ||
|
||
expectedFuncArgProto := &proto.FunctionArgument{ArgType: &proto.FunctionArgument_Type{ | ||
Type: expectedTypeProto, | ||
}} | ||
if diff := cmp.Diff(toVerifyType.ToProtoFuncArg(), expectedFuncArgProto, protocmp.Transform()); diff != "" { | ||
t.Errorf("IntervalDayType func arg proto didn't match, diff:\n%v", diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters