From 134bed8089cb9e47f384f86e430af1b0b05ab525 Mon Sep 17 00:00:00 2001
From: Teodor Sigaev <teodor@sigaev.ru>
Date: Wed, 9 Nov 2005 09:26:04 +0000
Subject: [PATCH] Fix rwrite(ARRAY) on 64-bit boxes:

Instead of getting elements of array manually call deconstruct_array
---
 contrib/tsearch2/expected/tsearch2.out |  8 ++++----
 contrib/tsearch2/query_rewrite.c       | 24 +++++++++++++++++-------
 contrib/tsearch2/tsearch.sql.in        |  1 +
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/contrib/tsearch2/expected/tsearch2.out b/contrib/tsearch2/expected/tsearch2.out
index 81c456d05c3..296c0ac676f 100644
--- a/contrib/tsearch2/expected/tsearch2.out
+++ b/contrib/tsearch2/expected/tsearch2.out
@@ -13,12 +13,12 @@ psql:tsearch2.sql:342: NOTICE:  argument type tsvector is only a shell
 psql:tsearch2.sql:396: NOTICE:  type "tsquery" is not yet defined
 DETAIL:  Creating a shell type definition.
 psql:tsearch2.sql:401: NOTICE:  argument type tsquery is only a shell
-psql:tsearch2.sql:543: NOTICE:  type "gtsvector" is not yet defined
+psql:tsearch2.sql:544: NOTICE:  type "gtsvector" is not yet defined
 DETAIL:  Creating a shell type definition.
-psql:tsearch2.sql:548: NOTICE:  argument type gtsvector is only a shell
-psql:tsearch2.sql:997: NOTICE:  type "gtsq" is not yet defined
+psql:tsearch2.sql:549: NOTICE:  argument type gtsvector is only a shell
+psql:tsearch2.sql:998: NOTICE:  type "gtsq" is not yet defined
 DETAIL:  Creating a shell type definition.
-psql:tsearch2.sql:1002: NOTICE:  argument type gtsq is only a shell
+psql:tsearch2.sql:1003: NOTICE:  argument type gtsq is only a shell
 --tsvector
 SELECT '1'::tsvector;
  tsvector 
diff --git a/contrib/tsearch2/query_rewrite.c b/contrib/tsearch2/query_rewrite.c
index c462097bce3..163801c230b 100644
--- a/contrib/tsearch2/query_rewrite.c
+++ b/contrib/tsearch2/query_rewrite.c
@@ -201,6 +201,8 @@ rewrite_accum(PG_FUNCTION_ARGS) {
 	QUERYTYPE	*q;
 	QTNode		*qex, *subs = NULL, *acctree;
 	bool isfind = false;
+	Datum		*elemsp;
+	int		nelemsp;
 
 	AggregateContext = ((AggState *) fcinfo->context)->aggcontext;
 	
@@ -230,14 +232,19 @@ rewrite_accum(PG_FUNCTION_ARGS) {
 	if (ARR_ELEMTYPE(qa) != tsqOid)
 		elog(ERROR, "array should contain tsquery type");
 
-	q = (QUERYTYPE*)ARR_DATA_PTR(qa);
-	if ( q->size == 0 ) 
+	deconstruct_array(qa, tsqOid, -1, false, 'i', &elemsp, &nelemsp); 
+
+	q = (QUERYTYPE*)DatumGetPointer( elemsp[0] );
+	if ( q->size == 0 ) {
+		pfree( elemsp ); 
 		PG_RETURN_POINTER( acc );
+	}
 	
 	if ( !acc->size ) {
-		if ( acc->len > HDRSIZEQT )
+		if ( acc->len > HDRSIZEQT ) {
+			pfree( elemsp ); 
 			PG_RETURN_POINTER( acc );	
-		else
+		} else
 			acctree = QT2QTN( GETQUERY(q), GETOPERAND(q) );
 	} else 
 		acctree = QT2QTN( GETQUERY(acc), GETOPERAND(acc) );
@@ -245,14 +252,16 @@ rewrite_accum(PG_FUNCTION_ARGS) {
 	QTNTernary( acctree );
 	QTNSort( acctree );
 
-	q = (QUERYTYPE*)( ((char*)ARR_DATA_PTR(qa)) + MAXALIGN( q->len ) );
-	if ( q->size == 0 ) 
+	q = (QUERYTYPE*)DatumGetPointer( elemsp[1] );
+	if ( q->size == 0 ) { 
+		pfree( elemsp ); 
 		PG_RETURN_POINTER( acc );
+	}
 	qex = QT2QTN( GETQUERY(q), GETOPERAND(q) );
 	QTNTernary( qex );
 	QTNSort( qex );
 	
-	q = (QUERYTYPE*)( ((char*)q) + MAXALIGN( q->len ) );
+	q = (QUERYTYPE*)DatumGetPointer( elemsp[2] );
 	if ( q->size ) 
 		subs = QT2QTN( GETQUERY(q), GETOPERAND(q) );
 
@@ -270,6 +279,7 @@ rewrite_accum(PG_FUNCTION_ARGS) {
 		}
 	}
 
+	pfree( elemsp ); 
 	QTNFree( qex );	
 	QTNFree( subs );
 	QTNFree( acctree );
diff --git a/contrib/tsearch2/tsearch.sql.in b/contrib/tsearch2/tsearch.sql.in
index dd51128e5d2..9bdf641e121 100644
--- a/contrib/tsearch2/tsearch.sql.in
+++ b/contrib/tsearch2/tsearch.sql.in
@@ -402,6 +402,7 @@ LANGUAGE 'C' with (isstrict);
 
 CREATE TYPE tsquery (
         INTERNALLENGTH = -1,
+        ALIGNMENT = int4,
         INPUT = tsquery_in,
         OUTPUT = tsquery_out
 );
-- 
GitLab