Skip to content

Commit c5b7cb2

Browse files
authored
Fix issue with query where any query with 'where' clause panicked (#36)
This PR fixes the nil pointer issue for queries containing where clause. Also fixes some help messages to be at par with other messages. Also update the goreleaser config to ensure 'v' is added in published release versions.
1 parent c11aaf6 commit c5b7cb2

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ builds:
1717
- -trimpath
1818
- -tags=kqueue
1919
ldflags:
20-
- -s -w -X main.Version={{ .Version }} -X main.Commit={{ .ShortCommit }}
20+
- -s -w -X main.Version=v{{ .Version }} -X main.Commit={{ .ShortCommit }}
2121

2222
archives:
2323
- format: tar.gz

cmd/query.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var query = &cobra.Command{
4646
Use: "query [query] [flags]",
4747
Example: " pb query \"select * from frontend\" --from=10m --to=now",
4848
Short: "Run SQL query on a log stream",
49-
Long: "\nqRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
49+
Long: "\nRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
5050
Args: cobra.MaximumNArgs(1),
5151
PreRunE: PreRunDefaultProfile,
5252
RunE: func(command *cobra.Command, args []string) error {
@@ -72,15 +72,15 @@ var query = &cobra.Command{
7272
start = defaultStart
7373
}
7474

75-
end, _ := command.Flags().GetString(endFlag)
75+
end, err := command.Flags().GetString(endFlag)
7676
if err != nil {
7777
return err
7878
}
7979
if end == "" {
8080
end = defaultEnd
8181
}
8282

83-
interactive, _ := command.Flags().GetBool(interactiveFlag)
83+
interactive, err := command.Flags().GetBool(interactiveFlag)
8484
if err != nil {
8585
return err
8686
}

cmd/role.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (user *RoleData) Render() string {
6262
}
6363

6464
var AddRoleCmd = &cobra.Command{
65-
Use: "upsert role-name",
66-
Example: " pb role upsert ingestors",
65+
Use: "add role-name",
66+
Example: " pb role add ingestors",
6767
Short: "Add a new role",
6868
Args: cobra.ExactArgs(1),
6969
RunE: func(cmd *cobra.Command, args []string) error {

cmd/tail.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
var TailCmd = &cobra.Command{
3636
Use: "tail stream-name",
3737
Example: " pb tail backend_logs",
38-
Short: "tail a log stream",
38+
Short: "Stream live events from a log stream",
3939
Args: cobra.ExactArgs(1),
4040
PreRunE: PreRunDefaultProfile,
4141
RunE: func(cmd *cobra.Command, args []string) error {

pkg/model/query.go

+36-27
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os"
2626
"pb/pkg/config"
2727
"pb/pkg/iterator"
28-
"regexp"
2928
"strings"
3029
"sync"
3130
"time"
@@ -167,33 +166,31 @@ func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, F
167166
startTime = startTime.Truncate(time.Minute)
168167
endTime = endTime.Truncate(time.Minute).Add(time.Minute)
169168

170-
regex := regexp.MustCompile(`^select\s+(?:\*|\w+(?:,\s*\w+)*)\s+from\s+(\w+)(?:\s+;)?$`)
171-
matches := regex.FindStringSubmatch(m.query.Value())
172-
if matches == nil {
173-
return nil
169+
table := streamNameFromQuery(m.query.Value())
170+
if table != "" {
171+
iter := iterator.NewQueryIterator(
172+
startTime, endTime,
173+
false,
174+
func(t1, t2 time.Time) (QueryData, FetchResult) {
175+
client := &http.Client{
176+
Timeout: time.Second * 50,
177+
}
178+
return fetchData(client, &m.profile, m.query.Value(), t1.UTC().Format(time.RFC3339), t2.UTC().Format(time.RFC3339))
179+
},
180+
func(t1, t2 time.Time) bool {
181+
client := &http.Client{
182+
Timeout: time.Second * 50,
183+
}
184+
res, err := fetchData(client, &m.profile, "select count(*) as count from "+table, m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
185+
if err == fetchErr {
186+
return false
187+
}
188+
count := res.Records[0]["count"].(float64)
189+
return count > 0
190+
})
191+
return &iter
174192
}
175-
table := matches[1]
176-
iter := iterator.NewQueryIterator(
177-
startTime, endTime,
178-
false,
179-
func(t1, t2 time.Time) (QueryData, FetchResult) {
180-
client := &http.Client{
181-
Timeout: time.Second * 50,
182-
}
183-
return fetchData(client, &m.profile, m.query.Value(), t1.UTC().Format(time.RFC3339), t2.UTC().Format(time.RFC3339))
184-
},
185-
func(t1, t2 time.Time) bool {
186-
client := &http.Client{
187-
Timeout: time.Second * 50,
188-
}
189-
res, err := fetchData(client, &m.profile, "select count(*) as count from "+table, m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
190-
if err == fetchErr {
191-
return false
192-
}
193-
count := res.Records[0]["count"].(float64)
194-
return count > 0
195-
})
196-
return &iter
193+
return nil
197194
}
198195

199196
func NewQueryModel(profile config.Profile, queryStr string, startTime, endTime time.Time) QueryModel {
@@ -653,3 +650,15 @@ func countDigits(num int) int {
653650
numDigits := int(math.Log10(math.Abs(float64(num)))) + 1
654651
return numDigits
655652
}
653+
654+
func streamNameFromQuery(query string) string {
655+
stream := ""
656+
tokens := strings.Split(query, " ")
657+
for i, token := range tokens {
658+
if token == "from" {
659+
stream = tokens[i+1]
660+
break
661+
}
662+
}
663+
return stream
664+
}

0 commit comments

Comments
 (0)