Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 73874a0

Browse files
committed
Change the parser to convert SQL "position" and "substring" syntax to
position() and substring() functions, so that it works transparently for bit types as well. Alias the text functions appropriately. Add position() for bit types. Add new constant node T_BitString that represents literals of the form B'1001 and pass those to zpbit type.
1 parent d397c1c commit 73874a0

File tree

14 files changed

+214
-47
lines changed

14 files changed

+214
-47
lines changed

src/backend/nodes/copyfuncs.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.127 2000/10/26 21:35:47 tgl Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.128 2000/10/31 10:22:10 petere Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2511,6 +2511,7 @@ _copyValue(Value *from)
25112511
break;
25122512
case T_Float:
25132513
case T_String:
2514+
case T_BitString:
25142515
newnode->val.str = pstrdup(from->val.str);
25152516
break;
25162517
default:
@@ -2703,6 +2704,7 @@ copyObject(void *from)
27032704
case T_Integer:
27042705
case T_Float:
27052706
case T_String:
2707+
case T_BitString:
27062708
retval = _copyValue(from);
27072709
break;
27082710
case T_List:

src/backend/nodes/equalfuncs.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.77 2000/10/18 16:16:04 momjian Exp $
23+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.78 2000/10/31 10:22:10 petere Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -1719,6 +1719,7 @@ _equalValue(Value *a, Value *b)
17191719
return a->val.ival == b->val.ival;
17201720
case T_Float:
17211721
case T_String:
1722+
case T_BitString:
17221723
return strcmp(a->val.str, b->val.str) == 0;
17231724
default:
17241725
break;
@@ -1874,6 +1875,7 @@ equal(void *a, void *b)
18741875
case T_Integer:
18751876
case T_Float:
18761877
case T_String:
1878+
case T_BitString:
18771879
retval = _equalValue(a, b);
18781880
break;
18791881

src/backend/nodes/list.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.35 2000/10/05 19:11:27 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.36 2000/10/31 10:22:10 petere Exp $
1212
*
1313
* NOTES
1414
* XXX a few of the following functions are duplicated to handle
@@ -70,6 +70,23 @@ makeString(char *str)
7070
return v;
7171
}
7272

73+
74+
/*
75+
* makeBitString
76+
*
77+
* Caller is responsible for passing a palloc'd string.
78+
*/
79+
Value *
80+
makeBitString(char *str)
81+
{
82+
Value *v = makeNode(Value);
83+
84+
v->type = T_BitString;
85+
v->val.str = str;
86+
return v;
87+
}
88+
89+
7390
/*
7491
* lcons
7592
*

src/backend/nodes/outfuncs.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.129 2000/10/26 21:35:48 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.130 2000/10/31 10:22:10 petere Exp $
1010
*
1111
* NOTES
1212
* Every (plan) node in POSTGRES has an associated "out" routine which
@@ -20,10 +20,10 @@
2020
* representation plus some other information (string length, etc.)
2121
*
2222
*/
23-
#include <ctype.h>
24-
2523
#include "postgres.h"
2624

25+
#include <ctype.h>
26+
2727
#include "access/heapam.h"
2828
#include "access/htup.h"
2929
#include "catalog/pg_type.h"
@@ -1352,6 +1352,9 @@ _outValue(StringInfo str, Value *value)
13521352
_outToken(str, value->val.str);
13531353
appendStringInfo(str, "\" ");
13541354
break;
1355+
case T_BitString:
1356+
appendStringInfo(str, " B%s ", value->val.str);
1357+
break;
13551358
default:
13561359
elog(NOTICE, "_outValue: don't know how to print type %d ",
13571360
value->type);

src/backend/nodes/read.c

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.23 2000/06/14 18:17:32 petere Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.24 2000/10/31 10:22:10 petere Exp $
1313
*
1414
* HISTORY
1515
* AUTHOR DATE MAJOR EVENT
1616
* Andrew Yu Nov 2, 1994 file creation
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
20+
#include "postgres.h"
21+
2022
#include <ctype.h>
2123
#include <errno.h>
2224

