2024-02-24 22:25:51 +00:00
|
|
|
package memoryexecutor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2024-02-27 20:38:59 +00:00
|
|
|
"github.com/pikami/cosmium/internal/logger"
|
2024-02-24 22:25:51 +00:00
|
|
|
"github.com/pikami/cosmium/parsers"
|
|
|
|
)
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) array_Concat(arguments []interface{}, row RowType) []interface{} {
|
2024-02-24 22:25:51 +00:00
|
|
|
var result []interface{}
|
|
|
|
for _, arg := range arguments {
|
2024-02-25 20:13:04 +00:00
|
|
|
array := c.parseArray(arg, row)
|
2024-02-24 22:25:51 +00:00
|
|
|
result = append(result, array...)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) array_Length(arguments []interface{}, row RowType) int {
|
|
|
|
array := c.parseArray(arguments[0], row)
|
2024-02-24 22:25:51 +00:00
|
|
|
if array == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(array)
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) array_Slice(arguments []interface{}, row RowType) []interface{} {
|
2024-02-24 22:25:51 +00:00
|
|
|
var ok bool
|
|
|
|
var start int
|
|
|
|
var length int
|
2024-02-25 20:13:04 +00:00
|
|
|
array := c.parseArray(arguments[0], row)
|
|
|
|
startEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
2024-02-24 22:25:51 +00:00
|
|
|
|
|
|
|
if arguments[2] != nil {
|
2024-02-25 20:13:04 +00:00
|
|
|
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
2024-02-24 22:25:51 +00:00
|
|
|
|
|
|
|
if length, ok = lengthEx.(int); !ok {
|
2024-02-27 20:38:59 +00:00
|
|
|
logger.Error("array_Slice - got length parameters of wrong type")
|
2024-02-24 22:25:51 +00:00
|
|
|
return []interface{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if start, ok = startEx.(int); !ok {
|
2024-02-27 20:38:59 +00:00
|
|
|
logger.Error("array_Slice - got start parameters of wrong type")
|
2024-02-24 22:25:51 +00:00
|
|
|
return []interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if start < 0 {
|
|
|
|
start = len(array) + start
|
|
|
|
}
|
|
|
|
|
|
|
|
if start < 0 {
|
|
|
|
start = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if array == nil || start >= len(array) {
|
|
|
|
return []interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
end := start + length
|
|
|
|
if end > len(array) {
|
|
|
|
end = len(array)
|
|
|
|
}
|
|
|
|
return array[start:end]
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) set_Intersect(arguments []interface{}, row RowType) []interface{} {
|
|
|
|
set1 := c.parseArray(arguments[0], row)
|
|
|
|
set2 := c.parseArray(arguments[1], row)
|
2024-02-24 22:25:51 +00:00
|
|
|
|
|
|
|
intersection := make(map[interface{}]struct{})
|
|
|
|
if set1 == nil || set2 == nil {
|
|
|
|
return []interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range set1 {
|
|
|
|
intersection[item] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []interface{}
|
|
|
|
for _, item := range set2 {
|
|
|
|
if _, exists := intersection[item]; exists {
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) set_Union(arguments []interface{}, row RowType) []interface{} {
|
|
|
|
set1 := c.parseArray(arguments[0], row)
|
|
|
|
set2 := c.parseArray(arguments[1], row)
|
2024-02-24 22:25:51 +00:00
|
|
|
|
|
|
|
var result []interface{}
|
|
|
|
union := make(map[interface{}]struct{})
|
|
|
|
for _, item := range set1 {
|
|
|
|
if _, ok := union[item]; !ok {
|
|
|
|
union[item] = struct{}{}
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range set2 {
|
|
|
|
if _, ok := union[item]; !ok {
|
|
|
|
union[item] = struct{}{}
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
func (c memoryExecutorContext) parseArray(argument interface{}, row RowType) []interface{} {
|
2024-02-24 22:25:51 +00:00
|
|
|
exItem := argument.(parsers.SelectItem)
|
2024-02-25 20:13:04 +00:00
|
|
|
ex := c.getFieldValue(exItem, row)
|
2024-02-24 22:25:51 +00:00
|
|
|
|
|
|
|
arrValue := reflect.ValueOf(ex)
|
|
|
|
if arrValue.Kind() != reflect.Slice {
|
2024-02-27 20:38:59 +00:00
|
|
|
logger.Error("parseArray got parameters of wrong type")
|
2024-02-24 22:25:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]interface{}, arrValue.Len())
|
|
|
|
|
|
|
|
for i := 0; i < arrValue.Len(); i++ {
|
|
|
|
result[i] = arrValue.Index(i).Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|