dbTalk Databases Forums  

Changing a relation's name in parser stage

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Changing a relation's name in parser stage in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Matthias Lenz
 
Posts: n/a

Default Changing a relation's name in parser stage - 05-10-2004 , 09:57 AM






I have to rewrite a query after beeing parsed by gram.y but before any transformations are applied to my SelectStmt structure in analyze.c. Therefore I wrote a function that creates a new SelectStmt structure, because I need some UNIONs in the rewritten query. Most of the sub strucure of the original query stays relevant, e.g. the GroupByClause, TargetClause and is assigned to the sub structure of the new SelectStmt structure. But I have to alterthe relation names in the FromClause. From_clause is a list of RangeVar nodes I think, generated by the gram.y. And one attribute of the RangeVar node is relname, of type char *. How to alter the relname. Why they use char *, I want to assign something like "relation_x".

the following leads to a crash of psql, without these lines (List... "s_overall"); the modification function on the ApproxStmt (my own type of SelectStmt) works fine, but I need to change the relname!

[in anylze.c]

static SelectStmt *modifyApproxStmt(ApproxStmt *stmt)
{

List *i;
i=nth(1, stmt->fromClause);
((RangeVar *) lfirst(i))->relname = "s_overall";

....
....
}

Any ideas? It's puzzling me!
Matthew

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: Changing a relation's name in parser stage - 05-10-2004 , 06:43 PM






"Matthias Lenz" <ml134883 (AT) inf (DOT) tu-dresden.de> writes:
Quote:
List *i;
i=nth(1, stmt->fromClause);
((RangeVar *) lfirst(i))->relname = "s_overall";
nth() is going to give you a list element, not a List*. So you don't
want the lfirst() call. I'm also dubious about whether you didn't mean
nth(0, stmt->fromClause) --- nth() counts from zero not one. Finally,
it seems extremely fragile to assume that fromClause entries are
necessarily RangeVars without checking. There are other node types that
could be there.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #3  
Old   
Matthias Lenz
 
Posts: n/a

Default Re: Changing a relation's name in parser stage - 05-13-2004 , 06:10 AM



Hello,

Firstly, thanks to Tom Lane! I mixed up the definitions of lfirst() and
nth() relating to "i". Now, I also create a new structure below
stmt->FromClause filling up with RangeVars!

But there's another problem. I try to get the second part of new relations'
names (evey name is a composite of s_GroupAttrName) from the GroupClause
via:

ch2=((Value *) nth(0,(((ColumnRef *) nth((i/2),
stmt->groupClause))->fields)))->val.str;

that works fine, i is a running variable, decreasing in steps of one from
(2*length(stmt->GroupClause))-1. Then I have to append the "String" pointed
to to another prepared one:

((RangeVar *) nth(0, arg[i]->fromClause))->relname = strncat(t[i],ch2,1);

that works fine, too! By the way, char *ch2.

BUT, I have to append all the grouping attributes' names to as many "s_". So
i thought using char *ch[10] would by a nice idea.

ch[i]=((Value *) nth(0,(((ColumnRef *) nth((i/2),
stmt->groupClause))->fields)))->val.str;
((RangeVar *) nth(0, arg[i]->fromClause))->relname = strncat(t[i],ch[i],1);

But I only get answers like relation "nnxa>nl" does not exist. There should
be a problem with adresses, but where?

Parts of the non working code:

