|
6 | 6 | package eval |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "math" |
| 10 | + |
9 | 11 | "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" |
10 | 12 | "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" |
11 | 13 | "github.com/cockroachdb/cockroach/pkg/util/json" |
|
18 | 20 | errIndexOnNonArray = pgerror.Newf(pgcode.SQLJSONArrayNotFound, "jsonpath array accessor can only be applied to an array") |
19 | 21 | errIndexOutOfBounds = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is out of bounds") |
20 | 22 | errIndexNotSingleNumValue = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is not a single numeric value") |
| 23 | + errInvalidSubscript = pgerror.Newf(pgcode.InvalidSQLJSONSubscript, "jsonpath array subscript is out of integer range") |
21 | 24 | ) |
22 | 25 |
|
23 | 26 | func (ctx *jsonpathCtx) evalArrayWildcard(jsonValue json.JSON) ([]json.JSON, error) { |
@@ -110,23 +113,25 @@ func (ctx *jsonpathCtx) resolveArrayIndex( |
110 | 113 | if len(evalResults) != 1 || evalResults[0].Type() != json.NumberJSONType { |
111 | 114 | return -1, errIndexNotSingleNumValue |
112 | 115 | } |
113 | | - // TODO(normanchenn): Postgres returns an error if the index is outside int32 |
114 | | - // range. (ex. `select jsonb_path_query('[1]', 'lax $[10000000000000000]'); |
115 | 116 | i, err := asInt(evalResults[0]) |
116 | 117 | if err != nil { |
117 | | - return -1, errIndexNotSingleNumValue |
| 118 | + return -1, err |
118 | 119 | } |
119 | 120 | return i, nil |
120 | 121 | } |
121 | 122 |
|
122 | 123 | func asInt(j json.JSON) (int, error) { |
123 | 124 | d, ok := j.AsDecimal() |
124 | 125 | if !ok { |
125 | | - return 0, errInternal |
| 126 | + return 0, errIndexNotSingleNumValue |
126 | 127 | } |
127 | 128 | i64, err := d.Int64() |
128 | 129 | if err != nil { |
129 | | - return 0, err |
| 130 | + return 0, errIndexNotSingleNumValue |
| 131 | + } |
| 132 | + // Postgres returns an error if the index is outside int32 range. |
| 133 | + if i64 < math.MinInt32 || i64 > math.MaxInt32 { |
| 134 | + return 0, errInvalidSubscript |
130 | 135 | } |
131 | 136 | return int(i64), nil |
132 | 137 | } |
|
0 commit comments