Skip to content

Commit 9507c8a

Browse files
committed
Add get_bit/set_bit functions for bit strings, paralleling those for bytea,
and implement OVERLAY() for bit strings and bytea. In passing also convert text OVERLAY() to a true built-in, instead of relying on a SQL function. Leonardo F, reviewed by Kevin Grittner
1 parent 1d1f425 commit 9507c8a

File tree

13 files changed

+553
-92
lines changed

13 files changed

+553
-92
lines changed

doc/src/sgml/func.sgml

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.497 2010/01/19 05:50:18 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.498 2010/01/25 20:55:32 tgl Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -2620,7 +2620,7 @@
26202620
<entry>Return Type</entry>
26212621
<entry>Description</entry>
26222622
<entry>Example</entry>
2623-
<entry>Result</entry>
2623+
<entry>Result</entry>
26242624
</row>
26252625
</thead>
26262626

@@ -2640,32 +2640,6 @@
26402640
<entry><literal>\\Post'gres\000</literal></entry>
26412641
</row>
26422642

2643-
<row>
2644-
<entry><function>get_bit</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
2645-
<entry><type>int</type></entry>
2646-
<entry>
2647-
Extract bit from string
2648-
<indexterm>
2649-
<primary>get_bit</primary>
2650-
</indexterm>
2651-
</entry>
2652-
<entry><literal>get_bit(E'Th\\000omas'::bytea, 45)</literal></entry>
2653-
<entry><literal>1</literal></entry>
2654-
</row>
2655-
2656-
<row>
2657-
<entry><function>get_byte</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
2658-
<entry><type>int</type></entry>
2659-
<entry>
2660-
Extract byte from string
2661-
<indexterm>
2662-
<primary>get_byte</primary>
2663-
</indexterm>
2664-
</entry>
2665-
<entry><literal>get_byte(E'Th\\000omas'::bytea, 4)</literal></entry>
2666-
<entry><literal>109</literal></entry>
2667-
</row>
2668-
26692643
<row>
26702644
<entry><literal><function>octet_length</function>(<parameter>string</parameter>)</literal></entry>
26712645
<entry><type>int</type></entry>
@@ -2675,39 +2649,21 @@
26752649
</row>
26762650

26772651
<row>
2678-
<entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
2679-
<entry><type>int</type></entry>
2680-
<entry>Location of specified substring</entry>
2681-
<entry><literal>position(E'\\000om'::bytea in E'Th\\000omas'::bytea)</literal></entry>
2682-
<entry><literal>3</literal></entry>
2683-
</row>
2684-
2685-
<row>
2686-
<entry><function>set_bit</function>(<parameter>string</parameter>,
2687-
<parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2652+
<entry><literal><function>overlay</function>(<parameter>string</parameter> placing <parameter>string</parameter> from <type>int</type> <optional>for <type>int</type></optional>)</literal></entry>
26882653
<entry><type>bytea</type></entry>
26892654
<entry>
2690-
Set bit in string
2691-
<indexterm>
2692-
<primary>set_bit</primary>
2693-
</indexterm>
2655+
Replace substring
26942656
</entry>
2695-
<entry><literal>set_bit(E'Th\\000omas'::bytea, 45, 0)</literal></entry>
2696-
<entry><literal>Th\000omAs</literal></entry>
2657+
<entry><literal>overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 2 for 3)</literal></entry>
2658+
<entry><literal>T\\002\\003mas</literal></entry>
26972659
</row>
26982660