static SelectStmt *modifyApproxStmt(ApproxStmt *stmt)
{
SelectStmt *arg[8];
char *ch[10];
int count,i;
char t[][4]={"s_","s_","s_"};

count=length(stmt->groupClause);
i=(2*count)-1;


while(i+1>0)
{
if (i==(2*count)-1)
{
arg[i] = makeNode(SelectStmt);
arg[i]->op = SETOP_NONE;
arg[i]->all = FALSE;
arg[i]->targetList = stmt->targetList;
arg[i]->fromClause = makeList1(makeNode(RangeVar));
((RangeVar *) nth(0, arg[i]->fromClause))->inhOpt = INH_DEFAULT;
((RangeVar *) nth(0, arg[i]->fromClause))->alias = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->schemaname = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->relname = "s_overall";
arg[i]->groupClause = stmt->groupClause;
};

if ((i % 2) ==0)
{
arg[i] = makeNode(SelectStmt);
arg[i]->op = SETOP_NONE;
arg[i]->all = FALSE;
arg[i]->targetList = stmt->targetList;
arg[i]->fromClause = makeList1(makeNode(RangeVar));
((RangeVar *) nth(0, arg[i]->fromClause))->inhOpt = INH_DEFAULT;
((RangeVar *) nth(0, arg[i]->fromClause))->alias = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->schemaname = NULL;
ch[i]=((Value *) nth(0,(((ColumnRef *) nth((i/2),
stmt->groupClause))->fields)))->val.str;
((RangeVar *) nth(0, arg[i]->fromClause))->relname =
strncat(t[i],ch[i],1);
arg[i]->groupClause = stmt->groupClause;
};
....

working code:

static SelectStmt *modifyApproxStmt(ApproxStmt *stmt)
{
SelectStmt *arg[8];
char *ch,*ch2;
int count,i;
char t[][4]={"s_","s_","s_"};
/*char u[2][2];
ch=&u[0];
ch2=&u[1];*/

count=length(stmt->groupClause);
i=(2*count)-1;
/*printf(count);*/

while(i+1>0)
{
if (i==(2*count)-1)
{
arg[i] = makeNode(SelectStmt);
arg[i]->op = SETOP_NONE;
arg[i]->all = FALSE;
arg[i]->targetList = stmt->targetList;
arg[i]->fromClause = makeList1(makeNode(RangeVar));
((RangeVar *) nth(0, arg[i]->fromClause))->inhOpt = INH_DEFAULT;
((RangeVar *) nth(0, arg[i]->fromClause))->alias = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->schemaname = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->relname = "s_overall";
arg[i]->groupClause = stmt->groupClause;
};

if ((i % 2) ==0)
{
arg[i] = makeNode(SelectStmt);
arg[i]->op = SETOP_NONE;
arg[i]->all = FALSE;
arg[i]->targetList = stmt->targetList;
arg[i]->fromClause = makeList1(makeNode(RangeVar));
((RangeVar *) nth(0, arg[i]->fromClause))->inhOpt = INH_DEFAULT;
((RangeVar *) nth(0, arg[i]->fromClause))->alias = NULL;
((RangeVar *) nth(0, arg[i]->fromClause))->schemaname = NULL;
if (i==2) {
ch2=((Value *) nth(0,(((ColumnRef *) nth((i/2),
stmt->groupClause))->fields)))->val.str;
((RangeVar *) nth(0, arg[i]->fromClause))->relname = strncat(t[i],ch2,1);
}
else {
ch=((Value *) nth(0,(((ColumnRef *) nth((i/2),
stmt->groupClause))->fields)))->val.str;
((RangeVar *) nth(0, arg[i]->fromClause))->relname = strncat(t[i],ch,1);
};
arg[i]->groupClause = stmt->groupClause;
};
.....

Thank you for ideas!


----- Original Message -----
From: "Tom Lane" <tgl (AT) sss (DOT) pgh.pa.us>
To: "Matthias Lenz" <ml134883 (AT) inf (DOT) tu-dresden.de>
Cc: <pgsql-novice (AT) postgresql (DOT) org>
Sent: Tuesday, May 11, 2004 1:43 AM
Subject: Re: [NOVICE] Changing a relation's name in parser stage


Quote:
"Matthias Lenz" <ml134883 (AT) inf (DOT) tu-dresden.de> writes:
List *i;
i=nth(1, stmt->fromClause);
((RangeVar *) lfirst(i))->relname = "s_overall";

nth() is going to give you a list element, not a List*. So you don't
want the lfirst() call. I'm also dubious about whether you didn't mean
nth(0, stmt->fromClause) --- nth() counts from zero not one. Finally,
it seems extremely fragile to assume that fromClause entries are
necessarily RangeVars without checking. There are other node types that
could be there.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.