-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add table and column metadata for bigquery (#141)
Co-authored-by: poundifdef <jay@scratchdata.com>
- Loading branch information
1 parent
82cb218
commit 25a72e4
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
) | ||
|
||
func (a *ScratchDataAPIStruct) Tables(w http.ResponseWriter, r *http.Request) { | ||
databaseID := a.AuthGetDatabaseID(r.Context()) | ||
dest, err := a.destinationManager.Destination(r.Context(), databaseID) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
tables, err := dest.Tables() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
render.JSON(w, r, tables) | ||
} | ||
|
||
func (a *ScratchDataAPIStruct) Columns(w http.ResponseWriter, r *http.Request) { | ||
table := chi.URLParam(r, "table") | ||
databaseID := a.AuthGetDatabaseID(r.Context()) | ||
|
||
dest, err := a.destinationManager.Destination(r.Context(), databaseID) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
columns, err := dest.Columns(table) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
render.JSON(w, r, columns) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package bigquery | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/scratchdata/scratchdata/models" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
func (b *BigQueryServer) Columns(table string) ([]models.Column, error) { | ||
rc := []models.Column{} | ||
|
||
tokens := strings.Split(table, ".") | ||
if len(tokens) != 2 { | ||
return nil, errors.New("Table should be in the format dataset.table") | ||
} | ||
|
||
datasetID := tokens[0] | ||
tableID := tokens[1] | ||
|
||
ctx := context.TODO() | ||
tableInfo := b.conn.Dataset(datasetID).Table(tableID) | ||
meta, err := tableInfo.Metadata(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, field := range meta.Schema { | ||
rc = append(rc, models.Column{ | ||
Name: field.Name, | ||
Type: string(field.Type), | ||
}) | ||
} | ||
return rc, nil | ||
} | ||
|
||
func (b *BigQueryServer) Tables() ([]string, error) { | ||
rc := []string{} | ||
|
||
ctx := context.TODO() | ||
it := b.conn.Datasets(ctx) | ||
for { | ||
dataset, err := it.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// For each dataset, list all tables. | ||
tableIt := dataset.Tables(ctx) | ||
for { | ||
table, err := tableIt.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
log.Error().Err(err).Str("dataset_id", dataset.DatasetID).Msg("Failed to list tables") | ||
continue | ||
} | ||
rc = append(rc, fmt.Sprintf("%s.%s", dataset.DatasetID, table.TableID)) | ||
} | ||
} | ||
|
||
return rc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/scratchdata/scratchdata/models" | ||
) | ||
|
||
func (b *ClickhouseServer) Columns(table string) ([]models.Column, error) { | ||
return []models.Column{}, errors.New("not implemented") | ||
} | ||
|
||
func (b *ClickhouseServer) Tables() ([]string, error) { | ||
return []string{}, errors.New("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package duckdb | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/scratchdata/scratchdata/models" | ||
) | ||
|
||
func (b *DuckDBServer) Columns(table string) ([]models.Column, error) { | ||
return []models.Column{}, errors.New("not implemented") | ||
} | ||
|
||
func (b *DuckDBServer) Tables() ([]string, error) { | ||
return []string{}, errors.New("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package redshift | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/scratchdata/scratchdata/models" | ||
) | ||
|
||
func (b *RedshiftServer) Columns(table string) ([]models.Column, error) { | ||
return []models.Column{}, errors.New("not implemented") | ||
} | ||
|
||
func (b *RedshiftServer) Tables() ([]string, error) { | ||
return []string{}, errors.New("not implemented") | ||
} |