23-
#include "postgres.h"
24-
2525
#include "nodes/pg_list.h"
2626
#include "nodes/readfuncs.h"
2727

@@ -184,7 +184,7 @@ debackslash(char *token, int length)
184184
* nodeTokenType -
185185
* returns the type of the node token contained in token.
186186
* It returns one of the following valid NodeTags:
187-
* T_Integer, T_Float, T_String
187+
* T_Integer, T_Float, T_String, T_BitString
188188
* and some of its own:
189189
* RIGHT_PAREN, LEFT_PAREN, PLAN_SYM, AT_SYMBOL, ATOM_TOKEN
190190
*
@@ -236,6 +236,8 @@ nodeTokenType(char *token, int length)
236236
retval = AT_SYMBOL;
237237
else if (*token == '\"' && length > 1 && token[length - 1] == '\"')
238238
retval = T_String;
239+
else if (*token == 'B')
240+
retval = T_BitString;
239241
else
240242
retval = ATOM_TOKEN;
241243
return retval;
@@ -346,6 +348,15 @@ nodeRead(bool read_car_only)
346348
this_value = (Node *) makeString(debackslash(token + 1, tok_len - 2));
347349
make_dotted_pair_cell = true;
348350
break;
351+
case T_BitString:
352+
{
353+
char * val = palloc(tok_len);
354+
/* skip leading 'B'*/
355+
strncpy(val, token + 1, tok_len - 1);
356+
val[tok_len - 1] = '\0';
357+
this_value = (Node *) makeBitString(val);
358+
break;
359+
}
349360
default:
350361
elog(ERROR, "nodeRead: Bad type %d", type);
351362
this_value = NULL; /* keep compiler happy */

src/backend/parser/gram.y

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.201 2000/10/29 16:11:33 petere Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.202 2000/10/31 10:22:10 petere Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -353,7 +353,7 @@ static void doNegateFloat(Value *v);
353353
%token UNIONJOIN
354354

355355
/* Special keywords, not in the query language - see the "lex" file */
356-
%token <str> IDENT, FCONST, SCONST, Op
356+
%token <str> IDENT, FCONST, SCONST, BITCONST, Op
357357
%token <ival> ICONST, PARAM
358358

359359
/* these are not real. they are here so that they get generated as #define's*/
@@ -1798,6 +1798,10 @@ TriggerFuncArg: ICONST
17981798
{
17991799
$$ = makeString($1);
18001800
}
1801+
| BITCONST
1802+
{
1803+
$$ = makeString($1);
1804+
}
18011805
| ColId
18021806
{
18031807
$$ = makeString($1);
@@ -4786,17 +4790,20 @@ c_expr: attr
47864790
}
47874791
| POSITION '(' position_list ')'
47884792
{
4793+
/* position(A in B) is converted to position(B, A) */
47894794
FuncCall *n = makeNode(FuncCall);
4790-
n->funcname = "strpos";
4795+
n->funcname = "position";
47914796
n->args = $3;
47924797
n->agg_star = FALSE;
47934798
n->agg_distinct = FALSE;
47944799
$$ = (Node *)n;
47954800
}
47964801
| SUBSTRING '(' substr_list ')'
47974802
{
4803+
/* substring(A from B for C) is converted to
4804+
* substring(A, B, C) */
47984805
FuncCall *n = makeNode(FuncCall);
4799-
n->funcname = "substr";
4806+
n->funcname = "substring";
48004807
n->args = $3;
48014808
n->agg_star = FALSE;
48024809
n->agg_distinct = FALSE;
@@ -5201,6 +5208,13 @@ AexprConst: Iconst
52015208
n->val.val.str = $1;
52025209
$$ = (Node *)n;
52035210
}
5211+
| BITCONST
5212+
{
5213+
A_Const *n = makeNode(A_Const);
5214+
n->val.type = T_BitString;
5215+
n->val.val.str = $1;
5216+
$$ = (Node *)n;
5217+
}
52045218
/* This rule formerly used Typename,
52055219
* but that causes reduce conflicts with subscripted column names.
52065220
* Now, separate into ConstTypename and ConstInterval,

src/backend/parser/parse_node.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.47 2000/09/29 18:21:36 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.48 2000/10/31 10:22:11 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -31,6 +31,7 @@
3131
#include "parser/parse_target.h"
3232
#include "parser/parse_type.h"
3333
#include "utils/builtins.h"
34+
#include "utils/varbit.h"
3435
#include "utils/lsyscache.h"
3536
#include "utils/syscache.h"
3637

@@ -473,6 +474,16 @@ make_const(Value *value)
473474
typebyval = false;
474475
break;
475476

477+
case T_BitString:
478+
val = DirectFunctionCall3(zpbit_in,
479+
CStringGetDatum(strVal(value)),
480+
ObjectIdGetDatum(InvalidOid),
481+
Int32GetDatum(-1));
482+
typeid = ZPBITOID;
483+
typelen = -1;
484+
typebyval = false;
485+
break;
486+
476487
default:
477488
elog(NOTICE, "make_const: unknown type %d", nodeTag(value));
478489
/* FALLTHROUGH */

