Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/loadjson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ func processFile(ctx context.Context, path string) {
// Note that this step can succeed with any valid JSON file. But we need to do some additional validation to skip
// invalid query JSON files.
if unmarshalErr := json.Unmarshal(bytes, queryInfo); unmarshalErr != nil {
log.Error().Err(unmarshalErr).Str("path", path).Msg("failed to unmarshal JSON")
return
}
if queryInfo.QueryId == "" || queryInfo.QueryStats == nil || queryInfo.QueryStats.CreateTime == nil {
log.Error().Msg("QueryId, QueryStats or QueryStats.CreateTime is empty")
return
}
log.Info().Str("path", path).Msg("start to process file")
Expand Down
13 changes: 8 additions & 5 deletions utils/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ func SqlInsertObject(ctx context.Context, db *sql.DB, obj any, tableNames ...Tab
if len(rows) == 0 {
continue
}
placeholders := strings.Repeat("?,", rows[0].ColumnCount())
// Get rid of the trailing comma.
placeholders = placeholders[:len(placeholders)-1]
sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
table, strings.Join(rows[0].ColumnNames, ","), placeholders)
if len(rows) == 1 && rows[0].ColumnCount() <= 1 {
continue
}
Comment on lines +52 to +54
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When data is empty, usually only has query_id, insert will fail


for _, row := range rows {
placeholders := strings.Repeat("?,", row.ColumnCount())
// Get rid of the trailing comma.
placeholders = placeholders[:len(placeholders)-1]
sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
table, strings.Join(row.ColumnNames, ","), placeholders)
Comment on lines +57 to +61
Copy link
Author

@unidevel unidevel Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes cmd loadjson with the sample data

$ ./pbench_darwin_arm64 loadjson -n test --mysql ./mysql.conf -o ./test ./cmd/loadjson/20240422_013209_00111_k6ve9_shows_schema.json
{"level":"info","output_path":"test/test","time":"2025-10-29T21:58:11Z","message":"output directory"}
{"level":"info","log_path":"test/test/loadjson.log","time":"2025-10-29T21:58:11Z","message":"log file will be saved to this path"}
{"level":"info","parallelism":16,"time":"2025-10-29T21:58:11Z"}
{"level":"info","path":"./cmd/loadjson/20240422_013209_00111_k6ve9_shows_schema.json","time":"2025-10-29T21:58:11Z","message":"start to process file"}
{"level":"error","error":"sql: expected 37 arguments, got 38","path":"./cmd/loadjson/20240422_013209_00111_k6ve9_shows_schema.json","time":"2025-10-29T21:58:12Z","message":"failed to insert event listener record"}
{"level":"info","file_loaded":0,"time":"2025-10-29T21:58:12Z"}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can this happen? I don't understand

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rows is an array of operator stats, when it extract the rows, the first row has 37 column with 37 values, and some of the other rows has 38 columns with 38 values, but the statement is created by the first row, that makes the values not matching for some of the other rows which have 38 columns

//log.Info().Str("sql", sqlStmt).Array("values", log.NewMarshaller(row.Values)).Msg("execute sql")
_, err := tx.Exec(sqlStmt, row.Values...)
if err != nil {
Expand Down