Re: Changing a relation's name in parser stage - Mailing list pgsql-novice

From Matthias Lenz
Subject Re: Changing a relation's name in parser stage
Date
Msg-id 000601c438da$dc694910$9100a8c0@noctuaeeprwinl
Whole thread Raw
In response to Changing a relation's name in parser stage  ("Matthias Lenz" <ml134883@inf.tu-dresden.de>)
List pgsql-novice
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


pgsql-novice by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: ORDER BY and Unicode
Next
From: "Rajan Bhide"
Date:
Subject: Should I keep INDEX on the table?