diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index dda561f..e6923c3 100644
*** a/doc/src/sgml/func.sgml
--- b/doc/src/sgml/func.sgml
***************
*** 1251,1256 ****
--- 1251,1262 ----
chr
+ concat
+
+
+ concat_ws
+
+
convert
***************
*** 1269,1274 ****
--- 1275,1283 ----
initcap
+ left
+
+
lpad
***************
*** 1296,1301 ****
--- 1305,1316 ----
replace
+ reverse
+
+
+ right
+
+
rpad
***************
*** 1376,1381 ****
--- 1391,1424 ----
+ concat(str "any"
+ [, str "any" [, ...] ])
+
+ text
+
+ Concatenate all arguments. NULL arguments are ignored.
+
+ concat('abcde', 2, NULL, 22)
+ abcde222
+
+
+
+
+ concat_ws(sep text,
+ str "any"
+ [, str "any" [, ...] ])
+
+ text
+
+ Concatenate all but first arguments with separators. The first
+ parameter is used as a separator. NULL arguments are ignored.
+
+ concat_ws(',', 'abcde', 2, NULL, 22)
+ abcde,2,22
+
+
+
+
convert(string bytea,
src_encoding name,
dest_encoding name)
***************
*** 1466,1471 ****
--- 1509,1528 ----
+
+ left(str text,
+ n int)
+
+ text
+
+ Return first n> characters in the string. When n>
+ is negative, return all but last |n>| characters.
+
+ left('abcde', 2)
+ ab
+
+
+
length(string)
int
***************
*** 1680,1685 ****
--- 1737,1768 ----
+ reverse(str)
+
+ text
+
+ Return reversed string.
+
+ reverse('abcde')
+ edcba
+
+
+
+
+ right(str text,
+ n int)
+
+ text
+
+ Return last n> characters in the string. When n>
+ is negative, return all but first |n>| characters.
+
+ right('abcde', 2)
+ de
+
+
+
+
rpad(string text,
length int
, fill text)
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 94766cd..0dd4bc1 100644
*** a/src/backend/utils/adt/varlena.c
--- b/src/backend/utils/adt/varlena.c
*************** string_agg_finalfn(PG_FUNCTION_ARGS)
*** 3556,3558 ****
--- 3556,3704 ----
else
PG_RETURN_NULL();
}
+
+ static text *
+ concat_internal(const char *sepstr, int seplen, int argidx, FunctionCallInfo fcinfo)
+ {
+ StringInfoData str;
+ text *result;
+ int i;
+
+ initStringInfo(&str);
+
+ for (i = argidx; i < PG_NARGS(); i++)
+ {
+ if (!PG_ARGISNULL(i))
+ {
+ Oid valtype;
+ Datum value;
+ Oid typOutput;
+ bool typIsVarlena;
+
+ if (i > argidx)
+ appendBinaryStringInfo(&str, sepstr, seplen);
+
+ /* append n-th value */
+ value = PG_GETARG_DATUM(i);
+ valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
+ getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
+ appendStringInfoString(&str,
+ OidOutputFunctionCall(typOutput, value));
+ }
+ }
+
+ result = cstring_to_text_with_len(str.data, str.len);
+ pfree(str.data);
+
+ return result;
+ }
+
+ /*
+ * Concatenate all arguments. NULL arguments are ignored.
+ */
+ Datum
+ text_concat(PG_FUNCTION_ARGS)
+ {
+ PG_RETURN_TEXT_P(concat_internal(NULL, 0, 0, fcinfo));
+ }
+
+ /*
+ * Concatenate all but first argument values with separators. The first
+ * parameter is used as a separator. NULL arguments are ignored.
+ */
+ Datum
+ text_concat_ws(PG_FUNCTION_ARGS)
+ {
+ text *sep;
+
+ /* return NULL when separator is NULL */
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+
+ sep = PG_GETARG_TEXT_PP(0);
+
+ PG_RETURN_TEXT_P(concat_internal(
+ VARDATA_ANY(sep), VARSIZE_ANY_EXHDR(sep), 1, fcinfo));
+ }
+
+ /*
+ * Return first n characters in the string. When n is negative,
+ * return all but last |n| characters.
+ */
+ Datum
+ text_left(PG_FUNCTION_ARGS)
+ {
+ text *str = PG_GETARG_TEXT_PP(0);
+ const char *p = VARDATA_ANY(str);
+ int len = VARSIZE_ANY_EXHDR(str);
+ int n = PG_GETARG_INT32(1);
+ int rlen;
+
+ if (n < 0)
+ n = pg_mbstrlen_with_len(p, len) + n;
+ rlen = pg_mbcharcliplen(p, len, n);
+
+ PG_RETURN_TEXT_P(cstring_to_text_with_len(p, rlen));
+ }
+
+ /*
+ * Return last n characters in the string. When n is negative,
+ * return all but first |n| characters.
+ */
+ Datum
+ text_right(PG_FUNCTION_ARGS)
+ {
+ text *str = PG_GETARG_TEXT_PP(0);
+ const char *p = VARDATA_ANY(str);
+ int len = VARSIZE_ANY_EXHDR(str);
+ int n = PG_GETARG_INT32(1);
+ int off;
+
+ if (n < 0)
+ n = -n;
+ else
+ n = pg_mbstrlen_with_len(p, len) - n;
+ off = pg_mbcharcliplen(p, len, n);
+
+ PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
+ }
+
+ /*
+ * Return reversed string
+ */
+ Datum
+ text_reverse(PG_FUNCTION_ARGS)
+ {
+ text *str = PG_GETARG_TEXT_PP(0);
+ const char *p = VARDATA_ANY(str);
+ int len = VARSIZE_ANY_EXHDR(str);
+ const char *endp = p + len;
+ text *result;
+ char *dst;
+
+ result = palloc(len + VARHDRSZ);
+ dst = (char*) VARDATA(result) + len;
+ SET_VARSIZE(result, len + VARHDRSZ);
+
+ if (pg_database_encoding_max_length() > 1)
+ {
+ /* multibyte version */
+ while (p < endp)
+ {
+ int sz;
+
+ sz = pg_mblen(p);
+ dst -= sz;
+ memcpy(dst, p, sz);
+ p += sz;
+ }
+ }
+ else
+ {
+ /* single byte version */
+ while (p < endp)
+ *(--dst) = *p++;
+ }
+
+ PG_RETURN_TEXT_P(result);
+ }
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 1c53418..f6364cd 100644
*** a/src/include/catalog/pg_proc.h
--- b/src/include/catalog/pg_proc.h
*************** DESCR("I/O");
*** 2722,2727 ****
--- 2722,2737 ----
DATA(insert OID = 1799 ( oidout PGNSP PGUID 12 1 0 0 f f f t f i 1 0 2275 "26" _null_ _null_ _null_ _null_ oidout _null_ _null_ _null_ ));
DESCR("I/O");
+ DATA(insert OID = 3058 ( concat PGNSP PGUID 12 1 0 2276 f f f f f s 1 0 25 "2276" "{2276}" "{v}" _null_ _null_ text_concat _null_ _null_ _null_ ));
+ DESCR("concatenate values");
+ DATA(insert OID = 3059 ( concat_ws PGNSP PGUID 12 1 0 2276 f f f f f s 2 0 25 "25 2276" "{25,2276}" "{i,v}" _null_ _null_ text_concat_ws _null_ _null_ _null_ ));
+ DESCR("concatenate values with separators");
+ DATA(insert OID = 3060 ( left PGNSP PGUID 12 1 0 0 f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ text_left _null_ _null_ _null_ ));
+ DESCR("return the first n characters");
+ DATA(insert OID = 3061 ( right PGNSP PGUID 12 1 0 0 f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ text_right _null_ _null_ _null_ ));
+ DESCR("return the last n characters");
+ DATA(insert OID = 3062 ( reverse PGNSP PGUID 12 1 0 0 f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ text_reverse _null_ _null_ _null_ ));
+ DESCR("reverse text");
DATA(insert OID = 1810 ( bit_length PGNSP PGUID 14 1 0 0 f f f t f i 1 0 23 "17" _null_ _null_ _null_ _null_ "select pg_catalog.octet_length($1) * 8" _null_ _null_ _null_ ));
DESCR("length in bits");
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index a4c6180..f064a89 100644
*** a/src/include/utils/builtins.h
--- b/src/include/utils/builtins.h
*************** extern Datum pg_column_size(PG_FUNCTION_
*** 733,738 ****
--- 733,744 ----
extern Datum string_agg_transfn(PG_FUNCTION_ARGS);
extern Datum string_agg_finalfn(PG_FUNCTION_ARGS);
+ extern Datum text_concat(PG_FUNCTION_ARGS);
+ extern Datum text_concat_ws(PG_FUNCTION_ARGS);
+ extern Datum text_left(PG_FUNCTION_ARGS);
+ extern Datum text_right(PG_FUNCTION_ARGS);
+ extern Datum text_reverse(PG_FUNCTION_ARGS);
+
/* version.c */
extern Datum pgsql_version(PG_FUNCTION_ARGS);
diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out
index 08d002f..84f4a5c 100644
*** a/src/test/regress/expected/text.out
--- b/src/test/regress/expected/text.out
*************** ERROR: operator does not exist: integer
*** 51,53 ****
--- 51,120 ----
LINE 1: select 3 || 4.0;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
+ /*
+ * string functions
+ */
+ select concat('one');
+ concat
+ --------
+ one
+ (1 row)
+
+ select concat(1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
+ concat
+ ----------------------
+ 123hellotf03-09-2010
+ (1 row)
+
+ select concat_ws('#','one');
+ concat_ws
+ -----------
+ one
+ (1 row)
+
+ select concat_ws('#',1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
+ concat_ws
+ ----------------------------
+ 1#2#3#hello#t#f#03-09-2010
+ (1 row)
+
+ select concat_ws(',',10,20,null,30);
+ concat_ws
+ -----------
+ 10,20,30
+ (1 row)
+
+ select concat_ws('',10,20,null,30);
+ concat_ws
+ -----------
+ 102030
+ (1 row)
+
+ select concat_ws(NULL,10,20,null,30) is null;
+ ?column?
+ ----------
+ t
+ (1 row)
+
+ select reverse('abcde');
+ reverse
+ ---------
+ edcba
+ (1 row)
+
+ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;
+ i | left | right
+ ----+------+-------
+ -5 | |
+ -4 | |
+ -3 | a | j
+ -2 | ah | oj
+ -1 | aho | hoj
+ 0 | |
+ 1 | a | j
+ 2 | ah | oj
+ 3 | aho | hoj
+ 4 | ahoj | ahoj
+ 5 | ahoj | ahoj
+ (11 rows)
+
diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql
index b739e56..a8768ee 100644
*** a/src/test/regress/sql/text.sql
--- b/src/test/regress/sql/text.sql
*************** select 'four: ' || 2+2;
*** 28,30 ****
--- 28,43 ----
-- but not this:
select 3 || 4.0;
+
+ /*
+ * string functions
+ */
+ select concat('one');
+ select concat(1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
+ select concat_ws('#','one');
+ select concat_ws('#',1,2,3,'hello',true, false, to_date('20100309','YYYYMMDD'));
+ select concat_ws(',',10,20,null,30);
+ select concat_ws('',10,20,null,30);
+ select concat_ws(NULL,10,20,null,30) is null;
+ select reverse('abcde');
+ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i;