Add missing commutators for distance operators

Some of <-> operators between geometric types have their commutators missed.
This commit adds them.  The motivation is upcoming kNN support for some of those
operators.

Discussion: https://postgr.es/m/f71ba19d-d989-63b6-f04a-abf02ad9345d%40postgrespro.ru
Author: Nikita Glukhov
Reviewed-by: Tom Lane, Alexander Korotkov
This commit is contained in:
Alexander Korotkov 2019-07-14 14:55:01 +03:00
parent 6e74c64bcf
commit 6254c55f81
5 changed files with 687 additions and 517 deletions

View File

@ -2348,6 +2348,17 @@ dist_pl(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(line_closept_point(NULL, line, pt));
}
/*
* Distance from a line to a point
*/
Datum
dist_lp(PG_FUNCTION_ARGS)
{
LINE *line = PG_GETARG_LINE_P(0);
Point *pt = PG_GETARG_POINT_P(1);
PG_RETURN_FLOAT8(line_closept_point(NULL, line, pt));
}
/*
* Distance from a point to a lseg
@ -2362,13 +2373,20 @@ dist_ps(PG_FUNCTION_ARGS)
}
/*
* Distance from a point to a path
* Distance from a lseg to a point
*/
Datum
dist_ppath(PG_FUNCTION_ARGS)
dist_sp(PG_FUNCTION_ARGS)
{
LSEG *lseg = PG_GETARG_LSEG_P(0);
Point *pt = PG_GETARG_POINT_P(1);
PG_RETURN_FLOAT8(lseg_closept_point(NULL, lseg, pt));
}
static float8
dist_ppath_internal(Point *pt, PATH *path)
{
Point *pt = PG_GETARG_POINT_P(0);
PATH *path = PG_GETARG_PATH_P(1);
float8 result = 0.0; /* keep compiler quiet */
bool have_min = false;
float8 tmp;
@ -2403,7 +2421,31 @@ dist_ppath(PG_FUNCTION_ARGS)
}
}
PG_RETURN_FLOAT8(result);
return result;
}
/*
* Distance from a point to a path
*/
Datum
dist_ppath(PG_FUNCTION_ARGS)
{
Point *pt = PG_GETARG_POINT_P(0);
PATH *path = PG_GETARG_PATH_P(1);
PG_RETURN_FLOAT8(dist_ppath_internal(pt, path));
}
/*
* Distance from a path to a point
*/
Datum
dist_pathp(PG_FUNCTION_ARGS)
{
PATH *path = PG_GETARG_PATH_P(0);
Point *pt = PG_GETARG_POINT_P(1);
PG_RETURN_FLOAT8(dist_ppath_internal(pt, path));
}
/*
@ -2418,6 +2460,18 @@ dist_pb(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(box_closept_point(NULL, box, pt));
}
/*
* Distance from a box to a point
*/
Datum
dist_bp(PG_FUNCTION_ARGS)
{
BOX *box = PG_GETARG_BOX_P(0);
Point *pt = PG_GETARG_POINT_P(1);
PG_RETURN_FLOAT8(box_closept_point(NULL, box, pt));
}
/*
* Distance from a lseg to a line
*/
@ -2430,6 +2484,18 @@ dist_sl(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(lseg_closept_line(NULL, lseg, line));
}
/*
* Distance from a line to a lseg
*/
Datum
dist_ls(PG_FUNCTION_ARGS)
{
LINE *line = PG_GETARG_LINE_P(0);
LSEG *lseg = PG_GETARG_LSEG_P(1);
PG_RETURN_FLOAT8(lseg_closept_line(NULL, lseg, line));
}
/*
* Distance from a lseg to a box
*/
@ -2442,6 +2508,18 @@ dist_sb(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(box_closept_lseg(NULL, box, lseg));
}
/*
* Distance from a box to a lseg
*/
Datum
dist_bs(PG_FUNCTION_ARGS)
{
BOX *box = PG_GETARG_BOX_P(0);
LSEG *lseg = PG_GETARG_LSEG_P(1);
PG_RETURN_FLOAT8(box_closept_lseg(NULL, box, lseg));
}
/*
* Distance from a line to a box
*/
@ -2462,13 +2540,27 @@ dist_lb(PG_FUNCTION_ARGS)
}
/*
* Distance from a circle to a polygon
* Distance from a box to a line
*/
Datum
dist_cpoly(PG_FUNCTION_ARGS)
dist_bl(PG_FUNCTION_ARGS)
{
#ifdef NOT_USED
BOX *box = PG_GETARG_BOX_P(0);
LINE *line = PG_GETARG_LINE_P(1);
#endif
/* need to think about this one for a while */
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function \"dist_bl\" not implemented")));
PG_RETURN_NULL();
}
static float8
dist_cpoly_internal(CIRCLE *circle, POLYGON *poly)
{
CIRCLE *circle = PG_GETARG_CIRCLE_P(0);
POLYGON *poly = PG_GETARG_POLYGON_P(1);
float8 result;
/* calculate distance to center, and subtract radius */
@ -2477,7 +2569,31 @@ dist_cpoly(PG_FUNCTION_ARGS)
if (result < 0.0)
result = 0.0;
PG_RETURN_FLOAT8(result);
return result;
}
/*
* Distance from a circle to a polygon
*/
Datum
dist_cpoly(PG_FUNCTION_ARGS)
{
CIRCLE *circle = PG_GETARG_CIRCLE_P(0);
POLYGON *poly = PG_GETARG_POLYGON_P(1);
PG_RETURN_FLOAT8(dist_cpoly_internal(circle, poly));
}
/*
* Distance from a polygon to a circle
*/
Datum
dist_polyc(PG_FUNCTION_ARGS)
{
POLYGON *poly = PG_GETARG_POLYGON_P(0);
CIRCLE *circle = PG_GETARG_CIRCLE_P(1);
PG_RETURN_FLOAT8(dist_cpoly_internal(circle, poly));
}
/*

View File

@ -660,22 +660,40 @@
{ oid => '613', descr => 'distance between',
oprname => '<->', oprleft => 'point', oprright => 'line',
oprresult => 'float8', oprcode => 'dist_pl' },
oprresult => 'float8', oprcom => '<->(line,point)',oprcode => 'dist_pl' },
{ oid => '760', descr => 'distance between',
oprname => '<->', oprleft => 'line', oprright => 'point',
oprresult => 'float8', oprcom => '<->(point,line)', oprcode => 'dist_lp' },
{ oid => '614', descr => 'distance between',
oprname => '<->', oprleft => 'point', oprright => 'lseg',
oprresult => 'float8', oprcode => 'dist_ps' },
oprresult => 'float8', oprcom => '<->(lseg,point)',oprcode => 'dist_ps' },
{ oid => '761', descr => 'distance between',
oprname => '<->', oprleft => 'lseg', oprright => 'point',
oprresult => 'float8', oprcom => '<->(point,lseg)', oprcode => 'dist_sp' },
{ oid => '615', descr => 'distance between',
oprname => '<->', oprleft => 'point', oprright => 'box',
oprresult => 'float8', oprcode => 'dist_pb' },
oprresult => 'float8', oprcom => '<->(box,point)', oprcode => 'dist_pb' },
{ oid => '606', descr => 'distance between',
oprname => '<->', oprleft => 'box', oprright => 'point',
oprresult => 'float8', oprcom => '<->(point,box)', oprcode => 'dist_bp' },
{ oid => '616', descr => 'distance between',
oprname => '<->', oprleft => 'lseg', oprright => 'line',
oprresult => 'float8', oprcode => 'dist_sl' },
oprresult => 'float8', oprcom => '<->(line,lseg)', oprcode => 'dist_sl' },
{ oid => '762', descr => 'distance between',
oprname => '<->', oprleft => 'line', oprright => 'lseg',
oprresult => 'float8', oprcom => '<->(lseg,line)', oprcode => 'dist_ls' },
{ oid => '617', descr => 'distance between',
oprname => '<->', oprleft => 'lseg', oprright => 'box', oprresult => 'float8',
oprcode => 'dist_sb' },
oprcom => '<->(box,lseg)', oprcode => 'dist_sb' },
{ oid => '763', descr => 'distance between',
oprname => '<->', oprleft => 'box', oprright => 'lseg', oprresult => 'float8',
oprcom => '<->(lseg,box)', oprcode => 'dist_bs' },
{ oid => '618', descr => 'distance between',
oprname => '<->', oprleft => 'point', oprright => 'path',
oprresult => 'float8', oprcode => 'dist_ppath' },
oprresult => 'float8', oprcom => '<->(path,point)', oprcode => 'dist_ppath' },
{ oid => '784', descr => 'distance between',
oprname => '<->', oprleft => 'path', oprright => 'point',
oprresult => 'float8', oprcom => '<->(point,path)', oprcode => 'dist_pathp' },
{ oid => '620', descr => 'equal',
oprname => '=', oprcanmerge => 't', oprcanhash => 't', oprleft => 'float4',
@ -1692,12 +1710,20 @@
oprcode => 'dist_polyp' },
{ oid => '1523', descr => 'distance between',
oprname => '<->', oprleft => 'circle', oprright => 'polygon',
oprresult => 'float8', oprcode => 'dist_cpoly' },
oprresult => 'float8', oprcom => '<->(polygon,circle)',
oprcode => 'dist_cpoly' },
{ oid => '1383', descr => 'distance between',
oprname => '<->', oprleft => 'polygon', oprright => 'circle',
oprresult => 'float8', oprcom => '<->(circle,polygon)',
oprcode => 'dist_polyc' },
# additional geometric operators - thomas 1997-07-09
{ oid => '1524', descr => 'distance between',
oprname => '<->', oprleft => 'line', oprright => 'box', oprresult => 'float8',
oprcode => 'dist_lb' },
oprcom => '<->(box,line)', oprcode => 'dist_lb' },
{ oid => '1382', descr => 'distance between',
oprname => '<->', oprleft => 'box', oprright => 'line', oprresult => 'float8',
oprcom => '<->(line,box)', oprcode => 'dist_bl' },
{ oid => '1525', descr => 'intersect',
oprname => '?#', oprleft => 'lseg', oprright => 'lseg', oprresult => 'bool',

View File

@ -1062,12 +1062,21 @@
{ oid => '363',
proname => 'dist_ps', prorettype => 'float8', proargtypes => 'point lseg',
prosrc => 'dist_ps' },
{ oid => '380',
proname => 'dist_sp', prorettype => 'float8', proargtypes => 'lseg point',
prosrc => 'dist_sp' },
{ oid => '364',
proname => 'dist_pb', prorettype => 'float8', proargtypes => 'point box',
prosrc => 'dist_pb' },
{ oid => '357',
proname => 'dist_bp', prorettype => 'float8', proargtypes => 'box point',
prosrc => 'dist_bp' },
{ oid => '365',
proname => 'dist_sb', prorettype => 'float8', proargtypes => 'lseg box',
prosrc => 'dist_sb' },
{ oid => '381',
proname => 'dist_bs', prorettype => 'float8', proargtypes => 'box lseg',
prosrc => 'dist_bs' },
{ oid => '366',
proname => 'close_ps', prorettype => 'point', proargtypes => 'point lseg',
prosrc => 'close_ps' },
@ -1086,6 +1095,9 @@
{ oid => '371',
proname => 'dist_ppath', prorettype => 'float8', proargtypes => 'point path',
prosrc => 'dist_ppath' },
{ oid => '421',
proname => 'dist_pathp', prorettype => 'float8', proargtypes => 'path point',
prosrc => 'dist_pathp' },
{ oid => '372',
proname => 'on_sb', prorettype => 'bool', proargtypes => 'lseg box',
prosrc => 'on_sb' },
@ -1403,15 +1415,28 @@
{ oid => '725',
proname => 'dist_pl', prorettype => 'float8', proargtypes => 'point line',
prosrc => 'dist_pl' },
{ oid => '702',
proname => 'dist_lp', prorettype => 'float8', proargtypes => 'line point',
prosrc => 'dist_lp' },
{ oid => '726',
proname => 'dist_lb', prorettype => 'float8', proargtypes => 'line box',
prosrc => 'dist_lb' },
{ oid => '703',
proname => 'dist_bl', prorettype => 'float8', proargtypes => 'box line',
prosrc => 'dist_bl' },
{ oid => '727',
proname => 'dist_sl', prorettype => 'float8', proargtypes => 'lseg line',
prosrc => 'dist_sl' },
{ oid => '704',
proname => 'dist_ls', prorettype => 'float8', proargtypes => 'line lseg',
prosrc => 'dist_ls' },
{ oid => '728',
proname => 'dist_cpoly', prorettype => 'float8',
proargtypes => 'circle polygon', prosrc => 'dist_cpoly' },
{ oid => '785',
proname => 'dist_polyc', prorettype => 'float8',
proargtypes => 'polygon circle', prosrc => 'dist_polyc' },
{ oid => '729',
proname => 'poly_distance', prorettype => 'float8',
proargtypes => 'polygon polygon', prosrc => 'poly_distance' },

File diff suppressed because it is too large Load Diff

View File

@ -71,19 +71,19 @@ SELECT p1.f1, p2.f1, p1.f1 / p2.f1 FROM POINT_TBL p1, POINT_TBL p2 WHERE p2.f1[0
SELECT p1.f1, p2.f1, p1.f1 / p2.f1 FROM POINT_TBL p1, POINT_TBL p2 WHERE p2.f1 ~= '(0,0)'::point;
-- Distance to line
SELECT p.f1, l.s, p.f1 <-> l.s FROM POINT_TBL p, LINE_TBL l;
SELECT p.f1, l.s, p.f1 <-> l.s AS dist_pl, l.s <-> p.f1 AS dist_lp FROM POINT_TBL p, LINE_TBL l;
-- Distance to line segment
SELECT p.f1, l.s, p.f1 <-> l.s FROM POINT_TBL p, LSEG_TBL l;
SELECT p.f1, l.s, p.f1 <-> l.s AS dist_ps, l.s <-> p.f1 AS dist_sp FROM POINT_TBL p, LSEG_TBL l;
-- Distance to box
SELECT p.f1, b.f1, p.f1 <-> b.f1 FROM POINT_TBL p, BOX_TBL b;
SELECT p.f1, b.f1, p.f1 <-> b.f1 AS dist_pb, b.f1 <-> p.f1 AS dist_bp FROM POINT_TBL p, BOX_TBL b;
-- Distance to path
SELECT p.f1, p1.f1, p.f1 <-> p1.f1 FROM POINT_TBL p, PATH_TBL p1;
SELECT p.f1, p1.f1, p.f1 <-> p1.f1 AS dist_ppath, p1.f1 <-> p.f1 AS dist_pathp FROM POINT_TBL p, PATH_TBL p1;
-- Distance to polygon
SELECT p.f1, p1.f1, p.f1 <-> p1.f1 FROM POINT_TBL p, POLYGON_TBL p1;
SELECT p.f1, p1.f1, p.f1 <-> p1.f1 AS dist_ppoly, p1.f1 <-> p.f1 AS dist_polyp FROM POINT_TBL p, POLYGON_TBL p1;
-- Closest point to line
SELECT p.f1, l.s, p.f1 ## l.s FROM POINT_TBL p, LINE_TBL l;
@ -127,6 +127,7 @@ SELECT l1.s, l2.s, l1.s <-> l2.s FROM LINE_TBL l1, LINE_TBL l2;
-- Distance to box
SELECT l.s, b.f1, l.s <-> b.f1 FROM LINE_TBL l, BOX_TBL b;
SELECT l.s, b.f1, b.f1 <-> l.s FROM LINE_TBL l, BOX_TBL b;
-- Intersect with line
SELECT l1.s, l2.s FROM LINE_TBL l1, LINE_TBL l2 WHERE l1.s ?# l2.s;
@ -191,13 +192,13 @@ SELECT l1.s, l2.s FROM LSEG_TBL l1, LSEG_TBL l2 WHERE l1.s ?|| l2.s;
SELECT l1.s, l2.s FROM LSEG_TBL l1, LSEG_TBL l2 WHERE l1.s ?-| l2.s;
-- Distance to line
SELECT l.s, l1.s, l.s <-> l1.s FROM LSEG_TBL l, LINE_TBL l1;
SELECT l.s, l1.s, l.s <-> l1.s AS dist_sl, l1.s <-> l.s AS dist_ls FROM LSEG_TBL l, LINE_TBL l1;
-- Distance to line segment
SELECT l1.s, l2.s, l1.s <-> l2.s FROM LSEG_TBL l1, LSEG_TBL l2;
-- Distance to box
SELECT l.s, b.f1, l.s <-> b.f1 FROM LSEG_TBL l, BOX_TBL b;
SELECT l.s, b.f1, l.s <-> b.f1 AS dist_sb, b.f1 <-> l.s AS dist_bs FROM LSEG_TBL l, BOX_TBL b;
-- Intersect with line segment
SELECT l.s, l1.s FROM LSEG_TBL l, LINE_TBL l1 WHERE l.s ?# l1.s;