Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbda5281

Browse files
committed
Fix behavior of ~> (cube, int) operator
~> (cube, int) operator was especially designed for knn-gist search.However, it appears that knn-gist search can't work correctly with currentbehavior of this operator when dataset contains cubes of variabledimensionality. In this case, the same value of second operator argumentcan point to different dimension depending on dimensionality of particular cube.Such behavior is incompatible with gist indexing of cubes, and knn-gist doesn'twork correctly for it.This patch changes behavior of ~> (cube, int) operator by introducing dimensionnumbering where value of second argument unambiguously identifies number ofdimension. With new behavior, this operator can be correctly supported byknn-gist. Relevant changes to cube operator class are also included.Backpatch to v9.6 where operator was introduced.Since behavior of ~> (cube, int) operator is changed, depending entitiesmust be refreshed after upgrade. Such as, expression indexes using thisoperator must be reindexed, materialized views must be rebuilt, storedprocedures and client code must be revised to correctly use new behavior.That should be mentioned in release notes.Noticed by: Tomas VondraAuthor: Alexander KorotkovReviewed by: Tomas Vondra, Andrey BorodinDiscussion:https://www.postgresql.org/message-id/flat/a9657f6a-b497-36ff-e56-482a2c7e3292@2ndquadrant.com
1 parent1226051 commitbda5281

File tree

7 files changed

+902
-528
lines changed

7 files changed

+902
-528
lines changed

‎contrib/cube/cube.c

Lines changed: 95 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,13 +1368,55 @@ g_cube_distance(PG_FUNCTION_ARGS)
13681368

13691369
if (strategy==CubeKNNDistanceCoord)
13701370
{
1371+
/*
1372+
* Handle ordering by ~> operator. See comments of cube_coord_llur()
1373+
* for details
1374+
*/
13711375
intcoord=PG_GETARG_INT32(1);
1376+
boolisLeaf=GistPageIsLeaf(entry->page);
13721377

1373-
if (IS_POINT(cube))
1374-
retval=cube->x[(coord-1) %DIM(cube)];
1378+
/* 0 is the only unsupported coordinate value */
1379+
if (coord <=0)
1380+
ereport(ERROR,
1381+
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1382+
errmsg("cube index %d is out of bounds",coord)));
1383+
1384+
if (coord <=2*DIM(cube))
1385+
{
1386+
/* dimension index */
1387+
intindex= (coord-1) /2;
1388+
/* whether this is upper bound (lower bound otherwise) */
1389+
boolupper= ((coord-1) %2==1);
1390+
1391+
if (IS_POINT(cube))
1392+
{
1393+
retval=cube->x[index];
1394+
}
1395+
else
1396+
{
1397+
if (isLeaf)
1398+
{
1399+
/* For leaf just return required upper/lower bound */
1400+
if (upper)
1401+
retval=Max(cube->x[index],cube->x[index+DIM(cube)]);
1402+
else
1403+
retval=Min(cube->x[index],cube->x[index+DIM(cube)]);
1404+
}
1405+
else
1406+
{
1407+
/*
1408+
* For non-leaf we should always return lower bound,
1409+
* because even upper bound of a child in the subtree can
1410+
* be as small as our lower bound.
1411+
*/
1412+
retval=Min(cube->x[index],cube->x[index+DIM(cube)]);
1413+
}
1414+
}
1415+
}
13751416
else
1376-
retval=Min(cube->x[(coord-1) %DIM(cube)],
1377-
cube->x[(coord-1) %DIM(cube)+DIM(cube)]);
1417+
{
1418+
retval=0.0;
1419+
}
13781420
}
13791421
else
13801422
{
@@ -1521,43 +1563,73 @@ cube_coord(PG_FUNCTION_ARGS)
15211563
}
15221564

15231565

1524-
/*
1525-
* This function works like cube_coord(),
1526-
* but rearranges coordinates of corners to get cube representation
1527-
* in the form of (lower left, upper right).
1528-
* For historical reasons that extension allows us to create cubes in form
1529-
* ((2,1),(1,2)) and instead of normalizing such cube to ((1,1),(2,2)) it
1530-
* stores cube in original way. But to get cubes ordered by one of dimensions
1531-
* directly from the index without extra sort step we need some
1532-
* representation-independent coordinate getter. This function implements it.
1566+
/*----
1567+
* This function works like cube_coord(), but rearranges coordinates in the
1568+
* way suitable to support coordinate ordering using KNN-GiST. For historical
1569+
* reasons this extension allows us to create cubes in form ((2,1),(1,2)) and
1570+
* instead of normalizing such cube to ((1,1),(2,2)) it stores cube in original
1571+
* way. But in order to get cubes ordered by one of dimensions from the index
1572+
* without explicit sort step we need this representation-independent coordinate
1573+
* getter. Moreover, indexed dataset may contain cubes of different dimensions
1574+
* number. Accordingly, this coordinate getter should be able to return
1575+
* lower/upper bound for particular dimension independently on number of cube
1576+
* dimensions.
1577+
*
1578+
* Long story short, this function uses following meaning of coordinates:
1579+
* # (2 * N - 1) -- lower bound of Nth dimension,
1580+
* # (2 * N) -- upper bound of Nth dimension.
1581+
*
1582+
* When given coordinate exceeds number of cube dimensions, then 0 returned
1583+
* (reproducing logic of GiST indexing of variable-length cubes).
15331584
*/
15341585
Datum
15351586
cube_coord_llur(PG_FUNCTION_ARGS)
15361587
{
15371588
NDBOX*cube=PG_GETARG_NDBOX(0);
15381589
intcoord=PG_GETARG_INT32(1);
1590+
boolinverse= false;
1591+
float8result;
15391592

1540-
if (coord <=0||coord>2*DIM(cube))
1593+
/* 0 is the only unsupported coordinate value */
1594+
if (coord <=0)
15411595
ereport(ERROR,
15421596
(errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
15431597
errmsg("cube index %d is out of bounds",coord)));
15441598

1545-
if (coord <=DIM(cube))
1599+
if (coord <=2*DIM(cube))
15461600
{
1601+
/* dimension index */
1602+
intindex= (coord-1) /2;
1603+
/* whether this is upper bound (lower bound otherwise) */
1604+
boolupper= ((coord-1) %2==1);
1605+
15471606
if (IS_POINT(cube))
1548-
PG_RETURN_FLOAT8(cube->x[coord-1]);
1607+
{
1608+
result=cube->x[index];
1609+
}
15491610
else
1550-
PG_RETURN_FLOAT8(Min(cube->x[coord-1],
1551-
cube->x[coord-1+DIM(cube)]));
1611+
{
1612+
if (upper)
1613+
result=Max(cube->x[index],cube->x[index+DIM(cube)]);
1614+
else
1615+
result=Min(cube->x[index],cube->x[index+DIM(cube)]);
1616+
}
15521617
}
15531618
else
15541619
{
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)]));
1620+
/*
1621+
* Return zero if coordinate is out of bound. That reproduces logic of
1622+
* how cubes with low dimension number are expanded during GiST
1623+
* indexing.
1624+
*/
1625+
result=0.0;
15601626
}
1627+
1628+
/* Inverse value if needed */
1629+
if (inverse)
1630+
result=-result;
1631+
1632+
PG_RETURN_FLOAT8(result);
15611633
}
15621634

15631635
/* Increase or decrease box size by a radius in at least n dimensions. */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp