<commit_before>package udf

import (
	"errors"
	"fmt"
	"math"
	"pfi/sensorbee/sensorbee/core"
	"pfi/sensorbee/sensorbee/data"
	"reflect"
	"time"
)

// ConvertGeneric creates a new UDF from various form of functions. Arguments
// of the function don't have to be tuple types, but some standard types are
// allowed. The UDF returned provide a weak type conversion, that is it uses
// data.To{Type} function to convert values. Therefore, a string may be
// passed as an integer or vice versa. If the function wants to provide
// strict type conversion, generate UDF by Func function.
//
// Acceptable types:
//	- bool
//	- standard integers
//	- standard floats
//	- string
//	- time.Time
//	- data.Bool, data.Int, data.Float, data.String, data.Blob,
//	  data.Timestamp, data.Array, data.Map, data.Value
//	- a slice of types above
func ConvertGeneric(function interface{}) (UDF, error) {
	t := reflect.TypeOf(function)
	if t.Kind() != reflect.Func {
		return nil, errors.New("the argument must be a function")
	}

	numArgs := t.NumIn()
	if genericFuncHasContext(t) {
		numArgs -= 1
	}

	return ConvertGenericAggregate(function, make([]bool, numArgs))
}

func MustConvertGeneric(function interface{}) UDF {
	f, err := ConvertGeneric(function)
	if err != nil {
		panic(err)
	}
	return f
}

// ConvertGenericAggregate creates a new aggregate UDF from various form of
// functions. aggParams arguement is used where the argument of function
// receives aggregation parameter.
// Supported and acceptable types are the same as ConvertGeneric.<commit_msg>Fix godoc comment for ConvertGenericAggregate<commit_after>package udf

import (
	"errors"
	"fmt"
	"math"
	"pfi/sensorbee/sensorbee/core"
	"pfi/sensorbee/sensorbee/data"
	"reflect"
	"time"
)

// ConvertGeneric creates a new UDF from various form of functions. Arguments
// of the function don't have to be tuple types, but some standard types are
// allowed. The UDF returned provide a weak type conversion, that is it uses
// data.To{Type} function to convert values. Therefore, a string may be
// passed as an integer or vice versa. If the function wants to provide
// strict type conversion, generate UDF by Func function.
//
// Acceptable types:
//	- bool
//	- standard integers
//	- standard floats
//	- string
//	- time.Time
//	- data.Bool, data.Int, data.Float, data.String, data.Blob,
//	  data.Timestamp, data.Array, data.Map, data.Value
//	- a slice of types above
func ConvertGeneric(function interface{}) (UDF, error) {
	t := reflect.TypeOf(function)
	if t.Kind() != reflect.Func {
		return nil, errors.New("the argument must be a function")
	}

	numArgs := t.NumIn()
	if genericFuncHasContext(t) {
		numArgs -= 1
	}

	return ConvertGenericAggregate(function, make([]bool, numArgs))
}

func MustConvertGeneric(function interface{}) UDF {
	f, err := ConvertGeneric(function)
	if err != nil {
		panic(err)
	}
	return f
}

// ConvertGenericAggregate creates a new aggregate UDF from various form of
// functions. aggParams argument is used to indicate which arguments of the
// function are aggregation parameter.
// receives aggregation parameter.
// Supported and acceptable types are the same as ConvertGeneric.
{'fraction_non_alphanumeric': 0.10876418663303909, 'fraction_numerical': 0.0006305170239596469, 'mean_word_length': 3.7217261904761907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7184766', 'n_tokens_mistral': 973, 'n_tokens_neox': 951, 'n_words': 358}