Re: general purpose array_sort
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Junwang Zhao <zhjwpku@gmail.com>
Cc: "David G. Johnston" <david.g.johnston@gmail.com>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>, "andreas@proxel.se" <andreas@proxel.se>, Robert Haas <robertmhaas@gmail.com>, Amit Langote <amitlangote09@gmail.com>
Date: 2024-10-02T01:50:47Z
Lists: pgsql-hackers
> >
> > + typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
> > + if (typentry == NULL || typentry->type_id != elmtyp)
> > + {
> > + typentry = lookup_type_cache(elmtyp, sort_asc ? TYPECACHE_LT_OPR :
> > TYPECACHE_GT_OPR);
> > + fcinfo->flinfo->fn_extra = (void *) typentry;
> > + }
> > you need to one-time check typentry->lt_opr or typentry->gt_opr exists?
> > see CreateStatistics.
> > /* Disallow data types without a less-than operator */
> > type = lookup_type_cache(attForm->atttypid, TYPECACHE_LT_OPR);
> > if (type->lt_opr == InvalidOid)
> > ereport(ERROR,
> > (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
> > errmsg("column \"%s\" cannot be used in
> > statistics because its type %s has no default btree operator class",
> > attname, format_type_be(attForm->atttypid))));
>
> I added an Assert for this part, not sure if that is enough.
>
i think it really should be:
if (typentry == NULL || typentry->type_id != elmtyp)
{
typentry = lookup_type_cache(elmtyp, sort_asc ? TYPECACHE_LT_OPR :
TYPECACHE_GT_OPR);
fcinfo->flinfo->fn_extra = (void *) typentry;
if ((sort_asc && !OidIsValid(typentry->lt_opr) || (!sort_as &&
OidIsValid(typentry->gt_opr));
ereport(ERROR,....)
}
Imagine a type that doesn't have TYPECACHE_LT_OPR or TYPECACHE_GT_OPR
then we cannot do the sort, we should just error out.
I just tried this colour type [1] with (CREATE TYPE colour (INPUT =
colour_in, OUTPUT = colour_out, LIKE = pg_catalog.int4);
select array_sort('{#FF0000, #FF0000}'::colour[]);
of course it will segfault with your new Assert.
[1] https://github.com/hlinnaka/colour-datatype/blob/master/colour.c
Commits
-
Introduce a SQL-callable function array_sort(anyarray).
- 6c12ae09f5a5 18.0 landed
-
Fix ARRAY_SUBLINK and ARRAY[] for int2vector and oidvector input.
- 4618045bee4a 18.0 cited
-
Re-implement the ereport() macro using __VA_ARGS__.
- e3a87b4991cc 13.0 cited