Skip to content

Commit

Permalink
Merge pull request #239 from danielmerken/fix-null-empty-arrays
Browse files Browse the repository at this point in the history
fix handling of empty/null arrays
  • Loading branch information
goccy authored Dec 7, 2023
2 parents 11d48fb + 15d0cc2 commit f08636e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/contentdata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ func (r *Repository) Query(ctx context.Context, tx *connection.Tx, projectID, da
resultValues := make([]interface{}, 0, len(values))
for idx, value := range values {
v := reflect.ValueOf(value).Elem().Interface()
if v == nil && fields[idx].Mode == string(types.RepeatedMode) {
// GoogleSQL for BigQuery translates a NULL array into an empty array in the query result
v = []interface{}{}
}
cell, err := r.convertValueToCell(v)
if err != nil {
return nil, fmt.Errorf("failed to convert value to cell: %w", err)
Expand Down Expand Up @@ -315,8 +319,8 @@ func (r *Repository) convertValueToCell(value interface{}) (*internaltypes.Table
}
// array type
var (
cells []*internaltypes.TableCell
totalBytes int64
cells = []*internaltypes.TableCell{}
totalBytes int64 = 0
)
for i := 0; i < rv.Len(); i++ {
cell, err := r.convertValueToCell(rv.Index(i).Interface())
Expand Down
42 changes: 42 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,48 @@ func TestSimpleQuery(t *testing.T) {
t.Log("row = ", row)
}
})

t.Run("empty array", func(t *testing.T) {
query := client.Query("SELECT []")
it, err := query.Read(ctx)
if err != nil {
t.Fatal(err)
}
var row []bigquery.Value
for {
if err := it.Next(&row); err != nil {
if err == iterator.Done {
break
}
t.Fatal(err)
}
t.Log("row = ", row)
}
if len(row) != 1 || row[0] == nil {
t.Fatal("Failed to query empty ARRAY")
}
})

t.Run("null array", func(t *testing.T) {
query := client.Query("SELECT CAST(NULL AS ARRAY<STRING>)")
it, err := query.Read(ctx)
if err != nil {
t.Fatal(err)
}
var row []bigquery.Value
for {
if err := it.Next(&row); err != nil {
if err == iterator.Done {
break
}
t.Fatal(err)
}
t.Log("row = ", row)
}
if len(row) != 1 || row[0] == nil {
t.Fatal("Failed to query null ARRAY")
}
})
}

func TestDataset(t *testing.T) {
Expand Down

0 comments on commit f08636e

Please sign in to comment.