Skip to content

Commit

Permalink
Add MatchWithNullability MatchWithoutNullability to FuncDefArgType in…
Browse files Browse the repository at this point in the history
…terface

* Also updated go version to 1.22 to fix golint error with reflect API TypeFor
  • Loading branch information
anshuldata committed Sep 12, 2024
1 parent 4265a21 commit 2240ec9
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 7 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module github.com/substrait-io/substrait-go

go 1.21
go 1.22

require (
github.com/alecthomas/participle/v2 v2.0.0
Expand All @@ -24,7 +24,6 @@ require (
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5Jzep
github.com/alecthomas/participle/v2 v2.0.0/go.mod h1:rAKZdJldHu8084ojcWevWAL8KmEU+AT+Olodb+WoN2Y=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -57,8 +55,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down
8 changes: 8 additions & 0 deletions types/any_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func (s AnyType) GetParameterizedParams() []interface{} {
// any type doesn't have any abstract parameters
return nil
}

func (m AnyType) MatchWithNullability(ot Type) bool {
return m.Nullability == ot.GetNullability()
}

func (m AnyType) MatchWithoutNullability(ot Type) bool {
return true
}
2 changes: 1 addition & 1 deletion types/integer_parameters/concrete_int_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewConcreteIntParam(v int32) IntegerParameter {

func (m *ConcreteIntParam) IsCompatible(o IntegerParameter) bool {
if t, ok := o.(*ConcreteIntParam); ok {
return t == m
return *t == *m
}
return false
}
Expand Down
16 changes: 16 additions & 0 deletions types/parameterized_decimal_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ func (m *ParameterizedDecimalType) GetParameterizedParams() []interface{} {
}
return params
}

func (m *ParameterizedDecimalType) MatchWithNullability(ot Type) bool {
if m.Nullability != ot.GetNullability() {
return false
}
return m.MatchWithoutNullability(ot)
}

func (m *ParameterizedDecimalType) MatchWithoutNullability(ot Type) bool {
if odt, ok := ot.(*DecimalType); ok {
concretePrecision := integer_parameters.NewConcreteIntParam(odt.Precision)
concreteScale := integer_parameters.NewConcreteIntParam(odt.Scale)
return m.Precision.IsCompatible(concretePrecision) && m.Scale.IsCompatible(concreteScale)
}
return false
}
18 changes: 18 additions & 0 deletions types/parameterized_list_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ func (m *ParameterizedListType) GetParameterizedParams() []interface{} {
}
return []interface{}{m.Type}
}

func (m *ParameterizedListType) MatchWithNullability(ot Type) bool {
if m.Nullability != ot.GetNullability() {
return false
}
if olt, ok := ot.(*ListType); ok {
result := m.Type.MatchWithNullability(olt.Type)
return result
}
return false
}

func (m *ParameterizedListType) MatchWithoutNullability(ot Type) bool {
if olt, ok := ot.(*ListType); ok {
return m.Type.MatchWithoutNullability(olt.Type)
}
return false
}
17 changes: 17 additions & 0 deletions types/parameterized_map_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func (m *ParameterizedMapType) GetParameterizedParams() []interface{} {
}
return abstractParams
}

func (m *ParameterizedMapType) MatchWithNullability(ot Type) bool {
if m.Nullability != ot.GetNullability() {
return false
}
if omt, ok := ot.(*MapType); ok {
return m.Key.MatchWithNullability(omt.Key) && m.Value.MatchWithNullability(omt.Value)
}
return false

Check warning on line 54 in types/parameterized_map_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_map_type.go#L54

Added line #L54 was not covered by tests
}

func (m *ParameterizedMapType) MatchWithoutNullability(ot Type) bool {
if omt, ok := ot.(*MapType); ok {
return m.Key.MatchWithoutNullability(omt.Key) && m.Value.MatchWithoutNullability(omt.Value)
}
return false

Check warning on line 61 in types/parameterized_map_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_map_type.go#L61

Added line #L61 was not covered by tests
}
22 changes: 22 additions & 0 deletions types/parameterized_single_integer_param_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ func (m *parameterizedTypeSingleIntegerParam[T]) GetParameterizedParams() []inte
}
return []interface{}{m.IntegerOption}
}

func (m *parameterizedTypeSingleIntegerParam[T]) MatchWithNullability(ot Type) bool {
if m.Nullability != ot.GetNullability() {
return false
}
return m.MatchWithoutNullability(ot)
}

func (m *parameterizedTypeSingleIntegerParam[T]) MatchWithoutNullability(ot Type) bool {
if reflect.TypeFor[T]() != reflect.TypeOf(ot) {
return false

Check warning on line 67 in types/parameterized_single_integer_param_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_single_integer_param_type.go#L67

Added line #L67 was not covered by tests
}
if odt, ok := ot.(FixedType); ok {
concreteLength := integer_parameters.NewConcreteIntParam(odt.GetLength())
return m.IntegerOption.IsCompatible(concreteLength)
}
if odt, ok := ot.(timestampPrecisionType); ok {
concreteLength := integer_parameters.NewConcreteIntParam(odt.GetPrecision().ToProtoVal())
return m.IntegerOption.IsCompatible(concreteLength)
}
return false

Check warning on line 77 in types/parameterized_single_integer_param_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_single_integer_param_type.go#L77

Added line #L77 was not covered by tests
}
33 changes: 33 additions & 0 deletions types/parameterized_struct_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ func (m *ParameterizedStructType) GetParameterizedParams() []interface{} {
}
return abstractParams
}

func (m *ParameterizedStructType) MatchWithNullability(ot Type) bool {
if m.Nullability != ot.GetNullability() {
return false
}
if omt, ok := ot.(*StructType); ok {
if len(m.Types) != len(omt.Types) {
return false
}
for i, typ := range m.Types {
if !typ.MatchWithNullability(omt.Types[i]) {
return false
}
}
return true
}
return false
}

func (m *ParameterizedStructType) MatchWithoutNullability(ot Type) bool {
if omt, ok := ot.(*StructType); ok {
if len(m.Types) != len(omt.Types) {
return false
}
for i, typ := range m.Types {
if !typ.MatchWithoutNullability(omt.Types[i]) {
return false
}
}
return true
}
return false
}
4 changes: 4 additions & 0 deletions types/precison_timestamp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func (m *PrecisionTimestampType) BaseString() string {
return typeNames[reflect.TypeOf(m)]
}

func (m *PrecisionTimestampType) GetPrecision() TimePrecision {
return m.Precision
}

// PrecisionTimestampTzType this is used to represent a type of Precision timestamp with TimeZone
type PrecisionTimestampTzType struct {
PrecisionTimestampType
Expand Down
33 changes: 33 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,26 @@ type (
// GetParameterizedParams returns all parameterized parameters
// it doesn't return concrete parameters
GetParameterizedParams() []interface{}

// MatchWithNullability This API return true if Type argument
// is compatible with this param otherwise it returns false.
// This method expects that nullability of argument is same as this type.
MatchWithNullability(ot Type) bool
// MatchWithoutNullability This API return true if Type argument
// is compatible with this param otherwise it returns false.
// This method ignores nullability for matching.
MatchWithoutNullability(ot Type) bool
}

FixedType interface {
CompositeType
WithLength(int32) FixedType
GetLength() int32
}

timestampPrecisionType interface {
CompositeType
GetPrecision() TimePrecision
}
)

Expand Down Expand Up @@ -644,6 +659,20 @@ func (s *PrimitiveType[T]) SetNullability(n Nullability) FuncDefArgType {
return s
}

func (s *PrimitiveType[T]) MatchWithNullability(ot Type) bool {
if s.Nullability != ot.GetNullability() {
return false
}
return s.MatchWithoutNullability(ot)
}

func (s *PrimitiveType[T]) MatchWithoutNullability(ot Type) bool {
if _, ok := ot.(*PrimitiveType[T]); ok {
return true
}
return false
}

// create type aliases to the generic structs
type (
BooleanType = PrimitiveType[bool]
Expand Down Expand Up @@ -730,6 +759,10 @@ func (s *FixedLenType[T]) WithLength(length int32) FixedType {
return &out
}

func (s *FixedLenType[T]) GetLength() int32 {
return s.Length
}

// DecimalType is a decimal type with concrete precision and scale parameters, e.g. Decimal(10, 2).
type DecimalType struct {
Nullability Nullability
Expand Down
Loading

0 comments on commit 2240ec9

Please sign in to comment.