Skip to content

Commit

Permalink
Support SELECT FROM with no target attribute, especially in UNIONs
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreSenellart committed Oct 2, 2024
1 parent 8d4b059 commit 758c731
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/provsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static List *get_provenance_attributes(const constants_t *constants, Query *q)
}
} else if(r->rtekind == RTE_SUBQUERY) {
bool *inner_removed = NULL;
int old_targetlist_length=r->subquery->targetList->length;
int old_targetlist_length=r->subquery->targetList?r->subquery->targetList->length:0;
Query *new_subquery = process_query(constants, r->subquery, &inner_removed);
if(new_subquery != NULL) {
int i=0;
Expand Down Expand Up @@ -1177,16 +1177,24 @@ static Query *rewrite_non_all_into_external_group_by(Query *q)
new_query->jointree = jointree;
new_query->targetList = copyObject(q->targetList);

foreach (lc, new_query->targetList)
{
TargetEntry *te = (TargetEntry *)lfirst(lc);
SortGroupClause *sgc = makeNode(SortGroupClause);
if(new_query->targetList) {
foreach (lc, new_query->targetList)
{
TargetEntry *te = (TargetEntry *)lfirst(lc);
SortGroupClause *sgc = makeNode(SortGroupClause);

sgc->tleSortGroupRef = te->ressortgroupref = ++sortgroupref;
sgc->tleSortGroupRef = te->ressortgroupref = ++sortgroupref;

get_sort_group_operators(exprType((Node *)te->expr), false, true, false, &sgc->sortop, &sgc->eqop, NULL, &sgc->hashable);
get_sort_group_operators(exprType((Node *)te->expr), false, true, false, &sgc->sortop, &sgc->eqop, NULL, &sgc->hashable);

new_query->groupClause = lappend(new_query->groupClause, sgc);
new_query->groupClause = lappend(new_query->groupClause, sgc);
}
} else {
GroupingSet *gs = makeNode(GroupingSet);
gs->kind=GROUPING_SET_EMPTY;
gs->content=0;
gs->location=-1;
new_query->groupingSets = list_make1(gs);
}

return new_query;
Expand Down
6 changes: 6 additions & 0 deletions test/expected/union.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ confidential|(Dave ⊕ Dave)
secret|(Ellen ⊕ Susan)
top_secret|(Magdalen ⊕ Magdalen)
(5 rows)
remove_provenance

(1 row)
formula
(Dave ⊕ Dave ⊕ Ellen ⊕ John ⊕ Magdalen ⊕ Magdalen ⊕ Nancy ⊕ Nancy ⊕ Paul ⊕ Susan)
(1 row)
11 changes: 11 additions & 0 deletions test/sql/union.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ SELECT *,formula(provenance(),'personnel_name') FROM (
SELECT remove_provenance('union_result');
SELECT * FROM union_result ORDER BY classification;
DROP TABLE union_result;

CREATE TABLE union_result AS
SELECT *,formula(provenance(),'personnel_name') FROM (
SELECT FROM personnel WHERE city='Paris'
UNION
SELECT FROM personnel
) t;

SELECT remove_provenance('union_result');
SELECT * FROM union_result;
DROP TABLE union_result;

0 comments on commit 758c731

Please sign in to comment.