26992661
<row>
2700-
<entry><function>set_byte</function>(<parameter>string</parameter>,
2701-
<parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2702-
<entry><type>bytea</type></entry>
2703-
<entry>
2704-
Set byte in string
2705-
<indexterm>
2706-
<primary>set_byte</primary>
2707-
</indexterm>
2708-
</entry>
2709-
<entry><literal>set_byte(E'Th\\000omas'::bytea, 4, 64)</literal></entry>
2710-
<entry><literal>Th\000o@as</literal></entry>
2662+
<entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
2663+
<entry><type>int</type></entry>
2664+
<entry>Location of specified substring</entry>
2665+
<entry><literal>position(E'\\000om'::bytea in E'Th\\000omas'::bytea)</literal></entry>
2666+
<entry><literal>3</literal></entry>
27112667
</row>
27122668

27132669
<row>
@@ -2784,7 +2740,7 @@
27842740
</entry>
27852741
<entry><type>bytea</type></entry>
27862742
<entry>
2787-
Decode binary string from <parameter>string</parameter> previously
2743+
Decode binary string from <parameter>string</parameter> previously
27882744
encoded with <function>encode</>. Parameter type is same as in <function>encode</>.
27892745
</entry>
27902746
<entry><literal>decode(E'123\\000456', 'escape')</literal></entry>
@@ -2805,6 +2761,36 @@
28052761
<entry><literal>123\000456</literal></entry>
28062762
</row>
28072763

2764+
<row>
2765+
<entry>
2766+
<literal><function>get_bit</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</literal>
2767+
</entry>
2768+
<entry><type>int</type></entry>
2769+
<entry>
2770+
Extract bit from string
2771+
<indexterm>
2772+
<primary>get_bit</primary>
2773+
</indexterm>
2774+
</entry>
2775+
<entry><literal>get_bit(E'Th\\000omas'::bytea, 45)</literal></entry>
2776+
<entry><literal>1</literal></entry>
2777+
</row>
2778+
2779+
<row>
2780+
<entry>
2781+
<literal><function>get_byte</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</literal>
2782+
</entry>
2783+
<entry><type>int</type></entry>
2784+
<entry>
2785+
Extract byte from string
2786+
<indexterm>
2787+
<primary>get_byte</primary>
2788+
</indexterm>
2789+
</entry>
2790+
<entry><literal>get_byte(E'Th\\000omas'::bytea, 4)</literal></entry>
2791+
<entry><literal>109</literal></entry>
2792+
</row>
2793+
28082794
<row>
28092795
<entry><literal><function>length</function>(<parameter>string</parameter>)</literal></entry>
28102796
<entry><type>int</type></entry>
@@ -2834,6 +2820,38 @@
28342820
<entry><literal>md5(E'Th\\000omas'::bytea)</literal></entry>
28352821
<entry><literal>8ab2d3c9689aaf18 b4958c334c82d8b1</literal></entry>
28362822
</row>
2823+
2824+
<row>
2825+
<entry>
2826+
<literal><function>set_bit</function>(<parameter>string</parameter>,
2827+
<parameter>offset</parameter>, <parameter>newvalue</>)</literal>
2828+
</entry>
2829+
<entry><type>bytea</type></entry>
2830+
<entry>
2831+
Set bit in string
2832+
<indexterm>
2833+
<primary>set_bit</primary>
2834+
</indexterm>
2835+
</entry>
2836+
<entry><literal>set_bit(E'Th\\000omas'::bytea, 45, 0)</literal></entry>
2837+
<entry><literal>Th\000omAs</literal></entry>
2838+
</row>
2839+
2840+
<row>
2841+
<entry>
2842+
<literal><function>set_byte</function>(<parameter>string</parameter>,
2843+
<parameter>offset</parameter>, <parameter>newvalue</>)</literal>
2844+
</entry>
2845+
<entry><type>bytea</type></entry>
2846+
<entry>
2847+
Set byte in string
2848+
<indexterm>
2849+
<primary>set_byte</primary>
2850+
</indexterm>
2851+
</entry>
2852+
<entry><literal>set_byte(E'Th\\000omas'::bytea, 4, 64)</literal></entry>
2853+
<entry><literal>Th\000o@as</literal></entry>
2854+
</row>
28372855
</tbody>
28382856
</tgroup>
28392857
</table>
@@ -2934,7 +2952,15 @@
29342952
<literal><function>bit_length</function></literal>,
29352953
<literal><function>octet_length</function></literal>,
29362954
<literal><function>position</function></literal>,
2937-
<literal><function>substring</function></literal>.
2955+
<literal><function>substring</function></literal>,
2956+
<literal><function>overlay</function></literal>.
2957+
</para>
2958+
2959+
<para>
2960+
The following functions work on bit strings as well as binary
2961+
strings:
2962+
<literal><function>get_bit</function></literal>,
2963+
<literal><function>set_bit</function></literal>.
29382964
</para>
29392965

29402966
<para>

src/backend/parser/gram.y

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.704 2010/01/22 16:40:18 rhaas Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.705 2010/01/25 20:55:32 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -9586,9 +9586,9 @@ func_expr: func_name '(' ')' over_clause
95869586
| OVERLAY '(' overlay_list ')'
95879587
{
95889588
/* overlay(A PLACING B FROM C FOR D) is converted to
9589-
* substring(A, 1, C-1) || B || substring(A, C+1, C+D)
9589+
* overlay(A, B, C, D)
95909590
* overlay(A PLACING B FROM C) is converted to
9591-
* substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
9591+
* overlay(A, B, C)
95929592
*/
95939593
FuncCall *n = makeNode(FuncCall);
95949594
n->funcname = SystemFuncName("overlay");
@@ -10150,6 +10150,7 @@ extract_arg:
1015010150
* SQL99 defines the OVERLAY() function:
1015110151
* o overlay(text placing text from int for int)
1015210152
* o overlay(text placing text from int)
10153+
* and similarly for binary strings
1015310154
*/
1015410155
overlay_list:
1015510156
a_expr overlay_placing substr_from substr_for

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy