cast_result_oids_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package pgserver
  2. import "testing"
  3. // TestLiteralCastResultOIDs pins the reported type of a cast whose operand is a
  4. // constant. evalCastExpr carries int64/float64 and does not range-check, so a
  5. // declared width narrower than the carrier is advertised only when the constant
  6. // fits it; otherwise the carrier type (int8/float8) is reported.
  7. func TestLiteralCastResultOIDs(t *testing.T) {
  8. c, client := newDataConnection(t)
  9. cases := []struct {
  10. sql string
  11. want int32
  12. value string
  13. }{
  14. {"SELECT 1::SMALLINT", 21, "1"}, // int2
  15. {"SELECT CAST(1 AS SMALLINT)", 21, "1"}, // int2
  16. {"SELECT 32767::SMALLINT", 21, "32767"}, // int2
  17. {"SELECT -1::SMALLINT", 21, "-1"}, // int2
  18. {"SELECT 32768::SMALLINT", 20, "32768"}, // int8 carrier
  19. {"SELECT 1::INTEGER", 23, "1"}, // int4
  20. {"SELECT 1::INT", 23, "1"}, // int4
  21. {"SELECT 2147483647::INTEGER", 23, "2147483647"},
  22. {"SELECT 2147483648::INTEGER", 20, "2147483648"}, // int8 carrier
  23. {"SELECT 1::BIGINT", 20, "1"}, // int8
  24. {"SELECT 1.5::REAL", 700, "1.5"}, // float4
  25. {"SELECT CAST(1 AS REAL)", 700, "1"}, // float4
  26. {"SELECT 3.5e38::REAL", 701, "3.5e+38"}, // float8 carrier
  27. {"SELECT 1.5::DOUBLE", 701, "1.5"}, // float8
  28. {"SELECT 1.5::DOUBLE PRECISION", 701, "1.5"}, // float8
  29. {"SELECT 1::NUMERIC", 701, "1"}, // float8 carrier
  30. {"SELECT 1.5::DECIMAL", 701, "1.5"}, // float8 carrier
  31. }
  32. for _, tc := range cases {
  33. t.Run(tc.sql, func(t *testing.T) {
  34. msgs := resultMessages(runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(tc.sql), 0)}))
  35. if errMsg := findError(msgs); errMsg != nil {
  36. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  37. }
  38. oids := rowDescriptionOIDs(t, findRowDescription(t, msgs).Data)
  39. if len(oids) != 1 || oids[0] != tc.want {
  40. t.Fatalf("%s: OIDs = %v, want [%d]", tc.sql, oids, tc.want)
  41. }
  42. if row := firstDataRow(t, msgs); len(row) != 1 || row[0] != tc.value {
  43. t.Fatalf("%s: row = %v, want [%s]", tc.sql, row, tc.value)
  44. }
  45. })
  46. }
  47. }
  48. // TestNumericResultMetadataIsFloat8 pins the scalar NUMERIC contract from both
  49. // directions: a declared NUMERIC/DECIMAL column and a NUMERIC cast report
  50. // float8 (701) because scalars are float64-backed, while NUMERIC[] keeps exact
  51. // element text and reports numeric[] (1231).
  52. func TestNumericResultMetadataIsFloat8(t *testing.T) {
  53. c, client := newDataConnection(t)
  54. for _, sql := range []string{
  55. `CREATE TABLE amounts (id INTEGER PRIMARY KEY, price NUMERIC, discount DECIMAL, history NUMERIC[])`,
  56. `INSERT INTO amounts VALUES (1, '12345678901234567890.50', '1.10', '{12345678901234567890.50,1.10}')`,
  57. } {
  58. if msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(sql), 0)}); findError(msgs) != nil {
  59. t.Fatalf("%s: %s", sql, errorCode(findError(msgs)))
  60. }
  61. }
  62. cases := []struct {
  63. sql string
  64. want int32
  65. value string
  66. }{
  67. {"SELECT price FROM amounts", 701, "12345678901234567890.50"},
  68. {"SELECT discount FROM amounts", 701, "1.10"},
  69. {"SELECT history FROM amounts", 1231, "{12345678901234567890.50,1.10}"},
  70. {"SELECT price::NUMERIC FROM amounts", 701, "12345678901234567890.50"},
  71. {"SELECT CAST(discount AS DECIMAL) FROM amounts", 701, "1.10"},
  72. {"SELECT price + 0 FROM amounts", 701, "1.2345678901234567e+19"},
  73. }
  74. for _, tc := range cases {
  75. t.Run(tc.sql, func(t *testing.T) {
  76. msgs := resultMessages(runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(tc.sql), 0)}))
  77. if errMsg := findError(msgs); errMsg != nil {
  78. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  79. }
  80. oids := rowDescriptionOIDs(t, findRowDescription(t, msgs).Data)
  81. if len(oids) != 1 || oids[0] != tc.want {
  82. t.Fatalf("%s: OIDs = %v, want [%d]", tc.sql, oids, tc.want)
  83. }
  84. if row := firstDataRow(t, msgs); len(row) != 1 || row[0] != tc.value {
  85. t.Fatalf("%s: row = %v, want [%s]", tc.sql, row, tc.value)
  86. }
  87. })
  88. }
  89. }
  90. // TestCastOverColumnKeepsCarrierOID guards the other half of the literal-cast
  91. // rule: the value of a column is not proven to fit, so it keeps the carrier
  92. // type even when the column's declared type is narrower.
  93. func TestCastOverColumnKeepsCarrierOID(t *testing.T) {
  94. c, client := newDataConnection(t)
  95. for _, sql := range []string{
  96. `CREATE TABLE wide (id INTEGER PRIMARY KEY, big BIGINT, score DOUBLE)`,
  97. `INSERT INTO wide VALUES (1, 40000, 3.5e38)`,
  98. } {
  99. if msgs := runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(sql), 0)}); findError(msgs) != nil {
  100. t.Fatalf("%s: %s", sql, errorCode(findError(msgs)))
  101. }
  102. }
  103. for _, tc := range []struct {
  104. sql string
  105. want int32
  106. }{
  107. {`SELECT big::SMALLINT FROM wide`, 20},
  108. {`SELECT big::INTEGER FROM wide`, 20},
  109. {`SELECT score::REAL FROM wide`, 701},
  110. } {
  111. t.Run(tc.sql, func(t *testing.T) {
  112. msgs := resultMessages(runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(tc.sql), 0)}))
  113. if errMsg := findError(msgs); errMsg != nil {
  114. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  115. }
  116. oids := rowDescriptionOIDs(t, findRowDescription(t, msgs).Data)
  117. if len(oids) != 1 || oids[0] != tc.want {
  118. t.Fatalf("%s: OIDs = %v, want [%d]", tc.sql, oids, tc.want)
  119. }
  120. })
  121. }
  122. }
  123. // TestExactCastOIDsSurviveBinaryResults checks that a binary result requests the
  124. // exact width the RowDescription advertises: int2 is two bytes, float4 is four,
  125. // and a scalar NUMERIC cast is a float8.
  126. func TestExactCastOIDsSurviveBinaryResults(t *testing.T) {
  127. cases := []struct {
  128. sql string
  129. want int32
  130. data []byte
  131. }{
  132. {"SELECT 1::SMALLINT", 21, []byte{0, 1}},
  133. {"SELECT 1.5::REAL", 700, []byte{0x3f, 0xc0, 0, 0}},
  134. {"SELECT 1::NUMERIC", 701, []byte{0x3f, 0xf0, 0, 0, 0, 0, 0, 0}},
  135. }
  136. for _, tc := range cases {
  137. t.Run(tc.sql, func(t *testing.T) {
  138. c, conn := extendedResultConnection(t)
  139. sendExtended(t, c,
  140. parseMessage("s", tc.sql),
  141. bindResultMessage("p", "s", nil, []int16{1}),
  142. executeMessage("p"),
  143. &Message{Type: MsgSync},
  144. )
  145. msgs := readAllMessages(t, conn.bytes())
  146. if errMsg := findError(msgs); errMsg != nil {
  147. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  148. }
  149. oids := rowDescriptionOIDs(t, findRowDescription(t, msgs).Data)
  150. if len(oids) != 1 || oids[0] != tc.want {
  151. t.Fatalf("%s: OIDs = %v, want [%d]", tc.sql, oids, tc.want)
  152. }
  153. rows := dataRowsRaw(t, msgs)
  154. if len(rows) != 1 || len(rows[0]) != 1 {
  155. t.Fatalf("%s: rows = %v", tc.sql, rows)
  156. }
  157. assertBytes(t, tc.sql, rows[0][0], tc.data)
  158. })
  159. }
  160. }
  161. // TestArrayConstructorOfExactCast keeps the array path consistent: an exact cast
  162. // used as an ARRAY[...] element maps back to its carrier array OID instead of
  163. // falling through to text[].
  164. func TestArrayConstructorOfExactCast(t *testing.T) {
  165. c, client := newDataConnection(t)
  166. cases := []struct {
  167. sql string
  168. want int32
  169. value string
  170. }{
  171. {"SELECT ARRAY[1::SMALLINT]", 1016, "{1}"},
  172. {"SELECT ARRAY[1.5::REAL]", 1021, "{1.5}"},
  173. }
  174. for _, tc := range cases {
  175. t.Run(tc.sql, func(t *testing.T) {
  176. msgs := resultMessages(runQuery(t, c, client, &Message{Type: MsgQuery, Data: append([]byte(tc.sql), 0)}))
  177. if errMsg := findError(msgs); errMsg != nil {
  178. t.Fatalf("%s failed: %s", tc.sql, errorCode(errMsg))
  179. }
  180. oids := rowDescriptionOIDs(t, findRowDescription(t, msgs).Data)
  181. if len(oids) != 1 || oids[0] != tc.want {
  182. t.Fatalf("%s: OIDs = %v, want [%d]", tc.sql, oids, tc.want)
  183. }
  184. if row := firstDataRow(t, msgs); len(row) != 1 || row[0] != tc.value {
  185. t.Fatalf("%s: row = %v, want [%s]", tc.sql, row, tc.value)
  186. }
  187. })
  188. }
  189. }