Thread: Changing a relation's name in parser stage

Changing a relation's name in parser stage

From
"Matthias Lenz"
Date:
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 alter the 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

Re: Changing a relation's name in parser stage

From
Tom Lane
Date:
"Matthias Lenz" <ml134883@inf.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

Re: Changing a relation's name in parser stage

From
"Matthias Lenz"
Date:
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@sss.pgh.pa.us>
To: "Matthias Lenz" <ml134883@inf.tu-dresden.de>
Cc: <pgsql-novice@postgresql.org>
Sent: Tuesday, May 11, 2004 1:43 AM
Subject: Re: [NOVICE] Changing a relation's name in parser stage


> "Matthias Lenz" <ml134883@inf.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