Subject: Operation fails when both sides are Record elements

From: [email-masked] (Mahlon Stacy 4-4558)
Date: Wed, 10 Jun 92 10:13:53 -0700

I have defined a C function to check equality of two varlena
elements:

/* sequal.c  Compares two strings and returns Boolean true if equal */
/* MCS 6/10/92 */

#include <stdio.h>
#include "tmp/postgres.h"

bool sequal(s1,s2)
struct varlena *s1, *s2;
{
        int i;
        char *a, *b;

        a = VARDATA(s1);
        b = VARDATA(s2);

        i = strcmp(a,b);

        if(i==0) return(true);
        return(false);
}

This compiles, has been defined, along with the "=" token. 

The following works:

* retrieve (FILE.name, FILE.directory) where FILE.name = "heartbeatmov"
\g

Query sent to backend is "retrieve (FILE.name, FILE.directory) where FILE.name = "heartbeatmov" "
-----------------------------
| name        | directory   |
-----------------------------
| heartbeatmov| /images/8/SUN_DEMO|
-----------------------------
| heartbeatmov| /images/8/SUN_DEMO|
-----------------------------
| heartbeatmov| /images/8/SUN_DEMO|
-----------------------------
| heartbeatmov| /images/rolls/demoan/dsr|
-----------------------------
| heartbeatmov| /images/rolls/demoan/dsr|
-----------------------------
| heartbeatmov| /images/rolls/demoan/dsr|
-----------------------------

The following fails with the indicated error:

* retrieve (F1.name, F2.name) from F1 in FILE, F2 in FILE where F1.name = F2.name \g

Query sent to backend is "retrieve (F1.name, F2.name) from F1 in FILE, F2 in FILE where F1.name = F2.name "
WARN:Jun 10 11:13:07:fmgr: Cache lookup failed for procedure 0

What's the difference?

	-Mahlon
-------------------------------------------------------------
Mahlon Stacy                   Internet:   [email-masked]
Mayo Foundation                Amateur:    KF0AW
Rochester, MN 55905            Medical Science Bldg 1-29
(507) 284-4558                 FAX (507) 284-1428
-------------------------------------------------------------

---

From: <[email-masked]>
Date: Wed, 10 Jun 92 10:54:18 PDT

In message <[email-masked]> you write:

> bool sequal(s1,s2)
> struct varlena *s1, *s2;
> {
>         int i;
>         char *a, *b;
> 
> 
>         a = VARDATA(s1);
>         b = VARDATA(s2);
> 
>         i = strcmp(a,b);			***************
> 
>         if(i==0) return(true);
>         return(false);
> }

you should change the strcmp() to be:

	len = (VARSIZE(s1) > VARSIZE(s2) ? VARSIZE(s1) : VARSIZE(s2));
	len -= sizeof(VARSIZE(s1));
	i = strncmp(a,b);

to avoid a core dump.  varlenas are not guaranteed to be null-terminated,
and the vl->len field includes the size of that field itself.

> The following works:
> * retrieve (FILE.name, FILE.directory) where FILE.name = "heartbeatmov"\g
>

can you show us the commands you used to define the '=' operator?  include
the c function definition.
					mike olson
					project sequoia 2000
					uc berkeley
					[email-masked]

---

From: <[email-masked]>
Date: Wed, 10 Jun 92 10:58:20 PDT

cheez, i should think before i type.  in my previous message, i said you
should use

but that's wrong.  for equality, you want

	if (VARSIZE(s1) != VARSIZE(s2))
		return (false);

	i = strncmp(a, b, VARSIZE(s1) - sizeof(VARSIZE(s1)));

this does the right equality comparison, and passes the right number
of arguments to strncmp().  sorry about that.

					mike olson
					project sequoia 2000
					uc berkeley
					[email-masked]