X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D1A2D9DC9F7 for ; Wed, 18 Jan 2006 15:37:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 11945-10 for ; Wed, 18 Jan 2006 15:37:43 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from bob.planeti.biz (unknown [66.98.130.45]) by postgresql.org (Postfix) with ESMTP id 5E78D9DC87F for ; Wed, 18 Jan 2006 15:37:41 -0400 (AST) Received: from fatchubby (cpe-071-070-134-248.nc.res.rr.com [71.70.134.248]) by bob.planeti.biz (8.12.10/8.12.5) with SMTP id k0IJSifR027142; Wed, 18 Jan 2006 14:28:45 -0500 From: J@Planeti.Biz Message-ID: <021e01c61c66$a0899190$81300d05@fatchubby> To: "Stephan Szabo" Cc: References: <001401c61bf3$aec1fef0$7f00a8c0@kicommunication.com> <00d801c61c38$52863030$81300d05@fatchubby> <20060118065824.K33528@megazone.bigpanda.com> <018a01c61c45$a49bc170$81300d05@fatchubby> <20060118090826.Y44833@megazone.bigpanda.com> <01a101c61c54$0c484010$81300d05@fatchubby> <20060118111040.J54519@megazone.bigpanda.com> Subject: Re: Multiple Order By Criteria Date: Wed, 18 Jan 2006 14:36:09 -0500 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2527 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.605 required=5 tests=[AWL=-0.114, BIZ_TLD=1.169, NO_REAL_NAME=0.55] X-Spam-Score: 1.605 X-Spam-Level: * X-Archive-Number: 200601/310 X-Sequence-Number: 16788 Here's some C to use to create the operator classes, seems to work ok. --- #include "postgres.h" #include #include "fmgr.h" #include "utils/date.h" /* For date sorts */ PG_FUNCTION_INFO_V1(ddd_date_revcmp); Datum ddd_date_revcmp(PG_FUNCTION_ARGS){ DateADT arg1=PG_GETARG_DATEADT(0); DateADT arg2=PG_GETARG_DATEADT(1); PG_RETURN_INT32(arg2 - arg1); } /* For integer sorts */ PG_FUNCTION_INFO_V1(ddd_int_revcmp); Datum ddd_int_revcmp(PG_FUNCTION_ARGS){ int32 arg1=PG_GETARG_INT32(0); int32 arg2=PG_GETARG_INT32(1); PG_RETURN_INT32(arg2 - arg1); } /* For string sorts */ PG_FUNCTION_INFO_V1(ddd_text_revcmp); Datum ddd_text_revcmp(PG_FUNCTION_ARGS){ text* arg1=PG_GETARG_TEXT_P(0); text* arg2=PG_GETARG_TEXT_P(1); PG_RETURN_INT32(strcmp((char*)VARDATA(arg2),(char*)VARDATA(arg1))); } /* create function ddd_date_revcmp(date,date) returns int4 as '/data/postgres/contrib/cmplib.so', 'ddd_date_revcmp' LANGUAGE C STRICT; create function ddd_int_revcmp(int4,int4) returns int4 as '/data/postgres/contrib/cmplib.so', 'ddd_int_revcmp' LANGUAGE C STRICT; create function ddd_text_revcmp(text,text) returns int4 as '/data/postgres/contrib/cmplib.so', 'ddd_text_revcmp' LANGUAGE C STRICT; */ ----- Original Message ----- From: "Stephan Szabo" To: Sent: Wednesday, January 18, 2006 2:24 PM Subject: Re: [PERFORM] Multiple Order By Criteria > On Wed, 18 Jan 2006 J@Planeti.Biz wrote: > >> Could you explain to me how do create this operator class for a text data >> type ? I think it will give me more of an understanding of what's going >> on >> if I could see this example. > > Using an SQL function (mostly because I'm too lazy to look up the C call > syntax) I think it'd be something like: > > create function bttextrevcmp(text, text) returns int4 as > 'select bttextcmp($2, $1)' language 'sql'; > > CREATE OPERATOR CLASS text_revop > FOR TYPE text USING btree AS > OPERATOR 1 > , > OPERATOR 2 >= , > OPERATOR 3 = , > OPERATOR 4 <= , > OPERATOR 5 < , > FUNCTION 1 bttextrevcmp(text,text); > > I believe bttextcmp is the standard text btree operator class helper > function, so we call it with reverse arguments to try to flip its results > (I think -bttextcmp($1,$2) would also work). >