Skip to content

Commit

Permalink
Fix STAR select issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pgollangi committed Jan 3, 2023
1 parent d367704 commit 95db299
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
19 changes: 12 additions & 7 deletions pkg/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,30 @@ func (sel *SelectStatement) readResults(docs *firestore.DocumentIterator, select
}

// Insert COLUMNS for START (*) selection
startIdx := -1
starIdx := -1
for idx, column := range selectedColumns {
if column.colType == Star {
startIdx = idx
starIdx = idx
break
}
}
if startIdx != -1 {
if starIdx != -1 {
// Remove star Select as we insert real columns
selectedColumns = append(selectedColumns[:starIdx], selectedColumns[starIdx+1:]...)
data := document.Data()
for key := range data {
newCol := &selectColumn{
field: key,
alias: key,
colType: Field,
}
if len(selectedColumns) == startIdx {
if len(selectedColumns) == starIdx {
selectedColumns = append(selectedColumns, newCol)
} else {
selectedColumns = append(selectedColumns[:startIdx+1], selectedColumns[startIdx:]...)
selectedColumns[startIdx] = newCol
selectedColumns = append(selectedColumns[:starIdx+1], selectedColumns[starIdx:]...)
selectedColumns[starIdx] = newCol
}
startIdx++
starIdx++
}
}

Expand Down Expand Up @@ -208,6 +211,7 @@ func (sel *SelectStatement) selectFields(fQuery firestore.Query, sQuery *sqlpars

func (sel *SelectStatement) collectSelectFields(columns []*selectColumn) []string {
var fields []string
loop:
for _, col := range columns {
switch col.colType {
case Field:
Expand All @@ -220,6 +224,7 @@ func (sel *SelectStatement) collectSelectFields(columns []*selectColumn) []strin
case Star:
// Don't select fields on firestore.Query to return all fields
fields = []string{}
break loop
}
}
return fields
Expand Down
19 changes: 18 additions & 1 deletion pkg/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ var selectTests = []TestExpect{
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
query: "select id as uid, * from users",
columns: []string{"uid", "id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
query: "select *, username as uname from users",
columns: []string{"id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
query: "select id as uid, *, username as uname from users",
columns: []string{"uid", "id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
query: "select id, email, address from users",
columns: []string{"id", "email", "address"},
Expand Down Expand Up @@ -152,7 +167,7 @@ func TestSelectQueries(t *testing.T) {
} else {
sort.Strings(actual.Columns)
sort.Strings(tt.columns)
if stringSlicesEqual(actual.Columns, tt.columns) {
if !stringSlicesEqual(actual.Columns, tt.columns) {
t.Errorf("QueryResult.Fields(%v): expected %v, actual %v", tt.query, tt.columns, actual.Columns)
}
if tt.length != "" && len(actual.Records) != first(strconv.Atoi(tt.length)) {
Expand All @@ -171,10 +186,12 @@ func first(n int, _ error) int {

func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
fmt.Printf("NO LEN %d != %d \n", len(a), len(b))
return false
}
for i, v := range a {
if v != b[i] {
fmt.Printf("NOT EQUAL %s != %s", v, b[i])
return false
}
}
Expand Down

0 comments on commit 95db299

Please sign in to comment.