Change the UNKNOWN type to have an internal representation matching
authorTom Lane <[email protected]>
Mon, 30 May 2005 01:20:50 +0000 (01:20 +0000)
committerTom Lane <[email protected]>
Mon, 30 May 2005 01:20:50 +0000 (01:20 +0000)
cstring, rather than text, so as to eliminate useless conversions
inside the parser.  Per recent discussion.

src/backend/parser/parse_coerce.c
src/backend/parser/parse_node.c
src/backend/utils/adt/varlena.c
src/include/catalog/catversion.h
src/include/catalog/pg_type.h

index 40cf25175f8e9da53c51ec57ab6a75a3deab174a..f922a317da79e693f4d27a0a7c857b588f7299d5 100644 (file)
@@ -168,8 +168,11 @@ coerce_type(ParseState *pstate, Node *node,
 
                if (!con->constisnull)
                {
-                       char       *val = DatumGetCString(DirectFunctionCall1(unknownout,
-                                                                                                          con->constvalue));
+                       /*
+                        * We assume here that UNKNOWN's internal representation is the
+                        * same as CSTRING
+                        */
+                       char       *val = DatumGetCString(con->constvalue);
 
                        /*
                         * We pass typmod -1 to the input routine, primarily because
@@ -183,7 +186,6 @@ coerce_type(ParseState *pstate, Node *node,
                         * ugly...
                         */
                        newcon->constvalue = stringTypeDatum(targetType, val, -1);
-                       pfree(val);
                }
 
                result = (Node *) newcon;
index 4b94356f8a2f11e05602cc205506aa5b6f022a58..b8044320e027b013c3e8a4e8dbf3167a3e5f17a8 100644 (file)
@@ -271,8 +271,8 @@ transformArraySubscripts(ParseState *pstate,
  *     have to guess what type is wanted.
  *
  *     For string literals we produce a constant of type UNKNOWN ---- whose
- *     representation is the same as text, but it indicates to later type
- *     resolution that we're not sure that it should be considered text.
+ *     representation is the same as cstring, but it indicates to later type
+ *     resolution that we're not sure yet what type it should be considered.
  *     Explicit "NULL" constants are also typed as UNKNOWN.
  *
  *     For integers and floats we produce int4, int8, or numeric depending
@@ -341,11 +341,14 @@ make_const(Value *value)
                        break;
 
                case T_String:
-                       val = DirectFunctionCall1(unknownin,
-                                                                         CStringGetDatum(strVal(value)));
+                       /*
+                        * We assume here that UNKNOWN's internal representation is the
+                        * same as CSTRING
+                        */
+                       val = CStringGetDatum(strVal(value));
 
                        typeid = UNKNOWNOID;    /* will be coerced later */
-                       typelen = -1;           /* variable len */
+                       typelen = -2;                   /* cstring-style varwidth type */
                        typebyval = false;
                        break;
 
@@ -362,7 +365,7 @@ make_const(Value *value)
                case T_Null:
                        /* return a null const */
                        con = makeConst(UNKNOWNOID,
-                                                       -1,
+                                                       -2,
                                                        (Datum) 0,
                                                        true,
                                                        false);
index b2bee1a19c4a9b853f509428ae45944f3f752375..a58a146d80736d26e69573e8e2624b249a762ffb 100644 (file)
@@ -330,18 +330,10 @@ textsend(PG_FUNCTION_ARGS)
 Datum
 unknownin(PG_FUNCTION_ARGS)
 {
-       char       *inputStr = PG_GETARG_CSTRING(0);
-       unknown    *result;
-       int                     len;
-
-       len = strlen(inputStr) + VARHDRSZ;
-
-       result = (unknown *) palloc(len);
-       VARATT_SIZEP(result) = len;
-
-       memcpy(VARDATA(result), inputStr, len - VARHDRSZ);
+       char       *str = PG_GETARG_CSTRING(0);
 
-       PG_RETURN_UNKNOWN_P(result);
+       /* representation is same as cstring */
+       PG_RETURN_CSTRING(pstrdup(str));
 }
 
 /*
@@ -350,16 +342,10 @@ unknownin(PG_FUNCTION_ARGS)
 Datum
 unknownout(PG_FUNCTION_ARGS)
 {
-       unknown    *t = PG_GETARG_UNKNOWN_P(0);
-       int                     len;
-       char       *result;
-
-       len = VARSIZE(t) - VARHDRSZ;
-       result = (char *) palloc(len + 1);
-       memcpy(result, VARDATA(t), len);
-       result[len] = '\0';
+       /* representation is same as cstring */
+       char       *str = PG_GETARG_CSTRING(0);
 
-       PG_RETURN_CSTRING(result);
+       PG_RETURN_CSTRING(pstrdup(str));
 }
 
 /*
@@ -369,28 +355,27 @@ Datum
 unknownrecv(PG_FUNCTION_ARGS)
 {
        StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
-       unknown    *result;
+       char       *str;
        int                     nbytes;
 
-       nbytes = buf->len - buf->cursor;
-       result = (unknown *) palloc(nbytes + VARHDRSZ);
-       VARATT_SIZEP(result) = nbytes + VARHDRSZ;
-       pq_copymsgbytes(buf, VARDATA(result), nbytes);
-       PG_RETURN_UNKNOWN_P(result);
+       str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
+       /* representation is same as cstring */
+       PG_RETURN_CSTRING(str);
 }
 
 /*
  *             unknownsend                     - converts unknown to binary format
- *
- * This is a special case: just copy the input, since it's
- * effectively the same format as bytea
  */
 Datum
 unknownsend(PG_FUNCTION_ARGS)
 {
-       unknown    *vlena = PG_GETARG_UNKNOWN_P_COPY(0);
+       /* representation is same as cstring */
+       char       *str = PG_GETARG_CSTRING(0);
+       StringInfoData buf;
 
-       PG_RETURN_UNKNOWN_P(vlena);
+       pq_begintypsend(&buf);
+       pq_sendtext(&buf, str, strlen(str));
+       PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
 
 
index 7d0e65aceb5d13c3e45d53df14a8838bf9f1e08c..7c7a7e576faf5beccc7a60e1dbc12d9bc7cf1871 100644 (file)
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200505201
+#define CATALOG_VERSION_NO     200505291
 
 #endif
index 102c399b64eb051ad487f0d2cb594250576ef5f0..7cbc5dc22d89bca78ed3254b7b2ec4be83e6ef45 100644 (file)
@@ -370,7 +370,7 @@ DESCR("relative, limited-range time interval (Unix delta time)");
 DATA(insert OID = 704 (  tinterval PGNSP PGUID 12 f b t \054 0  0 tintervalin tintervalout tintervalrecv tintervalsend - i p f 0 -1 0 _null_ _null_ ));
 DESCR("(abstime,abstime), time interval");
 #define TINTERVALOID   704
-DATA(insert OID = 705 (  unknown   PGNSP PGUID -1 f b t \054 0  0 unknownin unknownout unknownrecv unknownsend - i p f 0 -1 0 _null_ _null_ ));
+DATA(insert OID = 705 (  unknown   PGNSP PGUID -2 f b t \054 0  0 unknownin unknownout unknownrecv unknownsend - c p f 0 -1 0 _null_ _null_ ));
 DESCR("");
 #define UNKNOWNOID             705