Sorry, the patch had obvious bug..

-+                                                                              
          Int32GetDatum(inputTypeMod),
++                                                                              
          Int32GetDatum(targetTypeMod),

regards,


> Hello, I think this is a bug.
> 
> The core of this problem is that coerce_type() fails for Var of
> type UNKNOWNOID.
> 
> The comment for the function says that,
> 
> > * The caller should already have determined that the coercion is possible;
> > * see can_coerce_type.
> 
> But can_coerce_type() should say it's possible to convert from
> unknown to any type as it doesn't see the target node type. I
> think this as an inconsistency between can_coerce_type and
> coerce_type. So making this consistent would be right way.
> 
> Concerning only this issue, putting on-the-fly conversion for
> unkown nonconstant as attached patch worked for me. I'm not so
> confident on this, though..
> 
> regards,
> 
> At Wed, 22 Apr 2015 23:26:43 -0700, Jeff Davis <pg...@j-davis.com> wrote in 
> <1429770403.4604.22.camel@jeff-desktop>
> > On Wed, 2015-04-22 at 20:35 -0700, David G. Johnston wrote:
> > 
> > > But the fact that column "b" has the data type "unknown" is only a
> > > warning - not an error.
> > > 
> > I get an error:
> > 
> > postgres=# SELECT '  '::text = 'a';
> >  ?column? 
> > ----------
> >  f
> > (1 row)
> > 
> > postgres=# SELECT a=b FROM (SELECT ''::text, '  ') x(a,b);
> > ERROR:  failed to find conversion function from unknown to text

-- 
Kyotaro Horiguchi
NTT Open Source Software Center
diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c
index a4e494b..2e3a43c 100644
--- a/src/backend/parser/parse_coerce.c
+++ b/src/backend/parser/parse_coerce.c
@@ -221,7 +221,7 @@ coerce_type(ParseState *pstate, Node *node,
 			return node;
 		}
 	}
-	if (inputTypeId == UNKNOWNOID && IsA(node, Const))
+	if (inputTypeId == UNKNOWNOID)
 	{
 		/*
 		 * Input is a string constant with previously undetermined type. Apply
@@ -275,6 +275,29 @@ coerce_type(ParseState *pstate, Node *node,
 
 		targetType = typeidType(baseTypeId);
 
+		/* Perform on the fly conversion for non-constants */
+		if(!IsA(node, Const))
+		{
+			Form_pg_type typform = (Form_pg_type) GETSTRUCT(targetType);
+			Node *result = 
+				(Node*) makeFuncExpr(typform->typinput,
+						 targetTypeId,
+						 list_make3(node,
+									makeConst(OIDOID, -1, InvalidOid,
+											  sizeof(Oid),
+											  ObjectIdGetDatum(InvalidOid),
+											  false, true),
+									makeConst(INT4OID, -1, InvalidOid,
+											  sizeof(uint32),
+											  Int32GetDatum(targetTypeMod),
+											  false, true)),
+						  InvalidOid, InvalidOid,
+						  COERCE_IMPLICIT_CAST);
+			ReleaseSysCache(targetType);
+
+			return result;
+		}
+
 		newcon->consttype = baseTypeId;
 		newcon->consttypmod = inputTypeMod;
 		newcon->constcollid = typeTypeCollation(targetType);
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to