src/backend/parser/scan.l

+18-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/parser/scan.l,v 1.79 2000/10/30 17:54:16 petere Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/parser/scan.l,v 1.80 2000/10/31 10:22:11 petere Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -93,25 +93,25 @@ static void addlit(char *ytext, int yleng);
9393
* We use exclusive states for quoted strings, extended comments,
9494
* and to eliminate parsing troubles for numeric strings.
9595
* Exclusive states:
96-
* <xb> binary numeric string - thomas 1997-11-16
96+
* <xbit> bit string literal
9797
* <xc> extended C-style comments - thomas 1997-07-12
9898
* <xd> delimited identifiers (double-quoted identifiers) - thomas 1997-10-27
9999
* <xh> hexadecimal numeric string - thomas 1997-11-16
100100
* <xq> quoted strings - thomas 1997-07-30
101101
*/
102102

103-
%x xb
103+
%x xbit
104104
%x xc
105105
%x xd
106106
%x xh
107107
%x xq
108108

109-
/* Binary number
109+
/* Bit string
110110
*/
111-
xbstart [bB]{quote}
112-
xbstop {quote}
113-
xbinside [^']+
114-
xbcat {quote}{whitespace_with_newline}{quote}
111+
xbitstart [bB]{quote}
112+
xbitstop {quote}
113+
xbitinside [^']*
114+
xbitcat {quote}{whitespace_with_newline}{quote}
115115

116116
/* Hexadecimal number
117117
*/
@@ -279,30 +279,27 @@ other .
279279

280280
<xc><<EOF>> { elog(ERROR, "Unterminated /* comment"); }
281281

282-
{xbstart} {
283-
BEGIN(xb);
282+
{xbitstart} {
283+
BEGIN(xbit);
284284
startlit();
285285
}
286-
<xb>{xbstop} {
287-
char* endptr;
288-
286+
<xbit>{xbitstop} {
289287
BEGIN(INITIAL);
290-
errno = 0;
291-
yylval.ival = strtol(literalbuf, &endptr, 2);
292-
if (*endptr != '\0' || errno == ERANGE)
293-
elog(ERROR, "Bad binary integer input '%s'",
288+
if (literalbuf[strspn(literalbuf, "01")] != '\0')
289+
elog(ERROR, "invalid bit string input: '%s'",
294290
literalbuf);
295-
return ICONST;
291+
yylval.str = literalbuf;
292+
return BITCONST;
296293
}
297294
<xh>{xhinside} |
298-
<xb>{xbinside} {
295+
<xbit>{xbitinside} {
299296
addlit(yytext, yyleng);
300297
}
301298
<xh>{xhcat} |
302-
<xb>{xbcat} {
299+
<xbit>{xbitcat} {
303300
/* ignore */
304301
}
305-
<xb><<EOF>> { elog(ERROR, "Unterminated binary integer"); }
302+
<xbit><<EOF>> { elog(ERROR, "unterminated bit string literal"); }
306303

307304
{xhstart} {
308305
BEGIN(xh);

0 commit comments

Comments
 (0)