Skip to content

Commit a740455

Browse files
committed
Fix Issue 1329 - agtype_to_int4 crash (apache#1339)
Fixed issue 1329 where `agtype_to_int`<8,4,2> and `agtype_to_int4_array` crashed due to not properly checking input. As these functions take "any" input, the input has to be properly checked before casting it to a specific type. The input section assumed it was agtype, which caused crashes for non-agtypes. The functions `agtype_to_int`<8,4,2> will convert non-agtypes into agtype. However, there were no regression tests for this. The functions `agtype_to_int`<8,4,2> will convert non-agtypes to agtype ints but, did not for their string equivs. Meaning, passing a ('true') or ('3.14') would fail but, passing a (true) or (3.14) would not. This has been corrected for all 3 functions. TODO - The function `agtype_to_int4_array` only takes agtype, currently, and we should consider allowing it to take "any" types. Added regression tests. Added missing regression tests.
1 parent 7f29206 commit a740455

File tree

5 files changed

+721
-67
lines changed

5 files changed

+721
-67
lines changed

regress/expected/agtype.out

+316-8
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,91 @@ SELECT agtype_to_int8(agtype_in('false'));
22172217
0
22182218
(1 row)
22192219

2220+
-- should return SQL NULL
2221+
SELECT agtype_to_int8(agtype_in('null'));
2222+
agtype_to_int8
2223+
----------------
2224+
2225+
(1 row)
2226+
2227+
SELECT agtype_to_int8(NULL);
2228+
agtype_to_int8
2229+
----------------
2230+
2231+
(1 row)
2232+
2233+
-- non agtype input
2234+
SELECT agtype_to_int8(1);
2235+
agtype_to_int8
2236+
----------------
2237+
1
2238+
(1 row)
2239+
2240+
SELECT agtype_to_int8(3.14);
2241+
agtype_to_int8
2242+
----------------
2243+
3
2244+
(1 row)
2245+
2246+
SELECT agtype_to_int8(3.14::numeric);
2247+
agtype_to_int8
2248+
----------------
2249+
3
2250+
(1 row)
2251+
2252+
SELECT agtype_to_int8('3');
2253+
agtype_to_int8
2254+
----------------
2255+
3
2256+
(1 row)
2257+
2258+
SELECT agtype_to_int8(true);
2259+
agtype_to_int8
2260+
----------------
2261+
1
2262+
(1 row)
2263+
2264+
SELECT agtype_to_int8(false);
2265+
agtype_to_int8
2266+
----------------
2267+
0
2268+
(1 row)
2269+
2270+
SELECT agtype_to_int8('3.14');
2271+
agtype_to_int8
2272+
----------------
2273+
3
2274+
(1 row)
2275+
2276+
SELECT agtype_to_int8('true');
2277+
agtype_to_int8
2278+
----------------
2279+
1
2280+
(1 row)
2281+
2282+
SELECT agtype_to_int8('false');
2283+
agtype_to_int8
2284+
----------------
2285+
0
2286+
(1 row)
2287+
2288+
-- should fail
2289+
SELECT agtype_to_int8('neither');
2290+
ERROR: invalid input syntax for type agtype
2291+
DETAIL: Expected agtype value, but found "neither".
2292+
CONTEXT: agtype data, line 1: neither
2293+
SELECT agtype_to_int8('NaN');
2294+
ERROR: bigint out of range
2295+
SELECT agtype_to_int8('Inf');
2296+
ERROR: bigint out of range
2297+
SELECT agtype_to_int8(NaN);
2298+
ERROR: column "nan" does not exist
2299+
LINE 1: SELECT agtype_to_int8(NaN);
2300+
^
2301+
SELECT agtype_to_int8(Inf);
2302+
ERROR: column "inf" does not exist
2303+
LINE 1: SELECT agtype_to_int8(Inf);
2304+
^
22202305
--
22212306
-- Test boolean to integer cast
22222307
--
@@ -2232,14 +2317,8 @@ SELECT agtype_to_int4(agtype_in('false'));
22322317
0
22332318
(1 row)
22342319

2235-
SELECT agtype_to_int4(agtype_in('null'));
2236-
agtype_to_int4
2237-
----------------
2238-
2239-
(1 row)
2240-
22412320
--
2242-
-- Test agtype to integer cast
2321+
-- Test agtype to integer4 cast
22432322
--
22442323
SELECT agtype_to_int4(agtype_in('1'));
22452324
agtype_to_int4
@@ -2261,11 +2340,228 @@ SELECT agtype_to_int4(agtype_in('1.444::numeric'));
22612340

22622341
-- These should all fail
22632342
SELECT agtype_to_int4(agtype_in('"string"'));
2264-
ERROR: invalid input syntax for type integer: "string"
2343+
ERROR: invalid input syntax for type agtype
2344+
DETAIL: Expected agtype value, but found "string".
2345+
CONTEXT: agtype data, line 1: string
22652346
SELECT agtype_to_int4(agtype_in('[1, 2, 3]'));
22662347
ERROR: cannot cast agtype array to type int
22672348
SELECT agtype_to_int4(agtype_in('{"int":1}'));
22682349
ERROR: cannot cast agtype object to type int
2350+
-- should return SQL NULL
2351+
SELECT agtype_to_int4(agtype_in('null'));
2352+
agtype_to_int4
2353+
----------------
2354+
2355+
(1 row)
2356+
2357+
SELECT agtype_to_int4(NULL);
2358+
agtype_to_int4
2359+
----------------
2360+
2361+
(1 row)
2362+
2363+
-- non agtype input
2364+
SELECT agtype_to_int4(1);
2365+
agtype_to_int4
2366+
----------------
2367+
1
2368+
(1 row)
2369+
2370+
SELECT agtype_to_int4(3.14);
2371+
agtype_to_int4
2372+
----------------
2373+
3
2374+
(1 row)
2375+
2376+
SELECT agtype_to_int4(3.14::numeric);
2377+
agtype_to_int4
2378+
----------------
2379+
3
2380+
(1 row)
2381+
2382+
SELECT agtype_to_int4('3');
2383+
agtype_to_int4
2384+
----------------
2385+
3
2386+
(1 row)
2387+
2388+
SELECT agtype_to_int4(true);
2389+
agtype_to_int4
2390+
----------------
2391+
1
2392+
(1 row)
2393+
2394+
SELECT agtype_to_int4(false);
2395+
agtype_to_int4
2396+
----------------
2397+
0
2398+
(1 row)
2399+
2400+
SELECT agtype_to_int4('3.14');
2401+
agtype_to_int4
2402+
----------------
2403+
3
2404+
(1 row)
2405+
2406+
SELECT agtype_to_int4('true');
2407+
agtype_to_int4
2408+
----------------
2409+
1
2410+
(1 row)
2411+
2412+
SELECT agtype_to_int4('false');
2413+
agtype_to_int4
2414+
----------------
2415+
0
2416+
(1 row)
2417+
2418+
-- should error
2419+
SELECT agtype_to_int4('neither');
2420+
ERROR: invalid input syntax for type agtype
2421+
DETAIL: Expected agtype value, but found "neither".
2422+
CONTEXT: agtype data, line 1: neither
2423+
SELECT agtype_to_int4('NaN');
2424+
ERROR: integer out of range
2425+
SELECT agtype_to_int4('Inf');
2426+
ERROR: integer out of range
2427+
SELECT agtype_to_int4(NaN);
2428+
ERROR: column "nan" does not exist
2429+
LINE 1: SELECT agtype_to_int4(NaN);
2430+
^
2431+
SELECT agtype_to_int4(Inf);
2432+
ERROR: column "inf" does not exist
2433+
LINE 1: SELECT agtype_to_int4(Inf);
2434+
^
2435+
--
2436+
-- Test boolean to integer2 cast
2437+
--
2438+
SELECT agtype_to_int2(agtype_in('true'));
2439+
agtype_to_int2
2440+
----------------
2441+
1
2442+
(1 row)
2443+
2444+
SELECT agtype_to_int2(agtype_in('false'));
2445+
agtype_to_int2
2446+
----------------
2447+
0
2448+
(1 row)
2449+
2450+
--
2451+
-- Test agtype to integer2 cast
2452+
--
2453+
SELECT agtype_to_int2(agtype_in('1'));
2454+
agtype_to_int2
2455+
----------------
2456+
1
2457+
(1 row)
2458+
2459+
SELECT agtype_to_int2(agtype_in('1.45'));
2460+
agtype_to_int2
2461+
----------------
2462+
1
2463+
(1 row)
2464+
2465+
SELECT agtype_to_int2(agtype_in('1.444::numeric'));
2466+
agtype_to_int2
2467+
----------------
2468+
1
2469+
(1 row)
2470+
2471+
-- These should all fail
2472+
SELECT agtype_to_int2(agtype_in('"string"'));
2473+
ERROR: invalid input syntax for type agtype
2474+
DETAIL: Expected agtype value, but found "string".
2475+
CONTEXT: agtype data, line 1: string
2476+
SELECT agtype_to_int2(agtype_in('[1, 2, 3]'));
2477+
ERROR: cannot cast agtype array to type int
2478+
SELECT agtype_to_int2(agtype_in('{"int":1}'));
2479+
ERROR: cannot cast agtype object to type int
2480+
-- should return SQL NULL
2481+
SELECT agtype_to_int2(agtype_in('null'));
2482+
agtype_to_int2
2483+
----------------
2484+
2485+
(1 row)
2486+
2487+
SELECT agtype_to_int2(NULL);
2488+
agtype_to_int2
2489+
----------------
2490+
2491+
(1 row)
2492+
2493+
-- non agtype input
2494+
SELECT agtype_to_int2(1);
2495+
agtype_to_int2
2496+
----------------
2497+
1
2498+
(1 row)
2499+
2500+
SELECT agtype_to_int2(3.14);
2501+
agtype_to_int2
2502+
----------------
2503+
3
2504+
(1 row)
2505+
2506+
SELECT agtype_to_int2(3.14::numeric);
2507+
agtype_to_int2
2508+
----------------
2509+
3
2510+
(1 row)
2511+
2512+
SELECT agtype_to_int2('3');
2513+
agtype_to_int2
2514+
----------------
2515+
3
2516+
(1 row)
2517+
2518+
SELECT agtype_to_int2(true);
2519+
agtype_to_int2
2520+
----------------
2521+
1
2522+
(1 row)
2523+
2524+
SELECT agtype_to_int2(false);
2525+
agtype_to_int2
2526+
----------------
2527+
0
2528+
(1 row)
2529+
2530+
SELECT agtype_to_int2('3.14');
2531+
agtype_to_int2
2532+
----------------
2533+
3
2534+
(1 row)
2535+
2536+
SELECT agtype_to_int2('true');
2537+
agtype_to_int2
2538+
----------------
2539+
1
2540+
(1 row)
2541+
2542+
SELECT agtype_to_int2('false');
2543+
agtype_to_int2
2544+
----------------
2545+
0
2546+
(1 row)
2547+
2548+
-- should error
2549+
SELECT agtype_to_int2('neither');
2550+
ERROR: invalid input syntax for type agtype
2551+
DETAIL: Expected agtype value, but found "neither".
2552+
CONTEXT: agtype data, line 1: neither
2553+
SELECT agtype_to_int2('NaN');
2554+
ERROR: smallint out of range
2555+
SELECT agtype_to_int2('Inf');
2556+
ERROR: smallint out of range
2557+
SELECT agtype_to_int2(NaN);
2558+
ERROR: column "nan" does not exist
2559+
LINE 1: SELECT agtype_to_int2(NaN);
2560+
^
2561+
SELECT agtype_to_int2(Inf);
2562+
ERROR: column "inf" does not exist
2563+
LINE 1: SELECT agtype_to_int2(Inf);
2564+
^
22692565
--
22702566
-- Test agtype to int[]
22712567
--
@@ -2287,6 +2583,18 @@ SELECT agtype_to_int4_array(agtype_in('["6","7",3.66]'));
22872583
{6,7,4}
22882584
(1 row)
22892585

2586+
-- should error
2587+
SELECT agtype_to_int4_array(bool('true'));
2588+
ERROR: argument must resolve to agtype
2589+
SELECT agtype_to_int4_array((1,2,3,4,5));
2590+
ERROR: argument must resolve to agtype
2591+
-- should return SQL NULL
2592+
SELECT agtype_to_int4_array(NULL);
2593+
agtype_to_int4_array
2594+
----------------------
2595+
2596+
(1 row)
2597+
22902598
--
22912599
-- Map Literal
22922600
--

0 commit comments

Comments
 (0)