-
Notifications
You must be signed in to change notification settings - Fork 412
/
query_flags.go
153 lines (132 loc) · 4.09 KB
/
query_flags.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/spf13/cast"
"px.dev/pixie/src/carnot/planpb"
)
// The prefix which a PL Config line should begin with.
const plConfigPrefix = "#px:set "
// Default values for config flags. If a flag is not included in this map,
// it is not considered a valid flag that can be set.
var defaultQueryFlags = map[string]interface{}{
"explain": false,
"analyze": false,
"max_output_rows_per_table": 10000,
}
// QueryFlags represents a set of Pixie configuration flags.
type QueryFlags struct {
flags map[string]interface{}
}
func newQueryFlags() *QueryFlags {
return &QueryFlags{
flags: map[string]interface{}{},
}
}
// get gets the value of the given flag. It returns the default value if the flag
// has not been set.
func (f *QueryFlags) get(key string) interface{} {
// Check to see if the key is in the map.
if val, ok := f.flags[key]; ok {
return val
}
// Check if the key is defined in the default configs.
if val, ok := defaultQueryFlags[key]; ok {
return val
}
return nil
}
// GetInt64 gets the value of the given flag as an int64.
func (f *QueryFlags) GetInt64(key string) int64 {
val := f.get(key)
return cast.ToInt64(val)
}
// GetBool gets the value of the given flag as a bool.
func (f *QueryFlags) GetBool(key string) bool {
val := f.get(key)
return cast.ToBool(val)
}
// GetString gets the value of the given flag as a string.
func (f *QueryFlags) GetString(key string) string {
val := f.get(key)
return cast.ToString(val)
}
// GetFloat64 gets the value of the given flag as a float.
func (f *QueryFlags) GetFloat64(key string) float64 {
val := f.get(key)
return cast.ToFloat64(val)
}
func (f *QueryFlags) set(key string, value string) error {
// Ensure that the key is a valid flag that can be set, by checking it is
// defined in the defaults.
if defVal, ok := defaultQueryFlags[key]; ok {
var typedVal interface{}
var err error
switch defVal.(type) {
case int:
typedVal, err = strconv.ParseInt(value, 10, 64)
case float64:
typedVal, err = strconv.ParseFloat(value, 64)
case string:
typedVal = value
case bool:
typedVal, err = strconv.ParseBool(value)
}
if err != nil {
return err
}
f.flags[key] = typedVal
return nil
}
return fmt.Errorf("%s is not a valid flag", key)
}
// GetPlanOptions creates the plan option proto from the specified query flags.
func (f *QueryFlags) GetPlanOptions() *planpb.PlanOptions {
return &planpb.PlanOptions{
Explain: f.GetBool("explain"),
Analyze: f.GetBool("analyze"),
MaxOutputRowsPerTable: f.GetInt64("max_output_rows_per_table"),
}
}
// ParseQueryFlags takes a query string containing some config options and generates
// a QueryFlags object that can be used to retrieve those options.
func ParseQueryFlags(queryStr string) (*QueryFlags, error) {
qf := newQueryFlags()
for _, line := range strings.Split(strings.TrimSuffix(queryStr, "\n"), "\n") {
// If the line begins with the PL config prefix, attempt to parse the line.
if strings.HasPrefix(line, plConfigPrefix) {
queryComponents := strings.Split(line, " ")
if len(queryComponents) != 2 {
return nil, errors.New("Config setting is malformed")
}
keyVal := strings.Split(queryComponents[1], "=")
if len(keyVal) != 2 {
return nil, errors.New("Config setting is malformed")
}
err := qf.set(keyVal[0], keyVal[1])
if err != nil {
return nil, err
}
}
}
return qf, nil
}