Skip to content

Commit

Permalink
Clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 31, 2024
1 parent 57c7362 commit edd1e41
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
66 changes: 37 additions & 29 deletions clients/shared/table_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,8 @@ type GetTableCfgArgs struct {
DropDeletedColumns bool
}

func (g GetTableCfgArgs) GetTableConfig() (*types.DwhTableConfig, error) {
if tableConfig := g.ConfigMap.TableConfigCache(g.TableID); tableConfig != nil {
return tableConfig, nil
}

var tableMissing bool
sqlRows, err := g.Dwh.Query(g.Query, g.Args...)
if err != nil {
if g.Dwh.Dialect().IsTableDoesNotExistErr(err) {
// This branch is currently only used by Snowflake.
// Swallow the error, make sure all the metadata is created
tableMissing = true
err = nil
} else {
return nil, fmt.Errorf("failed to query %T, err: %w, query: %q", g.Dwh, err, g.Query)
}
}

rows, err := sql.RowsToObjects(sqlRows)
if err != nil {
return nil, fmt.Errorf("failed to convert rows to objects: %w", err)
}

if len(rows) == 0 {
tableMissing = true
}

var cols columns.Columns
func (g GetTableCfgArgs) parseColumns(rows []map[string]any) (*columns.Columns, error) {
cols := &columns.Columns{}
for _, row := range rows {
dataType, err := maputil.GetStringFromMap(row, g.ColumnNameForDataType)
if err != nil {
Expand Down Expand Up @@ -116,7 +90,41 @@ func (g GetTableCfgArgs) GetTableConfig() (*types.DwhTableConfig, error) {
cols.AddColumn(col)
}

tableCfg := types.NewDwhTableConfig(&cols, nil, tableMissing, g.DropDeletedColumns)
return cols, nil
}

func (g GetTableCfgArgs) GetTableConfig() (*types.DwhTableConfig, error) {
if tableConfig := g.ConfigMap.TableConfigCache(g.TableID); tableConfig != nil {
return tableConfig, nil
}

var tableMissing bool
sqlRows, err := g.Dwh.Query(g.Query, g.Args...)
if err != nil {
if g.Dwh.Dialect().IsTableDoesNotExistErr(err) {
// This branch is currently only used by Snowflake.
// Swallow the error, make sure all the metadata is created
tableMissing = true
err = nil
} else {
return nil, fmt.Errorf("failed to query %T, err: %w, query: %q", g.Dwh, err, g.Query)
}
}

cols := &columns.Columns{}
if sqlRows != nil {
rows, err := sql.RowsToObjects(sqlRows)
if err != nil {
return nil, fmt.Errorf("failed to convert rows to objects: %w", err)
}

cols, err = g.parseColumns(rows)
if err != nil {
return nil, fmt.Errorf("failed to parse columns: %w", err)
}
}

tableCfg := types.NewDwhTableConfig(cols, nil, tableMissing, g.DropDeletedColumns)
g.ConfigMap.AddTableToConfig(g.TableID, tableCfg)
return tableCfg, nil
}
4 changes: 0 additions & 4 deletions lib/sql/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
)

func RowsToObjects(rows *sql.Rows) ([]map[string]any, error) {
if rows == nil {
return []map[string]any{}, nil
}

defer rows.Close()

columns, err := rows.Columns()
Expand Down

0 comments on commit edd1e41

Please sign in to comment.