8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.97 2003/08/08 21:42:04 momjian Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.98 2003/08/15 00:22:26 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -2510,18 +2510,21 @@ array_cmp(FunctionCallInfo fcinfo)
2510
2510
{
2511
2511
ArrayType * array1 = PG_GETARG_ARRAYTYPE_P (0 );
2512
2512
ArrayType * array2 = PG_GETARG_ARRAYTYPE_P (1 );
2513
+ char * p1 = (char * ) ARR_DATA_PTR (array1 );
2514
+ char * p2 = (char * ) ARR_DATA_PTR (array2 );
2515
+ int ndims1 = ARR_NDIM (array1 );
2516
+ int ndims2 = ARR_NDIM (array2 );
2517
+ int * dims1 = ARR_DIMS (array1 );
2518
+ int * dims2 = ARR_DIMS (array2 );
2519
+ int nitems1 = ArrayGetNItems (ndims1 , dims1 );
2520
+ int nitems2 = ArrayGetNItems (ndims2 , dims2 );
2521
+ Oid element_type = ARR_ELEMTYPE (array1 );
2513
2522
FmgrInfo * ac_fmgr_info = fcinfo -> flinfo ;
2514
- Datum opresult ;
2515
2523
int result = 0 ;
2516
- Oid element_type = InvalidOid ;
2517
2524
int typlen ;
2518
2525
bool typbyval ;
2519
2526
char typalign ;
2520
- Datum * dvalues1 ;
2521
- int nelems1 ;
2522
- Datum * dvalues2 ;
2523
- int nelems2 ;
2524
- int min_nelems ;
2527
+ int min_nitems ;
2525
2528
int i ;
2526
2529
typedef struct
2527
2530
{
@@ -2534,7 +2537,6 @@ array_cmp(FunctionCallInfo fcinfo)
2534
2537
} ac_extra ;
2535
2538
ac_extra * my_extra ;
2536
2539
2537
- element_type = ARR_ELEMTYPE (array1 );
2538
2540
if (element_type != ARR_ELEMTYPE (array2 ))
2539
2541
ereport (ERROR ,
2540
2542
(errcode (ERRCODE_DATATYPE_MISMATCH ),
@@ -2573,42 +2575,49 @@ array_cmp(FunctionCallInfo fcinfo)
2573
2575
typbyval = my_extra -> typbyval ;
2574
2576
typalign = my_extra -> typalign ;
2575
2577
2576
- /* extract a C array of arg array datums */
2577
- deconstruct_array (array1 , element_type , typlen , typbyval , typalign ,
2578
- & dvalues1 , & nelems1 );
2578
+ /* Loop over source data */
2579
+ min_nitems = Min (nitems1 , nitems2 );
2580
+ for (i = 0 ; i < min_nitems ; i ++ )
2581
+ {
2582
+ Datum elt1 ;
2583
+ Datum elt2 ;
2584
+ Datum opresult ;
2585
+
2586
+ /* Get element pair */
2587
+ elt1 = fetch_att (p1 , typbyval , typlen );
2588
+ elt2 = fetch_att (p2 , typbyval , typlen );
2579
2589
2580
- deconstruct_array (array2 , element_type , typlen , typbyval , typalign ,
2581
- & dvalues2 , & nelems2 );
2590
+ p1 = att_addlength (p1 , typlen , PointerGetDatum (p1 ));
2591
+ p1 = (char * ) att_align (p1 , typalign );
2592
+
2593
+ p2 = att_addlength (p2 , typlen , PointerGetDatum (p2 ));
2594
+ p2 = (char * ) att_align (p2 , typalign );
2595
+
2596
+ /* Compare the pair of elements */
2582
2597
2583
- min_nelems = Min (nelems1 , nelems2 );
2584
- for (i = 0 ; i < min_nelems ; i ++ )
2585
- {
2586
2598
/* are they equal */
2587
- opresult = FunctionCall2 (& my_extra -> eqproc ,
2588
- dvalues1 [i ], dvalues2 [i ]);
2599
+ opresult = FunctionCall2 (& my_extra -> eqproc , elt1 , elt2 );
2600
+ if (DatumGetBool (opresult ))
2601
+ continue ;
2589
2602
2590
- if (!DatumGetBool (opresult ))
2603
+ /* nope, see if arg1 is less than arg2 */
2604
+ opresult = FunctionCall2 (& my_extra -> ordproc , elt1 , elt2 );
2605
+ if (DatumGetBool (opresult ))
2591
2606
{
2592
- /* nope, see if arg1 is less than arg2 */
2593
- opresult = FunctionCall2 (& my_extra -> ordproc ,
2594
- dvalues1 [i ], dvalues2 [i ]);
2595
- if (DatumGetBool (opresult ))
2596
- {
2597
- /* arg1 is less than arg2 */
2598
- result = -1 ;
2599
- break ;
2600
- }
2601
- else
2602
- {
2603
- /* arg1 is greater than arg2 */
2604
- result = 1 ;
2605
- break ;
2606
- }
2607
+ /* arg1 is less than arg2 */
2608
+ result = -1 ;
2609
+ break ;
2610
+ }
2611
+ else
2612
+ {
2613
+ /* arg1 is greater than arg2 */
2614
+ result = 1 ;
2615
+ break ;
2607
2616
}
2608
2617
}
2609
2618
2610
- if ((result == 0 ) && (nelems1 != nelems2 ))
2611
- result = (nelems1 < nelems2 ) ? -1 : 1 ;
2619
+ if ((result == 0 ) && (nitems1 != nitems2 ))
2620
+ result = (nitems1 < nitems2 ) ? -1 : 1 ;
2612
2621
2613
2622
/* Avoid leaking memory when handed toasted input. */
2614
2623
PG_FREE_IF_COPY (array1 , 0 );
0 commit comments