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

Support for Parameterized type #52

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions extensions/simple_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package extensions

import (
"errors"
"fmt"
"reflect"
"strings"

substraitgo "github.com/substrait-io/substrait-go"
"github.com/substrait-io/substrait-go/types"
"github.com/substrait-io/substrait-go/types/parser"
)

Expand Down Expand Up @@ -57,6 +59,7 @@

type Argument interface {
toTypeString() string
ArgType() (types.Type, error)
anshuldata marked this conversation as resolved.
Show resolved Hide resolved
}

type EnumArg struct {
Expand All @@ -69,6 +72,10 @@
return "req"
}

func (EnumArg) ArgType() (types.Type, error) {
return nil, errors.New("unimplemented")

Check warning on line 76 in extensions/simple_extension.go

View check run for this annotation

Codecov / codecov/patch

extensions/simple_extension.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

type ValueArg struct {
Name string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Expand All @@ -80,6 +87,10 @@
return v.Value.Expr.(*parser.Type).ShortType()
}

func (v ValueArg) ArgType() (types.Type, error) {
return v.Value.Expr.(*parser.Type).Type()

Check warning on line 91 in extensions/simple_extension.go

View check run for this annotation

Codecov / codecov/patch

extensions/simple_extension.go#L90-L91

Added lines #L90 - L91 were not covered by tests
}

type TypeArg struct {
Name string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Expand All @@ -88,6 +99,10 @@

func (TypeArg) toTypeString() string { return "type" }

func (TypeArg) ArgType() (types.Type, error) {
return nil, errors.New("unimplemented")

Check warning on line 103 in extensions/simple_extension.go

View check run for this annotation

Codecov / codecov/patch

extensions/simple_extension.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}

type ArgumentList []Argument

func (a *ArgumentList) UnmarshalYAML(fn func(interface{}) error) error {
Expand Down
35 changes: 35 additions & 0 deletions types/any_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package types
anshuldata marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
)

// AnyType to represent AnyType, this type is to indicate "any" type of argument
// This type is not used in function invocation. It is only used in function definition
type AnyType struct {
Name string
TypeVariationRef uint32
Nullability Nullability
}

func (AnyType) isRootRef() {}
func (m AnyType) WithNullability(nullability Nullability) Type {
m.Nullability = nullability
return m

Check warning on line 18 in types/any_type.go

View check run for this annotation

Codecov / codecov/patch

types/any_type.go#L15-L18

Added lines #L15 - L18 were not covered by tests
}
func (m AnyType) GetType() Type { return m }

Check warning on line 20 in types/any_type.go

View check run for this annotation

Codecov / codecov/patch

types/any_type.go#L20

Added line #L20 was not covered by tests
func (m AnyType) GetNullability() Nullability {
return m.Nullability
}
func (m AnyType) GetTypeVariationReference() uint32 {
return m.TypeVariationRef

Check warning on line 25 in types/any_type.go

View check run for this annotation

Codecov / codecov/patch

types/any_type.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}
func (AnyType) Equals(rhs Type) bool {

Check warning on line 27 in types/any_type.go

View check run for this annotation

Codecov / codecov/patch

types/any_type.go#L27

Added line #L27 was not covered by tests
// equal to every other type
return true

Check warning on line 29 in types/any_type.go

View check run for this annotation

Codecov / codecov/patch

types/any_type.go#L29

Added line #L29 was not covered by tests
}

func (t AnyType) ShortString() string { return t.Name }
func (t AnyType) String() string {
return fmt.Sprintf("%s%s", t.Name, strNullable(t))
}
32 changes: 32 additions & 0 deletions types/any_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types_test
anshuldata marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/substrait-io/substrait-go/types"
)

func TestAnyType(t *testing.T) {
for _, td := range []struct {
testName string
argName string
nullability types.Nullability
expectedString string
}{
{"any", "any", types.NullabilityNullable, "any?"},
{"anyrequired", "any", types.NullabilityRequired, "any"},
{"anyOtherName", "any1", types.NullabilityNullable, "any1?"},
{"T name", "T", types.NullabilityNullable, "T?"},
} {
t.Run(td.testName, func(t *testing.T) {
arg := &types.AnyType{
Name: td.argName,
Nullability: td.nullability,
}
require.Equal(t, td.expectedString, arg.String())
require.Equal(t, td.nullability, arg.GetNullability())
require.Equal(t, td.argName, arg.ShortString())
})
}
}
44 changes: 44 additions & 0 deletions types/parameterized_decimal_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package types

import (
"fmt"
)

// ParameterizedDecimalType is a decimal type with precision and scale parameters of string type
// example: Decimal(P,S). Kindly note concrete types Decimal(10, 2) are not represented by this type
// Concrete type is represented by DecimalType
type ParameterizedDecimalType struct {
anshuldata marked this conversation as resolved.
Show resolved Hide resolved
Nullability Nullability
TypeVariationRef uint32
Precision IntegerParam
Scale IntegerParam
}

func (ParameterizedDecimalType) isRootRef() {}
func (m ParameterizedDecimalType) WithNullability(n Nullability) Type {
m.Nullability = n
return m

Check warning on line 20 in types/parameterized_decimal_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_decimal_type.go#L17-L20

Added lines #L17 - L20 were not covered by tests
}

func (m ParameterizedDecimalType) GetType() Type { return m }

Check warning on line 23 in types/parameterized_decimal_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_decimal_type.go#L23

Added line #L23 was not covered by tests
func (m ParameterizedDecimalType) GetNullability() Nullability { return m.Nullability }
func (m ParameterizedDecimalType) GetTypeVariationReference() uint32 {
return m.TypeVariationRef

Check warning on line 26 in types/parameterized_decimal_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_decimal_type.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}
func (m ParameterizedDecimalType) Equals(rhs Type) bool {
if o, ok := rhs.(ParameterizedDecimalType); ok {
return o == m
}
return false

Check warning on line 32 in types/parameterized_decimal_type.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_decimal_type.go#L32

Added line #L32 was not covered by tests
}

func (m ParameterizedDecimalType) ShortString() string {
t := DecimalType{}
return t.ShortString()
}

func (m ParameterizedDecimalType) String() string {
t := DecimalType{}
parameterString := fmt.Sprintf("<%s,%s>", m.Precision.String(), m.Scale.String())
return fmt.Sprintf("%s%s%s", t.BaseString(), strNullable(m), parameterString)
}
101 changes: 101 additions & 0 deletions types/parameterized_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package types

import (
"fmt"
)

// IntegerParam represents a single integer parameter for a parameterized type
// Example: VARCHAR(L1) -> L1 is the integer parameter
type IntegerParam struct {
anshuldata marked this conversation as resolved.
Show resolved Hide resolved
anshuldata marked this conversation as resolved.
Show resolved Hide resolved
Name string
}

func (m IntegerParam) Equals(o IntegerParam) bool {
return m == o

Check warning on line 14 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L13-L14

Added lines #L13 - L14 were not covered by tests
}

func (m IntegerParam) String() string {
return m.Name
}

// ParameterizedTypeSingleIntegerParam This is a generic type to represent parameterized type with a single integer parameter
type ParameterizedTypeSingleIntegerParam[T VarCharType | FixedCharType | FixedBinaryType | PrecisionTimestampType | PrecisionTimestampTzType] struct {
anshuldata marked this conversation as resolved.
Show resolved Hide resolved
Nullability Nullability
TypeVariationRef uint32
IntegerOption IntegerParam
}

func (m ParameterizedTypeSingleIntegerParam[T]) WithIntegerOption(integerOption IntegerParam) Type {
m.IntegerOption = integerOption
return m
}

func (ParameterizedTypeSingleIntegerParam[T]) isRootRef() {}

Check warning on line 33 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L33

Added line #L33 was not covered by tests
func (m ParameterizedTypeSingleIntegerParam[T]) WithNullability(n Nullability) Type {
m.Nullability = n
return m
}

func (m ParameterizedTypeSingleIntegerParam[T]) GetType() Type { return m }

Check warning on line 39 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L39

Added line #L39 was not covered by tests
func (m ParameterizedTypeSingleIntegerParam[T]) GetNullability() Nullability { return m.Nullability }
func (m ParameterizedTypeSingleIntegerParam[T]) GetTypeVariationReference() uint32 {
return m.TypeVariationRef

Check warning on line 42 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}
func (m ParameterizedTypeSingleIntegerParam[T]) Equals(rhs Type) bool {
if o, ok := rhs.(ParameterizedTypeSingleIntegerParam[T]); ok {
return o == m
}
return false

Check warning on line 48 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L48

Added line #L48 was not covered by tests
}

func (m ParameterizedTypeSingleIntegerParam[T]) ShortString() string {
switch any(m).(type) {
case ParameterizedVarCharType:
t := VarCharType{}
return t.ShortString()
case ParameterizedFixedCharType:
t := FixedCharType{}
return t.ShortString()
case ParameterizedFixedBinaryType:
t := FixedBinaryType{}
return t.ShortString()
case ParameterizedPrecisionTimestampType:
t := PrecisionTimestampType{}
return t.ShortString()
case ParameterizedPrecisionTimestampTzType:
t := PrecisionTimestampTzType{}
return t.ShortString()
default:
panic("unknown type")

Check warning on line 69 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}
}

func (m ParameterizedTypeSingleIntegerParam[T]) String() string {
return fmt.Sprintf("%s%s%s", m.baseString(), strNullable(m), m.parameterString())
}

func (m ParameterizedTypeSingleIntegerParam[T]) parameterString() string {
return fmt.Sprintf("<%s>", m.IntegerOption.String())
}

func (m ParameterizedTypeSingleIntegerParam[T]) baseString() string {
switch any(m).(type) {
case ParameterizedVarCharType:
t := VarCharType{}
return t.BaseString()
case ParameterizedFixedCharType:
t := FixedCharType{}
return t.BaseString()
case ParameterizedFixedBinaryType:
t := FixedBinaryType{}
return t.BaseString()
case ParameterizedPrecisionTimestampType:
t := PrecisionTimestampType{}
return t.BaseString()
case ParameterizedPrecisionTimestampTzType:
t := PrecisionTimestampTzType{}
return t.BaseString()
default:
panic("unknown type")

Check warning on line 99 in types/parameterized_types.go

View check run for this annotation

Codecov / codecov/patch

types/parameterized_types.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}
}
63 changes: 63 additions & 0 deletions types/parameterized_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/substrait-io/substrait-go/types"
)

