{"record":{"id":"4181a81e9cfed013","repo":"t8y2/dbx","slug":"read-hive-initfile-statement-result-w","errorCode":null,"errorMessage":"read Hive initFile statement result: %w","messagePattern":"read Hive initFile statement result: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/argo-go/main.go","lineNumber":381,"sourceCode":"\t\ttimeout += server.config.BrowserResponseTimeout\n\t}\n\treturn timeout\n}\n\nfunc runHiveInitStatements(ctx context.Context, connection *sql.Conn, statements []string, fetchSize int) error {\n\tfor _, statement := range statements {\n\t\trows, _, hasResultSet, err := executeHiveStatement(ctx, connection, statement, fetchSize)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"execute Hive initFile statement: %w\", err)\n\t\t}\n\t\tif !hasResultSet {\n\t\t\tcontinue\n\t\t}\n\t\tfor rows.Next() {\n\t\t}\n\t\tif rowsErr := rows.Err(); rowsErr != nil {\n\t\t\t_ = rows.Close()\n\t\t\treturn fmt.Errorf(\"read Hive initFile statement result: %w\", rowsErr)\n\t\t}\n\t\tif closeErr := rows.Close(); closeErr != nil {\n\t\t\treturn fmt.Errorf(\"close Hive initFile statement result: %w\", closeErr)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (server *server) dispatch(method string, params map[string]json.RawMessage) (any, bool, error) {\n\tswitch method {\n\tcase \"validate_connection\":\n\t\treturn map[string]bool{\"ok\": true}, false, server.validateConnection()\n\tcase \"connection_info\":\n\t\tresult, err := server.connectionInfo()\n\t\treturn result, false, err\n\tcase \"list_databases\":\n\t\tresult, err := server.listDatabases()\n\t\treturn result, false, err","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/argo-go/main.go#L363-L399","documentation":"Error from `runHiveInitStatements` when iterating/draining a result set of an initFile statement succeeded mechanically but `rows.Err()` then reports a deferred iteration error (e.g. the connection broke mid-drain or the driver surfaced a late error). The rows are closed (error ignored) and the underlying rows.Err() value is wrapped with %w. It is distinct from the 'execute ...' error (statement submission failed) and the 'close ...' error (rows.Close() failed): here the read loop itself failed.","triggerScenarios":"An initFile statement that returns a result set (hasResultSet true); the `for rows.Next()` drain loop runs, and afterwards `rows.Err()` is non-nil — network drop between server and driver during result streaming, context cancellation/deadline exceeded mid-iteration, or Hive server-side failure while producing rows.","commonSituations":"Init statements accidentally left as full SELECTs over large tables so streaming times out; unstable network or load balancer idle-kill during drain; fetchSize too small causing long paginated reads that exceed timeouts; Hive server restarting mid-connect.","solutions":["Unwrap (%w) the rows.Err() cause; if it is a timeout/cancellation, increase the connection open timeout or make init statements cheap.","Replace result-returning init statements (SELECT/SHOW) with non-result statements (SET/USE/DDL) so nothing needs draining.","Raise fetchSize to reduce round trips when draining is unavoidable, and stabilize connectivity (keepalives, LB idle timeout).","Retry the connection open — this is often transient; the driver already closed rows and connection, so a fresh openConnection is safe."],"exampleFix":"// before (initFile)\nSELECT * FROM audit_log;  -- large result set drained at connect\n// after\nSET hive.exec.dynamic.partition=true;\nUSE audit;  -- no result set to drain","handlingStrategy":"retry","validationCode":"// avoid result-returning statements in initFile\nfor _, stmt := range initStatements {\n    if isResultStatement(stmt) { // SELECT/SHOW/DESCRIBE\n        log.Printf(\"init statement returns a result set and will be drained at connect: %q\", stmt)\n    }\n}","typeGuard":null,"tryCatchPattern":"var lastErr error\nfor attempt := 0; attempt < 3; attempt++ {\n    err := server.openConnection()\n    if err == nil { break }\n    lastErr = err\n    if !isTransientReadError(err) { return err } // only retry drain/read failures\n    time.Sleep(backoff(attempt))\n}\nreturn lastErr","preventionTips":["Keep init statements free of large result sets; they are drained row-by-row at connect time.","Size the connect timeout to cover the full drain, and set fetchSize high enough to limit round trips.","Enable TCP keepalives and align LB idle timeouts so streaming reads are not cut mid-drain.","Treat this error as transient: it already cleaned up, so a fresh connection attempt is safe."],"tags":["go","hive","sql","result-set","timeout"],"backgroundTag":"connection-reset-during-query","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}