Re: general purpose array_sort

From: Junwang Zhao <zhjwpku(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Aleksander Alekseev <aleksander(at)timescale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Amit Langote <amitlangote09(at)gmail(dot)com>, "andreas(at)proxel(dot)se" <andreas(at)proxel(dot)se>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Re: general purpose array_sort
Date: 2024-11-05 12:30:24
Message-ID: CAEG8a3LXHwFDPbEomFEFY2Le8itV4FT0LO_0J=Ojq+McBi30jg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi jian,

On Tue, Nov 5, 2024 at 3:13 PM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> On Mon, Nov 4, 2024 at 7:34 PM Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> wrote:
> >
> > Testing this with an array with non-default lower bounds, it fails to
> > preserve the array bounds, which I think it should (note:
> > array_reverse() and array_shuffle() do preserve the bounds):
> >
> > SELECT array_reverse(a), array_shuffle(a), array_sort(a)
> > FROM (VALUES ('[10:12][20:21]={{1,2},{10,20},{3,4}}'::int[])) v(a);
> >
> > -[ RECORD 1 ]-+-------------------------------------
> > array_reverse | [10:12][20:21]={{3,4},{10,20},{1,2}}
> > array_shuffle | [10:12][20:21]={{10,20},{3,4},{1,2}}
> > array_sort | [1:3][20:21]={{1,2},{3,4},{10,20}}
> >
>
> if i understand it correctly,
> array_create_iterator cannot cope with top dimension bound information.
> since input array arguments already have dims, lbs information.
> so at the end of array_sort directly copy
> from the input array argument to astate.
>
> tuplesort_performsort won't need array bounds, we should be safe?
>
>
>
> v12-0001 same as v11-0001-general-purpose-array_sort.patch, only
> resolve git conflict
> v12-0002 preserve array bound information.
> v12-0003 cache ArrayMetaState.
>
> after v12-0003 now
> typedef struct ArraySortCachedInfo
> {
> TypeCacheEntry *typentry;
> TypeCacheEntry *array_typentry;
> ArrayMetaState array_meta;
> } ArraySortCachedInfo;
>
> function array_create_iterator, get_typlenbyvalalign
> will do cache search, we can cache ArrayMetaState.
> so multiple array_create_iterator calls won't need to call get_typlenbyvalalign.
> every time.
>
>
> 0002, I also have a 3 dimensional array test.
> create table t(a int[]);
> insert into t values ('[-1:-0]={7,1}'::int[]),
> ('[-2:-0][20:21]={{1,2},{10,20},{1,-4}}'),
> ('[-2:-0][20:22]={{-11,2,-1},{-11,2, 1},{-11,-4, 10}}'),
> ('[-13:-10][0:1][20:22]={
> {{1,2,112},{1,2,-123}},
> {{10,-20,1},{11,123,3}},
> {{10,-20,1},{11,-123,-9}},
> {{1,2,-11},{1,2,211}}}'::int[]);
> SELECT array_sort(t.a) from t;
> SELECT array_sort((t.a) [-13:-10][0:1][21:22]) from t where array_ndims(a) = 3;
> SELECT array_sort((t.a) [-13:-11][0:1][21:22]) from t where array_ndims(a) = 3;
> SELECT array_sort((t.a) [-13:-11][0:0][20:21]) from t where array_ndims(a) = 3;
>
> The test output is ok to me.

Thanks for the bounds preserve solution, I just looked at 0002,

+ if (astate->arraystate != NULL)
+ {
+ memcpy(astate->arraystate->dims, dims, ndim * sizeof(int));
+ memcpy(astate->arraystate->lbs, lbs, ndim * sizeof(int));
+ Assert(ndim == astate->arraystate->ndims);
+ }

It seems to me we only need to set astate->arraystate->lbs[0] = lbs[0] ?

--
Regards
Junwang Zhao

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ranier Vilela 2024-11-05 12:49:53 Re: define pg_structiszero(addr, s, r)
Previous Message Alvaro Herrera 2024-11-05 12:09:23 Re: Useless field ispartitioned in CreateStmtContext