func TestParameterizedVarCharType(t *testing.T) {
for _, td := range []struct {
name string
typ types.ParameterizedSingleIntegerType
nullability types.Nullability
integerOption types.IntegerParam
expectedString string
expectedBaseString string
expectedShortString string
}{
{"nullable varchar", &types.ParameterizedVarCharType{}, types.NullabilityNullable, types.IntegerParam{Name: "L1"}, "varchar?<L1>", "varchar", "vchar"},
{"non nullable varchar", &types.ParameterizedVarCharType{}, types.NullabilityRequired, types.IntegerParam{Name: "L1"}, "varchar<L1>", "varchar", "vchar"},
{"nullable fixChar", &types.ParameterizedFixedCharType{}, types.NullabilityNullable, types.IntegerParam{Name: "L1"}, "char?<L1>", "char", "fchar"},
{"non nullable fixChar", &types.ParameterizedFixedCharType{}, types.NullabilityRequired, types.IntegerParam{Name: "L1"}, "char<L1>", "char", "fchar"},
{"nullable fixBinary", &types.ParameterizedFixedBinaryType{}, types.NullabilityNullable, types.IntegerParam{Name: "L1"}, "fixedbinary?<L1>", "fixedbinary", "fbin"},
{"non nullable fixBinary", &types.ParameterizedFixedBinaryType{}, types.NullabilityRequired, types.IntegerParam{Name: "L1"}, "fixedbinary<L1>", "fixedbinary", "fbin"},
{"nullable precisionTimeStamp", &types.ParameterizedPrecisionTimestampType{}, types.NullabilityNullable, types.IntegerParam{Name: "L1"}, "precision_timestamp?<L1>", "precision_timestamp", "prets"},
{"non nullable precisionTimeStamp", &types.ParameterizedPrecisionTimestampType{}, types.NullabilityRequired, types.IntegerParam{Name: "L1"}, "precision_timestamp<L1>", "precision_timestamp", "prets"},
{"nullable precisionTimeStampTz", &types.ParameterizedPrecisionTimestampTzType{}, types.NullabilityNullable, types.IntegerParam{Name: "L1"}, "precision_timestamp_tz?<L1>", "precision_timestamp_tz", "pretstz"},
{"non nullable precisionTimeStampTz", &types.ParameterizedPrecisionTimestampTzType{}, types.NullabilityRequired, types.IntegerParam{Name: "L1"}, "precision_timestamp_tz<L1>", "precision_timestamp_tz", "pretstz"},
} {
t.Run(td.name, func(t *testing.T) {
pt := td.typ.WithIntegerOption(td.integerOption).WithNullability(td.nullability)
require.Equal(t, td.expectedString, pt.String())
require.Equal(t, td.expectedShortString, pt.ShortString())
require.True(t, pt.Equals(pt))
})
}
}

func TestParameterizedDecimalType(t *testing.T) {
for _, td := range []struct {
name string
precision string
scale string
nullability types.Nullability
expectedString string
expectedBaseString string
expectedShortString string
}{
{"nullable decimal", "P", "S", types.NullabilityNullable, "decimal?<P,S>", "decimal", "dec"},
{"non nullable decimal", "P", "S", types.NullabilityRequired, "decimal<P,S>", "decimal", "dec"},
} {
t.Run(td.name, func(t *testing.T) {
precision := types.IntegerParam{Name: td.precision}
scale := types.IntegerParam{Name: td.scale}
pt := types.ParameterizedDecimalType{Precision: precision, Scale: scale, Nullability: td.nullability}
require.Equal(t, td.expectedString, pt.String())
//require.Equal(t, td.expectedBaseString, pt.BaseString())
require.Equal(t, td.expectedShortString, pt.ShortString())
require.True(t, pt.Equals(pt))
})
}
}
Loading
Loading