param_binary_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package pgserver
  2. import (
  3. "encoding/binary"
  4. "math"
  5. "testing"
  6. )
  7. // encodeBinaryArray builds a PostgreSQL binary array parameter. A nil elements
  8. // slice is the empty array (ndim 0); a nil element is SQL NULL.
  9. func encodeBinaryArray(elementOID uint32, elements [][]byte) []byte {
  10. hasNull := false
  11. for _, element := range elements {
  12. if element == nil {
  13. hasNull = true
  14. }
  15. }
  16. var buf []byte
  17. if elements == nil {
  18. buf = binary.BigEndian.AppendUint32(buf, 0)
  19. buf = binary.BigEndian.AppendUint32(buf, 0)
  20. buf = binary.BigEndian.AppendUint32(buf, elementOID)
  21. return buf
  22. }
  23. buf = binary.BigEndian.AppendUint32(buf, 1)
  24. flags := uint32(0)
  25. if hasNull {
  26. flags = 1
  27. }
  28. buf = binary.BigEndian.AppendUint32(buf, flags)
  29. buf = binary.BigEndian.AppendUint32(buf, elementOID)
  30. buf = binary.BigEndian.AppendUint32(buf, uint32(len(elements)))
  31. buf = binary.BigEndian.AppendUint32(buf, 1)
  32. for _, element := range elements {
  33. if element == nil {
  34. buf = binary.BigEndian.AppendUint32(buf, 0xFFFFFFFF)
  35. continue
  36. }
  37. buf = binary.BigEndian.AppendUint32(buf, uint32(len(element)))
  38. buf = append(buf, element...)
  39. }
  40. return buf
  41. }
  42. func encodeInt2(value int16) []byte {
  43. return binary.BigEndian.AppendUint16(nil, uint16(value))
  44. }
  45. func encodeInt4(value int32) []byte {
  46. return binary.BigEndian.AppendUint32(nil, uint32(value))
  47. }
  48. func encodeInt8(value int64) []byte {
  49. return binary.BigEndian.AppendUint64(nil, uint64(value))
  50. }
  51. func encodeFloat4(value float32) []byte {
  52. return binary.BigEndian.AppendUint32(nil, math.Float32bits(value))
  53. }
  54. func encodeFloat8(value float64) []byte {
  55. return binary.BigEndian.AppendUint64(nil, math.Float64bits(value))
  56. }
  57. // encodeNumeric mirrors PostgreSQL's binary numeric layout: digit count,
  58. // base-10000 weight, sign, display scale, then base-10000 digits.
  59. func encodeNumeric(sign uint16, weight int16, scale uint16, digits ...uint16) []byte {
  60. buf := binary.BigEndian.AppendUint16(nil, uint16(len(digits)))
  61. buf = binary.BigEndian.AppendUint16(buf, uint16(weight))
  62. buf = binary.BigEndian.AppendUint16(buf, sign)
  63. buf = binary.BigEndian.AppendUint16(buf, scale)
  64. for _, digit := range digits {
  65. buf = binary.BigEndian.AppendUint16(buf, digit)
  66. }
  67. return buf
  68. }
  69. // runBinaryParameter drives one extended-protocol cycle with a single
  70. // binary-format parameter.
  71. func runBinaryParameter(t *testing.T, c *Connection, conn *countingConn, sql string, oid int32, value []byte) []*Message {
  72. t.Helper()
  73. conn.reset()
  74. parse := NewMessageBuilder()
  75. parse.WriteString("s1")
  76. parse.WriteString(sql)
  77. parse.WriteInt16(1)
  78. parse.WriteInt32(oid)
  79. bind := NewMessageBuilder()
  80. bind.WriteString("p1")
  81. bind.WriteString("s1")
  82. bind.WriteInt16(1)
  83. bind.WriteInt16(1) // binary parameter format
  84. bind.WriteInt16(1)
  85. bind.WriteInt32(int32(len(value)))
  86. bind.WriteBytes(value)
  87. bind.WriteInt16(0)
  88. execute := NewMessageBuilder()
  89. execute.WriteString("p1")
  90. execute.WriteInt32(0)
  91. for _, msg := range []*Message{
  92. {Type: MsgParse, Data: parse.Bytes()},
  93. {Type: MsgBind, Data: bind.Bytes()},
  94. {Type: MsgExecute, Data: execute.Bytes()},
  95. {Type: MsgSync},
  96. } {
  97. if err := c.handleMessage(msg); err != nil {
  98. t.Fatalf("handle %c: %v", msg.Type, err)
  99. }
  100. }
  101. return readAllMessages(t, conn.bytes())
  102. }
  103. func TestBinaryArrayParameterElements(t *testing.T) {
  104. c, conn := arrayConnection(t)
  105. cases := []struct {
  106. name string
  107. sql string
  108. arrayOID int32
  109. value []byte
  110. want string
  111. }{
  112. {
  113. name: "int4", sql: `SELECT $1::INTEGER[]`, arrayOID: 1007,
  114. value: encodeBinaryArray(23, [][]byte{encodeInt4(1), encodeInt4(-2)}), want: "{1,-2}",
  115. },
  116. {
  117. name: "int4_null_element", sql: `SELECT $1::INTEGER[]`, arrayOID: 1007,
  118. value: encodeBinaryArray(23, [][]byte{encodeInt4(1), nil, encodeInt4(3)}), want: "{1,NULL,3}",
  119. },
  120. {
  121. name: "int4_empty", sql: `SELECT $1::INTEGER[]`, arrayOID: 1007,
  122. value: encodeBinaryArray(23, nil), want: "{}",
  123. },
  124. {
  125. name: "int2", sql: `SELECT $1::SMALLINT[]`, arrayOID: 1005,
  126. value: encodeBinaryArray(21, [][]byte{encodeInt2(1), encodeInt2(-2)}), want: "{1,-2}",
  127. },
  128. {
  129. name: "int8_exact", sql: `SELECT $1::BIGINT[]`, arrayOID: 1016,
  130. value: encodeBinaryArray(20, [][]byte{encodeInt8(9007199254740993)}), want: "{9007199254740993}",
  131. },
  132. {
  133. name: "bool", sql: `SELECT $1::BOOLEAN[]`, arrayOID: 1000,
  134. value: encodeBinaryArray(16, [][]byte{{0}, {1}}), want: "{f,t}",
  135. },
  136. {
  137. name: "float4", sql: `SELECT $1::REAL[]`, arrayOID: 1021,
  138. value: encodeBinaryArray(700, [][]byte{encodeFloat4(1.5)}), want: "{1.5}",
  139. },
  140. {
  141. name: "float8", sql: `SELECT $1::DOUBLE PRECISION[]`, arrayOID: 1022,
  142. value: encodeBinaryArray(701, [][]byte{encodeFloat8(0.1)}), want: "{0.1}",
  143. },
  144. {
  145. name: "text", sql: `SELECT $1::TEXT[]`, arrayOID: 1009,
  146. value: encodeBinaryArray(25, [][]byte{
  147. []byte("a,b"), []byte("NULL"), []byte(""), []byte(`q"t`), []byte(`back\slash`),
  148. }),
  149. want: `{"a,b","NULL","","q\"t","back\\slash"}`,
  150. },
  151. {
  152. name: "varchar", sql: `SELECT $1::VARCHAR(10)[]`, arrayOID: 1015,
  153. value: encodeBinaryArray(1043, [][]byte{[]byte("x"), []byte("y z")}), want: `{x,"y z"}`,
  154. },
  155. {
  156. name: "numeric_exact", sql: `SELECT $1::NUMERIC[]`, arrayOID: 1231,
  157. value: encodeBinaryArray(1700, [][]byte{
  158. encodeNumeric(0x0000, 4, 5, 1234, 5678, 9012, 3456, 7890, 1234, 5000),
  159. encodeNumeric(0x4000, 0, 2, 1, 5000),
  160. }),
  161. want: "{12345678901234567890.12345,-1.50}",
  162. },
  163. {
  164. name: "numeric_zero", sql: `SELECT $1::NUMERIC[]`, arrayOID: 1231,
  165. value: encodeBinaryArray(1700, [][]byte{
  166. encodeNumeric(0x0000, 0, 2),
  167. encodeNumeric(0x0000, -1, 1, 5000),
  168. }),
  169. want: "{0.00,0.5}",
  170. },
  171. }
  172. for _, tc := range cases {
  173. t.Run(tc.name, func(t *testing.T) {
  174. messages := runBinaryParameter(t, c, conn, tc.sql, tc.arrayOID, tc.value)
  175. if errMsg := findError(messages); errMsg != nil {
  176. t.Fatalf("%s failed: %s (%s)", tc.sql, errorCode(errMsg), errorMessage(errMsg))
  177. }
  178. row := firstDataRow(t, messages)
  179. if len(row) != 1 || row[0] != tc.want {
  180. t.Fatalf("%s = %v, want [%s]", tc.sql, row, tc.want)
  181. }
  182. })
  183. }
  184. }
  185. func TestBinaryArrayParameterMalformed(t *testing.T) {
  186. c, conn := arrayConnection(t)
  187. valid := encodeBinaryArray(23, [][]byte{encodeInt4(1)})
  188. nullWithoutFlag := encodeBinaryArray(23, [][]byte{nil, encodeInt4(1)})
  189. if nullWithoutFlag[7] != 1 {
  190. t.Fatal("test fixture expected the null flag set")
  191. }
  192. nullWithoutFlag[7] = 0 // clear hasNull while leaving the NULL element
  193. cases := []struct {
  194. name string
  195. value []byte
  196. }{
  197. {"truncated_header", valid[:10]},
  198. {"truncated_dimension", valid[:14]},
  199. {"truncated_elements", valid[:len(valid)-2]},
  200. {"trailing_bytes", append(append([]byte(nil), valid...), 0)},
  201. {"element_oid_mismatch", encodeBinaryArray(25, [][]byte{[]byte("x")})},
  202. {"null_without_flag", nullWithoutFlag},
  203. {"invalid_boolean", encodeBinaryArray(16, [][]byte{{2}})},
  204. {"short_int4", encodeBinaryArray(23, [][]byte{{0, 0}})},
  205. }
  206. for _, tc := range cases {
  207. t.Run(tc.name, func(t *testing.T) {
  208. messages := runBinaryParameter(t, c, conn, `SELECT $1::INTEGER[]`, 1007, tc.value)
  209. errMsg := findError(messages)
  210. if errMsg == nil {
  211. t.Fatalf("%s was accepted", tc.name)
  212. }
  213. if code := errorCode(errMsg); code != ErrCodeInvalidText {
  214. t.Fatalf("%s SQLSTATE = %q, want %q", tc.name, code, ErrCodeInvalidText)
  215. }
  216. })
  217. }
  218. }
  219. // Multidimensional binary arrays report the same unsupported-feature error as
  220. // their text-format equivalent.
  221. func TestBinaryArrayParameterMultidimensional(t *testing.T) {
  222. c, conn := arrayConnection(t)
  223. value := encodeBinaryArray(23, [][]byte{encodeInt4(1)})
  224. binary.BigEndian.PutUint32(value[0:4], 2)
  225. messages := runBinaryParameter(t, c, conn, `SELECT $1::INTEGER[]`, 1007, value)
  226. errMsg := findError(messages)
  227. if errMsg == nil {
  228. t.Fatal("multidimensional binary array was accepted")
  229. }
  230. if code := errorCode(errMsg); code != ErrCodeFeatureNotSupported {
  231. t.Fatalf("binary multidimensional SQLSTATE = %q, want %q", code, ErrCodeFeatureNotSupported)
  232. }
  233. messages = runExtendedParseBindExecute(t, c, conn, `SELECT $1::INTEGER[]`, 1007, "{{1}}")
  234. errMsg = findError(messages)
  235. if errMsg == nil {
  236. t.Fatal("multidimensional text array was accepted")
  237. }
  238. if code := errorCode(errMsg); code != ErrCodeFeatureNotSupported {
  239. t.Fatalf("text multidimensional SQLSTATE = %q, want %q", code, ErrCodeFeatureNotSupported)
  240. }
  241. }
  242. // The engine has no numeric NaN or infinity, so both the scalar and array
  243. // paths must reject those binary values as invalid text instead of splicing a
  244. // bare NaN into SQL.
  245. func TestBinaryNumericSpecialValuesRejected(t *testing.T) {
  246. c, conn := arrayConnection(t)
  247. for _, tc := range []struct {
  248. name string
  249. sql string
  250. oid int32
  251. value []byte
  252. }{
  253. {"scalar_nan", `SELECT $1::NUMERIC`, 1700, encodeNumeric(0xC000, 0, 0)},
  254. {"scalar_infinity", `SELECT $1::NUMERIC`, 1700, encodeNumeric(0xD000, 0, 0)},
  255. {"scalar_negative_infinity", `SELECT $1::NUMERIC`, 1700, encodeNumeric(0xF000, 0, 0)},
  256. {"array_nan", `SELECT $1::NUMERIC[]`, 1231, encodeBinaryArray(1700, [][]byte{encodeNumeric(0xC000, 0, 0)})},
  257. {"array_infinity", `SELECT $1::NUMERIC[]`, 1231, encodeBinaryArray(1700, [][]byte{encodeNumeric(0xD000, 0, 0)})},
  258. } {
  259. t.Run(tc.name, func(t *testing.T) {
  260. messages := runBinaryParameter(t, c, conn, tc.sql, tc.oid, tc.value)
  261. errMsg := findError(messages)
  262. if errMsg == nil {
  263. t.Fatalf("%s was accepted", tc.name)
  264. }
  265. if code := errorCode(errMsg); code != ErrCodeInvalidText {
  266. t.Fatalf("%s SQLSTATE = %q, want %q", tc.name, code, ErrCodeInvalidText)
  267. }
  268. })
  269. }
  270. }
  271. // Scalar binary booleans validate the payload byte like the array element path.
  272. func TestBinaryBooleanParameterValidation(t *testing.T) {
  273. c, conn := arrayConnection(t)
  274. for _, value := range [][]byte{{}, {2}, {0, 0}} {
  275. messages := runBinaryParameter(t, c, conn, `SELECT $1::BOOLEAN`, 16, value)
  276. errMsg := findError(messages)
  277. if errMsg == nil {
  278. t.Fatalf("binary boolean %v was accepted", value)
  279. }
  280. if code := errorCode(errMsg); code != ErrCodeInvalidText {
  281. t.Fatalf("binary boolean %v SQLSTATE = %q, want %q", value, code, ErrCodeInvalidText)
  282. }
  283. }
  284. }
  285. // Parameter formats are per parameter: one text parameter and one binary
  286. // parameter in the same Bind message decode independently.
  287. func TestMixedParameterFormats(t *testing.T) {
  288. c, conn := arrayConnection(t)
  289. conn.reset()
  290. parse := NewMessageBuilder()
  291. parse.WriteString("s1")
  292. parse.WriteString(`SELECT $1::INTEGER, $2::INTEGER`)
  293. parse.WriteInt16(0)
  294. bind := NewMessageBuilder()
  295. bind.WriteString("p1")
  296. bind.WriteString("s1")
  297. bind.WriteInt16(2) // one format code per parameter
  298. bind.WriteInt16(0) // $1 text
  299. bind.WriteInt16(1) // $2 binary
  300. bind.WriteInt16(2)
  301. bind.WriteInt32(1)
  302. bind.WriteBytes([]byte("5"))
  303. binaryValue := encodeInt4(7)
  304. bind.WriteInt32(int32(len(binaryValue)))
  305. bind.WriteBytes(binaryValue)
  306. bind.WriteInt16(0)
  307. execute := NewMessageBuilder()
  308. execute.WriteString("p1")
  309. execute.WriteInt32(0)
  310. for _, msg := range []*Message{
  311. {Type: MsgParse, Data: parse.Bytes()},
  312. {Type: MsgBind, Data: bind.Bytes()},
  313. {Type: MsgExecute, Data: execute.Bytes()},
  314. {Type: MsgSync},
  315. } {
  316. if err := c.handleMessage(msg); err != nil {
  317. t.Fatalf("handle %c: %v", msg.Type, err)
  318. }
  319. }
  320. messages := readAllMessages(t, conn.bytes())
  321. if errMsg := findError(messages); errMsg != nil {
  322. t.Fatalf("mixed formats failed: %s", errorCode(errMsg))
  323. }
  324. if row := firstDataRow(t, messages); len(row) != 2 || row[0] != "5" || row[1] != "7" {
  325. t.Fatalf("mixed formats row = %v, want [5 7]", row)
  326. }
  327. }
  328. // Array types the engine does not decode in binary form keep the explicit
  329. // feature-not-supported error.
  330. func TestBinaryArrayParameterUnsupportedElementType(t *testing.T) {
  331. c, conn := arrayConnection(t)
  332. for _, oid := range []int32{1014 /* bytea[] */, 1182 /* date[] */} {
  333. messages := runBinaryParameter(t, c, conn, `SELECT $1`, oid, []byte{0, 0, 0, 0})
  334. errMsg := findError(messages)
  335. if errMsg == nil {
  336. t.Fatalf("binary array OID %d was accepted", oid)
  337. }
  338. if code := errorCode(errMsg); code != ErrCodeFeatureNotSupported {
  339. t.Fatalf("binary array OID %d SQLSTATE = %q, want %q", oid, code, ErrCodeFeatureNotSupported)
  340. }
  341. }
  342. }
  343. // Scalar float parameters follow the same negotiated type: pgx sends float4 and
  344. // float8 in binary form.
  345. func TestBinaryFloatParameter(t *testing.T) {
  346. c, conn := arrayConnection(t)
  347. for _, tc := range []struct {
  348. name string
  349. sql string
  350. oid int32
  351. value []byte
  352. want string
  353. }{
  354. {"float4", `SELECT $1::REAL`, 700, encodeFloat4(1.5), "1.5"},
  355. {"float8", `SELECT $1::DOUBLE PRECISION`, 701, encodeFloat8(0.1), "0.1"},
  356. } {
  357. t.Run(tc.name, func(t *testing.T) {
  358. messages := runBinaryParameter(t, c, conn, tc.sql, tc.oid, tc.value)
  359. if errMsg := findError(messages); errMsg != nil {
  360. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  361. }
  362. if row := firstDataRow(t, messages); len(row) != 1 || row[0] != tc.want {
  363. t.Fatalf("%s = %v, want [%s]", tc.sql, row, tc.want)
  364. }
  365. })
  366. }
  367. }
  368. // Scalar binary numeric parameters keep their exact decimal digits. The
  369. // capability document advertises binary numeric support, and NUMERIC elements
  370. // use the same decoder inside arrays.
  371. func TestBinaryNumericParameter(t *testing.T) {
  372. c, conn := arrayConnection(t)
  373. value := encodeNumeric(0x0000, 1, 1, 1, 2345, 5000)
  374. messages := runBinaryParameter(t, c, conn, `SELECT $1::NUMERIC`, 1700, value)
  375. if errMsg := findError(messages); errMsg != nil {
  376. t.Fatalf("binary numeric failed: %s", errorCode(errMsg))
  377. }
  378. if row := firstDataRow(t, messages); len(row) != 1 || row[0] != "12345.5" {
  379. t.Fatalf("binary numeric = %v, want [12345.5]", row)
  380. }
  381. messages = runBinaryParameter(t, c, conn, `SELECT $1::NUMERIC`, 1700, encodeNumeric(0x4000, -1, 1, 5000))
  382. if errMsg := findError(messages); errMsg != nil {
  383. t.Fatalf("negative binary numeric failed: %s", errorCode(errMsg))
  384. }
  385. if row := firstDataRow(t, messages); len(row) != 1 || row[0] != "-0.5" {
  386. t.Fatalf("negative binary numeric = %v, want [-0.5]", row)
  387. }
  388. }