Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions plugin/output/clickhouse/column_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func NewColMapStringString() *ColMapStringString {
var _ InsaneColInput = (*ColMapStringString)(nil)

func (t *ColMapStringString) Append(node InsaneNode) error {
var m map[string]string
var kvs []proto.KV[string, string]
if node != nil {
var err error
m, err = node.AsMapStringString()
kvs, err = node.AsKVArray()
if err != nil {
return fmt.Errorf("converting node to the map of (string,string): %w", err)
}
}

t.ColMap.Append(m)
t.ColMap.AppendKV(kvs)

return nil
}
36 changes: 24 additions & 12 deletions plugin/output/clickhouse/insanenode.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package clickhouse

import (
"cmp"
"errors"
"fmt"
"net/netip"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -30,7 +32,7 @@ type InsaneNode interface {
AsIPv4() (proto.IPv4, error)
AsIPv6() (proto.IPv6, error)
AsTime(proto.Precision) (time.Time, error)
AsMapStringString() (map[string]string, error)
AsKVArray() ([]proto.KV[string, string], error) // sorted map[string]string

IsNull() bool
}
Expand Down Expand Up @@ -117,7 +119,7 @@ func (s StrictNode) AsStringArray() ([]string, error) {
return vals, nil
}

func (s StrictNode) AsMapStringString() (map[string]string, error) {
func (s StrictNode) AsKVArray() ([]proto.KV[string, string], error) {
if s.StrictNode == nil || s.IsNull() {
return nil, nil
}
Expand All @@ -127,22 +129,26 @@ func (s StrictNode) AsMapStringString() (map[string]string, error) {
return nil, err
}

m := make(map[string]string)
kvs := make([]proto.KV[string, string], 0, len(fields))
for _, f := range fields {
k := f.AsString()
vNode := f.AsFieldValue()
if vNode == nil || vNode.IsNull() {
m[k] = ""
kvs = append(kvs, proto.KV[string, string]{Key: k, Value: ""})
continue
}
v, err := vNode.MutateToStrict().AsString()
if err != nil {
return nil, err
}
m[k] = v
kvs = append(kvs, proto.KV[string, string]{Key: k, Value: v})
}

return m, nil
slices.SortStableFunc(kvs, func(a, b proto.KV[string, string]) int {
return cmp.Compare(a.Key, b.Key)
})

return kvs, nil
}

type NonStrictNode struct {
Expand Down Expand Up @@ -246,19 +252,25 @@ func (n NonStrictNode) AsTime(prec proto.Precision) (time.Time, error) {
return t, nil
}

func (n NonStrictNode) AsMapStringString() (map[string]string, error) {
func (n NonStrictNode) AsKVArray() ([]proto.KV[string, string], error) {
if n.Node == nil || n.Node.IsNull() || !n.IsObject() {
return nil, nil
}

m := make(map[string]string)
for _, f := range n.AsFields() {
fields := n.AsFields()

kvs := make([]proto.KV[string, string], 0, len(fields))
for _, f := range fields {
k := f.AsString()
v := nonStrictAsString(f.AsFieldValue())
m[k] = v
kvs = append(kvs, proto.KV[string, string]{Key: k, Value: v})
}

return m, nil
slices.SortStableFunc(kvs, func(a, b proto.KV[string, string]) int {
return cmp.Compare(a.Key, b.Key)
})

return kvs, nil
}

// ZeroValueNode returns a null-value for all called methods.
Expand Down Expand Up @@ -318,7 +330,7 @@ func (z ZeroValueNode) IsNull() bool {
return false
}

func (z ZeroValueNode) AsMapStringString() (map[string]string, error) {
func (z ZeroValueNode) AsKVArray() ([]proto.KV[string, string], error) {
return nil, nil
}

Expand Down
26 changes: 26 additions & 0 deletions plugin/output/clickhouse/insanenode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ func TestNodeAsTime(t *testing.T) {
test(`"1740643609"`, proto.PrecisionSecond, mustParseTime("2025-02-27T08:06:49.00Z"))
}

func TestNodeAsKVArray(t *testing.T) {
r := require.New(t)

root, err := insaneJSON.DecodeString(`{"b":"1","c":"2","b":"3","a":"4"}`)
defer insaneJSON.Release(root)
r.NoError(err)

// expected sorted keys
want := []proto.KV[string, string]{
{Key: "a", Value: "4"},
{Key: "b", Value: "1"},
{Key: "b", Value: "3"},
{Key: "c", Value: "2"},
}

n := NonStrictNode{root.Node}
got1, err := n.AsKVArray()
r.NoError(err)
r.Equal(want, got1)

sn := StrictNode{root.MutateToStrict()}
got2, err := sn.AsKVArray()
r.NoError(err)
r.Equal(want, got2)
}

func mustParseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
Expand Down
Loading