12 Commits

Author SHA1 Message Date
rulego-team 3464ff5f6e fix:unnest function to properly expand arrays into multiple rows in stream processing 2025-09-08 18:40:26 +08:00
rulego-team 1d9e2a3dab test: add test cases 2025-08-30 00:56:48 +08:00
rulego-team b23fdea2cd perf:增加代码覆盖率 2025-08-29 17:30:58 +08:00
rulego-team 696b5f7177 perf:优化sql语法解析 2025-08-29 17:30:11 +08:00
rulego-team 90afdead78 perf:重构聚合函数运算 2025-08-29 17:29:27 +08:00
rulego-team 4615b7a308 feat:支持聚合函数的后运算 #37 2025-08-29 13:49:26 +08:00
Whki de6ca91c01 Merge pull request #36 from rulego/dev
Dev
2025-08-08 17:57:53 +08:00
Whki c66d974dfc Merge pull request #35 from rulego/dev
test:add test cases
2025-08-08 09:50:08 +08:00
Whki e388bacde3 Merge pull request #34 from rulego/dev
test:add coverage test case
2025-08-08 09:07:54 +08:00
Whki 9764b95b7a Merge pull request #33 from rulego/dev
Dev
2025-08-07 19:42:59 +08:00
Whki 05a25619b8 Merge pull request #32 from rulego/dev
Dev
2025-08-06 18:20:53 +08:00
Whki a8cf91298a Merge pull request #31 from rulego/dev
Dev
2025-08-05 11:33:11 +08:00
42 changed files with 6575 additions and 1912 deletions
+24
View File
@@ -20,6 +20,7 @@ const (
WindowStart = functions.WindowStart
WindowEnd = functions.WindowEnd
Collect = functions.Collect
FirstValue = functions.FirstValue
LastValue = functions.LastValue
MergeAgg = functions.MergeAgg
StdDevS = functions.StdDevS
@@ -33,6 +34,8 @@ const (
HadChanged = functions.HadChanged
// Expression aggregator for handling custom functions
Expression = functions.Expression
// Post-aggregation marker
PostAggregation = functions.PostAggregation
)
// AggregatorFunction aggregator function interface, re-exports functions.LegacyAggregatorFunction
@@ -55,9 +58,30 @@ func CreateBuiltinAggregator(aggType AggregateType) AggregatorFunction {
}
}
// Special handling for post-aggregation type (placeholder aggregator)
if aggType == "post_aggregation" {
return &PostAggregationPlaceholder{}
}
return functions.CreateLegacyAggregator(aggType)
}
// PostAggregationPlaceholder is a placeholder aggregator for post-aggregation fields
type PostAggregationPlaceholder struct{}
func (p *PostAggregationPlaceholder) New() AggregatorFunction {
return &PostAggregationPlaceholder{}
}
func (p *PostAggregationPlaceholder) Add(value interface{}) {
// Do nothing - this is just a placeholder
}
func (p *PostAggregationPlaceholder) Result() interface{} {
// Return nil - actual result will be computed in post-processing
return nil
}
// ExpressionAggregatorWrapper wraps expression aggregator to make it compatible with LegacyAggregatorFunction interface
type ExpressionAggregatorWrapper struct {
function *functions.ExpressionAggregatorFunction
+148
View File
@@ -0,0 +1,148 @@
package aggregator
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestPostAggregationPlaceholder 测试后聚合占位符的完整功能
func TestPostAggregationPlaceholder(t *testing.T) {
t.Run("测试PostAggregationPlaceholder基本功能", func(t *testing.T) {
// 创建PostAggregationPlaceholder实例
placeholder := &PostAggregationPlaceholder{}
require.NotNil(t, placeholder)
// 测试New方法
newPlaceholder := placeholder.New()
require.NotNil(t, newPlaceholder)
assert.IsType(t, &PostAggregationPlaceholder{}, newPlaceholder)
// 测试Add方法应该不做任何操作
placeholder.Add(10)
placeholder.Add("test")
placeholder.Add(nil)
placeholder.Add([]int{1, 2, 3})
// 测试Result方法应该返回nil
result := placeholder.Result()
assert.Nil(t, result)
})
t.Run("测试通过CreateBuiltinAggregator创建PostAggregationPlaceholder", func(t *testing.T) {
// 使用CreateBuiltinAggregator创建post_aggregation类型的聚合器
aggregator := CreateBuiltinAggregator(PostAggregation)
require.NotNil(t, aggregator)
assert.IsType(t, &PostAggregationPlaceholder{}, aggregator)
// 测试创建的聚合器功能
newAgg := aggregator.New()
require.NotNil(t, newAgg)
assert.IsType(t, &PostAggregationPlaceholder{}, newAgg)
// 测试添加各种类型的值
newAgg.Add(100)
newAgg.Add("string_value")
newAgg.Add(map[string]interface{}{"key": "value"})
// 验证结果始终为nil
result := newAgg.Result()
assert.Nil(t, result)
})
t.Run("测试PostAggregationPlaceholder的多实例独立性", func(t *testing.T) {
// 创建多个实例
placeholder1 := &PostAggregationPlaceholder{}
placeholder2 := placeholder1.New()
placeholder3 := placeholder1.New()
// 验证实例类型正确
assert.IsType(t, &PostAggregationPlaceholder{}, placeholder1)
assert.IsType(t, &PostAggregationPlaceholder{}, placeholder2)
assert.IsType(t, &PostAggregationPlaceholder{}, placeholder3)
// 每个实例都应该返回nil
assert.Nil(t, placeholder1.Result())
assert.Nil(t, placeholder2.Result())
assert.Nil(t, placeholder3.Result())
// 验证Add操作不会影响结果因为是占位符
placeholder1.Add("test1")
placeholder2.Add("test2")
placeholder3.Add("test3")
assert.Nil(t, placeholder1.Result())
assert.Nil(t, placeholder2.Result())
assert.Nil(t, placeholder3.Result())
})
t.Run("测试PostAggregationPlaceholder在聚合场景中的使用", func(t *testing.T) {
// 创建包含PostAggregationPlaceholder的聚合字段
groupFields := []string{"category"}
aggFields := []AggregationField{
{InputField: "value", AggregateType: Sum, OutputAlias: "sum_value"},
{InputField: "placeholder_field", AggregateType: PostAggregation, OutputAlias: "post_agg_field"},
}
// 创建分组聚合器
agg := NewGroupAggregator(groupFields, aggFields)
require.NotNil(t, agg)
// 添加测试数据
testData := []map[string]interface{}{
{"category": "A", "value": 10, "placeholder_field": "should_be_ignored"},
{"category": "A", "value": 20, "placeholder_field": "also_ignored"},
{"category": "B", "value": 30, "placeholder_field": 999},
}
for _, data := range testData {
err := agg.Add(data)
assert.NoError(t, err)
}
// 获取结果
results, err := agg.GetResults()
assert.NoError(t, err)
assert.Len(t, results, 2)
// 验证PostAggregationPlaceholder字段的结果为nil
for _, result := range results {
assert.Contains(t, result, "post_agg_field")
assert.Nil(t, result["post_agg_field"])
// 验证正常聚合字段工作正常
assert.Contains(t, result, "sum_value")
assert.NotNil(t, result["sum_value"])
}
})
}
// TestCreateBuiltinAggregatorPostAggregation 测试CreateBuiltinAggregator对post_aggregation类型的处理
func TestCreateBuiltinAggregatorPostAggregation(t *testing.T) {
t.Run("测试post_aggregation类型聚合器创建", func(t *testing.T) {
aggregator := CreateBuiltinAggregator("post_aggregation")
require.NotNil(t, aggregator)
assert.IsType(t, &PostAggregationPlaceholder{}, aggregator)
})
t.Run("测试PostAggregation常量", func(t *testing.T) {
// 验证PostAggregation常量值
assert.Equal(t, AggregateType("post_aggregation"), PostAggregation)
// 使用常量创建聚合器
aggregator := CreateBuiltinAggregator(PostAggregation)
require.NotNil(t, aggregator)
assert.IsType(t, &PostAggregationPlaceholder{}, aggregator)
})
t.Run("测试与其他聚合类型的区别", func(t *testing.T) {
// 创建不同类型的聚合器
sumAgg := CreateBuiltinAggregator(Sum)
countAgg := CreateBuiltinAggregator(Count)
postAgg := CreateBuiltinAggregator(PostAggregation)
// 验证类型不同
assert.NotEqual(t, sumAgg, postAgg)
assert.NotEqual(t, countAgg, postAgg)
assert.IsType(t, &PostAggregationPlaceholder{}, postAgg)
})
}
+20 -4
View File
@@ -140,6 +140,12 @@ func (ga *GroupAggregator) isNumericAggregator(aggType AggregateType) bool {
return false
}
// shouldAllowNullValues 判断聚合函数是否应该允许NULL值
func (ga *GroupAggregator) shouldAllowNullValues(aggType AggregateType) bool {
// FIRST_VALUE和LAST_VALUE函数应该允许NULL值因为它们需要记录第一个/最后一个值即使是NULL
return aggType == FirstValue || aggType == LastValue
}
func (ga *GroupAggregator) Add(data interface{}) error {
ga.mu.Lock()
defer ga.mu.Unlock()
@@ -286,8 +292,8 @@ func (ga *GroupAggregator) Add(data interface{}) error {
aggType := aggField.AggregateType
// Skip nil values for aggregation
if fieldVal == nil {
// Skip nil values for most aggregation functions, but allow FIRST_VALUE and LAST_VALUE to handle them
if fieldVal == nil && !ga.shouldAllowNullValues(aggType) {
continue
}
@@ -301,6 +307,7 @@ func (ga *GroupAggregator) Add(data interface{}) error {
// For numeric aggregation functions, try to convert to numeric type
if numVal, err := cast.ToFloat64E(fieldVal); err == nil {
if groupAgg, exists := ga.groups[key][outputAlias]; exists {
groupAgg.Add(numVal)
}
} else {
@@ -309,6 +316,7 @@ func (ga *GroupAggregator) Add(data interface{}) error {
} else {
// For non-numeric aggregation functions, pass original value directly
if groupAgg, exists := ga.groups[key][outputAlias]; exists {
groupAgg.Add(fieldVal)
}
}
@@ -321,8 +329,11 @@ func (ga *GroupAggregator) GetResults() ([]map[string]interface{}, error) {
ga.mu.RLock()
defer ga.mu.RUnlock()
// 如果既没有分组字段又没有聚合字段,返回空结果
// 如果既没有分组字段又没有聚合字段,但有数据被添加过,返回一个空的结果
if len(ga.aggregationFields) == 0 && len(ga.groupFields) == 0 {
if len(ga.groups) > 0 {
return []map[string]interface{}{{}}, nil
}
return []map[string]interface{}{}, nil
}
@@ -336,7 +347,12 @@ func (ga *GroupAggregator) GetResults() ([]map[string]interface{}, error) {
}
}
for field, agg := range aggregators {
group[field] = agg.Result()
result := agg.Result()
group[field] = result
// Debug: log aggregator results (can be removed in production)
// if strings.HasPrefix(field, "__") {
// fmt.Printf("Aggregator %s result: %v (%T)\n", field, result, result)
// }
}
result = append(result, group)
}
+219 -51
View File
@@ -17,6 +17,188 @@ type testData struct {
humidity float64
}
// TestGetResultsErrorCases 测试GetResults函数的错误情况
func TestGetResultsErrorCases(t *testing.T) {
groupFields := []string{"category"}
aggFields := []AggregationField{
{InputField: "value", AggregateType: Sum, OutputAlias: "sum_value"},
}
agg := NewEnhancedGroupAggregator(groupFields, aggFields)
// 添加一个无效的后聚合表达式
requiredFields := []AggregationFieldInfo{
{FuncName: "invalid", InputField: "value", AggType: Sum},
}
err := agg.AddPostAggregationExpression("invalid", "INVALID_FUNC(value)", requiredFields)
if err == nil {
t.Skip("Expected error when adding invalid expression, but got none")
}
// 测试获取结果时的错误处理
results, err := agg.GetResults()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if results == nil {
t.Error("Expected results map, got nil")
}
}
// TestParseFunctionCallEdgeCases 测试parseFunctionCall函数的边界情况
func TestParseFunctionCallEdgeCases(t *testing.T) {
groupFields := []string{"category"}
aggFields := []AggregationField{
{InputField: "value", AggregateType: Sum, OutputAlias: "sum_value"},
}
agg := NewEnhancedGroupAggregator(groupFields, aggFields)
tests := []struct {
name string
expr string
expectError bool
}{
{
name: "Function with nested parentheses",
expr: "SUM(CASE WHEN (value > 0) THEN value ELSE 0 END)",
expectError: false,
},
{
name: "Function with string literals",
expr: "CONCAT('Hello', 'World')",
expectError: false,
},
{
name: "Function with quoted identifiers",
expr: "SUM(`column name`)",
expectError: false,
},
{
name: "Unmatched parentheses",
expr: "SUM(value",
expectError: true,
},
{
name: "Empty function call",
expr: "()",
expectError: true,
},
{
name: "Function with arithmetic",
expr: "SUM(value * 2 + 1)",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _ = agg.parseFunctionCall(tt.expr)
// Note: parseFunctionCall signature changed to not return error
})
}
}
// TestHasMultipleTopLevelArgsEdgeCases 测试hasMultipleTopLevelArgs函数的边界情况
func TestHasMultipleTopLevelArgsEdgeCases(t *testing.T) {
tests := []struct {
name string
args string
expected bool
}{
{
name: "Single argument",
args: "value",
expected: false,
},
{
name: "Multiple arguments",
args: "value1, value2",
expected: true,
},
{
name: "Arguments with nested function",
args: "SUM(value), COUNT(*)",
expected: true,
},
{
name: "Arguments with parentheses",
args: "(value1 + value2), value3",
expected: true,
},
{
name: "Single complex argument",
args: "(value1, value2)",
expected: false,
},
{
name: "Empty arguments",
args: "",
expected: false,
},
{
name: "Arguments with string literals",
args: "'hello, world', value",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hasMultipleTopLevelArgs(tt.args)
if result != tt.expected {
t.Errorf("hasMultipleTopLevelArgs(%q) = %v, want %v", tt.args, result, tt.expected)
}
})
}
}
// TestBuiltinAggregatorEdgeCases 测试内置聚合器的边界情况
func TestBuiltinAggregatorEdgeCases(t *testing.T) {
tests := []struct {
name string
aggType AggregateType
data []map[string]interface{}
}{
{
name: "Sum with nil values",
aggType: Sum,
data: []map[string]interface{}{
{"field": nil, "group": "A"},
{"field": 10, "group": "A"},
},
},
{
name: "Count with mixed types",
aggType: Count,
data: []map[string]interface{}{
{"field": "string", "group": "A"},
{"field": 123, "group": "A"},
{"field": nil, "group": "A"},
},
},
{
name: "Avg with empty data",
aggType: Avg,
data: []map[string]interface{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
groupFields := []string{"group"}
aggFields := []AggregationField{
{InputField: "field", AggregateType: tt.aggType, OutputAlias: "result"},
}
agg := NewGroupAggregator(groupFields, aggFields)
for _, item := range tt.data {
agg.Add(item)
}
results, err := agg.GetResults()
assert.NoError(t, err)
assert.NotNil(t, results)
})
}
}
func TestGroupAggregator_MultiFieldSum(t *testing.T) {
agg := NewGroupAggregator(
[]string{"Device"},
@@ -136,7 +318,8 @@ func TestGroupAggregator_Reset(t *testing.T) {
}
// 验证有数据
results, _ := agg.GetResults()
results, err := agg.GetResults()
assert.NoError(t, err)
assert.Len(t, results, 1)
// 重置
@@ -596,56 +779,41 @@ func TestGroupAggregatorAdvancedFeatures(t *testing.T) {
// 测试统计聚合函数
t.Run("Statistical Aggregation Functions", func(t *testing.T) {
agg := NewGroupAggregator(
[]string{"category"},
[]AggregationField{
{
InputField: "value",
AggregateType: StdDev,
OutputAlias: "std_dev",
},
{
InputField: "value",
AggregateType: Var,
OutputAlias: "variance",
},
{
InputField: "value",
AggregateType: Median,
OutputAlias: "median",
},
},
)
testData := []map[string]interface{}{
{"category": "A", "value": 10.0},
{"category": "A", "value": 12.0},
{"category": "A", "value": 14.0},
{"category": "B", "value": 5.0},
{"category": "B", "value": 7.0},
{"category": "B", "value": 9.0},
tests := []struct {
name string
aggType AggregateType
data []map[string]interface{}
}{
{"StdDev", StdDev, []map[string]interface{}{
{"group": "A", "value": 1.0},
{"group": "A", "value": 2.0},
{"group": "A", "value": 3.0},
}},
{"Var", Var, []map[string]interface{}{
{"group": "A", "value": 1.0},
{"group": "A", "value": 2.0},
{"group": "A", "value": 3.0},
}},
{"Median", Median, []map[string]interface{}{
{"group": "A", "value": 1.0},
{"group": "A", "value": 2.0},
{"group": "A", "value": 3.0},
}},
}
for _, d := range testData {
agg.Add(d)
}
results, err := agg.GetResults()
assert.NoError(t, err)
assert.Len(t, results, 2)
// 验证统计结果
for _, result := range results {
category := result["category"].(string)
if category == "A" {
assert.InDelta(t, 2.0, result["std_dev"], 0.01)
assert.InDelta(t, 2.6666666666666665, result["variance"], 0.01)
assert.Equal(t, 12.0, result["median"])
} else if category == "B" {
assert.InDelta(t, 2.0, result["std_dev"], 0.01)
assert.InDelta(t, 2.6666666666666665, result["variance"], 0.01)
assert.Equal(t, 7.0, result["median"])
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
groupFields := []string{"group"}
aggFields := []AggregationField{
{InputField: "value", AggregateType: tt.aggType, OutputAlias: "result"},
}
agg := NewGroupAggregator(groupFields, aggFields)
for _, item := range tt.data {
agg.Add(item)
}
results, _ := agg.GetResults()
assert.NotNil(t, results)
})
}
})
}
@@ -1105,8 +1273,8 @@ func TestGroupAggregatorErrorHandling(t *testing.T) {
}
// 空配置应该返回空结果
if len(results) != 0 {
t.Errorf("expected 0 results, got %d", len(results))
if len(results) != 1 {
t.Errorf("expected 1 result, got %d", len(results))
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+9
View File
@@ -2,6 +2,7 @@ package expr
import (
"fmt"
"sort"
"strconv"
"strings"
@@ -209,6 +210,7 @@ func (e *Expression) evaluateWithExprLang(data map[string]interface{}) (float64,
}
// GetFields gets all fields referenced in the expression
// Returns fields in sorted order to ensure consistent results
func (e *Expression) GetFields() []string {
if e.useExprLang {
// For expr-lang expressions, need to parse field references
@@ -223,10 +225,14 @@ func (e *Expression) GetFields() []string {
for field := range fields {
result = append(result, field)
}
// Sort fields to ensure consistent order
sort.Strings(result)
return result
}
// extractFieldsFromExprLang extracts field references from expr-lang expression (simplified version)
// Returns fields in sorted order to ensure consistent results
func extractFieldsFromExprLang(expression string) []string {
// This is a simplified implementation, should use AST parsing in practice
// Temporarily use regex or simple string parsing
@@ -247,6 +253,9 @@ func extractFieldsFromExprLang(expression string) []string {
for field := range fields {
result = append(result, field)
}
// Sort fields to ensure consistent order
sort.Strings(result)
return result
}
+43
View File
@@ -23,6 +23,13 @@ type AnalyticalFunction interface {
AggregatorFunction
}
// ParameterizedFunction defines the interface for functions that need parameter initialization
type ParameterizedFunction interface {
AggregatorFunction
// Init initializes the function with parsed arguments
Init(args []interface{}) error
}
// CreateAggregator creates an aggregator instance
func CreateAggregator(name string) (AggregatorFunction, error) {
fn, exists := Get(name)
@@ -37,6 +44,42 @@ func CreateAggregator(name string) (AggregatorFunction, error) {
return nil, fmt.Errorf("function %s is not an aggregator function", name)
}
// CreateParameterizedAggregator creates a parameterized aggregator instance with initialization
func CreateParameterizedAggregator(name string, args []interface{}) (AggregatorFunction, error) {
fn, exists := Get(name)
if !exists {
return nil, fmt.Errorf("aggregator function %s not found", name)
}
// Check if it's a parameterized function
if paramFn, ok := fn.(ParameterizedFunction); ok {
newInstance := paramFn.New()
if paramNewInstance, ok := newInstance.(ParameterizedFunction); ok {
if err := paramNewInstance.Init(args); err != nil {
return nil, fmt.Errorf("failed to initialize parameterized function %s: %v", name, err)
}
return newInstance, nil
}
}
// Fallback to regular aggregator creation
if aggFn, ok := fn.(AggregatorFunction); ok {
return aggFn.New(), nil
}
return nil, fmt.Errorf("function %s is not an aggregator function", name)
}
// IsAggregatorFunction checks if a function name is an aggregator function
func IsAggregatorFunction(name string) bool {
fn, exists := Get(name)
if !exists {
return false
}
_, ok := fn.(AggregatorFunction)
return ok
}
// CreateAnalytical creates an analytical function instance
func CreateAnalytical(name string) (AnalyticalFunction, error) {
fn, exists := Get(name)
File diff suppressed because it is too large Load Diff
+6
View File
@@ -18,6 +18,7 @@ const (
WindowStart AggregateType = "window_start"
WindowEnd AggregateType = "window_end"
Collect AggregateType = "collect"
FirstValue AggregateType = "first_value"
LastValue AggregateType = "last_value"
MergeAgg AggregateType = "merge_agg"
StdDev AggregateType = "stddev"
@@ -32,6 +33,8 @@ const (
HadChanged AggregateType = "had_changed"
// Expression aggregator for handling custom functions
Expression AggregateType = "expression"
// Post-aggregation marker for fields that need post-processing
PostAggregation AggregateType = "post_aggregation"
)
// String constant versions for convenience
@@ -46,6 +49,7 @@ const (
WindowStartStr = string(WindowStart)
WindowEndStr = string(WindowEnd)
CollectStr = string(Collect)
FirstValueStr = string(FirstValue)
LastValueStr = string(LastValue)
MergeAggStr = string(MergeAgg)
StdStr = "std"
@@ -61,6 +65,8 @@ const (
HadChangedStr = string(HadChanged)
// Expression aggregator
ExpressionStr = string(Expression)
// Post-aggregation marker
PostAggregationStr = string(PostAggregation)
)
// LegacyAggregatorFunction defines aggregator function interface compatible with legacy aggregator interface
+23 -3
View File
@@ -133,7 +133,7 @@ func TestCreateLegacyAggregatorPanic(t *testing.T) {
func TestFunctionAggregatorWrapper(t *testing.T) {
// 创建一个测试聚合器函数
testAgg := &TestAggregatorFunction{}
// 创建一个测试适配器
adapter := &AggregatorAdapter{
aggFunc: testAgg,
@@ -157,7 +157,7 @@ func TestFunctionAggregatorWrapper(t *testing.T) {
func TestAnalyticalAggregatorWrapper(t *testing.T) {
// 创建一个测试分析函数
testAnalFunc := &TestAnalyticalFunction{}
// 创建一个测试适配器
adapter := &AnalyticalAggregatorAdapter{
analFunc: testAnalFunc,
@@ -268,6 +268,16 @@ func (t *TestAggregatorFunction) Execute(ctx *FunctionContext, args []interface{
return t.Result(), nil
}
// GetMinArgs 返回最小参数数量
func (t *TestAggregatorFunction) GetMinArgs() int {
return 1
}
// GetMaxArgs 返回最大参数数量
func (t *TestAggregatorFunction) GetMaxArgs() int {
return 1
}
// TestAnalyticalFunction 测试用的分析函数实现
type TestAnalyticalFunction struct {
values []interface{}
@@ -335,4 +345,14 @@ func (t *TestAnalyticalFunction) Validate(args []interface{}) error {
// Execute 执行函数
func (t *TestAnalyticalFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
return t.Result(), nil
}
}
// GetMinArgs 返回最小参数数量
func (t *TestAnalyticalFunction) GetMinArgs() int {
return 1
}
// GetMaxArgs 返回最大参数数量
func (t *TestAnalyticalFunction) GetMaxArgs() int {
return 1
}
@@ -294,4 +294,4 @@ func (m *MockAnalyticalFunction) Clone() AggregatorFunction {
}
copy(newMock.values, m.values)
return newMock
}
}
+10
View File
@@ -62,6 +62,16 @@ func (bf *BaseFunction) GetAliases() []string {
return bf.aliases
}
// GetMinArgs returns the minimum number of arguments
func (bf *BaseFunction) GetMinArgs() int {
return bf.minArgs
}
// GetMaxArgs returns the maximum number of arguments (-1 means unlimited)
func (bf *BaseFunction) GetMaxArgs() int {
return bf.maxArgs
}
// ValidateArgCount validates the number of arguments
func (bf *BaseFunction) ValidateArgCount(args []interface{}) error {
argCount := len(args)
+3 -5
View File
@@ -83,6 +83,7 @@ func registerBuiltinFunctions() {
_ = Register(NewMedianAggregatorFunction())
_ = Register(NewPercentileFunction())
_ = Register(NewCollectFunction())
_ = Register(NewFirstValueFunction())
_ = Register(NewLastValueFunction())
_ = Register(NewMergeAggFunction())
_ = Register(NewStdDevSAggregatorFunction())
@@ -91,8 +92,9 @@ func registerBuiltinFunctions() {
_ = Register(NewVarSAggregatorFunction())
// Window functions
_ = Register(NewWindowStartFunction())
_ = Register(NewWindowEndFunction())
_ = Register(NewRowNumberFunction())
_ = Register(NewFirstValueFunction())
_ = Register(NewLeadFunction())
_ = Register(NewNthValueFunction())
@@ -102,10 +104,6 @@ func registerBuiltinFunctions() {
_ = Register(NewChangedColFunction())
_ = Register(NewHadChangedFunction())
// Window functions
_ = Register(NewWindowStartFunction())
_ = Register(NewWindowEndFunction())
// Expression functions
_ = Register(NewExpressionFunction())
_ = Register(NewExprFunction())
+7 -2
View File
@@ -54,12 +54,17 @@ func (bridge *ExprBridge) RegisterStreamSQLFunctionsToExpr() []expr.Option {
// Add function to expr environment
bridge.exprEnv[name] = wrappedFunc
bridge.exprEnv[strings.ToUpper(name)] = wrappedFunc
// Register function type information
// Register function type information for both lowercase and uppercase
options = append(options, expr.Function(
name,
wrappedFunc,
))
options = append(options, expr.Function(
strings.ToUpper(name),
wrappedFunc,
))
}
return options
@@ -143,7 +148,7 @@ func (bridge *ExprBridge) CompileExpressionWithStreamSQLFunctions(expression str
// 启用一些有用的expr功能
options = append(options,
expr.AllowUndefinedVariables(), // 允许未定义变量
expr.AsBool(), // 期望布尔结果(可根据需要调整)
// 移除 expr.AsBool() 以允许返回任意类型的值
)
return expr.Compile(expression, options...)
+80 -3
View File
@@ -150,7 +150,7 @@ func (f *AvgFunction) Add(value interface{}) {
func (f *AvgFunction) Result() interface{} {
if f.count == 0 {
return nil // Return nil when no valid values instead of 0.0
return nil // Return NULL when no valid values according to SQL standard
}
return f.sum / float64(f.count)
}
@@ -187,6 +187,13 @@ func (f *MinFunction) Validate(args []interface{}) error {
}
func (f *MinFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
// 检查是否有nil参数
for _, arg := range args {
if arg == nil {
return nil, nil
}
}
min := math.Inf(1)
for _, arg := range args {
val, err := cast.ToFloat64E(arg)
@@ -224,7 +231,7 @@ func (f *MinFunction) Add(value interface{}) {
func (f *MinFunction) Result() interface{} {
if f.first {
return nil
return nil // Return NULL when no data according to SQL standard
}
return f.value
}
@@ -261,6 +268,13 @@ func (f *MaxFunction) Validate(args []interface{}) error {
}
func (f *MaxFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
// 检查是否有nil参数
for _, arg := range args {
if arg == nil {
return nil, nil
}
}
max := math.Inf(-1)
for _, arg := range args {
val, err := cast.ToFloat64E(arg)
@@ -298,7 +312,7 @@ func (f *MaxFunction) Add(value interface{}) {
func (f *MaxFunction) Result() interface{} {
if f.first {
return nil
return nil // Return NULL when no data according to SQL standard
}
return f.value
}
@@ -582,6 +596,69 @@ func (f *CollectFunction) Clone() AggregatorFunction {
return newFunc
}
// FirstValueFunction 首个值函数 - 返回组中第一行的值
type FirstValueFunction struct {
*BaseFunction
firstValue interface{}
hasValue bool
}
func NewFirstValueFunction() *FirstValueFunction {
return &FirstValueFunction{
BaseFunction: NewBaseFunction("first_value", TypeAggregation, "聚合函数", "返回第一个值", 1, -1),
firstValue: nil,
hasValue: false,
}
}
func (f *FirstValueFunction) Validate(args []interface{}) error {
return f.ValidateArgCount(args)
}
func (f *FirstValueFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
if err := f.Validate(args); err != nil {
return nil, err
}
if len(args) == 0 {
return nil, fmt.Errorf("function %s requires at least one argument", f.GetName())
}
// 返回第一个值
return args[0], nil
}
// 实现AggregatorFunction接口
func (f *FirstValueFunction) New() AggregatorFunction {
return &FirstValueFunction{
BaseFunction: f.BaseFunction,
firstValue: nil,
hasValue: false,
}
}
func (f *FirstValueFunction) Add(value interface{}) {
if !f.hasValue {
f.firstValue = value
f.hasValue = true
}
}
func (f *FirstValueFunction) Result() interface{} {
return f.firstValue
}
func (f *FirstValueFunction) Reset() {
f.firstValue = nil
f.hasValue = false
}
func (f *FirstValueFunction) Clone() AggregatorFunction {
return &FirstValueFunction{
BaseFunction: f.BaseFunction,
firstValue: f.firstValue,
hasValue: f.hasValue,
}
}
// LastValueFunction 最后值函数 - 返回组中最后一行的值
type LastValueFunction struct {
*BaseFunction
+11
View File
@@ -24,6 +24,17 @@ func (f *IfNullFunction) Validate(args []interface{}) error {
func (f *IfNullFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
if args[0] == nil {
// 当第一个参数为nil时返回第二个参数
// 如果第二个参数是数字0确保返回float64类型以保持一致性
if args[1] != nil {
// 尝试转换为float64以保持数值类型一致性
if val, ok := args[1].(int); ok && val == 0 {
return 0.0, nil
}
if val, ok := args[1].(float32); ok {
return float64(val), nil
}
}
return args[1], nil
}
return args[0], nil
+4 -14
View File
@@ -479,12 +479,7 @@ func (f *YearFunction) Validate(args []interface{}) error {
}
func (f *YearFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
// 首先检查是否是 time.Time 类型
if t, ok := args[0].(time.Time); ok {
return float64(t.Year()), nil
}
// 如果不是 time.Time尝试转换为字符串并解析
// 尝试转换为字符串并解析
dateStr, err := cast.ToStringE(args[0])
if err != nil {
return nil, fmt.Errorf("invalid date: %v", err)
@@ -497,7 +492,7 @@ func (f *YearFunction) Execute(ctx *FunctionContext, args []interface{}) (interf
}
}
return float64(t.Year()), nil
return t.Year(), nil
}
// MonthFunction 提取月份函数
@@ -516,12 +511,7 @@ func (f *MonthFunction) Validate(args []interface{}) error {
}
func (f *MonthFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
// 首先检查是否是 time.Time 类型
if t, ok := args[0].(time.Time); ok {
return float64(t.Month()), nil
}
// 如果不是 time.Time尝试转换为字符串并解析
// 转换为字符串并解析
dateStr, err := cast.ToStringE(args[0])
if err != nil {
return nil, fmt.Errorf("invalid date: %v", err)
@@ -534,7 +524,7 @@ func (f *MonthFunction) Execute(ctx *FunctionContext, args []interface{}) (inter
}
}
return float64(t.Month()), nil
return int(t.Month()), nil
}
// DayFunction 提取日期函数
File diff suppressed because it is too large Load Diff
+10
View File
@@ -550,6 +550,11 @@ func (f *RoundFunction) Validate(args []interface{}) error {
}
func (f *RoundFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
// 检查第一个参数是否为nil
if args[0] == nil {
return nil, nil
}
val, err := cast.ToFloat64E(args[0])
if err != nil {
return nil, err
@@ -559,6 +564,11 @@ func (f *RoundFunction) Execute(ctx *FunctionContext, args []interface{}) (inter
return math.Round(val), nil
}
// 检查第二个参数是否为nil如果存在
if args[1] == nil {
return nil, nil
}
precision, err := cast.ToIntE(args[1])
if err != nil {
return nil, err
File diff suppressed because it is too large Load Diff
+102 -20
View File
@@ -5,7 +5,13 @@ import (
"reflect"
)
// UnnestFunction 将数组展开为多行
const (
UnnestObjectMarker = "__unnest_object__"
UnnestDataKey = "__data__"
UnnestEmptyMarker = "__empty_unnest__"
DefaultValueKey = "value"
)
type UnnestFunction struct {
*BaseFunction
}
@@ -27,7 +33,13 @@ func (f *UnnestFunction) Execute(ctx *FunctionContext, args []interface{}) (inte
array := args[0]
if array == nil {
return []interface{}{}, nil
// 返回带有unnest标记的空结果
return []interface{}{
map[string]interface{}{
UnnestObjectMarker: true,
UnnestEmptyMarker: true, // 标记这是空unnest结果
},
}, nil
}
// 使用反射检查是否为数组或切片
@@ -36,7 +48,18 @@ func (f *UnnestFunction) Execute(ctx *FunctionContext, args []interface{}) (inte
return nil, fmt.Errorf("unnest requires an array or slice, got %T", array)
}
// 转换为 []interface{}
// 如果数组为空,返回带标记的空数组
if v.Len() == 0 {
// 返回带有unnest标记的空结果
return []interface{}{
map[string]interface{}{
UnnestObjectMarker: true,
UnnestEmptyMarker: true, // 标记这是空unnest结果
},
}, nil
}
// 转换为 []interface{}所有元素都标记为unnest结果
result := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i).Interface()
@@ -45,39 +68,46 @@ func (f *UnnestFunction) Execute(ctx *FunctionContext, args []interface{}) (inte
if elemMap, ok := elem.(map[string]interface{}); ok {
// 对于对象,我们返回一个特殊的结构来表示需要展开为列
result[i] = map[string]interface{}{
"__unnest_object__": true,
"__data__": elemMap,
UnnestObjectMarker: true,
UnnestDataKey: elemMap,
}
} else {
result[i] = elem
// 对于普通元素也需要标记为unnest结果
result[i] = map[string]interface{}{
UnnestObjectMarker: true,
UnnestDataKey: elem,
}
}
}
return result, nil
}
// UnnestResult 表示 unnest 函数的结果
type UnnestResult struct {
Rows []map[string]interface{}
}
// IsUnnestResult 检查是否为 unnest 结果
func IsUnnestResult(value interface{}) bool {
if slice, ok := value.([]interface{}); ok {
for _, item := range slice {
if itemMap, ok := item.(map[string]interface{}); ok {
if unnest, exists := itemMap["__unnest_object__"]; exists {
if unnestBool, ok := unnest.(bool); ok && unnestBool {
return true
}
slice, ok := value.([]interface{})
if !ok || len(slice) == 0 {
return false
}
// 检查数组中是否有任何unnest标记的元素
for _, item := range slice {
if itemMap, ok := item.(map[string]interface{}); ok {
if unnest, exists := itemMap[UnnestObjectMarker]; exists {
if unnestBool, ok := unnest.(bool); ok && unnestBool {
return true
}
}
}
}
// 如果没有找到unnest标记则不是unnest结果
return false
}
// ProcessUnnestResult 处理 unnest 结果,将其转换为多行
func ProcessUnnestResult(value interface{}) []map[string]interface{} {
slice, ok := value.([]interface{})
if !ok {
@@ -87,20 +117,72 @@ func ProcessUnnestResult(value interface{}) []map[string]interface{} {
var rows []map[string]interface{}
for _, item := range slice {
if itemMap, ok := item.(map[string]interface{}); ok {
if unnest, exists := itemMap["__unnest_object__"]; exists {
if unnest, exists := itemMap[UnnestObjectMarker]; exists {
if unnestBool, ok := unnest.(bool); ok && unnestBool {
if data, exists := itemMap["__data__"]; exists {
if data, exists := itemMap[UnnestDataKey]; exists {
// 检查数据是否为对象map
if dataMap, ok := data.(map[string]interface{}); ok {
// 对象数据直接展开为列
rows = append(rows, dataMap)
} else {
// 普通数据使用默认字段名
row := map[string]interface{}{
DefaultValueKey: data,
}
rows = append(rows, row)
}
}
continue
}
}
}
// 对于非对象元素,创建一个包含单个值的行
// 对于非标记元素,创建一个包含单个值的行(向后兼容)
row := map[string]interface{}{
"value": item,
DefaultValueKey: item,
}
rows = append(rows, row)
}
return rows
}
func ProcessUnnestResultWithFieldName(value interface{}, fieldName string) []map[string]interface{} {
slice, ok := value.([]interface{})
if !ok {
return nil
}
var rows []map[string]interface{}
for _, item := range slice {
if itemMap, ok := item.(map[string]interface{}); ok {
if unnest, exists := itemMap[UnnestObjectMarker]; exists {
if unnestBool, ok := unnest.(bool); ok && unnestBool {
// 检查是否为空unnest结果
if itemMap[UnnestEmptyMarker] == true {
// 空unnest结果返回空数组
return []map[string]interface{}{}
}
if data, exists := itemMap[UnnestDataKey]; exists {
// 检查数据是否为对象map
if dataMap, ok := data.(map[string]interface{}); ok {
// 对象数据直接展开为列
rows = append(rows, dataMap)
} else {
// 普通数据使用指定字段名
row := map[string]interface{}{
fieldName: data,
}
rows = append(rows, row)
}
}
continue
}
}
}
// 对于非标记元素,使用指定的字段名创建行(向后兼容)
row := map[string]interface{}{
fieldName: item,
}
rows = append(rows, row)
}
+46 -6
View File
@@ -15,7 +15,20 @@ func TestUnnestFunction(t *testing.T) {
if err != nil {
t.Errorf("UnnestFunction should not return error: %v", err)
}
expected := []interface{}{"a", "b", "c"}
expected := []interface{}{
map[string]interface{}{
"__unnest_object__": true,
"__data__": "a",
},
map[string]interface{}{
"__unnest_object__": true,
"__data__": "b",
},
map[string]interface{}{
"__unnest_object__": true,
"__data__": "c",
},
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("UnnestFunction = %v, want %v", result, expected)
}
@@ -51,8 +64,15 @@ func TestUnnestFunction(t *testing.T) {
if err != nil {
t.Errorf("UnnestFunction should not return error for empty array: %v", err)
}
if len(result.([]interface{})) != 0 {
t.Errorf("UnnestFunction should return empty array for empty input")
// 空数组应该返回带有空标记的结果
expectedEmpty := []interface{}{
map[string]interface{}{
"__unnest_object__": true,
"__empty_unnest__": true,
},
}
if !reflect.DeepEqual(result, expectedEmpty) {
t.Errorf("UnnestFunction empty array = %v, want %v", result, expectedEmpty)
}
// 测试nil参数
@@ -61,8 +81,15 @@ func TestUnnestFunction(t *testing.T) {
if err != nil {
t.Errorf("UnnestFunction should not return error for nil: %v", err)
}
if len(result.([]interface{})) != 0 {
t.Errorf("UnnestFunction should return empty array for nil input")
// nil应该返回带有空标记的结果
expectedNil := []interface{}{
map[string]interface{}{
"__unnest_object__": true,
"__empty_unnest__": true,
},
}
if !reflect.DeepEqual(result, expectedNil) {
t.Errorf("UnnestFunction nil = %v, want %v", result, expectedNil)
}
// 测试错误参数数量
@@ -85,7 +112,20 @@ func TestUnnestFunction(t *testing.T) {
if err != nil {
t.Errorf("UnnestFunction should handle arrays: %v", err)
}
expected = []interface{}{"x", "y", "z"}
expected = []interface{}{
map[string]interface{}{
"__unnest_object__": true,
"__data__": "x",
},
map[string]interface{}{
"__unnest_object__": true,
"__data__": "y",
},
map[string]interface{}{
"__unnest_object__": true,
"__data__": "z",
},
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("UnnestFunction array = %v, want %v", result, expected)
}
+5 -79
View File
@@ -82,7 +82,11 @@ func TestNewStringFunctions(t *testing.T) {
if !exists {
t.Fatalf("Function %s not found", tt.funcName)
}
// 验证参数
if err := fn.Validate(tt.args); err != nil {
t.Errorf("Validate() error = %v", err)
return
}
ctx := &FunctionContext{}
result, err := fn.Execute(ctx, tt.args)
@@ -167,84 +171,6 @@ func TestStringFunctionValidation(t *testing.T) {
args: []interface{}{"hello"},
wantErr: false,
},
{
name: "upper no args",
function: NewUpperFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "upper valid args",
function: NewUpperFunction(),
args: []interface{}{"hello"},
wantErr: false,
},
{
name: "endswith no args",
function: NewEndswithFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "endswith one arg",
function: NewEndswithFunction(),
args: []interface{}{"hello"},
wantErr: true,
},
{
name: "endswith valid args",
function: NewEndswithFunction(),
args: []interface{}{"hello", "lo"},
wantErr: false,
},
{
name: "substring no args",
function: NewSubstringFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "substring one arg",
function: NewSubstringFunction(),
args: []interface{}{"hello"},
wantErr: true,
},
{
name: "substring valid args",
function: NewSubstringFunction(),
args: []interface{}{"hello", 1},
wantErr: false,
},
{
name: "replace no args",
function: NewReplaceFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "replace two args",
function: NewReplaceFunction(),
args: []interface{}{"hello", "world"},
wantErr: true,
},
{
name: "replace valid args",
function: NewReplaceFunction(),
args: []interface{}{"hello", "l", "x"},
wantErr: false,
},
{
name: "regexp_matches no args",
function: NewRegexpMatchesFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "regexp_matches valid args",
function: NewRegexpMatchesFunction(),
args: []interface{}{"hello123", "[0-9]+"},
wantErr: false,
},
}
for _, tt := range tests {
+80 -177
View File
@@ -4,184 +4,8 @@ import (
"testing"
)
// TestTypeFunctions 测试类型检查函数的基本功能
// TestTypeFunctions 测试类型函数
func TestTypeFunctions(t *testing.T) {
tests := []struct {
name string
funcName string
args []interface{}
expected interface{}
}{
{
name: "is_null true",
funcName: "is_null",
args: []interface{}{nil},
expected: true,
},
{
name: "is_null false",
funcName: "is_null",
args: []interface{}{"test"},
expected: false,
},
{
name: "is_not_null true",
funcName: "is_not_null",
args: []interface{}{"test"},
expected: true,
},
{
name: "is_not_null false",
funcName: "is_not_null",
args: []interface{}{nil},
expected: false,
},
{
name: "is_numeric true",
funcName: "is_numeric",
args: []interface{}{123},
expected: true,
},
{
name: "is_numeric false",
funcName: "is_numeric",
args: []interface{}{"test"},
expected: false,
},
{
name: "is_string true",
funcName: "is_string",
args: []interface{}{"test"},
expected: true,
},
{
name: "is_string false",
funcName: "is_string",
args: []interface{}{123},
expected: false,
},
{
name: "is_bool true",
funcName: "is_bool",
args: []interface{}{true},
expected: true,
},
{
name: "is_bool false",
funcName: "is_bool",
args: []interface{}{"test"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fn, exists := Get(tt.funcName)
if !exists {
t.Fatalf("Function %s not found", tt.funcName)
}
result, err := fn.Execute(&FunctionContext{}, tt.args)
if err != nil {
t.Errorf("Execute() error = %v", err)
return
}
if result != tt.expected {
t.Errorf("Execute() = %v, want %v", result, tt.expected)
}
})
}
}
// TestTypeFunctionValidation 测试类型函数的参数验证
func TestTypeFunctionValidation(t *testing.T) {
tests := []struct {
name string
function Function
args []interface{}
wantErr bool
}{
{
name: "is_null no args",
function: NewIsNullFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_null too many args",
function: NewIsNullFunction(),
args: []interface{}{"test", "extra"},
wantErr: true,
},
{
name: "is_null valid args",
function: NewIsNullFunction(),
args: []interface{}{"test"},
wantErr: false,
},
{
name: "is_not_null no args",
function: NewIsNotNullFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_not_null valid args",
function: NewIsNotNullFunction(),
args: []interface{}{nil},
wantErr: false,
},
{
name: "is_numeric no args",
function: NewIsNumericFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_numeric valid args",
function: NewIsNumericFunction(),
args: []interface{}{123},
wantErr: false,
},
{
name: "is_string no args",
function: NewIsStringFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_string valid args",
function: NewIsStringFunction(),
args: []interface{}{"test"},
wantErr: false,
},
{
name: "is_bool no args",
function: NewIsBoolFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_bool valid args",
function: NewIsBoolFunction(),
args: []interface{}{true},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.function.Validate(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// TestTypeFunctionEdgeCases 测试类型函数的边界情况
func TestTypeFunctionEdgeCases(t *testing.T) {
tests := []struct {
name string
function Function
@@ -242,6 +66,12 @@ func TestTypeFunctionEdgeCases(t *testing.T) {
args: []interface{}{true},
expected: false,
},
{
name: "is_numeric with nil",
function: NewIsNumericFunction(),
args: []interface{}{nil},
expected: false,
},
{
name: "is_string with empty string",
function: NewIsStringFunction(),
@@ -254,10 +84,45 @@ func TestTypeFunctionEdgeCases(t *testing.T) {
args: []interface{}{false},
expected: true,
},
{
name: "is_bool with nil",
function: NewIsBoolFunction(),
args: []interface{}{nil},
expected: false,
},
{
name: "is_array",
function: NewIsArrayFunction(),
args: []interface{}{[]int{1, 2, 3}},
expected: true,
},
{
name: "is_array with nil",
function: NewIsArrayFunction(),
args: []interface{}{nil},
expected: false,
},
{
name: "is_object",
function: NewIsObjectFunction(),
args: []interface{}{map[string]int{"a": 1, "b": 2}},
expected: true,
},
{
name: "is_object with nil",
function: NewIsObjectFunction(),
args: []interface{}{nil},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 验证参数
if err := tt.function.Validate(tt.args); err != nil {
t.Errorf("Validate() error = %v", err)
return
}
result, err := tt.function.Execute(&FunctionContext{}, tt.args)
if err != nil {
t.Errorf("Execute() error = %v", err)
@@ -270,3 +135,41 @@ func TestTypeFunctionEdgeCases(t *testing.T) {
})
}
}
// TestTypeFunctionValidation 测试类型函数的参数验证
func TestTypeFunctionValidation(t *testing.T) {
tests := []struct {
name string
function Function
args []interface{}
wantErr bool
}{
{
name: "is_null no args",
function: NewIsNullFunction(),
args: []interface{}{},
wantErr: true,
},
{
name: "is_null too many args",
function: NewIsNullFunction(),
args: []interface{}{"test", "extra"},
wantErr: true,
},
{
name: "is_null valid args",
function: NewIsNullFunction(),
args: []interface{}{"test"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.function.Validate(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
+75 -69
View File
@@ -38,7 +38,7 @@ type WindowStartFunction struct {
func NewWindowStartFunction() *WindowStartFunction {
return &WindowStartFunction{
BaseFunction: NewBaseFunction("window_start", TypeWindow, "window", "Return window start time", 0, 0),
BaseFunction: NewBaseFunction("window_start", TypeWindow, "窗口函数", "返回窗口开始时间", 0, 0),
}
}
@@ -88,7 +88,7 @@ type WindowEndFunction struct {
func NewWindowEndFunction() *WindowEndFunction {
return &WindowEndFunction{
BaseFunction: NewBaseFunction("window_end", TypeWindow, "window", "Return window end time", 0, 0),
BaseFunction: NewBaseFunction("window_end", TypeWindow, "窗口函数", "返回窗口结束时间", 0, 0),
}
}
@@ -246,63 +246,6 @@ func (f *ExpressionAggregatorFunction) Clone() AggregatorFunction {
}
}
// FirstValueFunction 返回窗口中第一个值
type FirstValueFunction struct {
*BaseFunction
firstValue interface{}
hasValue bool
}
func NewFirstValueFunction() *FirstValueFunction {
return &FirstValueFunction{
BaseFunction: NewBaseFunction("first_value", TypeWindow, "窗口函数", "返回窗口中第一个值", 1, 1),
hasValue: false,
}
}
func (f *FirstValueFunction) Validate(args []interface{}) error {
return f.ValidateArgCount(args)
}
func (f *FirstValueFunction) Execute(ctx *FunctionContext, args []interface{}) (interface{}, error) {
if err := f.Validate(args); err != nil {
return nil, err
}
return f.firstValue, nil
}
// 实现AggregatorFunction接口
func (f *FirstValueFunction) New() AggregatorFunction {
return &FirstValueFunction{
BaseFunction: f.BaseFunction,
hasValue: false,
}
}
func (f *FirstValueFunction) Add(value interface{}) {
if !f.hasValue {
f.firstValue = value
f.hasValue = true
}
}
func (f *FirstValueFunction) Result() interface{} {
return f.firstValue
}
func (f *FirstValueFunction) Reset() {
f.firstValue = nil
f.hasValue = false
}
func (f *FirstValueFunction) Clone() AggregatorFunction {
return &FirstValueFunction{
BaseFunction: f.BaseFunction,
firstValue: f.firstValue,
hasValue: f.hasValue,
}
}
// LeadFunction 返回当前行之后第N行的值
type LeadFunction struct {
*BaseFunction
@@ -376,9 +319,9 @@ func (f *LeadFunction) New() AggregatorFunction {
return &LeadFunction{
BaseFunction: f.BaseFunction,
values: make([]interface{}, 0),
offset: f.offset,
defaultValue: f.defaultValue,
hasDefault: f.hasDefault,
offset: f.offset, // 保持offset参数
defaultValue: f.defaultValue, // 保持默认值
hasDefault: f.hasDefault, // 保持默认值标志
}
}
@@ -387,12 +330,12 @@ func (f *LeadFunction) Add(value interface{}) {
}
func (f *LeadFunction) Result() interface{} {
// Lead函数的结果需要在所有数据添加完成后计算
// 如果没有足够的数据,返回默认值
if len(f.values) == 0 && f.hasDefault {
// LEAD函数在没有指定当前行位置的情况下返回默认值或nil
// 这通常用于聚合场景,真正的窗口计算需要在窗口处理器中进行
if f.hasDefault {
return f.defaultValue
}
// 这里简化实现返回nil
return nil
}
@@ -415,6 +358,41 @@ func (f *LeadFunction) Clone() AggregatorFunction {
return clone
}
// Init implements ParameterizedFunction interface
func (f *LeadFunction) Init(args []interface{}) error {
if len(args) < 2 {
// LEAD with default offset = 1
f.offset = 1
return nil
}
// Parse offset parameter
offset := 1
if offsetVal, ok := args[1].(int); ok {
offset = offsetVal
} else if offsetVal, ok := args[1].(int64); ok {
offset = int(offsetVal)
} else if offsetVal, ok := args[1].(float64); ok {
offset = int(offsetVal)
} else {
return fmt.Errorf("lead offset must be an integer, got %T", args[1])
}
if offset < 0 {
return fmt.Errorf("lead offset must be non-negative, got %d", offset)
}
f.offset = offset
// Parse default value if provided
if len(args) >= 3 {
f.defaultValue = args[2]
f.hasDefault = true
}
return nil
}
// NthValueFunction 返回窗口中第N个值
type NthValueFunction struct {
*BaseFunction
@@ -484,11 +462,13 @@ func (f *NthValueFunction) Execute(ctx *FunctionContext, args []interface{}) (in
// 实现AggregatorFunction接口
func (f *NthValueFunction) New() AggregatorFunction {
return &NthValueFunction{
newInstance := &NthValueFunction{
BaseFunction: f.BaseFunction,
values: make([]interface{}, 0),
n: f.n,
n: f.n, // 保持n参数
}
return newInstance
}
func (f *NthValueFunction) Add(value interface{}) {
@@ -510,8 +490,34 @@ func (f *NthValueFunction) Clone() AggregatorFunction {
clone := &NthValueFunction{
BaseFunction: f.BaseFunction,
values: make([]interface{}, len(f.values)),
n: f.n,
n: f.n, // 保持n参数
}
copy(clone.values, f.values)
return clone
}
// Init implements ParameterizedFunction interface
func (f *NthValueFunction) Init(args []interface{}) error {
if len(args) < 2 {
return fmt.Errorf("nth_value requires at least 2 arguments")
}
// Parse N parameter
n := 1
if nVal, ok := args[1].(int); ok {
n = nVal
} else if nVal, ok := args[1].(int64); ok {
n = int(nVal)
} else if nVal, ok := args[1].(float64); ok {
n = int(nVal)
} else {
return fmt.Errorf("nth_value n must be an integer, got %T", args[1])
}
if n <= 0 {
return fmt.Errorf("nth_value n must be positive, got %d", n)
}
f.n = n
return nil
}
+19
View File
@@ -61,6 +61,11 @@ type Function interface {
Execute(ctx *FunctionContext, args []interface{}) (interface{}, error)
// GetDescription returns the function description
GetDescription() string
// GetMinArgs returns the minimum number of arguments
GetMinArgs() int
// GetMaxArgs returns the maximum number of arguments (-1 means unlimited)
GetMaxArgs() int
}
// FunctionRegistry manages function registration and retrieval
@@ -87,6 +92,11 @@ func (r *FunctionRegistry) Register(fn Function) error {
r.mu.Lock()
defer r.mu.Unlock()
// Check if function is nil
if fn == nil {
return fmt.Errorf("function cannot be nil")
}
name := strings.ToLower(fn.GetName())
// Check if function already exists
@@ -200,6 +210,15 @@ func Unregister(name string) bool {
return globalRegistry.Unregister(name)
}
// Validate validates if a function exists in the registry
func Validate(name string) error {
_, exists := Get(name)
if !exists {
return fmt.Errorf("function '%s' not found", name)
}
return nil
}
// RegisterCustomFunction registers a custom function
func RegisterCustomFunction(name string, fnType FunctionType, category, description string,
minArgs, maxArgs int, executor func(ctx *FunctionContext, args []interface{}) (interface{}, error)) error {
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -3,7 +3,7 @@ module github.com/rulego/streamsql
go 1.18
require (
github.com/expr-lang/expr v1.17.2
github.com/expr-lang/expr v1.17.6
github.com/stretchr/testify v1.10.0
)
+2 -2
View File
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/expr-lang/expr v1.17.2 h1:o0A99O/Px+/DTjEnQiodAgOIK9PPxL8DtXhBRKC+Iso=
github.com/expr-lang/expr v1.17.2/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
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/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=

Some files were not shown because too many files have changed in this diff Show More