-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_example.go
More file actions
107 lines (95 loc) · 2.93 KB
/
inspect_example.go
File metadata and controls
107 lines (95 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import "C"
import (
"fmt"
"strings"
)
// inspect is example of UDF function to inspect different arguments
// it is stateless, and just output info about provided args as string
// You can load it into the daemon with
// CREATE FUNCTION inspect RETURNS STRING SONAME 'udfexample.so';
// most comprehensive usage example:
// SELECT inspect(id*1000,tags,1.145,tagl,'abc',WEIGHT(),jsn.data,jsn,packedfactors())
// FROM testall where match(' what | foo | what ') option ranker=expr('1')
// in this function we check only num of args (1 or 2) and the type of them (must be string)
// Nothing from init section is necessary and used.
//export inspect_init
func inspect_init(init *SPH_UDF_INIT, args *SPH_UDF_ARGS, errmsg *ERR_MSG) int32 {
return 0
}
func (tp SPH_UDF_TYPE) String() string {
switch tp {
case SPH_UDF_TYPE_UINT32:
return "UINT32"
case SPH_UDF_TYPE_UINT32SET:
return "UINT32SET"
case SPH_UDF_TYPE_INT64:
return "INT64"
case SPH_UDF_TYPE_FLOAT:
return "FLOAT"
case SPH_UDF_TYPE_STRING:
return "STRING"
case SPH_UDF_TYPE_INT64SET:
return "INT64SET"
case SPH_UDF_TYPE_FACTORS:
return "FACTORS"
case SPH_UDF_TYPE_JSON:
return "JSON"
default:
return fmt.Sprintf("unknown(%d)", tp)
}
}
func format_mva32(values []uint32) string {
var mvas []string
for _, num := range values {
mvas = append(mvas, fmt.Sprintf("%d", num))
}
return "(" + strings.Join(mvas, ",") + ")"
}
func format_mva64(values []int64) string {
var mvas []string
for _, num := range values {
mvas = append(mvas, fmt.Sprintf("%d", num))
}
return "(" + strings.Join(mvas, ",") + ")"
}
// here we execute provided action: extract arguments,
// and make print details about each of them
//export inspect
func inspect(init *SPH_UDF_INIT, args *SPH_UDF_ARGS, err *ERR_FLAG) uintptr {
var res string
res += fmt.Sprintf("passed %d args\n", args.arg_count)
for i := 0; i < int(args.arg_count); i++ {
tp := args.arg_type(i)
res += fmt.Sprintf("arg %d: type %v", i, tp)
// print len, if necessary
switch tp {
case SPH_UDF_TYPE_UINT32SET, SPH_UDF_TYPE_STRING, SPH_UDF_TYPE_INT64SET, SPH_UDF_TYPE_JSON:
res += fmt.Sprintf(", len %d", args.lenval(i))
default:
}
res += ": "
// output the value
// here you can see how different types may be extracted from args
switch tp {
case SPH_UDF_TYPE_UINT32:
res += fmt.Sprintf("%v", *(*uint32)(args.valueptr(i)))
case SPH_UDF_TYPE_UINT32SET:
res += format_mva32(args.mva32(i))
case SPH_UDF_TYPE_INT64:
res += fmt.Sprintf("%v", *(*int64)(args.valueptr(i)))
case SPH_UDF_TYPE_FLOAT:
res += fmt.Sprintf("%v", *(*float32)(args.valueptr(i)))
case SPH_UDF_TYPE_STRING, SPH_UDF_TYPE_JSON:
res += "'" + args.stringval(i) + "'"
case SPH_UDF_TYPE_INT64SET:
res += format_mva64(args.mva64(i))
case SPH_UDF_TYPE_FACTORS:
res += format_factors(args.valueptr(i))
default:
res += fmt.Sprintf("other (%d) todo!", tp)
}
res += "\n" // trailing cr
}
return args.return_string(res)
}