Skip to content

Commit

Permalink
database_observability: fix fetch table columns spec (#2703)
Browse files Browse the repository at this point in the history
database_observability: fix fetch table columns spec

Fix nil pointer dereference by always returning the table object on
error.
  • Loading branch information
cristiangreco authored Feb 12, 2025
1 parent 77d7573 commit 978c8f3
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,36 +320,36 @@ func (c *SchemaTable) fetchTableDefinitions(ctx context.Context, fullyQualifiedT
row := c.dbConnection.QueryRowContext(ctx, showCreateTable+" "+fullyQualifiedTable)
if err := row.Err(); err != nil {
level.Error(c.logger).Log("msg", "failed to show create table", "schema", table.schema, "table", table.tableName, "err", err)
return nil, row.Err()
return table, err
}

var tableName, createStmt, characterSetClient, collationConnection string
switch table.tableType {
case "BASE TABLE":
if err := row.Scan(&tableName, &createStmt); err != nil {
level.Error(c.logger).Log("msg", "failed to scan create table", "schema", table.schema, "table", table.tableName, "err", err)
return nil, err
return table, err
}
case "VIEW":
if err := row.Scan(&tableName, &createStmt, &characterSetClient, &collationConnection); err != nil {
level.Error(c.logger).Log("msg", "failed to scan create view", "schema", table.schema, "table", table.tableName, "err", err)
return nil, err
return table, err
}
default:
level.Error(c.logger).Log("msg", "unknown table type", append(logKVs, "table_type", table.tableType))
level.Error(c.logger).Log("msg", "unknown table type", "schema", table.schema, "table", table.tableName, "table_type", table.tableType)
return nil, fmt.Errorf("unknown table type: %s", table.tableType)
}
table.b64CreateStmt = base64.StdEncoding.EncodeToString([]byte(createStmt))

spec, err := c.fetchColumnsDefinitions(ctx, table.schema, table.tableName)
if err != nil {
level.Error(c.logger).Log("msg", "failed to analyze table spec", "schema", table.schema, "table", table.tableName, "err", err)
return nil, err
return table, err
}
jsonSpec, err := json.Marshal(spec)
if err != nil {
level.Error(c.logger).Log("msg", "failed to marshal table spec", "schema", table.schema, "table", table.tableName, "err", err)
return nil, err
return table, err
}
table.b64TableSpec = base64.StdEncoding.EncodeToString(jsonSpec)

Expand Down

0 comments on commit 978c8f3

Please sign in to comment.