Skip to content

Instantly share code, notes, and snippets.

@krk
Created June 27, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krk/d7eccc3ddaf27b52bbd342f3bc98d05d to your computer and use it in GitHub Desktop.
Save krk/d7eccc3ddaf27b52bbd342f3bc98d05d to your computer and use it in GitHub Desktop.
else if(linitial(stmt->correspondingClause) == NULL)
{
// CORRESPONDING clause, find matching column names from both tables. If there are none then it is a syntax error.
Query *largQuery;
Query *rargQuery;
List *matchingColumns;
/* Analyze left query to resolve column names. */
largQuery = parse_sub_analyze((Node *) stmt->larg, pstate, NULL, false);
/* Analyze right query to resolve column names. */
rargQuery = parse_sub_analyze((Node *) stmt->rarg, pstate, NULL, false);
/* Find matching columns from both queries. */
matchingColumns = determineMatchingColumns(largQuery->targetList,
rargQuery->targetList);
op->correspondingColumns = matchingColumns;
op->hasCorrespondingBy = false;
/* If matchingColumns is empty, there is an error. At least one column in the select lists must have the same name. */
if(list_length(matchingColumns) == 0)
{
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s queries with a CORRESPONDING clause must have at least one column with the same name",
context)));
}
// Create subquery for larg, selecting column names from matchingColumns.
stmt->larg = createSubqueryForCorresponding(matchingColumns, stmt->larg);
// Assign newly generated query to original left query.
op->larg = transformSetOperationTree(pstate, stmt->larg,
false,
<argetlist);
// Create subquery for rarg, selecting column names from matchingColumns.
stmt->rarg = createSubqueryForCorresponding(matchingColumns, stmt->rarg);
// Assign newly generated query to original right query.
op->rarg = transformSetOperationTree(pstate, stmt->rarg,
false,
&rtargetlist);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment