Обсуждение: [GSoC] create type questions
Hi mentors and hackers, 
			
		   I have a new type defined like this 
CREATE TYPE thrift_binary (
    INPUT = thrift_binary_in,
    OUTPUT = thrift_binary_out,
    LIKE = bytea
);
in thrift_binary_in, it accepts cstring and returns thrift_binary. And in this function I returned a bytea because the create type tells the system thrift_binary and bytea are the same. However, the test passes only when I explicitly tell thrift_binary_in to return a bytea (change the return type from thrift_binary to bytea), and it does not work when returns thrift_binary. 
Any ideas why?
Thanks, Charles.
Charles Cui <charles.cui1984@gmail.com> writes:
>    I have a new type defined like this
> CREATE TYPE thrift_binary (
>     INPUT = thrift_binary_in,
>     OUTPUT = thrift_binary_out,
>     LIKE = bytea
> );
> in thrift_binary_in, it accepts cstring and returns thrift_binary.
OK, that's what it should do.
> And in
> this function I returned a bytea because the create type tells the system
> thrift_binary and bytea are the same.
Sure, PG_RETURN_BYTEA_P() would work in the C code.  I think it might be
worth inventing a macro PG_RETURN_THRIFT_BINARY_P that's a thin wrapper
around that, just for clarity's sake, but it doesn't matter functionally.
> However, the test passes only when I
> explicitly tell thrift_binary_in to return a bytea (change the return type
> from thrift_binary to bytea), and it does not work when returns
> thrift_binary.
What do you mean by "the test passes" (or doesn't pass)?  What do you
mean by "tell thrift_binary_in to return a bytea"?  You just said you
were already doing that.
This works for me:
regression=# create type thrift_binary;
CREATE TYPE
regression=# create function thrift_binary_in(cstring) returns thrift_binary
regression-# strict immutable language internal as 'byteain';
NOTICE:  return type thrift_binary is only a shell
CREATE FUNCTION
regression=# create function thrift_binary_out(thrift_binary) returns cstring
regression-# strict immutable language internal as 'byteaout';
NOTICE:  argument type thrift_binary is only a shell
CREATE FUNCTION
regression=# CREATE TYPE thrift_binary (
regression(# INPUT = thrift_binary_in,
regression(# OUTPUT = thrift_binary_out,
regression(# LIKE = bytea
regression(# );
CREATE TYPE
I cheated here by pointing to byteain/byteaout instead of C functions
that'd actually do what you want, but CREATE TYPE doesn't know that.
I suspect you may have forgotten the initial dummy creation of
thrift_binary as a "shell type".
            regards, tom lane
			
		Hi Tom, 
   Thanks for your comments, I do forget create type shell. 
But even if I add this line still does not work. 
Here is the commit that can demo my bug (https://github.com/charles-cui/pg_thrift/commit/8b43f3e2172f4a1b4e61211f7d76b061a90c38f7)
To see it, download the repo and do make install && make installcheck.
It cannot return a bytea as expected. 
Thanks Charles!
2018-06-01 18:28 GMT-07:00 Tom Lane <tgl@sss.pgh.pa.us>:
Charles Cui <charles.cui1984@gmail.com> writes:
> I have a new type defined like this
> CREATE TYPE thrift_binary (
> INPUT = thrift_binary_in,
> OUTPUT = thrift_binary_out,
> LIKE = bytea
> );
> in thrift_binary_in, it accepts cstring and returns thrift_binary.
OK, that's what it should do.
> And in
> this function I returned a bytea because the create type tells the system
> thrift_binary and bytea are the same.
Sure, PG_RETURN_BYTEA_P() would work in the C code. I think it might be
worth inventing a macro PG_RETURN_THRIFT_BINARY_P that's a thin wrapper
around that, just for clarity's sake, but it doesn't matter functionally.
> However, the test passes only when I
> explicitly tell thrift_binary_in to return a bytea (change the return type
> from thrift_binary to bytea), and it does not work when returns
> thrift_binary.
What do you mean by "the test passes" (or doesn't pass)? What do you
mean by "tell thrift_binary_in to return a bytea"? You just said you
were already doing that.
This works for me:
regression=# create type thrift_binary;
CREATE TYPE
regression=# create function thrift_binary_in(cstring) returns thrift_binary
regression-# strict immutable language internal as 'byteain';
NOTICE: return type thrift_binary is only a shell
CREATE FUNCTION
regression=# create function thrift_binary_out(thrift_binary) returns cstring 
regression-# strict immutable language internal as 'byteaout';
NOTICE: argument type thrift_binary is only a shell
CREATE FUNCTION
regression=# CREATE TYPE thrift_binary (
regression(# INPUT = thrift_binary_in,
regression(# OUTPUT = thrift_binary_out,
regression(# LIKE = bytea
regression(# );
CREATE TYPE
I cheated here by pointing to byteain/byteaout instead of C functions
that'd actually do what you want, but CREATE TYPE doesn't know that.
I suspect you may have forgotten the initial dummy creation of
thrift_binary as a "shell type".
regards, tom lane
>>>>> "Charles" == Charles Cui <charles.cui1984@gmail.com> writes: Charles> Hi Tom, Charles> Thanks for your comments, I do forget create type shell. Charles> But even if I add this line still does not work. Charles> Here is the commit that can demo my bug ( Charles> https://github.com/charles-cui/pg_thrift/commit/8b43f3e2172f4a1b4e61211f7d76b061a90c38f7 Charles> ) Charles> To see it, download the repo and do make install && make installcheck. Charles> It cannot return a bytea as expected. Your output function isn't doing anything, and in particular it's not returning any value (just dropping off the end), so as soon as pg tries to output a query result containing a thrift_binary value, it either outputs garbage or crashes. -- Andrew (irc:RhodiumToad)
Thanks Andrew, that should be the reason!
Forget thrift_binary_in is not a simple function, it is always coupled with output function.
2018-06-02 10:44 GMT-07:00 Andrew Gierth <andrew@tao11.riddles.org.uk>:
>>>>> "Charles" == Charles Cui <charles.cui1984@gmail.com> writes:
Charles> Hi Tom,
Charles> Thanks for your comments, I do forget create type shell.
Charles> But even if I add this line still does not work.
Charles> Here is the commit that can demo my bug (
Charles> https://github.com/charles-cui/pg_thrift/commit/ 8b43f3e2172f4a1b4e61211f7d76b0 61a90c38f7 
Charles> )
Charles> To see it, download the repo and do make install && make installcheck.
Charles> It cannot return a bytea as expected.
Your output function isn't doing anything, and in particular it's not
returning any value (just dropping off the end), so as soon as pg tries
to output a query result containing a thrift_binary value, it either
outputs garbage or crashes.
--
Andrew (irc:RhodiumToad)