Skip to content

Commit

Permalink
Fix issue with query where any query with 'where' clause panicked
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nitisht committed Nov 13, 2023
1 parent c11aaf6 commit aa3a42a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ builds:
- -trimpath
- -tags=kqueue
ldflags:
- -s -w -X main.Version={{ .Version }} -X main.Commit={{ .ShortCommit }}
- -s -w -X main.Version=v{{ .Version }} -X main.Commit={{ .ShortCommit }}

archives:
- format: tar.gz
Expand Down
9 changes: 5 additions & 4 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"fmt"
"io"
"os"
"pb/pkg/model"
"time"

"pb/pkg/model"

tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
Expand All @@ -46,7 +47,7 @@ var query = &cobra.Command{
Use: "query [query] [flags]",
Example: " pb query \"select * from frontend\" --from=10m --to=now",
Short: "Run SQL query on a log stream",
Long: "\nqRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
Long: "\nRun SQL query on a log stream. Default output format is json. Use -i flag to open interactive table view.",
Args: cobra.MaximumNArgs(1),
PreRunE: PreRunDefaultProfile,
RunE: func(command *cobra.Command, args []string) error {
Expand All @@ -72,15 +73,15 @@ var query = &cobra.Command{
start = defaultStart
}

end, _ := command.Flags().GetString(endFlag)
end, err := command.Flags().GetString(endFlag)
if err != nil {
return err
}
if end == "" {
end = defaultEnd
}

interactive, _ := command.Flags().GetBool(interactiveFlag)
interactive, err := command.Flags().GetBool(interactiveFlag)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"fmt"
"io"
"os"
"pb/pkg/model/role"
"strings"
"sync"

"pb/pkg/model/role"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,8 +63,8 @@ func (user *RoleData) Render() string {
}

var AddRoleCmd = &cobra.Command{
Use: "upsert role-name",
Example: " pb role upsert ingestors",
Use: "add role-name",
Example: " pb role add ingestors",
Short: "Add a new role",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"

"pb/pkg/config"

"github.com/apache/arrow/go/v13/arrow/array"
Expand All @@ -35,7 +36,7 @@ import (
var TailCmd = &cobra.Command{
Use: "tail stream-name",
Example: " pb tail backend_logs",
Short: "tail a log stream",
Short: "Stream live events from a log stream",
Args: cobra.ExactArgs(1),
PreRunE: PreRunDefaultProfile,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
68 changes: 39 additions & 29 deletions pkg/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"math"
"net/http"
"os"
"pb/pkg/config"
"pb/pkg/iterator"
"regexp"
"strings"
"sync"
"time"

"pb/pkg/config"
"pb/pkg/iterator"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
Expand Down Expand Up @@ -167,33 +167,31 @@ func createIteratorFromModel(m *QueryModel) *iterator.QueryIterator[QueryData, F
startTime = startTime.Truncate(time.Minute)
endTime = endTime.Truncate(time.Minute).Add(time.Minute)

regex := regexp.MustCompile(`^select\s+(?:\*|\w+(?:,\s*\w+)*)\s+from\s+(\w+)(?:\s+;)?$`)
matches := regex.FindStringSubmatch(m.query.Value())
if matches == nil {
return nil
table := streamNameFromQuery(m.query.Value())
if table != "" {
iter := iterator.NewQueryIterator(
startTime, endTime,
false,
func(t1, t2 time.Time) (QueryData, FetchResult) {
client := &http.Client{
Timeout: time.Second * 50,
}
return fetchData(client, &m.profile, m.query.Value(), t1.UTC().Format(time.RFC3339), t2.UTC().Format(time.RFC3339))
},
func(t1, t2 time.Time) bool {
client := &http.Client{
Timeout: time.Second * 50,
}
res, err := fetchData(client, &m.profile, "select count(*) as count from "+table, m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
if err == fetchErr {
return false
}
count := res.Records[0]["count"].(float64)
return count > 0
})
return &iter
}
table := matches[1]
iter := iterator.NewQueryIterator(
startTime, endTime,
false,
func(t1, t2 time.Time) (QueryData, FetchResult) {
client := &http.Client{
Timeout: time.Second * 50,
}
return fetchData(client, &m.profile, m.query.Value(), t1.UTC().Format(time.RFC3339), t2.UTC().Format(time.RFC3339))
},
func(t1, t2 time.Time) bool {
client := &http.Client{
Timeout: time.Second * 50,
}
res, err := fetchData(client, &m.profile, "select count(*) as count from "+table, m.timeRange.StartValueUtc(), m.timeRange.EndValueUtc())
if err == fetchErr {
return false
}
count := res.Records[0]["count"].(float64)
return count > 0
})
return &iter
return nil
}

func NewQueryModel(profile config.Profile, queryStr string, startTime, endTime time.Time) QueryModel {
Expand Down Expand Up @@ -653,3 +651,15 @@ func countDigits(num int) int {
numDigits := int(math.Log10(math.Abs(float64(num)))) + 1
return numDigits
}

func streamNameFromQuery(query string) string {
stream := ""
tokens := strings.Split(query, " ")
for i, token := range tokens {
if token == "from" {
stream = tokens[i+1]
break
}
}
return stream
}

0 comments on commit aa3a42a

Please sign in to comment.