Skip to content

Instantly share code, notes, and snippets.

@ograycode
Created October 13, 2015 17:44
Show Gist options
  • Select an option

  • Save ograycode/5c53e099272dc16e65d4 to your computer and use it in GitHub Desktop.

Select an option

Save ograycode/5c53e099272dc16e65d4 to your computer and use it in GitHub Desktop.
/*
* If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
* semantics from ORDER BY items that match DISTINCT ON items, and also
* adopt their column sort order. We insist that the distinctClause and
* sortClause match, so throw error if we find the need to add any more
* distinctClause items after we've skipped an ORDER BY item that wasn't
* in DISTINCT ON.
*/
skipped_sortitem = false;
foreach(lc, sortClause)
{
SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
{
if (skipped_sortitem)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
parser_errposition(pstate,
get_matching_location(scl->tleSortGroupRef,
sortgrouprefs,
distinctlist))));
else
result = lappend(result, copyObject(scl));
}
else
skipped_sortitem = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment