Skip to content

Commit 81ee726

Browse files
committed
Code and docs review for cube kNN support.
Commit 33bd250 could have done with some more review: Adjust coding so that compilers unfamiliar with elog/ereport don't complain about uninitialized values. Fix misuse of PG_GETARG_INT16 to retrieve arguments declared as "integer" at the SQL level. (This was evidently copied from cube_ll_coord and cube_ur_coord, but those were wrong too.) Fix non-style-guide-conforming error messages. Fix underparenthesized if statements, which pgindent would have made a hash of, and remove some unnecessary parens elsewhere. Run pgindent over new code. Revise documentation: repeated accretion of more operators without any rethinking of the text already there had left things in a bit of a mess. Merge all the cube operators into one table and adjust surrounding text appropriately. David Rowley and Tom Lane
1 parent ac443d1 commit 81ee726

File tree

6 files changed

+186
-197
lines changed

6 files changed

+186
-197
lines changed

contrib/cube/cube.c

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ distance_taxicab(PG_FUNCTION_ARGS)
12751275
if (DIM(a) < DIM(b))
12761276
{
12771277
NDBOX *tmp = b;
1278+
12781279
b = a;
12791280
a = tmp;
12801281
swapped = true;
@@ -1283,11 +1284,13 @@ distance_taxicab(PG_FUNCTION_ARGS)
12831284
distance = 0.0;
12841285
/* compute within the dimensions of (b) */
12851286
for (i = 0; i < DIM(b); i++)
1286-
distance += fabs(distance_1D(LL_COORD(a,i), UR_COORD(a,i), LL_COORD(b,i), UR_COORD(b,i)));
1287+
distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1288+
LL_COORD(b, i), UR_COORD(b, i)));
12871289

12881290
/* compute distance to zero for those dimensions in (a) absent in (b) */
12891291
for (i = DIM(b); i < DIM(a); i++)
1290-
distance += fabs(distance_1D(LL_COORD(a,i), UR_COORD(a,i), 0.0, 0.0));
1292+
distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1293+
0.0, 0.0));
12911294

12921295
if (swapped)
12931296
{
@@ -1309,13 +1312,15 @@ distance_chebyshev(PG_FUNCTION_ARGS)
13091312
NDBOX *a = PG_GETARG_NDBOX(0),
13101313
*b = PG_GETARG_NDBOX(1);
13111314
bool swapped = false;
1312-
double d, distance;
1315+
double d,
1316+
distance;
13131317
int i;
13141318

13151319
/* swap the box pointers if needed */
13161320
if (DIM(a) < DIM(b))
13171321
{
13181322
NDBOX *tmp = b;
1323+
13191324
b = a;
13201325
a = tmp;
13211326
swapped = true;
@@ -1325,15 +1330,16 @@ distance_chebyshev(PG_FUNCTION_ARGS)
13251330
/* compute within the dimensions of (b) */
13261331
for (i = 0; i < DIM(b); i++)
13271332
{
1328-
d = fabs(distance_1D(LL_COORD(a,i), UR_COORD(a,i), LL_COORD(b,i), UR_COORD(b,i)));
1333+
d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1334+
LL_COORD(b, i), UR_COORD(b, i)));
13291335
if (d > distance)
13301336
distance = d;
13311337
}
13321338

13331339
/* compute distance to zero for those dimensions in (a) absent in (b) */
13341340
for (i = DIM(b); i < DIM(a); i++)
13351341
{
1336-
d = fabs(distance_1D(LL_COORD(a,i), UR_COORD(a,i), 0.0, 0.0));
1342+
d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0));
13371343
if (d > distance)
13381344
distance = d;
13391345
}
@@ -1357,44 +1363,41 @@ g_cube_distance(PG_FUNCTION_ARGS)
13571363
{
13581364
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
13591365
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
1360-
NDBOX *cube = DatumGetNDBOX(entry->key);
1361-
double retval;
1366+
NDBOX *cube = DatumGetNDBOX(entry->key);
1367+
double retval;
13621368

13631369
if (strategy == CubeKNNDistanceCoord)
13641370
{
1365-
int coord = PG_GETARG_INT32(1);
1371+
int coord = PG_GETARG_INT32(1);
13661372

1367-
if IS_POINT(cube)
1368-
{
1369-
retval = (cube)->x[(coord-1)%DIM(cube)];
1370-
}
1373+
if (IS_POINT(cube))
1374+
retval = cube->x[(coord - 1) % DIM(cube)];
13711375
else
1372-
{
1373-
retval = Min(
1374-
(cube)->x[(coord-1)%DIM(cube)],
1375-
(cube)->x[(coord-1)%DIM(cube) + DIM(cube)]
1376-
);
1377-
}
1376+
retval = Min(cube->x[(coord - 1) % DIM(cube)],
1377+
cube->x[(coord - 1) % DIM(cube) + DIM(cube)]);
13781378
}
13791379
else
13801380
{
1381-
NDBOX *query = PG_GETARG_NDBOX(1);
1382-
switch(strategy)
1381+
NDBOX *query = PG_GETARG_NDBOX(1);
1382+
1383+
switch (strategy)
13831384
{
1384-
case CubeKNNDistanceTaxicab:
1385-
retval = DatumGetFloat8(DirectFunctionCall2(distance_taxicab,
1386-
PointerGetDatum(cube), PointerGetDatum(query)));
1387-
break;
1388-
case CubeKNNDistanceEuclid:
1389-
retval = DatumGetFloat8(DirectFunctionCall2(cube_distance,
1390-
PointerGetDatum(cube), PointerGetDatum(query)));
1391-
break;
1392-
case CubeKNNDistanceChebyshev:
1393-
retval = DatumGetFloat8(DirectFunctionCall2(distance_chebyshev,
1394-
PointerGetDatum(cube), PointerGetDatum(query)));
1395-
break;
1396-
default:
1397-
elog(ERROR, "Cube: unknown strategy number.");
1385+
case CubeKNNDistanceTaxicab:
1386+
retval = DatumGetFloat8(DirectFunctionCall2(distance_taxicab,
1387+
PointerGetDatum(cube), PointerGetDatum(query)));
1388+
break;
1389+
case CubeKNNDistanceEuclid:
1390+
retval = DatumGetFloat8(DirectFunctionCall2(cube_distance,
1391+
PointerGetDatum(cube), PointerGetDatum(query)));
1392+
break;
1393+
case CubeKNNDistanceChebyshev:
1394+
retval = DatumGetFloat8(DirectFunctionCall2(distance_chebyshev,
1395+
PointerGetDatum(cube), PointerGetDatum(query)));
1396+
break;
1397+
default:
1398+
elog(ERROR, "unrecognized cube strategy number: %d", strategy);
1399+
retval = 0; /* keep compiler quiet */
1400+
break;
13981401
}
13991402
}
14001403
PG_RETURN_FLOAT8(retval);
@@ -1466,7 +1469,7 @@ Datum
14661469
cube_ll_coord(PG_FUNCTION_ARGS)
14671470
{
14681471
NDBOX *c = PG_GETARG_NDBOX(0);
1469-
int n = PG_GETARG_INT16(1);
1472+
int n = PG_GETARG_INT32(1);
14701473
double result;
14711474

14721475
if (DIM(c) >= n && n > 0)
@@ -1483,7 +1486,7 @@ Datum
14831486
cube_ur_coord(PG_FUNCTION_ARGS)
14841487
{
14851488
NDBOX *c = PG_GETARG_NDBOX(0);
1486-
int n = PG_GETARG_INT16(1);
1489+
int n = PG_GETARG_INT32(1);
14871490
double result;
14881491

14891492
if (DIM(c) >= n && n > 0)
@@ -1504,21 +1507,17 @@ Datum
15041507
cube_coord(PG_FUNCTION_ARGS)
15051508
{
15061509
NDBOX *cube = PG_GETARG_NDBOX(0);
1507-
int coord = PG_GETARG_INT16(1);
1510+
int coord = PG_GETARG_INT32(1);
15081511

1509-
if ((coord > 0) && (coord <= 2*DIM(cube)))
1510-
{
1511-
if IS_POINT(cube)
1512-
PG_RETURN_FLOAT8( (cube)->x[(coord-1)%DIM(cube)] );
1513-
else
1514-
PG_RETURN_FLOAT8( (cube)->x[coord-1] );
1515-
}
1516-
else
1517-
{
1512+
if (coord <= 0 || coord > 2 * DIM(cube))
15181513
ereport(ERROR,
1519-
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1520-
errmsg("Cube index out of bounds")));
1521-
}
1514+
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1515+
errmsg("cube index %d is out of bounds", coord)));
1516+
1517+
if (IS_POINT(cube))
1518+
PG_RETURN_FLOAT8(cube->x[(coord - 1) % DIM(cube)]);
1519+
else
1520+
PG_RETURN_FLOAT8(cube->x[coord - 1]);
15221521
}
15231522

15241523

@@ -1536,27 +1535,28 @@ Datum
15361535
cube_coord_llur(PG_FUNCTION_ARGS)
15371536
{
15381537
NDBOX *cube = PG_GETARG_NDBOX(0);
1539-
int coord = PG_GETARG_INT16(1);
1538+
int coord = PG_GETARG_INT32(1);
15401539

1541-
if ((coord > 0) && (coord <= DIM(cube)))
1542-
{
1543-
if IS_POINT(cube)
1544-
PG_RETURN_FLOAT8( (cube)->x[coord-1] );
1545-
else
1546-
PG_RETURN_FLOAT8( Min((cube)->x[coord-1], (cube)->x[coord-1+DIM(cube)]) );
1547-
}
1548-
else if ((coord > DIM(cube)) && (coord <= 2*DIM(cube)))
1540+
if (coord <= 0 || coord > 2 * DIM(cube))
1541+
ereport(ERROR,
1542+
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1543+
errmsg("cube index %d is out of bounds", coord)));
1544+
1545+
if (coord <= DIM(cube))
15491546
{
1550-
if IS_POINT(cube)
1551-
PG_RETURN_FLOAT8( (cube)->x[(coord-1)%DIM(cube)] );
1547+
if (IS_POINT(cube))
1548+
PG_RETURN_FLOAT8(cube->x[coord - 1]);
15521549
else
1553-
PG_RETURN_FLOAT8( Max((cube)->x[coord-1], (cube)->x[coord-1-DIM(cube)]) );
1550+
PG_RETURN_FLOAT8(Min(cube->x[coord - 1],
1551+
cube->x[coord - 1 + DIM(cube)]));
15541552
}
15551553
else
15561554
{
1557-
ereport(ERROR,
1558-
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1559-
errmsg("Cube index out of bounds")));
1555+
if (IS_POINT(cube))
1556+
PG_RETURN_FLOAT8(cube->x[(coord - 1) % DIM(cube)]);
1557+
else
1558+
PG_RETURN_FLOAT8(Max(cube->x[coord - 1],
1559+
cube->x[coord - 1 - DIM(cube)]));
15601560
}
15611561
}
15621562

contrib/cube/expected/cube.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,13 @@ SELECT cube(array[10,20,30], array[40,50,60])->6;
14581458
(1 row)
14591459

14601460
SELECT cube(array[10,20,30], array[40,50,60])->0;
1461-
ERROR: Cube index out of bounds
1461+
ERROR: cube index 0 is out of bounds
14621462
SELECT cube(array[10,20,30], array[40,50,60])->7;
1463-
ERROR: Cube index out of bounds
1463+
ERROR: cube index 7 is out of bounds
14641464
SELECT cube(array[10,20,30], array[40,50,60])->-1;
1465-
ERROR: Cube index out of bounds
1465+
ERROR: cube index -1 is out of bounds
14661466
SELECT cube(array[10,20,30], array[40,50,60])->-6;
1467-
ERROR: Cube index out of bounds
1467+
ERROR: cube index -6 is out of bounds
14681468
SELECT cube(array[10,20,30])->3;
14691469
?column?
14701470
----------
@@ -1478,7 +1478,7 @@ SELECT cube(array[10,20,30])->6;
14781478
(1 row)
14791479

14801480
SELECT cube(array[10,20,30])->-6;
1481-
ERROR: Cube index out of bounds
1481+
ERROR: cube index -6 is out of bounds
14821482
-- "normalized" coordinate access
14831483
SELECT cube(array[10,20,30], array[40,50,60])~>1;
14841484
?column?
@@ -1517,15 +1517,15 @@ SELECT cube(array[40,50,60], array[10,20,30])~>3;
15171517
(1 row)
15181518

15191519
SELECT cube(array[40,50,60], array[10,20,30])~>0;
1520-
ERROR: Cube index out of bounds
1520+
ERROR: cube index 0 is out of bounds
15211521
SELECT cube(array[40,50,60], array[10,20,30])~>4;
15221522
?column?
15231523
----------
15241524
40
15251525
(1 row)
15261526

15271527
SELECT cube(array[40,50,60], array[10,20,30])~>(-1);
1528-
ERROR: Cube index out of bounds
1528+
ERROR: cube index -1 is out of bounds
15291529
-- Load some example data and build the index
15301530
--
15311531
CREATE TABLE test_cube (c cube);

contrib/cube/expected/cube_1.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,13 @@ SELECT cube(array[10,20,30], array[40,50,60])->6;
14581458
(1 row)
14591459

14601460
SELECT cube(array[10,20,30], array[40,50,60])->0;
1461-
ERROR: Cube index out of bounds
1461+
ERROR: cube index 0 is out of bounds
14621462
SELECT cube(array[10,20,30], array[40,50,60])->7;
1463-
ERROR: Cube index out of bounds
1463+
ERROR: cube index 7 is out of bounds
14641464
SELECT cube(array[10,20,30], array[40,50,60])->-1;
1465-
ERROR: Cube index out of bounds
1465+
ERROR: cube index -1 is out of bounds
14661466
SELECT cube(array[10,20,30], array[40,50,60])->-6;
1467-
ERROR: Cube index out of bounds
1467+
ERROR: cube index -6 is out of bounds
14681468
SELECT cube(array[10,20,30])->3;
14691469
?column?
14701470
----------
@@ -1478,7 +1478,7 @@ SELECT cube(array[10,20,30])->6;
14781478
(1 row)
14791479

14801480
SELECT cube(array[10,20,30])->-6;
1481-
ERROR: Cube index out of bounds
1481+
ERROR: cube index -6 is out of bounds
14821482
-- "normalized" coordinate access
14831483
SELECT cube(array[10,20,30], array[40,50,60])~>1;
14841484
?column?
@@ -1517,15 +1517,15 @@ SELECT cube(array[40,50,60], array[10,20,30])~>3;
15171517
(1 row)
15181518

15191519
SELECT cube(array[40,50,60], array[10,20,30])~>0;
1520-
ERROR: Cube index out of bounds
1520+
ERROR: cube index 0 is out of bounds
15211521
SELECT cube(array[40,50,60], array[10,20,30])~>4;
15221522
?column?
15231523
----------
15241524
40
15251525
(1 row)
15261526

15271527
SELECT cube(array[40,50,60], array[10,20,30])~>(-1);
1528-
ERROR: Cube index out of bounds
1528+
ERROR: cube index -1 is out of bounds
15291529
-- Load some example data and build the index
15301530
--
15311531
CREATE TABLE test_cube (c cube);

contrib/cube/expected/cube_2.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,13 @@ SELECT cube(array[10,20,30], array[40,50,60])->6;
14581458
(1 row)
14591459

14601460
SELECT cube(array[10,20,30], array[40,50,60])->0;
1461-
ERROR: Cube index out of bounds
1461+
ERROR: cube index 0 is out of bounds
14621462
SELECT cube(array[10,20,30], array[40,50,60])->7;
1463-
ERROR: Cube index out of bounds
1463+
ERROR: cube index 7 is out of bounds
14641464
SELECT cube(array[10,20,30], array[40,50,60])->-1;
1465-
ERROR: Cube index out of bounds
1465+
ERROR: cube index -1 is out of bounds
14661466
SELECT cube(array[10,20,30], array[40,50,60])->-6;
1467-
ERROR: Cube index out of bounds
1467+
ERROR: cube index -6 is out of bounds
14681468
SELECT cube(array[10,20,30])->3;
14691469
?column?
14701470
----------
@@ -1478,7 +1478,7 @@ SELECT cube(array[10,20,30])->6;
14781478
(1 row)
14791479

14801480
SELECT cube(array[10,20,30])->-6;
1481-
ERROR: Cube index out of bounds
1481+
ERROR: cube index -6 is out of bounds
14821482
-- "normalized" coordinate access
14831483
SELECT cube(array[10,20,30], array[40,50,60])~>1;
14841484
?column?
@@ -1517,15 +1517,15 @@ SELECT cube(array[40,50,60], array[10,20,30])~>3;
15171517
(1 row)
15181518

15191519
SELECT cube(array[40,50,60], array[10,20,30])~>0;
1520-
ERROR: Cube index out of bounds
1520+
ERROR: cube index 0 is out of bounds
15211521
SELECT cube(array[40,50,60], array[10,20,30])~>4;
15221522
?column?
15231523
----------
15241524
40
15251525
(1 row)
15261526

15271527
SELECT cube(array[40,50,60], array[10,20,30])~>(-1);
1528-
ERROR: Cube index out of bounds
1528+
ERROR: cube index -1 is out of bounds
15291529
-- Load some example data and build the index
15301530
--
15311531
CREATE TABLE test_cube (c cube);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy