1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-29 10:41:53 +03:00
I have attached a patch to allow GROUP BY and/or ORDER BY function or
expressions.  Note worthy items:

1. The expression or function need not be in the target list.
Example:
            SELECT  name FROM foo GROUP BY lower(name);

2.   Simplified the grammar to use expressions only.

3.  Cleaned up earlier patch in this area to make use of existing
utility functions.

3.  Reduced some of the members in the SortGroupBy parse node.   The
original data members were redundant with the new expression node.
(MUST do a "make clean" now)

4.  Added a new parse node "JoinUsing".   The JOIN USING clause was
overloading this SortGroupBy structure.   With the afore mentioned
reduction of members, the two clauses lost all their commonality.

5.  A bug still exist where, if a function or expression is GROUPed BY,
and an aggregate function does not include a attribute from the
expression or function, the backend crashes.   (or something like
that)   The bug pre-dates this patch.    Example:

    SELECT lower(a) AS lowcase, count(b) FROM foo GROUP BY lowcase;
                 *** BOOM  ***

    --Also when not in target list
    SELECT  count(b) FROM foo GROUP BY lower(a);
                *** BOOM  AGAIN ***
This commit is contained in:
Marc G. Fournier
1998-08-05 04:49:19 +00:00
parent 186aeb1d67
commit a1627a1d64
8 changed files with 438 additions and 198 deletions

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.19 1998/07/20 19:53:52 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.20 1998/08/05 04:49:11 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
@ -32,11 +32,7 @@
static List *ExpandAllTables(ParseState *pstate);
static char *FigureColname(Node *expr, Node *resval);
static TargetEntry *
MakeTargetlistExpr(ParseState *pstate,
char *colname,
Node *expr,
List *arrayRef);
Node *
SizeTargetExpr(ParseState *pstate,
Node *expr,
@ -129,7 +125,7 @@ printf("transformTargetIdent- transform type %d to %d\n",
{
expr = coerce_type(pstate, node, attrtype_id, attrtype_target);
expr = transformExpr(pstate, expr, EXPR_COLUMN_FIRST);
tent = MakeTargetlistExpr(pstate, *resname, expr, FALSE);
tent = MakeTargetlistExpr(pstate, *resname, expr, FALSE, FALSE);
expr = tent->expr;
}
else
@ -293,7 +289,7 @@ printf("transformTargetList: decode T_Expr\n");
constval->val.str = save_str;
tent = MakeTargetlistExpr(pstate, res->name,
(Node *) make_const(constval),
NULL);
NULL, FALSE);
pfree(save_str);
}
else
@ -326,7 +322,7 @@ printf("transformTargetList: decode T_Expr\n");
}
res->name = colname;
tent = MakeTargetlistExpr(pstate, res->name, expr,
res->indirection);
res->indirection, FALSE);
}
break;
}
@ -570,12 +566,17 @@ printf("SizeTargetExpr: no conversion function for sizing\n");
* For type mismatches between expressions and targets, use the same
* techniques as for function and operator type coersion.
* - thomas 1998-05-08
*
* Added resjunk flag and made extern so that it can be use by GROUP/
* ORDER BY a function or expersion not in the target_list
* - daveh@insightdist.com 1998-07-31
*/
static TargetEntry *
TargetEntry *
MakeTargetlistExpr(ParseState *pstate,
char *colname,
Node *expr,
List *arrayRef)
List *arrayRef,
int16 resjunk)
{
Oid type_id,
attrtype;
@ -698,7 +699,7 @@ printf("MakeTargetlistExpr: attrtypmod is %d\n", (int4) attrtypmod);
colname,
(Index) 0,
(Oid) 0,
0);
resjunk);
tent = makeTargetEntry(resnode, expr);