Skip to content

Commit

Permalink
handled RECORD data type for bigquery
Browse files Browse the repository at this point in the history
Signed-off-by: adarsh-jaiss <[email protected]>
  • Loading branch information
Adarsh-jaiss committed May 22, 2024
1 parent c833f7d commit f5a5810
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
23 changes: 20 additions & 3 deletions databases/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (b *BigQuery) Execute(query string) ([]byte, error) {
}

// Scan the result into a slice of slices
var results [][]interface{}
var results []map[string]interface{}
for rows.Next() {
// create a slice of values and pointers
values := make([]interface{}, len(columns))
Expand All @@ -139,7 +139,24 @@ func (b *BigQuery) Execute(query string) ([]byte, error) {
return nil, fmt.Errorf("error scanning row: %v", err)
}

results = append(results, values)
// Create a map for the current row
rowMap := make(map[string]interface{})
for i, colName := range columns {
// If the value is of type []byte (which is used for RECORD data types),
// we attempt to unmarshal it into a map[string]interface{}
if b, ok := values[i].([]byte); ok {
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err == nil {
rowMap[colName] = m
} else {
rowMap[colName] = values[i]
}
} else {
rowMap[colName] = values[i]
}
}

results = append(results, rowMap)
}

// Check for errors from iterating over rows
Expand All @@ -148,7 +165,7 @@ func (b *BigQuery) Execute(query string) ([]byte, error) {
}

// Convert the result to JSON
queryResult := types.QueryResult{
queryResult := types.BigQueryResult{
Columns: columns,
Rows: results,
}
Expand Down
7 changes: 7 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ type QueryResult struct {
Error string `json:"error"` // Error is any error that occurred while executing the query.
}

type BigQueryResult struct {
Columns []string `json:"columns"` // Columns are the names of the columns in the result.
Rows []map[string]interface{} `json:"rows"` // Rows are the rows in the result.
Time int64 `json:"time"` // Time is the time it took to execute the query.
Error string `json:"error"` // Error is any error that occurred while executing the query.
}

// DbType represents a type of SQL database.
type DbType int

Expand Down

0 comments on commit f5a5810

Please sign in to comment.