Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Gpdb
提交
49b27ab5
G
Gpdb
项目概览
Greenplum
/
Gpdb
通知
7
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gpdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
49b27ab5
编写于
8月 24, 2010
作者:
I
Itagaki Takahiro
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add string functions: concat(), concat_ws(), left(), right(), and reverse().
Pavel Stehule, reviewed by me.
上级
b9defe04
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
331 addition
and
6 deletion
+331
-6
doc/src/sgml/func.sgml
doc/src/sgml/func.sgml
+84
-1
src/backend/utils/adt/varlena.c
src/backend/utils/adt/varlena.c
+147
-1
src/include/catalog/catversion.h
src/include/catalog/catversion.h
+2
-2
src/include/catalog/pg_proc.h
src/include/catalog/pg_proc.h
+11
-1
src/include/utils/builtins.h
src/include/utils/builtins.h
+7
-1
src/test/regress/expected/text.out
src/test/regress/expected/text.out
+67
-0
src/test/regress/sql/text.sql
src/test/regress/sql/text.sql
+13
-0
未找到文件。
doc/src/sgml/func.sgml
浏览文件 @
49b27ab5
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.5
29 2010/08/17 04:37:20 petere
Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.5
30 2010/08/24 06:30:43 itagaki
Exp $ -->
<chapter
id=
"functions"
>
<title>
Functions and Operators
</title>
...
...
@@ -1250,6 +1250,12 @@
<indexterm>
<primary>
chr
</primary>
</indexterm>
<indexterm>
<primary>
concat
</primary>
</indexterm>
<indexterm>
<primary>
concat_ws
</primary>
</indexterm>
<indexterm>
<primary>
convert
</primary>
</indexterm>
...
...
@@ -1268,6 +1274,9 @@
<indexterm>
<primary>
initcap
</primary>
</indexterm>
<indexterm>
<primary>
left
</primary>
</indexterm>
<indexterm>
<primary>
lpad
</primary>
</indexterm>
...
...
@@ -1295,6 +1304,12 @@
<indexterm>
<primary>
replace
</primary>
</indexterm>
<indexterm>
<primary>
reverse
</primary>
</indexterm>
<indexterm>
<primary>
right
</primary>
</indexterm>
<indexterm>
<primary>
rpad
</primary>
</indexterm>
...
...
@@ -1374,6 +1389,34 @@
<entry><literal>
A
</literal></entry>
</row>
<row>
<entry>
<literal><function>
concat
</function>
(
<parameter>
str
</parameter>
<type>
"any"
</type>
[,
<parameter>
str
</parameter>
<type>
"any"
</type>
[, ...] ])
</literal>
</entry>
<entry><type>
text
</type></entry>
<entry>
Concatenate all arguments. NULL arguments are ignored.
</entry>
<entry><literal>
concat('abcde', 2, NULL, 22)
</literal></entry>
<entry><literal>
abcde222
</literal></entry>
</row>
<row>
<entry>
<literal><function>
concat_ws
</function>
(
<parameter>
sep
</parameter>
<type>
text
</type>
,
<parameter>
str
</parameter>
<type>
"any"
</type>
[,
<parameter>
str
</parameter>
<type>
"any"
</type>
[, ...] ])
</literal>
</entry>
<entry><type>
text
</type></entry>
<entry>
Concatenate all but first arguments with separators. The first
parameter is used as a separator. NULL arguments are ignored.
</entry>
<entry><literal>
concat_ws(',', 'abcde', 2, NULL, 22)
</literal></entry>
<entry><literal>
abcde,2,22
</literal></entry>
</row>
<row>
<entry>
<literal><function>
convert
</function>
(
<parameter>
string
</parameter>
<type>
bytea
</type>
,
...
...
@@ -1465,6 +1508,20 @@
<entry><literal>
Hi Thomas
</literal></entry>
</row>
<row>
<entry>
<literal><function>
left
</function>
(
<parameter>
str
</parameter>
<type>
text
</type>
,
<parameter>
n
</parameter>
<type>
int
</type>
)
</literal>
</entry>
<entry><type>
text
</type></entry>
<entry>
Return first
<replaceable>
n
</>
characters in the string. When
<replaceable>
n
</>
is negative, return all but last |
<replaceable>
n
</>
| characters.
</entry>
<entry><literal>
left('abcde', 2)
</literal></entry>
<entry><literal>
ab
</literal></entry>
</row>
<row>
<entry><literal><function>
length
</function>
(
<parameter>
string
</parameter>
)
</literal></entry>
<entry><type>
int
</type></entry>
...
...
@@ -1678,6 +1735,32 @@
<entry><literal>
abXXefabXXef
</literal></entry>
</row>
<row>
<entry>
<literal><function>
reverse
</function>
(
<parameter>
str
</parameter>
)
</literal>
</entry>
<entry><type>
text
</type></entry>
<entry>
Return reversed string.
</entry>
<entry><literal>
reverse('abcde')
</literal></entry>
<entry><literal>
edcba
</literal></entry>
</row>
<row>
<entry>
<literal><function>
right
</function>
(
<parameter>
str
</parameter>
<type>
text
</type>
,
<parameter>
n
</parameter>
<type>
int
</type>
)
</literal>
</entry>
<entry><type>
text
</type></entry>
<entry>
Return last
<replaceable>
n
</>
characters in the string. When
<replaceable>
n
</>
is negative, return all but first |
<replaceable>
n
</>
| characters.
</entry>
<entry><literal>
right('abcde', 2)
</literal></entry>
<entry><literal>
de
</literal></entry>
</row>
<row>
<entry>
<literal><function>
rpad
</function>
(
<parameter>
string
</parameter>
<type>
text
</type>
,
...
...
src/backend/utils/adt/varlena.c
浏览文件 @
49b27ab5
...
...
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.1
79 2010/08/10 21:51:00 tgl
Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.1
80 2010/08/24 06:30:43 itagaki
Exp $
*
*-------------------------------------------------------------------------
*/
...
...
@@ -3556,3 +3556,149 @@ string_agg_finalfn(PG_FUNCTION_ARGS)
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
);
}
src/include/catalog/catversion.h
浏览文件 @
49b27ab5
...
...
@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.59
6 2010/08/21 10:59:17 mha
Exp $
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.59
7 2010/08/24 06:30:43 itagaki
Exp $
*
*-------------------------------------------------------------------------
*/
...
...
@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 2010082
1
1
#define CATALOG_VERSION_NO 2010082
4
1
#endif
src/include/catalog/pg_proc.h
浏览文件 @
49b27ab5
...
...
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.58
0 2010/08/21 10:59:17 mha
Exp $
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.58
1 2010/08/24 06:30:43 itagaki
Exp $
*
* NOTES
* The script catalog/genbki.pl reads this file and generates .bki
...
...
@@ -2722,6 +2722,16 @@ DESCR("I/O");
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"
);
...
...
src/include/utils/builtins.h
浏览文件 @
49b27ab5
...
...
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.35
4 2010/08/10 21:51:00 tgl
Exp $
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.35
5 2010/08/24 06:30:44 itagaki
Exp $
*
*-------------------------------------------------------------------------
*/
...
...
@@ -733,6 +733,12 @@ extern Datum pg_column_size(PG_FUNCTION_ARGS);
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
);
...
...
src/test/regress/expected/text.out
浏览文件 @
49b27ab5
...
...
@@ -51,3 +51,70 @@ ERROR: operator does not exist: integer || numeric
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)
src/test/regress/sql/text.sql
浏览文件 @
49b27ab5
...
...
@@ -28,3 +28,16 @@ select 'four: ' || 2+2;
-- 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
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录