Skip to content

Commit

Permalink
started to add feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Sep 18, 2024
1 parent bfef4ee commit 9f8546d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 170 deletions.
40 changes: 18 additions & 22 deletions scalar_udf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ import (
"unsafe"
)

type ScalarFuncConfig interface {
InputTypeInfos() []TypeInfo
ResultTypeInfo() TypeInfo
}
type ScalarFuncConfig struct {
InputTypeInfos []TypeInfo
ResultTypeInfo TypeInfo

type ScalarFuncExtraInfo interface {
VariadicTypeInfo() TypeInfo
Volatile() bool
SpecialNullHandling() bool
VariadicTypeInfo *TypeInfo
Volatile bool
SpecialNullHandling bool
}

type ScalarFunc interface {
Config() ScalarFuncConfig
ExtraInfo() ScalarFuncExtraInfo
ExecuteRow(args []driver.Value) (any, error)
}

Expand Down Expand Up @@ -100,24 +97,24 @@ func scalar_udf_delete_callback(extraInfo unsafe.Pointer) {
h.Delete()
}

func registerInputParams(config ScalarFuncConfig, extraInfo ScalarFuncExtraInfo, f C.duckdb_scalar_function) error {
func registerInputParams(config ScalarFuncConfig, f C.duckdb_scalar_function) error {
// Set variadic input parameters.
if extraInfo != nil && extraInfo.VariadicTypeInfo() != nil {
t := extraInfo.VariadicTypeInfo().logicalType()
if config.VariadicTypeInfo != nil {
t := (*config.VariadicTypeInfo).logicalType()
C.duckdb_scalar_function_set_varargs(f, t)
C.duckdb_destroy_logical_type(&t)
return nil
}

// Set normal input parameters.
if config.InputTypeInfos() == nil {
if config.InputTypeInfos == nil {
return errScalarUDFNilInputTypes
}
if len(config.InputTypeInfos()) == 0 {
if len(config.InputTypeInfos) == 0 {
return errScalarUDFEmptyInputTypes
}

for i, info := range config.InputTypeInfos() {
for i, info := range config.InputTypeInfos {
if info == nil {
return addIndexToError(errScalarUDFInputTypeIsNil, i)
}
Expand All @@ -129,13 +126,13 @@ func registerInputParams(config ScalarFuncConfig, extraInfo ScalarFuncExtraInfo,
}

func registerResultParams(config ScalarFuncConfig, f C.duckdb_scalar_function) error {
if config.ResultTypeInfo() == nil {
if config.ResultTypeInfo == nil {
return errScalarUDFResultTypeIsNil
}
if config.ResultTypeInfo().InternalType() == TYPE_ANY {
if config.ResultTypeInfo.InternalType() == TYPE_ANY {
return errScalarUDFResultTypeIsANY
}
t := config.ResultTypeInfo().logicalType()
t := config.ResultTypeInfo.logicalType()
C.duckdb_scalar_function_set_return_type(f, t)
C.duckdb_destroy_logical_type(&t)
return nil
Expand All @@ -157,17 +154,16 @@ func createScalarFunc(name string, f ScalarFunc) (C.duckdb_scalar_function, erro

// Configure the scalar function.
config := f.Config()
extraInfo := f.ExtraInfo()
if err := registerInputParams(config, extraInfo, function); err != nil {
if err := registerInputParams(config, function); err != nil {
return nil, err
}
if err := registerResultParams(config, function); err != nil {
return nil, err
}
if extraInfo != nil && extraInfo.SpecialNullHandling() {
if config.SpecialNullHandling {
C.duckdb_scalar_function_set_special_handling(function)
}
if extraInfo != nil && extraInfo.Volatile() {
if config.Volatile {
C.duckdb_scalar_function_set_volatile(function)
}

Expand Down
187 changes: 40 additions & 147 deletions scalar_udf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@ var currentInfo TypeInfo

type simpleSUDF struct{}

type simpleSUDFConfig struct{}

func (*simpleSUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{currentInfo, currentInfo}
}

func (*simpleSUDFConfig) ResultTypeInfo() TypeInfo {
return currentInfo
}

func (*simpleSUDF) Config() ScalarFuncConfig {
return &simpleSUDFConfig{}
}

func (*simpleSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{currentInfo, currentInfo},
ResultTypeInfo: currentInfo,
}
}

func (*simpleSUDF) ExecuteRow(args []driver.Value) (any, error) {
Expand Down Expand Up @@ -74,22 +63,11 @@ func TestSimpleScalarUDF(t *testing.T) {

type typesSUDF struct{}

type typesSUDFConfig struct{}

func (*typesSUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{currentInfo}
}

func (*typesSUDFConfig) ResultTypeInfo() TypeInfo {
return currentInfo
}

func (*typesSUDF) Config() ScalarFuncConfig {
return &typesSUDFConfig{}
}

func (*typesSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{currentInfo},
ResultTypeInfo: currentInfo,
}
}

func (*typesSUDF) ExecuteRow(args []driver.Value) (any, error) {
Expand Down Expand Up @@ -158,36 +136,13 @@ func TestScalarUDFSet(t *testing.T) {

type variadicSUDF struct{}

type variadicSUDFConfig struct{}

type variadicSUDFExtraInfo struct{}

func (*variadicSUDFConfig) InputTypeInfos() []TypeInfo {
return nil
}

func (*variadicSUDFConfig) ResultTypeInfo() TypeInfo {
return currentInfo
}

func (*variadicSUDFExtraInfo) VariadicTypeInfo() TypeInfo {
return currentInfo
}

func (*variadicSUDFExtraInfo) Volatile() bool {
return true
}

func (*variadicSUDFExtraInfo) SpecialNullHandling() bool {
return true
}

func (*variadicSUDF) Config() ScalarFuncConfig {
return &variadicSUDFConfig{}
}

func (*variadicSUDF) ExtraInfo() ScalarFuncExtraInfo {
return &variadicSUDFExtraInfo{}
return ScalarFuncConfig{
ResultTypeInfo: currentInfo,
VariadicTypeInfo: &currentInfo,
Volatile: true,
SpecialNullHandling: true,
}
}

func (*variadicSUDF) ExecuteRow(args []driver.Value) (any, error) {
Expand Down Expand Up @@ -242,30 +197,17 @@ func TestVariadicScalarUDF(t *testing.T) {

type anyTypeSUDF struct{}

type anyTypeSUDFExtraInfo struct{}

func (*anyTypeSUDFExtraInfo) VariadicTypeInfo() TypeInfo {
func (*anyTypeSUDF) Config() ScalarFuncConfig {
info, err := NewTypeInfo(TYPE_ANY)
if err != nil {
panic(err)
}
return info
}

func (*anyTypeSUDFExtraInfo) Volatile() bool {
return true
}

func (*anyTypeSUDFExtraInfo) SpecialNullHandling() bool {
return true
}

func (*anyTypeSUDF) Config() ScalarFuncConfig {
return &variadicSUDFConfig{}
}

func (*anyTypeSUDF) ExtraInfo() ScalarFuncExtraInfo {
return &anyTypeSUDFExtraInfo{}
return ScalarFuncConfig{
ResultTypeInfo: currentInfo,
VariadicTypeInfo: &info,
SpecialNullHandling: true,
}
}

func (*anyTypeSUDF) ExecuteRow(args []driver.Value) (any, error) {
Expand Down Expand Up @@ -320,11 +262,9 @@ func TestANYScalarUDF(t *testing.T) {
type errInputSUDF struct{}

func (*errInputSUDF) Config() ScalarFuncConfig {
return &variadicSUDFConfig{}
}

func (*errInputSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
ResultTypeInfo: currentInfo,
}
}

func (*errInputSUDF) ExecuteRow([]driver.Value) (any, error) {
Expand All @@ -333,22 +273,11 @@ func (*errInputSUDF) ExecuteRow([]driver.Value) (any, error) {

type errEmptyInputSUDF struct{}

type errEmptyInputSUDFConfig struct{}

func (*errEmptyInputSUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{}
}

func (*errEmptyInputSUDFConfig) ResultTypeInfo() TypeInfo {
return currentInfo
}

func (*errEmptyInputSUDF) Config() ScalarFuncConfig {
return &errEmptyInputSUDFConfig{}
}

func (*errEmptyInputSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{},
ResultTypeInfo: currentInfo,
}
}

func (*errEmptyInputSUDF) ExecuteRow([]driver.Value) (any, error) {
Expand All @@ -357,22 +286,11 @@ func (*errEmptyInputSUDF) ExecuteRow([]driver.Value) (any, error) {

type errInputNilSUDF struct{}

type errInputNilSUDFConfig struct{}

func (*errInputNilSUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{nil}
}

func (*errInputNilSUDFConfig) ResultTypeInfo() TypeInfo {
return currentInfo
}

func (*errInputNilSUDF) Config() ScalarFuncConfig {
return &errInputNilSUDFConfig{}
}

func (*errInputNilSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{nil},
ResultTypeInfo: currentInfo,
}
}

func (*errInputNilSUDF) ExecuteRow([]driver.Value) (any, error) {
Expand All @@ -381,22 +299,11 @@ func (*errInputNilSUDF) ExecuteRow([]driver.Value) (any, error) {

type errResultNilSUDF struct{}

type errResultNilSUDFConfig struct{}

func (*errResultNilSUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{currentInfo}
}

func (*errResultNilSUDFConfig) ResultTypeInfo() TypeInfo {
return nil
}

func (*errResultNilSUDF) Config() ScalarFuncConfig {
return &errResultNilSUDFConfig{}
}

func (*errResultNilSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{currentInfo},
ResultTypeInfo: nil,
}
}

func (*errResultNilSUDF) ExecuteRow([]driver.Value) (any, error) {
Expand All @@ -405,26 +312,16 @@ func (*errResultNilSUDF) ExecuteRow([]driver.Value) (any, error) {

type errResultAnySUDF struct{}

type errResultAnySUDFConfig struct{}

func (*errResultAnySUDFConfig) InputTypeInfos() []TypeInfo {
return []TypeInfo{currentInfo}
}

func (*errResultAnySUDFConfig) ResultTypeInfo() TypeInfo {
func (*errResultAnySUDF) Config() ScalarFuncConfig {
info, err := NewTypeInfo(TYPE_ANY)
if err != nil {
panic(err)
}
return info
}

func (*errResultAnySUDF) Config() ScalarFuncConfig {
return &errResultAnySUDFConfig{}
}

func (*errResultAnySUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
return ScalarFuncConfig{
InputTypeInfos: []TypeInfo{currentInfo},
ResultTypeInfo: info,
}
}

func (*errResultAnySUDF) ExecuteRow([]driver.Value) (any, error) {
Expand All @@ -438,10 +335,6 @@ func (*errExecSUDF) Config() ScalarFuncConfig {
return scalarUDF.Config()
}

func (*errExecSUDF) ExtraInfo() ScalarFuncExtraInfo {
return nil
}

func (*errExecSUDF) ExecuteRow([]driver.Value) (any, error) {
return nil, errors.New("test invalid execution")
}
Expand Down
3 changes: 2 additions & 1 deletion vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type vector struct {
// The child vectors of nested data types.
childVectors []vector

// FIXME: this is a workaround until the C API exposes SQLNULL.
// FIXME: This is a workaround until the C API exposes SQLNULL.
// FIXME: Then, SQLNULL becomes another Type value (C.DUCKDB_TYPE_SQLNULL).
isSQLNull bool

// The vector's type information.
Expand Down

0 comments on commit 9f8546d

Please sign in to comment.