Index: doc/src/sgml/array.sgml =================================================================== RCS file: /opt/src/cvs/pgsql-server/doc/src/sgml/array.sgml,v retrieving revision 1.25 diff -c -r1.25 array.sgml *** doc/src/sgml/array.sgml 13 Mar 2003 01:30:26 -0000 1.25 --- doc/src/sgml/array.sgml 29 May 2003 04:30:53 -0000 *************** *** 9,15 **** PostgreSQL allows columns of a table to be ! defined as variable-length multidimensional arrays. Arrays of any built-in type or user-defined type can be created. --- 9,15 ---- PostgreSQL allows columns of a table to be ! defined as variable-length multi-dimensional arrays. Arrays of any built-in type or user-defined type can be created. *************** *** 60,73 **** ! A limitation of the present array implementation is that individual ! elements of an array cannot be SQL null values. The entire array can be set ! to null, but you can't have an array with some elements null and some ! not. Fixing this is on the to-do list. --- 60,133 ---- + + A limitation of the present array implementation is that individual + elements of an array cannot be SQL null values. The entire array can be set + to null, but you can't have an array with some elements null and some + not. + + + This can lead to surprising results. For example, the result of the + previous two inserts looks like this: + + SELECT * FROM sal_emp; + name | pay_by_quarter | schedule + -------+---------------------------+-------------------- + Bill | {10000,10000,10000,10000} | {{meeting},{""}} + Carol | {20000,25000,25000,25000} | {{talk},{meeting}} + (2 rows) + + Because the [2][2] element of + schedule is missing in each of the + INSERT statements, the [1][2] + element is discarded. + + + + + Fixing this is on the to-do list. + + + + + The ARRAY expression syntax may also be used: + + INSERT INTO sal_emp + VALUES ('Bill', + ARRAY[10000, 10000, 10000, 10000], + ARRAY[['meeting', 'lunch'], ['','']]); + + INSERT INTO sal_emp + VALUES ('Carol', + ARRAY[20000, 25000, 25000, 25000], + ARRAY[['talk', 'consult'], ['meeting', '']]); + SELECT * FROM sal_emp; + name | pay_by_quarter | schedule + -------+---------------------------+------------------------------- + Bill | {10000,10000,10000,10000} | {{meeting,lunch},{"",""}} + Carol | {20000,25000,25000,25000} | {{talk,consult},{meeting,""}} + (2 rows) + + Note that with this syntax, multi-dimensional arrays must have matching + extents for each dimension. This eliminates the missing-array-elements + problem above. For example: + + INSERT INTO sal_emp + VALUES ('Carol', + ARRAY[20000, 25000, 25000, 25000], + ARRAY[['talk', 'consult'], ['meeting']]); + ERROR: Multiple dimension arrays must have array expressions with matching dimensions + + Also notice that string literals are single quoted instead of double quoted. + + ! The examples in the rest of this section are based on the ! ARRAY expression syntax INSERTs. + *************** *** 132,142 **** with the same result. An array subscripting operation is always taken to ! represent an array slice if any of the subscripts are written in the ! form lower:upper. A lower bound of 1 is assumed for any subscript where only one value ! is specified. --- 192,221 ---- with the same result. An array subscripting operation is always taken to ! represent an array slice if any of the subscripts are written in the form lower:upper. A lower bound of 1 is assumed for any subscript where only one value ! is specified; another example follows: ! ! SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill'; ! schedule ! --------------------------- ! {{meeting,lunch},{"",""}} ! (1 row) ! ! ! ! ! Additionally, we can also access a single arbitrary array element of ! a one-dimensional array with the array_subscript ! function: ! ! SELECT array_subscript(pay_by_quarter, 2) FROM sal_emp WHERE name = 'Bill'; ! array_subscript ! ----------------- ! 10000 ! (1 row) ! *************** *** 147,153 **** WHERE name = 'Carol'; ! or updated at a single element: UPDATE sal_emp SET pay_by_quarter[4] = 15000 --- 226,248 ---- WHERE name = 'Carol'; ! or using the ARRAY expression syntax: ! ! ! UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000] ! WHERE name = 'Carol'; ! ! ! ! ! Anywhere you can use the curly braces array syntax, ! you can also use the ARRAY expression syntax. The ! remainder of this section will illustrate only one or the other, but ! not both. ! ! ! ! An array may also be updated at a single element: UPDATE sal_emp SET pay_by_quarter[4] = 15000 *************** *** 160,165 **** --- 255,268 ---- UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}' WHERE name = 'Carol'; + + A one-dimensional array may also be updated with the + array_assign function: + + + UPDATE sal_emp SET pay_by_quarter = array_assign(pay_by_quarter, 4, 15000) + WHERE name = 'Bill'; + *************** *** 169,175 **** value currently has 4 elements, it will have five elements after an update that assigns to array[5]. Currently, enlargement in this fashion is only allowed for one-dimensional arrays, not ! multidimensional arrays. --- 272,278 ---- value currently has 4 elements, it will have five elements after an update that assigns to array[5]. Currently, enlargement in this fashion is only allowed for one-dimensional arrays, not ! multi-dimensional arrays. *************** *** 179,184 **** --- 282,367 ---- + An array can also be enlarged by using the functions + array_prepend, array_append, + or array_cat. The first two only support one-dimensional + arrays, but array_cat supports multi-dimensional arrays. + Some examples: + + + SELECT array_prepend(1, ARRAY[2,3]); + array_prepend + --------------- + {1,2,3} + (1 row) + + SELECT array_append(ARRAY[1,2], 3); + array_append + -------------- + {1,2,3} + (1 row) + + SELECT array_cat(ARRAY[1,2], ARRAY[3,4]); + array_cat + --------------- + {{1,2},{3,4}} + (1 row) + + SELECT array_cat(ARRAY[[1,2],[3,4]], ARRAY[5,6]); + array_cat + --------------------- + {{1,2},{3,4},{5,6}} + (1 row) + + SELECT array_cat(ARRAY[5,6], ARRAY[[1,2],[3,4]]); + array_cat + --------------------- + {{5,6},{1,2},{3,4}} + + + array_prepend and array_append + work with a one-dimensional array and a single element to be pushed on + to the beginning or end of the array, respectively. The array is extended + in the direction of the push. Hence, by pushing onto the beginning of an + array with a one-based subscript, a zero-based subscript array is created: + + + SELECT array_dims(t.f) FROM (SELECT array_prepend(1, ARRAY[2,3]) AS f) AS t; + array_dims + ------------ + [0:2] + (1 row) + + + array_cat works with either two + n-dimension arrays, or an n-dimension + and an n+1 dimension array. In the former case, the two + n-dimension arrays become outer elements of an + n+1 dimension array. In the latter, the + n-dimension array is added as either the first or last + outer element of the n+1 dimension array. + + + + A final method of enlarging arrays is through the concatenation operator, + ||, which works exactly as array_cat + does. + + SELECT ARRAY[1,2] || ARRAY[3,4]; + ?column? + --------------- + {{1,2},{3,4}} + (1 row) + + SELECT ARRAY[5,6] || ARRAY[[1,2],[3,4]]; + ?column? + --------------------- + {{5,6},{1,2},{3,4}} + (1 row) + + + + The syntax for CREATE TABLE allows fixed-length arrays to be defined: *************** *** 194,199 **** --- 377,392 ---- + An alternative syntax for one-dimensional arrays may be used. + pay_by_quarter could have been defined as: + + pay_by_quarter integer ARRAY[4], + + This syntax may only be used with the integer + constant to denote the array size. + + + Actually, the current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number *************** *** 292,298 **** for the array's element type. (Among the standard data types provided in the PostgreSQL distribution, type box uses a semicolon (;) but all the others ! use comma.) In a multidimensional array, each dimension (row, plane, cube, etc.) gets its own level of curly braces, and delimiters must be written between adjacent curly-braced entities of the same level. You may write whitespace before a left brace, after a right --- 485,491 ---- for the array's element type. (Among the standard data types provided in the PostgreSQL distribution, type box uses a semicolon (;) but all the others ! use comma.) In a multi-dimensional array, each dimension (row, plane, cube, etc.) gets its own level of curly braces, and delimiters must be written between adjacent curly-braced entities of the same level. You may write whitespace before a left brace, after a right *************** *** 300,305 **** --- 493,564 ---- is not ignored, however: after skipping leading whitespace, everything up to the next right brace or delimiter is taken as the item value. + + + As illustrated earlier in this chapter, arrays may also be represented + using the ARRAY expression syntax. This representation + of an array value consists of items that are interpreted according to the + I/O conversion rules for the array's element type, plus decoration that + indicates the array structure. The decoration consists of the keyword + ARRAY and square brackets ([ and + ]) around the array values, plus delimiter characters between + adjacent items. The delimiter character is always a comma (,). + When representing multi-dimensional arrays, the keyword + ARRAY is only necessary for the outer level. For example, + '{{"hello world", "happy birthday"}}' could be written as: + + SELECT ARRAY[['hello world', 'happy birthday']]; + array + ------------------------------------ + {{"hello world","happy birthday"}} + (1 row) + + or it also could be written as: + + SELECT ARRAY[ARRAY['hello world', 'happy birthday']]; + array + ------------------------------------ + {{"hello world","happy birthday"}} + (1 row) + + + + + A final method to represent an array, is through an + ARRAY sub-select expression. For example: + + SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%'); + ?column? + ------------------------------------------------------------- + {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31} + (1 row) + + The sub-select may only return a single column. The + resulting one-dimensional array will have an element for each row in the + sub-select result, with an element type matching that of the sub-select's + target column. + + + + Arrays may be cast from one type to another in similar fashion to other + data types: + + + SELECT ARRAY[1,2,3]::oid[]; + array + --------- + {1,2,3} + (1 row) + + SELECT CAST(ARRAY[1,2,3] AS float8[]); + array + --------- + {1,2,3} + (1 row) + + + + *************** *** 316,321 **** --- 575,588 ---- Alternatively, you can use backslash-escaping to protect all data characters that would otherwise be taken as array syntax or ignorable white space. + + + + The discussion in the preceding paragraph with respect to double quoting does + not pertain to the ARRAY expression syntax. In that case, + each element is quoted exactly as any other literal value of the element type. + + The array output routine will put double quotes around element values Index: doc/src/sgml/func.sgml =================================================================== RCS file: /opt/src/cvs/pgsql-server/doc/src/sgml/func.sgml,v retrieving revision 1.154 diff -c -r1.154 func.sgml *** doc/src/sgml/func.sgml 5 May 2003 15:08:49 -0000 1.154 --- doc/src/sgml/func.sgml 29 May 2003 04:53:00 -0000 *************** *** 6962,6967 **** --- 6962,7209 ---- + + Array Functions + + + shows the operators + available for the array types. + + + + <type>array</type> Operators + + + + Operator + Description + Example + Result + + + + + = + equals + ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3] + t + + + || + array-to-array concatenation + ARRAY[1,2,3] || ARRAY[4,5,6] + {{1,2,3},{4,5,6}} + + + || + array-to-array concatenation + ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]] + {{1,2,3},{4,5,6},{7,8,9}} + + + || + element-to-array concatenation + 3 || ARRAY[4,5,6] + {3,4,5,6} + + + || + array-to-element concatenation + ARRAY[4,5,6] || 7 + {4,5,6,7} + + + +
+ + + shows the functions + available for use with array types. See + for more discussion and examples for the use of these functions. + + + + <type>array</type> Functions + + + + Function + Return Type + Description + Example + Result + + + + + + + array_append + (anyarray, anyelement) + + + anyarray + + append an element to the end of an array, returning + NULL for NULL inputs + + array_append(ARRAY[1,2], 3) + {1,2,3} + + + + + array_assign + (anyarray, integer, anyelement) + + + anyarray + + assign a value to a specific array element, returning + NULL for NULL inputs + + array_assign(ARRAY[1,2,3], 2, 99) + {1,99,3} + + + + + array_cat + (anyarray, anyarray) + + + anyarray + + concatenate two arrays, returning NULL + for NULL inputs + + array_cat(ARRAY[1,2,3], ARRAY[4,5,6]) + {{1,2,3},{4,5,6}} + + + + + array_dims + (anyarray) + + + text + + returns a text representation of array dimension lower and upper bounds, + generating an ERROR for NULL inputs + + array_dims(array[[1,2,3],[4,5,6]]) + [1:2][1:3] + + + + + array_lower + (anyarray, integer) + + + integer + + returns lower bound of the requested array dimension, returning + NULL for NULL inputs + + array_lower(array_prepend(0, ARRAY[1,2,3]), 1) + 0 + + + + + array_prepend + (anyelement, anyarray) + + + anyarray + + append an element to the beginning of an array, returning + NULL for NULL inputs + + array_prepend(1, ARRAY[2,3]) + {1,2,3} + + + + + array_subscript + (anyarray, integer) + + + anyelement + + returns requested array element, returning + NULL for NULL inputs + + array_subscript(ARRAY[1,2,3], 3) + 3 + + + + + array_to_str + (anyarray, text) + + + text + + concatenates array elements using provided delimiter, returning + NULL for NULL inputs + + array_to_str(array[1.1,2.2,3.3]::numeric(4,2)[],'~^~') + 1.10~^~2.20~^~3.30 + + + + + array_upper + (anyarray, integer) + + + integer + + returns upper bound of the requested array dimension, returning + NULL for NULL inputs + + array_upper(array_append(ARRAY[1,2,3], 4), 1) + 4 + + + + + singleton_array + (anyelement) + + + anyarray + + create an array from the provided element, returning + NULL for NULL inputs + + singleton_array(1) + {1} + + + + + str_to_array + (text, text) + + + text[] + + splits string into array elements using provided delimiter, returning + NULL for NULL inputs + + str_to_array('1.10~^~2.20~^~3.30','~^~')::float8[] + {1.1,2.2,3.3} + + + +
+
Aggregate Functions Index: src/backend/catalog/pg_aggregate.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/catalog/pg_aggregate.c,v retrieving revision 1.56 diff -c -r1.56 pg_aggregate.c *** src/backend/catalog/pg_aggregate.c 18 Sep 2002 21:35:20 -0000 1.56 --- src/backend/catalog/pg_aggregate.c 29 May 2003 04:30:53 -0000 *************** *** 50,59 **** Oid finalfn = InvalidOid; /* can be omitted */ Oid finaltype; Oid fnArgs[FUNC_MAX_ARGS]; ! int nargs; Oid procOid; TupleDesc tupDesc; int i; ObjectAddress myself, referenced; --- 50,65 ---- Oid finalfn = InvalidOid; /* can be omitted */ Oid finaltype; Oid fnArgs[FUNC_MAX_ARGS]; ! int nargs_transfn; ! int nargs_finalfn; Oid procOid; TupleDesc tupDesc; int i; + Oid rettype; + Oid *true_oid_array_transfn; + Oid *true_oid_array_finalfn; + bool retset; + FuncDetailCode fdresult; ObjectAddress myself, referenced; *************** *** 68,91 **** MemSet(fnArgs, 0, FUNC_MAX_ARGS * sizeof(Oid)); fnArgs[0] = aggTransType; if (aggBaseType == ANYOID) ! nargs = 1; else { fnArgs[1] = aggBaseType; ! nargs = 2; } ! transfn = LookupFuncName(aggtransfnName, nargs, fnArgs); if (!OidIsValid(transfn)) ! func_error("AggregateCreate", aggtransfnName, nargs, fnArgs, NULL); tup = SearchSysCache(PROCOID, ObjectIdGetDatum(transfn), 0, 0, 0); if (!HeapTupleIsValid(tup)) ! func_error("AggregateCreate", aggtransfnName, nargs, fnArgs, NULL); proc = (Form_pg_proc) GETSTRUCT(tup); - if (proc->prorettype != aggTransType) - elog(ERROR, "return type of transition function %s is not %s", - NameListToString(aggtransfnName), format_type_be(aggTransType)); /* * If the transfn is strict and the initval is NULL, make sure input --- 74,122 ---- MemSet(fnArgs, 0, FUNC_MAX_ARGS * sizeof(Oid)); fnArgs[0] = aggTransType; if (aggBaseType == ANYOID) ! nargs_transfn = 1; else { fnArgs[1] = aggBaseType; ! nargs_transfn = 2; } ! ! /* ! * func_get_detail looks up the function in the catalogs, does ! * disambiguation for polymorphic functions, handles inheritance, and ! * returns the funcid and type and set or singleton status of the ! * function's return value. it also returns the true argument types ! * to the function. ! */ ! fdresult = func_get_detail(aggtransfnName, NIL, nargs_transfn, fnArgs, ! &transfn, &rettype, &retset, ! &true_oid_array_transfn); ! ! /* only valid case is a normal function */ ! if (fdresult != FUNCDETAIL_NORMAL) ! func_error("AggregateCreate", aggtransfnName, nargs_transfn, fnArgs, NULL); ! if (!OidIsValid(transfn)) ! func_error("AggregateCreate", aggtransfnName, nargs_transfn, fnArgs, NULL); ! ! /* ! * enforce consistency with ANYARRAY and ANYELEMENT argument ! * and return types, possibly modifying return type along the way ! */ ! rettype = enforce_generic_type_consistency(fnArgs, true_oid_array_transfn, ! nargs_transfn, rettype); ! ! if (rettype != aggTransType) ! elog(ERROR, "return type of transition function %s is not %s", ! NameListToString(aggtransfnName), format_type_be(aggTransType)); ! tup = SearchSysCache(PROCOID, ObjectIdGetDatum(transfn), 0, 0, 0); if (!HeapTupleIsValid(tup)) ! func_error("AggregateCreate", aggtransfnName, ! nargs_transfn, fnArgs, NULL); proc = (Form_pg_proc) GETSTRUCT(tup); /* * If the transfn is strict and the initval is NULL, make sure input *************** *** 105,121 **** { MemSet(fnArgs, 0, FUNC_MAX_ARGS * sizeof(Oid)); fnArgs[0] = aggTransType; ! finalfn = LookupFuncName(aggfinalfnName, 1, fnArgs); ! if (!OidIsValid(finalfn)) func_error("AggregateCreate", aggfinalfnName, 1, fnArgs, NULL); ! tup = SearchSysCache(PROCOID, ! ObjectIdGetDatum(finalfn), ! 0, 0, 0); ! if (!HeapTupleIsValid(tup)) func_error("AggregateCreate", aggfinalfnName, 1, fnArgs, NULL); ! proc = (Form_pg_proc) GETSTRUCT(tup); ! finaltype = proc->prorettype; ! ReleaseSysCache(tup); } else { --- 136,161 ---- { MemSet(fnArgs, 0, FUNC_MAX_ARGS * sizeof(Oid)); fnArgs[0] = aggTransType; ! nargs_finalfn = 1; ! ! fdresult = func_get_detail(aggfinalfnName, NIL, 1, fnArgs, ! &finalfn, &rettype, &retset, ! &true_oid_array_finalfn); ! ! /* only valid case is a normal function */ ! if (fdresult != FUNCDETAIL_NORMAL) func_error("AggregateCreate", aggfinalfnName, 1, fnArgs, NULL); ! ! if (!OidIsValid(finalfn)) func_error("AggregateCreate", aggfinalfnName, 1, fnArgs, NULL); ! ! /* ! * enforce consistency with ANYARRAY and ANYELEMENT argument ! * and return types, possibly modifying return type along the way ! */ ! finaltype = enforce_generic_type_consistency(fnArgs, ! true_oid_array_finalfn, ! nargs_finalfn, rettype); } else { *************** *** 125,130 **** --- 165,191 ---- finaltype = aggTransType; } Assert(OidIsValid(finaltype)); + + /* + * special disallowed cases: + * 1) if finaltype is polymorphic, basetype cannot be ANY + * 2) if finaltype is polymorphic, both args to transfn must be + * polymorphic + */ + if (finaltype == ANYARRAYOID || finaltype == ANYELEMENTOID) + { + if (aggBaseType == ANYOID) + elog(ERROR, "aggregate with base type ANY must have a " \ + "non-polymorphic return type"); + + if (nargs_transfn > 1 && ( + (true_oid_array_transfn[0] != ANYARRAYOID && + true_oid_array_transfn[0] != ANYELEMENTOID) || + (true_oid_array_transfn[1] != ANYARRAYOID && + true_oid_array_transfn[1] != ANYELEMENTOID))) + elog(ERROR, "aggregate with polymorphic return type requires " \ + "state function with both arguments polymorphic"); + } /* * Everything looks okay. Try to create the pg_proc entry for the Index: src/backend/commands/aggregatecmds.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/commands/aggregatecmds.c,v retrieving revision 1.5 diff -c -r1.5 aggregatecmds.c *** src/backend/commands/aggregatecmds.c 4 Sep 2002 20:31:14 -0000 1.5 --- src/backend/commands/aggregatecmds.c 29 May 2003 04:30:53 -0000 *************** *** 119,125 **** baseTypeId = typenameTypeId(baseType); transTypeId = typenameTypeId(transType); ! if (get_typtype(transTypeId) == 'p') elog(ERROR, "Aggregate transition datatype cannot be %s", format_type_be(transTypeId)); --- 119,127 ---- baseTypeId = typenameTypeId(baseType); transTypeId = typenameTypeId(transType); ! if (get_typtype(transTypeId) == 'p' && ! transTypeId != ANYARRAYOID && ! transTypeId != ANYELEMENTOID) elog(ERROR, "Aggregate transition datatype cannot be %s", format_type_be(transTypeId)); Index: src/backend/executor/nodeAgg.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/executor/nodeAgg.c,v retrieving revision 1.104 diff -c -r1.104 nodeAgg.c *** src/backend/executor/nodeAgg.c 9 Feb 2003 00:30:39 -0000 1.104 --- src/backend/executor/nodeAgg.c 29 May 2003 04:30:53 -0000 *************** *** 58,63 **** --- 58,64 ---- #include "executor/executor.h" #include "executor/nodeAgg.h" #include "miscadmin.h" + #include "nodes/makefuncs.h" #include "optimizer/clauses.h" #include "parser/parse_coerce.h" #include "parser/parse_expr.h" *************** *** 212,218 **** static void agg_fill_hash_table(AggState *aggstate); static TupleTableSlot *agg_retrieve_hash_table(AggState *aggstate); static Datum GetAggInitVal(Datum textInitVal, Oid transtype); ! /* * Initialize all aggregates for a new group of input values. --- 213,219 ---- static void agg_fill_hash_table(AggState *aggstate); static TupleTableSlot *agg_retrieve_hash_table(AggState *aggstate); static Datum GetAggInitVal(Datum textInitVal, Oid transtype); ! static Oid resolve_type(Oid type_to_resolve, Oid context_type); /* * Initialize all aggregates for a new group of input values. *************** *** 351,364 **** fcinfo.context = NULL; fcinfo.resultinfo = NULL; fcinfo.isnull = false; - fcinfo.flinfo = &peraggstate->transfn; fcinfo.nargs = 2; fcinfo.arg[0] = pergroupstate->transValue; fcinfo.argnull[0] = pergroupstate->transValueIsNull; fcinfo.arg[1] = newVal; fcinfo.argnull[1] = isNull; - newVal = FunctionCallInvoke(&fcinfo); /* --- 352,363 ---- *************** *** 1176,1182 **** --- 1175,1195 ---- AclResult aclresult; Oid transfn_oid, finalfn_oid; + FuncExpr *transfnexpr, + *finalfnexpr; Datum textInitVal; + List *fargs; + Oid agg_rt_type; + Oid *transfn_arg_types; + List *transfn_args = NIL; + int transfn_nargs; + Oid transfn_ret_type; + Oid *finalfn_arg_types = NULL; + List *finalfn_args = NIL; + Oid finalfn_ret_type = InvalidOid; + int finalfn_nargs = 0; + Node *arg0; + Node *arg1; int i; /* Look for a previous duplicate aggregate */ *************** *** 1224,1229 **** --- 1237,1402 ---- &peraggstate->transtypeLen, &peraggstate->transtypeByVal); + peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn; + peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn; + + /* get the runtime aggregate argument type */ + fargs = aggref->args; + agg_rt_type = exprType((Node *) nth(0, fargs)); + + /* get the transition function argument and return types */ + transfn_ret_type = get_func_rettype(transfn_oid); + transfn_arg_types = get_func_argtypes(transfn_oid, &transfn_nargs); + + /* resolve any polymorphic types */ + if (transfn_nargs == 2) + /* base type was not ANY */ + { + if (transfn_arg_types[1] == ANYARRAYOID || + transfn_arg_types[1] == ANYELEMENTOID) + transfn_arg_types[1] = agg_rt_type; + + transfn_arg_types[0] = resolve_type(transfn_arg_types[0], + agg_rt_type); + + /* + * Build arg list to use on the transfn FuncExpr node. We really + * only care that the node type is correct so that the transfn + * can discover the actual argument types at runtime using + * get_fn_expr_argtype() + */ + arg0 = (Node *) makeRelabelType((Expr *) NULL, transfn_arg_types[0], + -1, COERCE_DONTCARE); + arg1 = (Node *) makeRelabelType((Expr *) NULL, transfn_arg_types[1], + -1, COERCE_DONTCARE); + transfn_args = makeList2(arg0, arg1); + + /* + * the state transition function always returns the same type + * as its first argument + */ + if (transfn_ret_type == ANYARRAYOID || + transfn_ret_type == ANYELEMENTOID) + transfn_ret_type = transfn_arg_types[0]; + } + else if (transfn_nargs == 1) + /* + * base type was ANY, therefore the aggregate return type should + * be non-polymorphic + */ + { + Oid finaltype = get_func_rettype(aggref->aggfnoid); + + /* + * this should have been prevented in AggregateCreate, + * but check anyway + */ + if (finaltype == ANYARRAYOID || finaltype == ANYELEMENTOID) + elog(ERROR, "aggregate with base type ANY must have a " \ + "non-polymorphic return type"); + + /* see if we have a final function */ + if (OidIsValid(finalfn_oid)) + { + finalfn_arg_types = get_func_argtypes(finalfn_oid, &finalfn_nargs); + if (finalfn_nargs != 1) + elog(ERROR, "final function takes unexpected number " \ + "of arguments: %d", finalfn_nargs); + + /* + * final function argument is always the same as the state + * function return type + */ + if (finalfn_arg_types[0] != ANYARRAYOID && + finalfn_arg_types[0] != ANYELEMENTOID) + { + /* if it is not ambiguous, use it */ + transfn_ret_type = finalfn_arg_types[0]; + } + else + { + /* if it is ambiguous, try to derive it */ + finalfn_ret_type = finaltype; + finalfn_arg_types[0] = resolve_type(finalfn_arg_types[0], + finalfn_ret_type); + transfn_ret_type = finalfn_arg_types[0]; + } + } + else + transfn_ret_type = finaltype; + + transfn_arg_types[0] = resolve_type(transfn_arg_types[0], + transfn_ret_type); + + /* + * Build arg list to use on the transfn FuncExpr node. We really + * only care that the node type is correct so that the transfn + * can discover the actual argument types at runtime using + * get_fn_expr_argtype() + */ + arg0 = (Node *) makeRelabelType((Expr *) NULL, transfn_arg_types[0], + -1, COERCE_DONTCARE); + transfn_args = makeList1(arg0); + } + else + elog(ERROR, "state transition function takes unexpected number " \ + "of arguments: %d", transfn_nargs); + + if (OidIsValid(finalfn_oid)) + { + /* get the final function argument and return types */ + if (finalfn_ret_type == InvalidOid) + finalfn_ret_type = get_func_rettype(finalfn_oid); + + if (!finalfn_arg_types) + { + finalfn_arg_types = get_func_argtypes(finalfn_oid, &finalfn_nargs); + if (finalfn_nargs != 1) + elog(ERROR, "final function takes unexpected number " \ + "of arguments: %d", finalfn_nargs); + } + + /* + * final function argument is always the same as the state + * function return type, which by now should have been resolved + */ + if (finalfn_arg_types[0] == ANYARRAYOID || + finalfn_arg_types[0] == ANYELEMENTOID) + finalfn_arg_types[0] = transfn_ret_type; + + /* + * Build arg list to use on the finalfn FuncExpr node. We really + * only care that the node type is correct so that the finalfn + * can discover the actual argument type at runtime using + * get_fn_expr_argtype() + */ + arg0 = (Node *) makeRelabelType((Expr *) NULL, finalfn_arg_types[0], + -1, COERCE_DONTCARE); + finalfn_args = makeList1(arg0); + + finalfn_ret_type = resolve_type(finalfn_ret_type, + finalfn_arg_types[0]); + } + + fmgr_info(transfn_oid, &peraggstate->transfn); + transfnexpr = (FuncExpr *) make_funcclause(transfn_oid, + transfn_ret_type, + false, /* cannot be a set */ + COERCE_DONTCARE, /* to match any user expr */ + transfn_args); + peraggstate->transfn.fn_expr = (Node *) transfnexpr; + + if (OidIsValid(finalfn_oid)) + { + fmgr_info(finalfn_oid, &peraggstate->finalfn); + finalfnexpr = (FuncExpr *) make_funcclause(finalfn_oid, + finalfn_ret_type, + false, /* cannot be a set */ + COERCE_DONTCARE, /* to match any user expr */ + finalfn_args); + peraggstate->finalfn.fn_expr = (Node *) finalfnexpr; + } + /* * initval is potentially null, so don't try to access it as a * struct field. Must do it the hard way with SysCacheGetAttr. *************** *** 1236,1249 **** peraggstate->initValue = (Datum) 0; else peraggstate->initValue = GetAggInitVal(textInitVal, ! aggform->aggtranstype); ! ! peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn; ! peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn; ! ! fmgr_info(transfn_oid, &peraggstate->transfn); ! if (OidIsValid(finalfn_oid)) ! fmgr_info(finalfn_oid, &peraggstate->finalfn); /* * If the transfn is strict and the initval is NULL, make sure --- 1409,1415 ---- peraggstate->initValue = (Datum) 0; else peraggstate->initValue = GetAggInitVal(textInitVal, ! transfn_arg_types[0]); /* * If the transfn is strict and the initval is NULL, make sure *************** *** 1425,1428 **** --- 1591,1627 ---- elog(ERROR, "Aggregate function %u called as normal function", fcinfo->flinfo->fn_oid); return (Datum) 0; /* keep compiler quiet */ + } + + static Oid + resolve_type(Oid type_to_resolve, Oid context_type) + { + Oid resolved_type; + + if (context_type == ANYARRAYOID || context_type == ANYELEMENTOID) + resolved_type = type_to_resolve; + else if (type_to_resolve == ANYARRAYOID) + /* any array */ + { + Oid context_type_arraytype = get_array_type(context_type); + + if (context_type_arraytype != InvalidOid) + resolved_type = context_type_arraytype; + else + resolved_type = context_type; + } + else if (type_to_resolve == ANYELEMENTOID) + /* any element */ + { + Oid context_type_elemtype = get_element_type(context_type); + + if (context_type_elemtype != InvalidOid) + resolved_type = context_type_elemtype; + else + resolved_type = context_type; + } + else + resolved_type = type_to_resolve; + + return resolved_type; } Index: src/backend/executor/nodeSubplan.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/executor/nodeSubplan.c,v retrieving revision 1.45 diff -c -r1.45 nodeSubplan.c *** src/backend/executor/nodeSubplan.c 8 Apr 2003 23:20:01 -0000 1.45 --- src/backend/executor/nodeSubplan.c 29 May 2003 04:30:53 -0000 *************** *** 28,50 **** #include "utils/datum.h" #include "utils/lsyscache.h" - - typedef struct ArrayBuildState - { - MemoryContext mcontext; /* where all the temp stuff is kept */ - Datum *dvalues; /* array of accumulated Datums */ - /* - * The allocated size of dvalues[] is always a multiple of - * ARRAY_ELEMS_CHUNKSIZE - */ - #define ARRAY_ELEMS_CHUNKSIZE 64 - int nelems; /* number of valid Datums in dvalues[] */ - Oid element_type; /* data type of the Datums */ - int16 typlen; /* needed info about datatype */ - bool typbyval; - char typalign; - } ArrayBuildState; - static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull); --- 28,33 ---- *************** *** 54,66 **** static void buildSubPlanHash(SubPlanState *node); static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot); static bool tupleAllNulls(HeapTuple tuple); - static ArrayBuildState *accumArrayResult(ArrayBuildState *astate, - Datum dvalue, bool disnull, - Oid element_type, - MemoryContext rcontext); - static Datum makeArrayResult(ArrayBuildState *astate, - MemoryContext rcontext); - /* ---------------------------------------------------------------- * ExecSubPlan --- 37,42 ---- *************** *** 1084,1185 **** prm->execPlan = node; parent->chgParam = bms_add_member(parent->chgParam, paramid); } - } - - /* - * accumArrayResult - accumulate one (more) Datum for an ARRAY_SUBLINK - * - * astate is working state (NULL on first call) - * rcontext is where to keep working state - */ - static ArrayBuildState * - accumArrayResult(ArrayBuildState *astate, - Datum dvalue, bool disnull, - Oid element_type, - MemoryContext rcontext) - { - MemoryContext arr_context, - oldcontext; - - if (astate == NULL) - { - /* First time through --- initialize */ - - /* Make a temporary context to hold all the junk */ - arr_context = AllocSetContextCreate(rcontext, - "ARRAY_SUBLINK Result", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); - oldcontext = MemoryContextSwitchTo(arr_context); - astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState)); - astate->mcontext = arr_context; - astate->dvalues = (Datum *) - palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(Datum)); - astate->nelems = 0; - astate->element_type = element_type; - get_typlenbyvalalign(element_type, - &astate->typlen, - &astate->typbyval, - &astate->typalign); - } - else - { - oldcontext = MemoryContextSwitchTo(astate->mcontext); - Assert(astate->element_type == element_type); - /* enlarge dvalues[] if needed */ - if ((astate->nelems % ARRAY_ELEMS_CHUNKSIZE) == 0) - astate->dvalues = (Datum *) - repalloc(astate->dvalues, - (astate->nelems + ARRAY_ELEMS_CHUNKSIZE) * sizeof(Datum)); - } - - if (disnull) - elog(ERROR, "NULL elements not allowed in Arrays"); - - /* Use datumCopy to ensure pass-by-ref stuff is copied into mcontext */ - astate->dvalues[astate->nelems++] = - datumCopy(dvalue, astate->typbyval, astate->typlen); - - MemoryContextSwitchTo(oldcontext); - - return astate; - } - - /* - * makeArrayResult - produce final result of ARRAY_SUBLINK - * - * astate is working state (not NULL) - * rcontext is where to construct result - */ - static Datum - makeArrayResult(ArrayBuildState *astate, - MemoryContext rcontext) - { - ArrayType *result; - int dims[1]; - int lbs[1]; - MemoryContext oldcontext; - - /* Build the final array result in rcontext */ - oldcontext = MemoryContextSwitchTo(rcontext); - - dims[0] = astate->nelems; - lbs[0] = 1; - - result = construct_md_array(astate->dvalues, - 1, - dims, - lbs, - astate->element_type, - astate->typlen, - astate->typbyval, - astate->typalign); - - MemoryContextSwitchTo(oldcontext); - - /* Clean up all the junk */ - MemoryContextDelete(astate->mcontext); - - return PointerGetDatum(result); } --- 1060,1063 ---- Index: src/backend/nodes/copyfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/nodes/copyfuncs.c,v retrieving revision 1.251 diff -c -r1.251 copyfuncs.c *** src/backend/nodes/copyfuncs.c 28 May 2003 16:03:56 -0000 1.251 --- src/backend/nodes/copyfuncs.c 29 May 2003 04:30:53 -0000 *************** *** 727,732 **** --- 727,733 ---- COPY_NODE_FIELD(target); COPY_SCALAR_FIELD(aggstar); COPY_SCALAR_FIELD(aggdistinct); + COPY_NODE_FIELD(args); return newnode; } Index: src/backend/nodes/equalfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/nodes/equalfuncs.c,v retrieving revision 1.194 diff -c -r1.194 equalfuncs.c *** src/backend/nodes/equalfuncs.c 28 May 2003 16:03:56 -0000 1.194 --- src/backend/nodes/equalfuncs.c 29 May 2003 04:30:53 -0000 *************** *** 204,209 **** --- 204,210 ---- COMPARE_NODE_FIELD(target); COMPARE_SCALAR_FIELD(aggstar); COMPARE_SCALAR_FIELD(aggdistinct); + COMPARE_NODE_FIELD(args); return true; } Index: src/backend/nodes/outfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/nodes/outfuncs.c,v retrieving revision 1.206 diff -c -r1.206 outfuncs.c *** src/backend/nodes/outfuncs.c 28 May 2003 16:03:56 -0000 1.206 --- src/backend/nodes/outfuncs.c 29 May 2003 04:30:53 -0000 *************** *** 615,620 **** --- 615,621 ---- WRITE_NODE_FIELD(target); WRITE_BOOL_FIELD(aggstar); WRITE_BOOL_FIELD(aggdistinct); + WRITE_NODE_FIELD(args); } static void Index: src/backend/nodes/readfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/nodes/readfuncs.c,v retrieving revision 1.153 diff -c -r1.153 readfuncs.c *** src/backend/nodes/readfuncs.c 6 May 2003 00:20:32 -0000 1.153 --- src/backend/nodes/readfuncs.c 29 May 2003 04:30:53 -0000 *************** *** 415,420 **** --- 415,421 ---- READ_NODE_FIELD(target); READ_BOOL_FIELD(aggstar); READ_BOOL_FIELD(aggdistinct); + READ_NODE_FIELD(args); READ_DONE(); } Index: src/backend/optimizer/util/clauses.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/optimizer/util/clauses.c,v retrieving revision 1.138 diff -c -r1.138 clauses.c *** src/backend/optimizer/util/clauses.c 28 May 2003 22:32:49 -0000 1.138 --- src/backend/optimizer/util/clauses.c 29 May 2003 04:30:53 -0000 *************** *** 133,138 **** --- 133,160 ---- } /***************************************************************************** + * FUNCTION clause functions + *****************************************************************************/ + + /* + * make_funcclause + * Creates a function clause given its function info and argument list. + */ + Expr * + make_funcclause(Oid funcid, Oid funcresulttype, bool funcretset, + CoercionForm funcformat, List *funcargs) + { + FuncExpr *expr = makeNode(FuncExpr); + + expr->funcid = funcid; + expr->funcresulttype = funcresulttype; + expr->funcretset = funcretset; + expr->funcformat = funcformat; + expr->args = funcargs; + return (Expr *) expr; + } + + /***************************************************************************** * NOT clause functions *****************************************************************************/ Index: src/backend/parser/parse_coerce.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/parser/parse_coerce.c,v retrieving revision 2.97 diff -c -r2.97 parse_coerce.c *** src/backend/parser/parse_coerce.c 26 May 2003 00:11:27 -0000 2.97 --- src/backend/parser/parse_coerce.c 29 May 2003 04:30:53 -0000 *************** *** 859,865 **** /* Get the element type based on the array type, if we have one */ if (OidIsValid(array_typeid)) { ! array_typelem = get_element_type(array_typeid); if (!OidIsValid(array_typelem)) elog(ERROR, "Argument declared ANYARRAY is not an array: %s", format_type_be(array_typeid)); --- 859,869 ---- /* Get the element type based on the array type, if we have one */ if (OidIsValid(array_typeid)) { ! if (array_typeid != ANYARRAYOID) ! array_typelem = get_element_type(array_typeid); ! else ! array_typelem = ANYELEMENTOID; ! if (!OidIsValid(array_typelem)) elog(ERROR, "Argument declared ANYARRAY is not an array: %s", format_type_be(array_typeid)); *************** *** 919,925 **** { if (!OidIsValid(array_typeid)) { ! array_typeid = get_array_type(elem_typeid); if (!OidIsValid(array_typeid)) elog(ERROR, "Cannot find array type for datatype %s", format_type_be(elem_typeid)); --- 923,933 ---- { if (!OidIsValid(array_typeid)) { ! if (elem_typeid != ANYELEMENTOID) ! array_typeid = get_array_type(elem_typeid); ! else ! array_typeid = ANYARRAYOID; ! if (!OidIsValid(array_typeid)) elog(ERROR, "Cannot find array type for datatype %s", format_type_be(elem_typeid)); Index: src/backend/parser/parse_func.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/parser/parse_func.c,v retrieving revision 1.148 diff -c -r1.148 parse_func.c *** src/backend/parser/parse_func.c 26 May 2003 00:11:27 -0000 1.148 --- src/backend/parser/parse_func.c 29 May 2003 04:30:53 -0000 *************** *** 335,340 **** --- 335,341 ---- aggref->target = lfirst(fargs); aggref->aggstar = agg_star; aggref->aggdistinct = agg_distinct; + aggref->args = fargs; retval = (Node *) aggref; Index: src/backend/utils/adt/array_userfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/adt/array_userfuncs.c,v retrieving revision 1.1 diff -c -r1.1 array_userfuncs.c *** src/backend/utils/adt/array_userfuncs.c 8 Apr 2003 23:20:02 -0000 1.1 --- src/backend/utils/adt/array_userfuncs.c 29 May 2003 04:51:01 -0000 *************** *** 42,48 **** else ndims = 1; ! PG_RETURN_ARRAYTYPE_P(create_singleton_array(elem_type, PG_GETARG_DATUM(0), ndims)); } --- 42,48 ---- else ndims = 1; ! PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, elem_type, PG_GETARG_DATUM(0), ndims)); } *************** *** 70,75 **** --- 70,76 ---- Oid arg1_typeid = get_fn_expr_argtype(fcinfo, 1); Oid arg0_elemid; Oid arg1_elemid; + ArrayMetaState *my_extra; if (arg0_typeid == InvalidOid || arg1_typeid == InvalidOid) elog(ERROR, "array_push: cannot determine input data types"); *************** *** 95,122 **** PG_RETURN_NULL(); /* keep compiler quiet */ } ! /* Sanity check: do we have a one-dimensional array */ ! if (ARR_NDIM(v) != 1) ! elog(ERROR, "Arrays greater than one-dimension are not supported"); ! ! lb = ARR_LBOUND(v); ! dimv = ARR_DIMS(v); ! if (arg0_elemid != InvalidOid) { ! /* append newelem */ ! int ub = dimv[0] + lb[0] - 1; ! indx = ub + 1; } else { ! /* prepend newelem */ ! indx = lb[0] - 1; } ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); ! result = array_set(v, 1, &indx, newelem, -1, ! typlen, typbyval, typalign, &isNull); PG_RETURN_ARRAYTYPE_P(result); } --- 96,156 ---- PG_RETURN_NULL(); /* keep compiler quiet */ } ! if (ARR_NDIM(v) == 1) { ! lb = ARR_LBOUND(v); ! dimv = ARR_DIMS(v); ! ! if (arg0_elemid != InvalidOid) ! { ! /* append newelem */ ! int ub = dimv[0] + lb[0] - 1; ! indx = ub + 1; ! } ! else ! { ! /* prepend newelem */ ! indx = lb[0] - 1; ! } } + else if (ARR_NDIM(v) == 0) + indx = 1; else + elog(ERROR, "only empty and one-dimensional arrays are supported"); + + + /* + * We arrange to look up info about element type only once per series + * of calls, assuming the element type doesn't change underneath us. + */ + my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; + if (my_extra == NULL) { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; } ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type */ ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typalign = typalign; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typalign = my_extra->typalign; ! } ! ! result = array_set(v, 1, &indx, newelem, -1, typlen, typbyval, ! typalign, &isNull); PG_RETURN_ARRAYTYPE_P(result); } *************** *** 145,157 **** /* * We must have one of the following combinations of inputs: ! * 1) two arrays with ndims1 == ndims2 ! * 2) ndims1 == ndims2 - 1 ! * 3) ndims1 == ndims2 + 1 */ ndims1 = ARR_NDIM(v1); ndims2 = ARR_NDIM(v2); if (ndims1 != ndims2 && ndims1 != ndims2 - 1 && ndims1 != ndims2 + 1) elog(ERROR, "Cannot concatenate incompatible arrays of %d and " "%d dimensions", ndims1, ndims2); --- 179,206 ---- /* * We must have one of the following combinations of inputs: ! * 1) one empty array, and one non-empty array ! * 2) both arrays empty ! * 3) two arrays with ndims1 == ndims2 ! * 4) ndims1 == ndims2 - 1 ! * 5) ndims1 == ndims2 + 1 */ ndims1 = ARR_NDIM(v1); ndims2 = ARR_NDIM(v2); + /* + * short circuit - if one input array is empty, and the other is not, + * we return the non-empty one as the result + * + * if both are empty, return the first one + */ + if (ndims1 == 0 && ndims2 > 0) + PG_RETURN_ARRAYTYPE_P(v2); + + if (ndims2 == 0) + PG_RETURN_ARRAYTYPE_P(v1); + + /* the rest fall into combo 2, 3, or 4 */ if (ndims1 != ndims2 && ndims1 != ndims2 - 1 && ndims1 != ndims2 + 1) elog(ERROR, "Cannot concatenate incompatible arrays of %d and " "%d dimensions", ndims1, ndims2); *************** *** 266,314 **** PG_RETURN_ARRAYTYPE_P(result); } - /*---------------------------------------------------------------------------- - * array_accum : - * accumulator to build a 1-D array from input values -- this can be used - * to create custom aggregates. - * - * This function is not marked strict, so we have to be careful about nulls. - *---------------------------------------------------------------------------- - */ - Datum - array_accum(PG_FUNCTION_ARGS) - { - /* return NULL if both arguments are NULL */ - if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) - PG_RETURN_NULL(); - - /* create a new 1-D array from the new element if the array is NULL */ - if (PG_ARGISNULL(0)) - { - Oid tgt_type = get_fn_expr_rettype(fcinfo); - Oid tgt_elem_type; - - if (tgt_type == InvalidOid) - elog(ERROR, "Cannot determine target array type"); - tgt_elem_type = get_element_type(tgt_type); - if (tgt_elem_type == InvalidOid) - elog(ERROR, "Target type is not an array"); - - PG_RETURN_ARRAYTYPE_P(create_singleton_array(tgt_elem_type, - PG_GETARG_DATUM(1), - 1)); - } - - /* return the array if the new element is NULL */ - if (PG_ARGISNULL(1)) - PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P_COPY(0)); - - /* - * Otherwise this is equivalent to array_push. We hack the call a little - * so that array_push can see the fn_expr information. - */ - return array_push(fcinfo); - } - /*----------------------------------------------------------------------------- * array_assign : * assign an element of an array to a new value and return the --- 315,320 ---- *************** *** 329,334 **** --- 335,341 ---- int16 typlen; bool typbyval; char typalign; + ArrayMetaState *my_extra; v = PG_GETARG_ARRAYTYPE_P(0); idx_to_chg = PG_GETARG_INT32(1); *************** *** 349,355 **** if (element_type == 0) elog(ERROR, "Invalid array element type: %u", element_type); ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); result = array_set(v, 1, &idx_to_chg, newelem, -1, typlen, typbyval, typalign, &isNull); --- 356,390 ---- if (element_type == 0) elog(ERROR, "Invalid array element type: %u", element_type); ! /* ! * We arrange to look up info about element type only once per series ! * of calls, assuming the element type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type */ ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typalign = typalign; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typalign = my_extra->typalign; ! } result = array_set(v, 1, &idx_to_chg, newelem, -1, typlen, typbyval, typalign, &isNull); *************** *** 375,380 **** --- 410,416 ---- int16 typlen; bool typbyval; char typalign; + ArrayMetaState *my_extra; v = PG_GETARG_ARRAYTYPE_P(0); idx = PG_GETARG_INT32(1); *************** *** 394,400 **** if (element_type == 0) elog(ERROR, "Invalid array element type: %u", element_type); ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); result = array_ref(v, 1, &idx, -1, typlen, typbyval, typalign, &isNull); --- 430,464 ---- if (element_type == 0) elog(ERROR, "Invalid array element type: %u", element_type); ! /* ! * We arrange to look up info about element type only once per series ! * of calls, assuming the element type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type */ ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typalign = typalign; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typalign = my_extra->typalign; ! } result = array_ref(v, 1, &idx, -1, typlen, typbyval, typalign, &isNull); *************** *** 402,412 **** } /* ! * actually does the work for singleton_array(), and array_accum() if it is ! * given a null input array. */ ArrayType * ! create_singleton_array(Oid element_type, Datum element, int ndims) { Datum dvalues[1]; int16 typlen; --- 466,478 ---- } /* ! * actually does the work for singleton_array() */ ArrayType * ! create_singleton_array(FunctionCallInfo fcinfo, ! Oid element_type, ! Datum element, ! int ndims) { Datum dvalues[1]; int16 typlen; *************** *** 415,420 **** --- 481,487 ---- int dims[MAXDIM]; int lbs[MAXDIM]; int i; + ArrayMetaState *my_extra; if (element_type == 0) elog(ERROR, "Invalid array element type: %u", element_type); *************** *** 429,435 **** lbs[i] = 1; } ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); return construct_md_array(dvalues, ndims, dims, lbs, element_type, typlen, typbyval, typalign); --- 496,530 ---- lbs[i] = 1; } ! /* ! * We arrange to look up info about element type only once per series ! * of calls, assuming the element type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type */ ! get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typalign = typalign; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typalign = my_extra->typalign; ! } return construct_md_array(dvalues, ndims, dims, lbs, element_type, typlen, typbyval, typalign); Index: src/backend/utils/adt/arrayfuncs.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/adt/arrayfuncs.c,v retrieving revision 1.89 diff -c -r1.89 arrayfuncs.c *** src/backend/utils/adt/arrayfuncs.c 9 May 2003 23:01:45 -0000 1.89 --- src/backend/utils/adt/arrayfuncs.c 29 May 2003 04:55:31 -0000 *************** *** 21,28 **** --- 21,30 ---- #include "catalog/pg_type.h" #include "libpq/pqformat.h" #include "parser/parse_coerce.h" + #include "parser/parse_oper.h" #include "utils/array.h" #include "utils/builtins.h" + #include "utils/datum.h" #include "utils/memutils.h" #include "utils/lsyscache.h" #include "utils/syscache.h" *************** *** 70,85 **** #define RETURN_NULL(type) do { *isNull = true; return (type) 0; } while (0) - /* I/O function selector for system_cache_lookup */ - typedef enum IOFuncSelector - { - IOFunc_input, - IOFunc_output, - IOFunc_receive, - IOFunc_send - } IOFuncSelector; - - static int ArrayCount(char *str, int *dim, char typdelim); static Datum *ReadArrayStr(char *arrayStr, int nitems, int ndim, int *dim, FmgrInfo *inputproc, Oid typelem, int32 typmod, --- 72,77 ---- *************** *** 93,102 **** static void CopyArrayEls(char *p, Datum *values, int nitems, int typlen, bool typbyval, char typalign, bool freedata); - static void system_cache_lookup(Oid element_type, IOFuncSelector which_func, - int *typlen, bool *typbyval, - char *typdelim, Oid *typelem, - Oid *proc, char *typalign); static Datum ArrayCast(char *value, bool byval, int len); static int ArrayCastAndSet(Datum src, int typlen, bool typbyval, char typalign, --- 85,90 ---- *************** *** 120,126 **** int *st, int *endp, char *srcPtr, int typlen, bool typbyval, char typalign); - /*--------------------------------------------------------------------- * array_in : * converts an array from the external format in "string" to --- 108,113 ---- *************** *** 154,165 **** dim[MAXDIM], lBound[MAXDIM]; char typalign; ! /* Get info about element type, including its input conversion proc */ ! system_cache_lookup(element_type, IOFunc_input, ! &typlen, &typbyval, &typdelim, ! &typelem, &typinput, &typalign); ! fmgr_info(typinput, &inputproc); /* Make a modifiable copy of the input */ /* XXX why are we allocating an extra 2 bytes here? */ --- 141,189 ---- dim[MAXDIM], lBound[MAXDIM]; char typalign; + ArrayMetaState *my_extra; ! /* ! * We arrange to look up info about element type, including its input ! * conversion proc only once per series of calls, assuming the element ! * type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type, including its input conversion proc */ ! get_type_metadata(element_type, IOFunc_input, ! &typlen, &typbyval, &typdelim, ! &typelem, &typinput, &typalign); ! fmgr_info(typinput, &inputproc); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typdelim = typdelim; ! my_extra->typelem = typelem; ! my_extra->typiofunc = typinput; ! my_extra->typalign = typalign; ! my_extra->proc = inputproc; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typdelim = my_extra->typdelim; ! typelem = my_extra->typelem; ! typinput = my_extra->typiofunc; ! typalign = my_extra->typalign; ! inputproc = my_extra->proc; ! } /* Make a modifiable copy of the input */ /* XXX why are we allocating an extra 2 bytes here? */ *************** *** 636,647 **** indx[MAXDIM]; int ndim, *dim; element_type = ARR_ELEMTYPE(v); ! system_cache_lookup(element_type, IOFunc_output, ! &typlen, &typbyval, &typdelim, ! &typelem, &typoutput, &typalign); ! fmgr_info(typoutput, &outputproc); ndim = ARR_NDIM(v); dim = ARR_DIMS(v); --- 660,710 ---- indx[MAXDIM]; int ndim, *dim; + ArrayMetaState *my_extra; element_type = ARR_ELEMTYPE(v); ! ! /* ! * We arrange to look up info about element type, including its input ! * conversion proc only once per series of calls, assuming the element ! * type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type, including its output conversion proc */ ! get_type_metadata(element_type, IOFunc_output, ! &typlen, &typbyval, &typdelim, ! &typelem, &typoutput, &typalign); ! fmgr_info(typoutput, &outputproc); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typdelim = typdelim; ! my_extra->typelem = typelem; ! my_extra->typiofunc = typoutput; ! my_extra->typalign = typalign; ! my_extra->proc = outputproc; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typdelim = my_extra->typdelim; ! typelem = my_extra->typelem; ! typoutput = my_extra->typiofunc; ! typalign = my_extra->typalign; ! outputproc = my_extra->proc; ! } ndim = ARR_NDIM(v); dim = ARR_DIMS(v); *************** *** 800,805 **** --- 863,869 ---- dim[MAXDIM], lBound[MAXDIM]; char typalign; + ArrayMetaState *my_extra; /* Get the array header information */ ndim = pq_getmsgint(buf, 4); *************** *** 831,844 **** PG_RETURN_ARRAYTYPE_P(retval); } ! /* Get info about element type, including its receive conversion proc */ ! system_cache_lookup(element_type, IOFunc_receive, ! &typlen, &typbyval, &typdelim, ! &typelem, &typreceive, &typalign); ! if (!OidIsValid(typreceive)) ! elog(ERROR, "No binary input function available for type %s", ! format_type_be(element_type)); ! fmgr_info(typreceive, &receiveproc); dataPtr = ReadArrayBinary(buf, nitems, &receiveproc, typelem, typlen, typbyval, typalign, --- 895,944 ---- PG_RETURN_ARRAYTYPE_P(retval); } ! /* ! * We arrange to look up info about element type, including its receive ! * conversion proc only once per series of calls, assuming the element ! * type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type, including its receive conversion proc */ ! get_type_metadata(element_type, IOFunc_receive, ! &typlen, &typbyval, &typdelim, ! &typelem, &typreceive, &typalign); ! if (!OidIsValid(typreceive)) ! elog(ERROR, "No binary input function available for type %s", ! format_type_be(element_type)); ! fmgr_info(typreceive, &receiveproc); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typdelim = typdelim; ! my_extra->typelem = typelem; ! my_extra->typiofunc = typreceive; ! my_extra->typalign = typalign; ! my_extra->proc = receiveproc; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typdelim = my_extra->typdelim; ! typelem = my_extra->typelem; ! typreceive = my_extra->typiofunc; ! typalign = my_extra->typalign; ! receiveproc = my_extra->proc; ! } dataPtr = ReadArrayBinary(buf, nitems, &receiveproc, typelem, typlen, typbyval, typalign, *************** *** 976,990 **** int ndim, *dim; StringInfoData buf; /* Get information about the element type and the array dimensions */ element_type = ARR_ELEMTYPE(v); ! system_cache_lookup(element_type, IOFunc_send, &typlen, &typbyval, ! &typdelim, &typelem, &typsend, &typalign); ! if (!OidIsValid(typsend)) ! elog(ERROR, "No binary output function available for type %s", ! format_type_be(element_type)); ! fmgr_info(typsend, &sendproc); ndim = ARR_NDIM(v); dim = ARR_DIMS(v); --- 1076,1129 ---- int ndim, *dim; StringInfoData buf; + ArrayMetaState *my_extra; /* Get information about the element type and the array dimensions */ element_type = ARR_ELEMTYPE(v); ! ! /* ! * We arrange to look up info about element type, including its send ! * proc only once per series of calls, assuming the element ! * type doesn't change underneath us. ! */ ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; ! my_extra->element_type = InvalidOid; ! } ! ! if (my_extra->element_type != element_type) ! { ! /* Get info about element type, including its send proc */ ! get_type_metadata(element_type, IOFunc_send, &typlen, &typbyval, ! &typdelim, &typelem, &typsend, &typalign); ! if (!OidIsValid(typsend)) ! elog(ERROR, "No binary output function available for type %s", ! format_type_be(element_type)); ! fmgr_info(typsend, &sendproc); ! ! my_extra->element_type = element_type; ! my_extra->typlen = typlen; ! my_extra->typbyval = typbyval; ! my_extra->typdelim = typdelim; ! my_extra->typelem = typelem; ! my_extra->typiofunc = typsend; ! my_extra->typalign = typalign; ! my_extra->proc = sendproc; ! } ! else ! { ! typlen = my_extra->typlen; ! typbyval = my_extra->typbyval; ! typdelim = my_extra->typdelim; ! typelem = my_extra->typelem; ! typsend = my_extra->typiofunc; ! typalign = my_extra->typalign; ! sendproc = my_extra->proc; ! } ndim = ARR_NDIM(v); dim = ARR_DIMS(v); *************** *** 1476,1481 **** --- 1615,1640 ---- array = DatumGetArrayTypeP(PointerGetDatum(array)); ndim = ARR_NDIM(array); + + /* + * if number of dims is zero, i.e. an empty array, create an array + * with nSubscripts dimensions, and set the lower bounds to the supplied + * subscripts + */ + if (ndim == 0) + { + Oid elmtype = ARR_ELEMTYPE(array); + + for (i = 0; i < nSubscripts; i++) + { + dim[i] = 1; + lb[i] = indx[i]; + } + + return construct_md_array(&dataValue, nSubscripts, dim, lb, elmtype, + elmlen, elmbyval, elmalign); + } + if (ndim != nSubscripts || ndim <= 0 || ndim > MAXDIM) elog(ERROR, "Invalid array subscripts"); *************** *** 1632,1637 **** --- 1791,1821 ---- /* note: we assume srcArray contains no toasted elements */ ndim = ARR_NDIM(array); + + /* + * if number of dims is zero, i.e. an empty array, create an array + * with nSubscripts dimensions, and set the upper and lower bounds + * to the supplied subscripts + */ + if (ndim == 0) + { + Datum *dvalues; + int nelems; + Oid elmtype = ARR_ELEMTYPE(array); + + deconstruct_array(srcArray, elmtype, elmlen, elmbyval, elmalign, + &dvalues, &nelems); + + for (i = 0; i < nSubscripts; i++) + { + dim[i] = 1 + upperIndx[i] - lowerIndx[i]; + lb[i] = lowerIndx[i]; + } + + return construct_md_array(dvalues, nSubscripts, dim, lb, elmtype, + elmlen, elmbyval, elmalign); + } + if (ndim < nSubscripts || ndim <= 0 || ndim > MAXDIM) elog(ERROR, "Invalid array subscripts"); *************** *** 1811,1816 **** --- 1995,2007 ---- Oid typelem; Oid proc; char *s; + typedef struct { + ArrayMetaState *inp_extra; + ArrayMetaState *ret_extra; + } am_extra; + am_extra *my_extra; + ArrayMetaState *inp_extra; + ArrayMetaState *ret_extra; /* Get input array */ if (fcinfo->nargs < 1) *************** *** 1829,1839 **** if (nitems <= 0) PG_RETURN_ARRAYTYPE_P(v); ! /* Lookup source and result types. Unneeded variables are reused. */ ! system_cache_lookup(inpType, IOFunc_input, &inp_typlen, &inp_typbyval, ! &typdelim, &typelem, &proc, &inp_typalign); ! system_cache_lookup(retType, IOFunc_input, &typlen, &typbyval, ! &typdelim, &typelem, &proc, &typalign); /* Allocate temporary array for new values */ values = (Datum *) palloc(nitems * sizeof(Datum)); --- 2020,2100 ---- if (nitems <= 0) PG_RETURN_ARRAYTYPE_P(v); ! /* ! * We arrange to look up info about input and return element types only ! * once per series of calls, assuming the element type doesn't change ! * underneath us. ! */ ! my_extra = (am_extra *) fcinfo->flinfo->fn_extra; ! if (my_extra == NULL) ! { ! fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(am_extra)); ! my_extra = (am_extra *) fcinfo->flinfo->fn_extra; ! ! my_extra->inp_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! inp_extra = my_extra->inp_extra; ! inp_extra->element_type = InvalidOid; ! ! my_extra->ret_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, ! sizeof(ArrayMetaState)); ! ret_extra = my_extra->ret_extra; ! ret_extra->element_type = InvalidOid; ! } ! else ! { ! inp_extra = my_extra->inp_extra; ! ret_extra = my_extra->ret_extra; ! } ! ! if (inp_extra->element_type != inpType) ! { ! /* Lookup source and result types. Unneeded variables are reused. */ ! get_type_metadata(inpType, IOFunc_input, &inp_typlen, &inp_typbyval, ! &typdelim, &typelem, &proc, &inp_typalign); ! ! inp_extra->element_type = inpType; ! inp_extra->typlen = inp_typlen; ! inp_extra->typbyval = inp_typbyval; ! inp_extra->typdelim = typdelim; ! inp_extra->typelem = typelem; ! inp_extra->typiofunc = proc; ! inp_extra->typalign = inp_typalign; ! } ! else ! { ! inp_typlen = inp_extra->typlen; ! inp_typbyval = inp_extra->typbyval; ! typdelim = inp_extra->typdelim; ! typelem = inp_extra->typelem; ! proc = inp_extra->typiofunc; ! inp_typalign = inp_extra->typalign; ! } ! ! if (ret_extra->element_type != retType) ! { ! /* Lookup source and result types. Unneeded variables are reused. */ ! get_type_metadata(retType, IOFunc_input, &typlen, &typbyval, ! &typdelim, &typelem, &proc, &typalign); ! ! ret_extra->element_type = retType; ! ret_extra->typlen = typlen; ! ret_extra->typbyval = typbyval; ! ret_extra->typdelim = typdelim; ! ret_extra->typelem = typelem; ! ret_extra->typiofunc = proc; ! ret_extra->typalign = typalign; ! } ! else ! { ! typlen = ret_extra->typlen; ! typbyval = ret_extra->typbyval; ! typdelim = ret_extra->typdelim; ! typelem = ret_extra->typelem; ! proc = ret_extra->typiofunc; ! typalign = ret_extra->typalign; ! } /* Allocate temporary array for new values */ values = (Datum *) palloc(nitems * sizeof(Datum)); *************** *** 2049,2056 **** * compares two arrays for equality * result : * returns true if the arrays are equal, false otherwise. - * - * XXX bitwise equality is pretty bogus ... *----------------------------------------------------------------------------- */ Datum --- 2310,2315 ---- *************** *** 2058,2069 **** { ArrayType *array1 = PG_GETARG_ARRAYTYPE_P(0); ArrayType *array2 = PG_GETARG_ARRAYTYPE_P(1); bool result = true; ! if (ARR_SIZE(array1) != ARR_SIZE(array2)) ! result = false; ! else if (memcmp(array1, array2, ARR_SIZE(array1)) != 0) result = false; /* Avoid leaking memory when handed toasted input. */ PG_FREE_IF_COPY(array1, 0); --- 2317,2434 ---- { ArrayType *array1 = PG_GETARG_ARRAYTYPE_P(0); ArrayType *array2 = PG_GETARG_ARRAYTYPE_P(1); + char *p1 = (char *) ARR_DATA_PTR(array1); + char *p2 = (char *) ARR_DATA_PTR(array2); + int ndims1 = ARR_NDIM(array1); + int ndims2 = ARR_NDIM(array2); + int *dims1 = ARR_DIMS(array1); + int *dims2 = ARR_DIMS(array2); + int nitems1 = ArrayGetNItems(ndims1, dims1); + int nitems2 = ArrayGetNItems(ndims2, dims2); + Oid element_type = ARR_ELEMTYPE(array1); + FmgrInfo *ae_fmgr_info = fcinfo->flinfo; bool result = true; + int typlen; + bool typbyval; + char typdelim; + Oid typelem; + char typalign; + Oid typiofunc; + int i; + ArrayMetaState *my_extra; + FunctionCallInfoData locfcinfo; ! /* fast path if the arrays do not have the same number of elements */ ! if (nitems1 != nitems2) result = false; + else + { + /* + * We arrange to look up the equality function only once per series of + * calls, assuming the element type doesn't change underneath us. + */ + my_extra = (ArrayMetaState *) ae_fmgr_info->fn_extra; + if (my_extra == NULL) + { + ae_fmgr_info->fn_extra = MemoryContextAlloc(ae_fmgr_info->fn_mcxt, + sizeof(ArrayMetaState)); + my_extra = (ArrayMetaState *) ae_fmgr_info->fn_extra; + my_extra->element_type = InvalidOid; + } + + if (my_extra->element_type != element_type) + { + Oid opfuncid = equality_oper_funcid(element_type); + + if (OidIsValid(opfuncid)) + fmgr_info_cxt(opfuncid, &my_extra->proc, ae_fmgr_info->fn_mcxt); + else + elog(ERROR, + "array_eq: cannot find equality operator for type: %u", + element_type); + + get_type_metadata(element_type, IOFunc_output, + &typlen, &typbyval, &typdelim, + &typelem, &typiofunc, &typalign); + + my_extra->element_type = element_type; + my_extra->typlen = typlen; + my_extra->typbyval = typbyval; + my_extra->typdelim = typdelim; + my_extra->typelem = typelem; + my_extra->typiofunc = typiofunc; + my_extra->typalign = typalign; + } + else + { + typlen = my_extra->typlen; + typbyval = my_extra->typbyval; + typdelim = my_extra->typdelim; + typelem = my_extra->typelem; + typiofunc = my_extra->typiofunc; + typalign = my_extra->typalign; + } + + /* + * apply the operator to each pair of array elements. + */ + MemSet(&locfcinfo, 0, sizeof(locfcinfo)); + locfcinfo.flinfo = &my_extra->proc; + locfcinfo.nargs = 2; + + /* Loop over source data */ + for (i = 0; i < nitems1; i++) + { + Datum elt1; + Datum elt2; + bool oprresult; + + /* Get element pair */ + elt1 = fetch_att(p1, typbyval, typlen); + elt2 = fetch_att(p2, typbyval, typlen); + + p1 = att_addlength(p1, typlen, PointerGetDatum(p1)); + p1 = (char *) att_align(p1, typalign); + + p2 = att_addlength(p2, typlen, PointerGetDatum(p2)); + p2 = (char *) att_align(p2, typalign); + + /* + * Apply the operator to the element pair + */ + locfcinfo.arg[0] = elt1; + locfcinfo.arg[1] = elt2; + locfcinfo.argnull[0] = false; + locfcinfo.argnull[1] = false; + locfcinfo.isnull = false; + oprresult = DatumGetBool(FunctionCallInvoke(&locfcinfo)); + if (!oprresult) + { + result = false; + break; + } + } + } /* Avoid leaking memory when handed toasted input. */ PG_FREE_IF_COPY(array1, 0); *************** *** 2077,2125 **** /******************| Support Routines |*****************/ /***************************************************************************/ - static void - system_cache_lookup(Oid element_type, - IOFuncSelector which_func, - int *typlen, - bool *typbyval, - char *typdelim, - Oid *typelem, - Oid *proc, - char *typalign) - { - HeapTuple typeTuple; - Form_pg_type typeStruct; - - typeTuple = SearchSysCache(TYPEOID, - ObjectIdGetDatum(element_type), - 0, 0, 0); - if (!HeapTupleIsValid(typeTuple)) - elog(ERROR, "cache lookup failed for type %u", element_type); - typeStruct = (Form_pg_type) GETSTRUCT(typeTuple); - - *typlen = typeStruct->typlen; - *typbyval = typeStruct->typbyval; - *typdelim = typeStruct->typdelim; - *typelem = typeStruct->typelem; - *typalign = typeStruct->typalign; - switch (which_func) - { - case IOFunc_input: - *proc = typeStruct->typinput; - break; - case IOFunc_output: - *proc = typeStruct->typoutput; - break; - case IOFunc_receive: - *proc = typeStruct->typreceive; - break; - case IOFunc_send: - *proc = typeStruct->typsend; - break; - } - ReleaseSysCache(typeTuple); - } - /* * Fetch array element at pointer, converted correctly to a Datum */ --- 2442,2447 ---- *************** *** 2423,2428 **** --- 2745,2762 ---- if (tgt_elem_type == InvalidOid) elog(ERROR, "Target type is not an array"); + /* + * We don't deal with domain constraints yet, so bail out. + * This isn't currently a problem, because we also don't + * support arrays of domain type elements either. But in the + * future we might. At that point consideration should be given + * to removing the check below and adding a domain constraints + * check to the coercion. + */ + if (getBaseType(tgt_elem_type) != tgt_elem_type) + elog(ERROR, "array coercion to domain type elements not " \ + "currently supported"); + if (!find_coercion_pathway(tgt_elem_type, src_elem_type, COERCION_EXPLICIT, &funcId)) { *************** *** 2439,2448 **** } /* ! * If it's binary-compatible, return the array unmodified. */ if (my_extra->coerce_finfo.fn_oid == InvalidOid) ! PG_RETURN_ARRAYTYPE_P(src); /* * Use array_map to apply the function to each array element. --- 2773,2788 ---- } /* ! * If it's binary-compatible, modify the element type in the array header, ! * but otherwise leave the array as we received it. */ if (my_extra->coerce_finfo.fn_oid == InvalidOid) ! { ! ArrayType *result = DatumGetArrayTypePCopy(PG_GETARG_DATUM(0)); ! ! ARR_ELEMTYPE(result) = my_extra->desttype; ! PG_RETURN_ARRAYTYPE_P(result); ! } /* * Use array_map to apply the function to each array element. *************** *** 2453,2456 **** --- 2793,2911 ---- locfcinfo.arg[0] = PointerGetDatum(src); return array_map(&locfcinfo, my_extra->srctype, my_extra->desttype); + } + + /* + * accumArrayResult - accumulate one (more) Datum for an ARRAY_SUBLINK + * + * astate is working state (NULL on first call) + * rcontext is where to keep working state + */ + ArrayBuildState * + accumArrayResult(ArrayBuildState *astate, + Datum dvalue, bool disnull, + Oid element_type, + MemoryContext rcontext) + { + MemoryContext arr_context, + oldcontext; + + if (astate == NULL) + { + /* First time through --- initialize */ + + /* Make a temporary context to hold all the junk */ + arr_context = AllocSetContextCreate(rcontext, + "accumArrayResult", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + oldcontext = MemoryContextSwitchTo(arr_context); + astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState)); + astate->mcontext = arr_context; + astate->dvalues = (Datum *) + palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(Datum)); + astate->nelems = 0; + astate->element_type = element_type; + get_typlenbyvalalign(element_type, + &astate->typlen, + &astate->typbyval, + &astate->typalign); + } + else + { + oldcontext = MemoryContextSwitchTo(astate->mcontext); + Assert(astate->element_type == element_type); + /* enlarge dvalues[] if needed */ + if ((astate->nelems % ARRAY_ELEMS_CHUNKSIZE) == 0) + astate->dvalues = (Datum *) + repalloc(astate->dvalues, + (astate->nelems + ARRAY_ELEMS_CHUNKSIZE) * sizeof(Datum)); + } + + if (disnull) + elog(ERROR, "NULL elements not allowed in Arrays"); + + /* Use datumCopy to ensure pass-by-ref stuff is copied into mcontext */ + astate->dvalues[astate->nelems++] = + datumCopy(dvalue, astate->typbyval, astate->typlen); + + MemoryContextSwitchTo(oldcontext); + + return astate; + } + + /* + * makeArrayResult - produce final result of accumArrayResult + * + * astate is working state (not NULL) + * rcontext is where to construct result + */ + Datum + makeArrayResult(ArrayBuildState *astate, + MemoryContext rcontext) + { + int dims[1]; + int lbs[1]; + + dims[0] = astate->nelems; + lbs[0] = 1; + + return makeMdArrayResult(astate, 1, dims, lbs, rcontext); + } + + /* + * makeMdArrayResult - produce md final result of accumArrayResult + * + * astate is working state (not NULL) + * rcontext is where to construct result + */ + Datum + makeMdArrayResult(ArrayBuildState *astate, + int ndims, + int *dims, + int *lbs, + MemoryContext rcontext) + { + ArrayType *result; + MemoryContext oldcontext; + + /* Build the final array result in rcontext */ + oldcontext = MemoryContextSwitchTo(rcontext); + + result = construct_md_array(astate->dvalues, + ndims, + dims, + lbs, + astate->element_type, + astate->typlen, + astate->typbyval, + astate->typalign); + + MemoryContextSwitchTo(oldcontext); + + /* Clean up all the junk */ + MemoryContextDelete(astate->mcontext); + + return PointerGetDatum(result); } Index: src/backend/utils/adt/varlena.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/adt/varlena.c,v retrieving revision 1.98 diff -c -r1.98 varlena.c *** src/backend/utils/adt/varlena.c 15 May 2003 15:50:19 -0000 1.98 --- src/backend/utils/adt/varlena.c 29 May 2003 04:30:53 -0000 *************** *** 19,29 **** --- 19,32 ---- #include "mb/pg_wchar.h" #include "miscadmin.h" #include "access/tuptoaster.h" + #include "catalog/pg_type.h" #include "lib/stringinfo.h" #include "libpq/crypt.h" #include "libpq/pqformat.h" + #include "utils/array.h" #include "utils/builtins.h" #include "utils/pg_locale.h" + #include "utils/lsyscache.h" typedef struct varlena unknown; *************** *** 1983,1990 **** if (fldnum == 1) /* first field - just return the input * string */ PG_RETURN_TEXT_P(inputstring); ! else ! /* otherwise return an empty string */ PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } --- 1986,1992 ---- if (fldnum == 1) /* first field - just return the input * string */ PG_RETURN_TEXT_P(inputstring); ! else /* otherwise return an empty string */ PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } *************** *** 2004,2011 **** if (fldnum == 1) /* first field - just return the input * string */ PG_RETURN_TEXT_P(inputstring); ! else ! /* otherwise return an empty string */ PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } else if ((start_posn != 0) && (end_posn == 0)) --- 2006,2012 ---- if (fldnum == 1) /* first field - just return the input * string */ PG_RETURN_TEXT_P(inputstring); ! else /* otherwise return an empty string */ PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); } else if ((start_posn != 0) && (end_posn == 0)) *************** *** 2026,2031 **** --- 2027,2217 ---- result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, end_posn - start_posn - fldsep_len, false); PG_RETURN_TEXT_P(result_text); } + } + + /* + * text_to_array + * parse input string + * return text array of elements + * based on provided field separator + */ + Datum + text_to_array(PG_FUNCTION_ARGS) + { + text *inputstring = PG_GETARG_TEXT_P(0); + int inputstring_len = TEXTLEN(inputstring); + text *fldsep = PG_GETARG_TEXT_P(1); + int fldsep_len = TEXTLEN(fldsep); + int fldnum; + int start_posn = 0; + int end_posn = 0; + text *result_text = NULL; + ArrayBuildState *astate = NULL; + MemoryContext oldcontext = CurrentMemoryContext; + + /* return NULL for empty input string */ + if (inputstring_len < 1) + PG_RETURN_NULL(); + + /* empty field separator + * return one element, 1D, array using the input string */ + if (fldsep_len < 1) + PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID, + CStringGetDatum(inputstring), 1)); + + /* start with end position holding the initial start position */ + end_posn = 0; + for (fldnum=1;;fldnum++) /* field number is 1 based */ + { + Datum dvalue; + bool disnull = false; + + start_posn = end_posn; + end_posn = text_position(PointerGetDatum(inputstring), + PointerGetDatum(fldsep), + fldnum); + + if ((start_posn == 0) && (end_posn == 0)) /* fldsep not found */ + { + if (fldnum == 1) + { + /* first element + * return one element, 1D, array using the input string */ + PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID, + CStringGetDatum(inputstring), 1)); + } + else + { + /* otherwise create array and exit */ + PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, oldcontext)); + } + } + else if ((start_posn != 0) && (end_posn == 0)) + { + /* last field requested */ + result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, -1, true); + } + else if ((start_posn == 0) && (end_posn != 0)) + { + /* first field requested */ + result_text = LEFT(inputstring, fldsep); + } + else + { + /* prior to last field requested */ + result_text = text_substring(PointerGetDatum(inputstring), start_posn + fldsep_len, end_posn - start_posn - fldsep_len, false); + } + + /* stash away current value */ + dvalue = PointerGetDatum(result_text); + astate = accumArrayResult(astate, dvalue, + disnull, TEXTOID, oldcontext); + + } + + /* never reached -- keep compiler quiet */ + PG_RETURN_NULL(); + } + + /* + * array_to_text + * concatenate Cstring representation of input array elements + * using provided field separator + */ + Datum + array_to_text(PG_FUNCTION_ARGS) + { + ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); + char *fldsep = PG_TEXTARG_GET_STR(1); + int nitems, *dims, ndims; + char *p; + Oid element_type; + int typlen; + bool typbyval; + char typdelim; + Oid typoutput, + typelem; + FmgrInfo outputproc; + char typalign; + StringInfo result_str = makeStringInfo(); + int i; + ArrayMetaState *my_extra; + + p = ARR_DATA_PTR(v); + ndims = ARR_NDIM(v); + dims = ARR_DIMS(v); + nitems = ArrayGetNItems(ndims, dims); + + /* if there are no elements, return an empty string */ + if (nitems == 0) + PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); + + element_type = ARR_ELEMTYPE(v); + + /* + * We arrange to look up info about element type, including its output + * conversion proc only once per series of calls, assuming the element + * type doesn't change underneath us. + */ + my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; + if (my_extra == NULL) + { + fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, + sizeof(ArrayMetaState)); + my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra; + my_extra->element_type = InvalidOid; + } + + if (my_extra->element_type != element_type) + { + /* Get info about element type, including its output conversion proc */ + get_type_metadata(element_type, IOFunc_output, + &typlen, &typbyval, &typdelim, + &typelem, &typoutput, &typalign); + fmgr_info(typoutput, &outputproc); + + my_extra->element_type = element_type; + my_extra->typlen = typlen; + my_extra->typbyval = typbyval; + my_extra->typdelim = typdelim; + my_extra->typelem = typelem; + my_extra->typiofunc = typoutput; + my_extra->typalign = typalign; + my_extra->proc = outputproc; + } + else + { + typlen = my_extra->typlen; + typbyval = my_extra->typbyval; + typdelim = my_extra->typdelim; + typelem = my_extra->typelem; + typoutput = my_extra->typiofunc; + typalign = my_extra->typalign; + outputproc = my_extra->proc; + } + + for (i = 0; i < nitems; i++) + { + Datum itemvalue; + char *value; + + itemvalue = fetch_att(p, typbyval, typlen); + + value = DatumGetCString(FunctionCall3(&outputproc, + itemvalue, + ObjectIdGetDatum(typelem), + Int32GetDatum(-1))); + + if (i > 0) + appendStringInfo(result_str, "%s%s", fldsep, value); + else + appendStringInfo(result_str, "%s", value); + + p = att_addlength(p, typlen, PointerGetDatum(p)); + p = (char *) att_align(p, typalign); + } + + PG_RETURN_TEXT_P(PG_STR_GET_TEXT(result_str->data)); } #define HEXBASE 16 Index: src/backend/utils/cache/lsyscache.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/cache/lsyscache.c,v retrieving revision 1.95 diff -c -r1.95 lsyscache.c *** src/backend/utils/cache/lsyscache.c 26 May 2003 00:11:27 -0000 1.95 --- src/backend/utils/cache/lsyscache.c 29 May 2003 04:30:53 -0000 *************** *** 625,630 **** --- 625,664 ---- } /* + * get_func_argtypes + * Given procedure id, return the function's argument types. + * Also pass back the number of arguments. + */ + Oid * + get_func_argtypes(Oid funcid, int *nargs) + { + HeapTuple tp; + Form_pg_proc procstruct; + Oid *result = NULL; + int i; + + tp = SearchSysCache(PROCOID, + ObjectIdGetDatum(funcid), + 0, 0, 0); + if (!HeapTupleIsValid(tp)) + elog(ERROR, "Function OID %u does not exist", funcid); + + procstruct = (Form_pg_proc) GETSTRUCT(tp); + *nargs = (int) procstruct->pronargs; + + if (*nargs > 0) + { + result = (Oid *) palloc(*nargs * sizeof(Oid)); + + for (i = 0; i < *nargs; i++) + result[i] = procstruct->proargtypes[i]; + } + + ReleaseSysCache(tp); + return result; + } + + /* * get_func_retset * Given procedure id, return the function's proretset flag. */ *************** *** 994,999 **** --- 1028,1083 ---- *typbyval = typtup->typbyval; *typalign = typtup->typalign; ReleaseSysCache(tp); + } + + /* + * get_type_metadata + * + * A six-fer: given the type OID, return typlen, typbyval, typalign, + * typdelim, typelem, IO function Oid. The IO function + * returned is controlled by IOFuncSelector + */ + void + get_type_metadata(Oid element_type, + IOFuncSelector which_func, + int *typlen, + bool *typbyval, + char *typdelim, + Oid *typelem, + Oid *proc, + char *typalign) + { + HeapTuple typeTuple; + Form_pg_type typeStruct; + + typeTuple = SearchSysCache(TYPEOID, + ObjectIdGetDatum(element_type), + 0, 0, 0); + if (!HeapTupleIsValid(typeTuple)) + elog(ERROR, "cache lookup failed for type %u", element_type); + typeStruct = (Form_pg_type) GETSTRUCT(typeTuple); + + *typlen = typeStruct->typlen; + *typbyval = typeStruct->typbyval; + *typdelim = typeStruct->typdelim; + *typelem = typeStruct->typelem; + *typalign = typeStruct->typalign; + switch (which_func) + { + case IOFunc_input: + *proc = typeStruct->typinput; + break; + case IOFunc_output: + *proc = typeStruct->typoutput; + break; + case IOFunc_receive: + *proc = typeStruct->typreceive; + break; + case IOFunc_send: + *proc = typeStruct->typsend; + break; + } + ReleaseSysCache(typeTuple); } #ifdef NOT_USED Index: src/backend/utils/fmgr/fmgr.c =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/fmgr/fmgr.c,v retrieving revision 1.68 diff -c -r1.68 fmgr.c *** src/backend/utils/fmgr/fmgr.c 8 Apr 2003 23:20:02 -0000 1.68 --- src/backend/utils/fmgr/fmgr.c 29 May 2003 04:54:06 -0000 *************** *** 1673,1675 **** --- 1673,1701 ---- return exprType((Node *) nth(argnum, args)); } + + /* + * Get the OID of the function or operator + * + * Returns InvalidOid if information is not available + */ + Oid + get_fn_expr_functype(FunctionCallInfo fcinfo) + { + Node *expr; + + /* + * can't return anything useful if we have no FmgrInfo or if + * its fn_expr node has not been initialized + */ + if (!fcinfo || !fcinfo->flinfo || !fcinfo->flinfo->fn_expr) + return InvalidOid; + + expr = fcinfo->flinfo->fn_expr; + if (IsA(expr, FuncExpr)) + return ((FuncExpr *) expr)->funcid; + else if (IsA(expr, OpExpr)) + return ((OpExpr *) expr)->opno; + else + return InvalidOid; + } Index: src/include/fmgr.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/fmgr.h,v retrieving revision 1.27 diff -c -r1.27 fmgr.h *** src/include/fmgr.h 8 Apr 2003 23:20:04 -0000 1.27 --- src/include/fmgr.h 29 May 2003 04:54:04 -0000 *************** *** 18,23 **** --- 18,24 ---- #ifndef FMGR_H #define FMGR_H + #include "nodes/nodes.h" /* * All functions that can be called directly by fmgr must have this signature. *************** *** 372,385 **** Datum arg6, Datum arg7, Datum arg8, Datum arg9); - /* * Routines in fmgr.c */ extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname); ! extern Oid fmgr_internal_function(const char *proname); ! extern Oid get_fn_expr_rettype(FunctionCallInfo fcinfo); ! extern Oid get_fn_expr_argtype(FunctionCallInfo fcinfo, int argnum); /* * Routines in dfmgr.c --- 373,386 ---- Datum arg6, Datum arg7, Datum arg8, Datum arg9); /* * Routines in fmgr.c */ extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname); ! extern Oid fmgr_internal_function(const char *proname); ! extern Oid get_fn_expr_rettype(FunctionCallInfo fcinfo); ! extern Oid get_fn_expr_argtype(FunctionCallInfo fcinfo, int argnum); ! extern Oid get_fn_expr_functype(FunctionCallInfo fcinfo); /* * Routines in dfmgr.c Index: src/include/catalog/pg_operator.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/catalog/pg_operator.h,v retrieving revision 1.114 diff -c -r1.114 pg_operator.h *** src/include/catalog/pg_operator.h 26 May 2003 00:11:27 -0000 1.114 --- src/include/catalog/pg_operator.h 29 May 2003 04:56:31 -0000 *************** *** 116,125 **** DATA(insert OID = 97 ( "<" PGNSP PGUID b f 23 23 16 521 525 0 0 0 0 int4lt scalarltsel scalarltjoinsel )); DATA(insert OID = 98 ( "=" PGNSP PGUID b t 25 25 16 98 531 664 664 664 666 texteq eqsel eqjoinsel )); ! DATA(insert OID = 329 ( "=" PGNSP PGUID b f 2277 2277 16 329 0 0 0 0 0 array_eq eqsel eqjoinsel )); ! DATA(insert OID = 349 ( "||" PGNSP PGUID b f 2277 2283 2277 0 0 0 0 0 0 array_append - - )); ! DATA(insert OID = 374 ( "||" PGNSP PGUID b f 2283 2277 2277 0 0 0 0 0 0 array_prepend - - )); ! DATA(insert OID = 375 ( "||" PGNSP PGUID b f 2277 2277 2277 0 0 0 0 0 0 array_cat - - )); DATA(insert OID = 352 ( "=" PGNSP PGUID b t 28 28 16 352 0 0 0 0 0 xideq eqsel eqjoinsel )); DATA(insert OID = 353 ( "=" PGNSP PGUID b t 28 23 16 0 0 0 0 0 0 xideqint4 eqsel eqjoinsel )); --- 116,125 ---- DATA(insert OID = 97 ( "<" PGNSP PGUID b f 23 23 16 521 525 0 0 0 0 int4lt scalarltsel scalarltjoinsel )); DATA(insert OID = 98 ( "=" PGNSP PGUID b t 25 25 16 98 531 664 664 664 666 texteq eqsel eqjoinsel )); ! DATA(insert OID = 329 ( "=" PGNSP PGUID b f 2277 2277 16 329 0 0 0 0 0 array_eq eqsel eqjoinsel )); ! DATA(insert OID = 349 ( "||" PGNSP PGUID b f 2277 2283 2277 0 0 0 0 0 0 array_append - - )); ! DATA(insert OID = 374 ( "||" PGNSP PGUID b f 2283 2277 2277 0 0 0 0 0 0 array_prepend - - )); ! DATA(insert OID = 375 ( "||" PGNSP PGUID b f 2277 2277 2277 0 0 0 0 0 0 array_cat - - )); DATA(insert OID = 352 ( "=" PGNSP PGUID b t 28 28 16 352 0 0 0 0 0 xideq eqsel eqjoinsel )); DATA(insert OID = 353 ( "=" PGNSP PGUID b t 28 23 16 0 0 0 0 0 0 xideqint4 eqsel eqjoinsel )); Index: src/include/catalog/pg_proc.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/catalog/pg_proc.h,v retrieving revision 1.302 diff -c -r1.302 pg_proc.h *** src/include/catalog/pg_proc.h 26 May 2003 00:11:27 -0000 1.302 --- src/include/catalog/pg_proc.h 29 May 2003 04:56:54 -0000 *************** *** 1008,1015 **** DESCR("append element onto end of array"); DATA(insert OID = 379 ( array_prepend PGNSP PGUID 12 f f t f i 2 2277 "2283 2277" array_push - _null_ )); DESCR("prepend element onto front of array"); - DATA(insert OID = 380 ( array_accum PGNSP PGUID 12 f f f f i 2 2277 "2277 2283" array_accum - _null_ )); - DESCR("push element onto end of array, creating array if needed"); DATA(insert OID = 381 ( array_assign PGNSP PGUID 12 f f t f i 3 2277 "2277 23 2283" array_assign - _null_ )); DESCR("assign specific array element"); DATA(insert OID = 382 ( array_subscript PGNSP PGUID 12 f f t f i 2 2283 "2277 23" array_subscript - _null_ )); --- 1008,1013 ---- *************** *** 1018,1023 **** --- 1016,1025 ---- DESCR("concatenate two arrays"); DATA(insert OID = 384 ( array_coerce PGNSP PGUID 12 f f t f i 1 2277 "2277" array_type_coerce - _null_ )); DESCR("coerce array type to another array type"); + DATA(insert OID = 394 ( str_to_array PGNSP PGUID 12 f f t f i 2 1009 "25 25" text_to_array - _null_ )); + DESCR("split delimited text into text[]"); + DATA(insert OID = 395 ( array_to_str PGNSP PGUID 12 f f t f i 2 25 "2277 25" array_to_text - _null_ )); + DESCR("concatenate array elements, using delimiter, into text"); DATA(insert OID = 760 ( smgrin PGNSP PGUID 12 f f t f s 1 210 "2275" smgrin - _null_ )); DESCR("I/O"); Index: src/include/nodes/primnodes.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/nodes/primnodes.h,v retrieving revision 1.82 diff -c -r1.82 primnodes.h *** src/include/nodes/primnodes.h 6 May 2003 00:20:33 -0000 1.82 --- src/include/nodes/primnodes.h 29 May 2003 04:30:53 -0000 *************** *** 225,230 **** --- 225,231 ---- Expr *target; /* expression we are aggregating on */ bool aggstar; /* TRUE if argument was really '*' */ bool aggdistinct; /* TRUE if it's agg(DISTINCT ...) */ + List *args; /* arguments to the aggregate */ } Aggref; /* ---------------- Index: src/include/optimizer/clauses.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/optimizer/clauses.h,v retrieving revision 1.63 diff -c -r1.63 clauses.h *** src/include/optimizer/clauses.h 28 May 2003 16:04:02 -0000 1.63 --- src/include/optimizer/clauses.h 29 May 2003 04:30:53 -0000 *************** *** 28,33 **** --- 28,36 ---- extern Node *get_leftop(Expr *clause); extern Node *get_rightop(Expr *clause); + extern Expr *make_funcclause(Oid funcid, Oid funcresulttype, bool funcretset, + CoercionForm funcformat, List *funcargs); + extern bool not_clause(Node *clause); extern Expr *make_notclause(Expr *notclause); extern Expr *get_notclausearg(Expr *notclause); Index: src/include/utils/array.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/utils/array.h,v retrieving revision 1.38 diff -c -r1.38 array.h *** src/include/utils/array.h 8 May 2003 22:19:57 -0000 1.38 --- src/include/utils/array.h 29 May 2003 04:58:53 -0000 *************** *** 32,37 **** --- 32,68 ---- Oid elemtype; /* element type OID */ } ArrayType; + typedef struct ArrayBuildState + { + MemoryContext mcontext; /* where all the temp stuff is kept */ + Datum *dvalues; /* array of accumulated Datums */ + /* + * The allocated size of dvalues[] is always a multiple of + * ARRAY_ELEMS_CHUNKSIZE + */ + #define ARRAY_ELEMS_CHUNKSIZE 64 + int nelems; /* number of valid Datums in dvalues[] */ + Oid element_type; /* data type of the Datums */ + int16 typlen; /* needed info about datatype */ + bool typbyval; + char typalign; + } ArrayBuildState; + + /* + * structure to cache type metadata needed for array manipulation + */ + typedef struct ArrayMetaState + { + Oid element_type; + int typlen; + bool typbyval; + char typdelim; + Oid typelem; + Oid typiofunc; + char typalign; + FmgrInfo proc; + } ArrayMetaState; + /* * fmgr macros for array objects */ *************** *** 124,130 **** Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, int *nelemsp); ! /* * prototypes for functions defined in arrayutils.c --- 155,168 ---- Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, int *nelemsp); ! extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate, ! Datum dvalue, bool disnull, ! Oid element_type, ! MemoryContext rcontext); ! extern Datum makeArrayResult(ArrayBuildState *astate, ! MemoryContext rcontext); ! extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, ! int *dims, int *lbs, MemoryContext rcontext); /* * prototypes for functions defined in arrayutils.c *************** *** 143,152 **** */ extern Datum singleton_array(PG_FUNCTION_ARGS); extern Datum array_push(PG_FUNCTION_ARGS); - extern Datum array_accum(PG_FUNCTION_ARGS); extern Datum array_cat(PG_FUNCTION_ARGS); ! extern ArrayType *create_singleton_array(Oid element_type, Datum element, int ndims); --- 181,190 ---- */ extern Datum singleton_array(PG_FUNCTION_ARGS); extern Datum array_push(PG_FUNCTION_ARGS); extern Datum array_cat(PG_FUNCTION_ARGS); ! extern ArrayType *create_singleton_array(FunctionCallInfo fcinfo, ! Oid element_type, Datum element, int ndims); Index: src/include/utils/builtins.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/utils/builtins.h,v retrieving revision 1.219 diff -c -r1.219 builtins.h *** src/include/utils/builtins.h 26 May 2003 00:11:28 -0000 1.219 --- src/include/utils/builtins.h 29 May 2003 04:30:53 -0000 *************** *** 530,535 **** --- 530,537 ---- List **namelist); extern Datum replace_text(PG_FUNCTION_ARGS); extern Datum split_text(PG_FUNCTION_ARGS); + extern Datum text_to_array(PG_FUNCTION_ARGS); + extern Datum array_to_text(PG_FUNCTION_ARGS); extern Datum to_hex32(PG_FUNCTION_ARGS); extern Datum to_hex64(PG_FUNCTION_ARGS); extern Datum md5_text(PG_FUNCTION_ARGS); Index: src/include/utils/lsyscache.h =================================================================== RCS file: /opt/src/cvs/pgsql-server/src/include/utils/lsyscache.h,v retrieving revision 1.70 diff -c -r1.70 lsyscache.h *** src/include/utils/lsyscache.h 26 May 2003 00:11:28 -0000 1.70 --- src/include/utils/lsyscache.h 29 May 2003 04:30:53 -0000 *************** *** 15,20 **** --- 15,29 ---- #include "access/htup.h" + /* I/O function selector for system_cache_lookup */ + typedef enum IOFuncSelector + { + IOFunc_input, + IOFunc_output, + IOFunc_receive, + IOFunc_send + } IOFuncSelector; + extern bool op_in_opclass(Oid opno, Oid opclass); extern bool op_requires_recheck(Oid opno, Oid opclass); extern Oid get_opclass_member(Oid opclass, int16 strategy); *************** *** 39,44 **** --- 48,54 ---- extern RegProcedure get_oprjoin(Oid opno); extern char *get_func_name(Oid funcid); extern Oid get_func_rettype(Oid funcid); + extern Oid *get_func_argtypes(Oid funcid, int *nargs); extern bool get_func_retset(Oid funcid); extern bool func_strict(Oid funcid); extern char func_volatile(Oid funcid); *************** *** 54,59 **** --- 64,77 ---- extern void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval); extern void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign); + extern void get_type_metadata(Oid element_type, + IOFuncSelector which_func, + int *typlen, + bool *typbyval, + char *typdelim, + Oid *typelem, + Oid *proc, + char *typalign); extern char get_typstorage(Oid typid); extern int32 get_typtypmod(Oid typid); extern Node *get_typdefault(Oid typid);