Type Type_getCoerceIn(Type self, Type other) { Oid funcId; Type coerce; Oid fromOid = other->typeId; Oid toOid = self->typeId; if(self->inCoercions != 0) { coerce = HashMap_getByOid(self->inCoercions, fromOid); if(coerce != 0) return coerce; } if (!find_coercion_pathway(toOid, fromOid, COERCION_EXPLICIT, &funcId)) { elog(ERROR, "no conversion function from %s to %s", format_type_be(fromOid), format_type_be(toOid)); } if(funcId == InvalidOid) /* * Binary compatible type. No need for a special coercer */ return self; if(self->inCoercions == 0) self->inCoercions = HashMap_create(7, GetMemoryChunkContext(self)); coerce = Coerce_createIn(self, other, funcId); HashMap_putByOid(self->inCoercions, fromOid, coerce); return coerce; }
/* * Determine how we want to render values of a given type in datum_to_jsonb. * * Given the datatype OID, return its JsonbTypeCategory, as well as the type's * output function OID. If the returned category is JSONBTYPE_JSONCAST, * we return the OID of the relevant cast function instead. */ static void jsonb_categorize_type(Oid typoid, JsonbTypeCategory *tcategory, Oid *outfuncoid) { bool typisvarlena; /* Look through any domain */ typoid = getBaseType(typoid); *outfuncoid = InvalidOid; /* * We need to get the output function for everything except date and * timestamp types, booleans, array and composite types, json and jsonb, * and non-builtin types where there's a cast to json. In this last case * we return the oid of the cast function instead. */ switch (typoid) { case BOOLOID: *tcategory = JSONBTYPE_BOOL; break; case INT2OID: case INT4OID: case INT8OID: case FLOAT4OID: case FLOAT8OID: case NUMERICOID: getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); *tcategory = JSONBTYPE_NUMERIC; break; case DATEOID: *tcategory = JSONBTYPE_DATE; break; case TIMESTAMPOID: *tcategory = JSONBTYPE_TIMESTAMP; break; case TIMESTAMPTZOID: *tcategory = JSONBTYPE_TIMESTAMPTZ; break; case JSONBOID: *tcategory = JSONBTYPE_JSONB; break; case JSONOID: *tcategory = JSONBTYPE_JSON; break; default: /* Check for arrays and composites */ if (OidIsValid(get_element_type(typoid))) *tcategory = JSONBTYPE_ARRAY; else if (type_is_rowtype(typoid)) *tcategory = JSONBTYPE_COMPOSITE; else { /* It's probably the general case ... */ *tcategory = JSONBTYPE_OTHER; /* * but first let's look for a cast to json (note: not to * jsonb) if it's not built-in. */ if (typoid >= FirstNormalObjectId) { Oid castfunc; CoercionPathType ctype; ctype = find_coercion_pathway(JSONOID, typoid, COERCION_EXPLICIT, &castfunc); if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc)) { *tcategory = JSONBTYPE_JSONCAST; *outfuncoid = castfunc; } else { /* not a cast type, so just get the usual output func */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } } else { /* any other builtin type */ getTypeOutputInfo(typoid, outfuncoid, &typisvarlena); } break; } } }