## Code Analysis ### Scalar FOR rows use declared-target conversion `server/plpgsql/interpreter_stack.go:584-601` ```go // UpdateVariables assigns a query result row to a list of scalar variables. A FOR .. IN query LOOP // requires one target for each result column, and each value is converted to the declared target type. func (is *InterpreterStack) UpdateVariables(ctx *sql.Context, names []string, schema sql.Schema, row sql.Row) error { if len(names) != len(schema) { return fmt.Errorf("FOR query returned %d columns for %d target variables", len(schema), len(names)) } for i, name := range names { iv := is.findVariable(name) if iv == nil || iv.Type == nil { return fmt.Errorf("variable `%s` could not be found", name) } value, _, err := iv.Type.Convert(ctx, row[i]) if err != nil { return err } iv.Value = value } return nil } ``` The scalar assignment helper passes each query value to the declared variable type before storing it. ### Scalar FOR execution reaches the helper `server/plpgsql/interpreter_logic.go:498-515` ```go case OpCode_ForQueryNext: schema, row, ok := stack.AdvanceCursor() if !ok { counter = operation.Index - 1 } else { stack.MarkScopeLoop(true) var err error if len(operation.SecondaryData) > 0 { err = stack.UpdateVariables(ctx, operation.SecondaryData, schema, row) } else { err = stack.UpdateRecord(operation.Target, schema, row) } if err != nil { return nil, err } } ``` When the scalar target list is present, each row from the FOR query is routed through `UpdateVariables`; a conversion error is returned from the function execution. ### The integer conversion branch rejects the text value `server/types/type.go:548-616` ```go case "int4": if _, ok := v.(int32); ok { return v, sql.InRange, nil } // ... other already-typed cases ... return nil, sql.InRange, ErrUnhandledType.New(t.String(), v) ``` For an `int4` target, this code accepts an `int32` value but has no string-to-integer branch before returning `ErrUnhandledType`. The tested query supplies the text value `'7'`, so the scalar assignment path cannot produce the declared integer value and propagates the conversion error instead. ### Observed execution The captured SQL command created the function and invoked the failing scalar conversion case: ```sql CREATE OR REPLACE FUNCTION f_for_scalar_conversion() RETURNS integer LANGUAGE plpgsql AS $$ DECLARE v integer; acc integer := 0; BEGIN FOR v IN SELECT '7'::text LOOP acc := acc + v; END LOOP; RETURN acc; END; $$; SELECT f_for_scalar_conversion(); ``` The runner recorded the invocation result as `integer: unhandled type: string` instead of `7`; the per-test artifact contains the command but not a raw response line. ### Result The source path and captured SQL trigger support the reported SCALAR-3 failure: a text scalar returned by the FOR query reaches the declared integer target conversion, which rejects the string input and returns an unhandled-type error. ### Test context The command was executed with `psql` against the local database endpoint. No stubs, mocks, or bypasses were recorded.