Skip to content

Commit aa3a42a

Browse files
committed
Fix issue with query where any query with 'where' clause panicked
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 aa3a42a

File tree

5 files changed

+51
-38
lines changed

5 files changed

+51
-38
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25-
"pb/pkg/model"
2625
"time"
2726

27+
"pb/pkg/model"
28+
2829
tea "github.com/charmbracelet/bubbletea"
2930
"github.com/spf13/cobra"
3031
)
@@ -46,7 +47,7 @@ var query = &cobra.Command{
4647
Use: "query [query] [flags]",
4748
Example: " pb query \"select * from frontend\" --from=10m --to=now",
4849
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.",
50+
Long: "\nRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
5051
Args: cobra.MaximumNArgs(1),
5152
PreRunE: PreRunDefaultProfile,
5253
RunE: func(command *cobra.Command, args []string) error {
@@ -72,15 +73,15 @@ var query = &cobra.Command{
7273
start = defaultStart
7374
}
7475

75-
end, _ := command.Flags().GetString(endFlag)
76+
end, err := command.Flags().GetString(endFlag)
7677
if err != nil {
7778
return err
7879
}
7980
if end == "" {
8081
end = defaultEnd
8182
}
8283

83-
interactive, _ := command.Flags().GetBool(interactiveFlag)
84+
interactive, err := command.Flags().GetBool(interactiveFlag)
8485
if err != nil {
8586
return err
8687
}

cmd/role.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
"fmt"
2222
"io"
2323
"os"
24-
"pb/pkg/model/role"
2524
"strings"
2625
"sync"
2726

27+
"pb/pkg/model/role"
28+
2829
tea "github.com/charmbracelet/bubbletea"
2930
"github.com/charmbracelet/lipgloss"
3031
"github.com/spf13/cobra"
@@ -62,8 +63,8 @@ func (user *RoleData) Render() string {
6263
}
6364

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

cmd/tail.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/base64"
2323
"encoding/json"
2424
"fmt"
25+
2526
"pb/pkg/config"
2627

2728
"github.com/apache/arrow/go/v13/arrow/array"
@@ -35,7 +36,7 @@ import (
3536
var TailCmd = &cobra.Command{
3637
Use: "tail stream-name",
3738
Example: " pb tail backend_logs",
38-
Short: "tail a log stream",
39+
Short: "Stream live events from a log stream",
3940
Args: cobra.ExactArgs(1),
4041
PreRunE: PreRunDefaultProfile,
4142
RunE: func(cmd *cobra.Command, args []string) error {

pkg/model/query.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
"math"
2424
"net/http"
2525
"os"
26-
"pb/pkg/config"
27-
"pb/pkg/iterator"
28-
"regexp"
2926
"strings"
3027
"sync"
3128
"time"
3229

30+
"pb/pkg/config"
31+
"pb/pkg/iterator"
32+
3333
"github.com/charmbracelet/bubbles/help"
3434
"github.com/charmbracelet/bubbles/key"
3535
"github.com/charmbracelet/bubbles/textarea"
@@ -167,33 +167,31 @@ func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, F
167167
startTime = startTime.Truncate(time.Minute)
168168
endTime = endTime.Truncate(time.Minute).Add(time.Minute)
169169

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
170+
table := streamNameFromQuery(m.query.Value())
171+
if table != "" {
172+
iter := iterator.NewQueryIterator(
173+
startTime, endTime,
174+
false,
175+
func(t1, t2 time.Time) (QueryData, FetchResult) {
176+
client := &http.Client{
177+
Timeout: time.Second * 50,
178+
}
179+
return fetchData(client, &m.profile, m.query.Value(), t1.UTC().Format(time.RFC3339), t2.UTC().Format(time.RFC3339))
180+
},
181+
func(t1, t2 time.Time) bool {
182+
client := &http.Client{
183+
Timeout: time.Second * 50,
184+
}
185+
res, err := fetchData(client, &m.profile, "select count(*) as count from "+table, m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
186+
if err == fetchErr {
187+
return false
188+
}
189+
count := res.Records[0]["count"].(float64)
190+
return count > 0
191+
})
192+
return &iter
174193
}
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
194+
return nil
197195
}
198196

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

0 commit comments

Comments
 (0)