static void deparseWindowDef()

in ext/pg_query/pg_query_deparse.c [2379:2481]


static void deparseWindowDef(StringInfo str, WindowDef* window_def)
{
	ListCell *lc;

	// The parent node is responsible for outputting window_def->name

	appendStringInfoChar(str, '(');

	if (window_def->refname != NULL)
	{
		appendStringInfoString(str, quote_identifier(window_def->refname));
		appendStringInfoChar(str, ' ');
	}

	if (list_length(window_def->partitionClause) > 0)
	{
		appendStringInfoString(str, "PARTITION BY ");
		deparseExprList(str, window_def->partitionClause);
		appendStringInfoChar(str, ' ');
	}

	deparseOptSortClause(str, window_def->orderClause);

	if (window_def->frameOptions & FRAMEOPTION_NONDEFAULT)
	{
		if (window_def->frameOptions & FRAMEOPTION_RANGE)
			appendStringInfoString(str, "RANGE ");
		else if (window_def->frameOptions & FRAMEOPTION_ROWS)
			appendStringInfoString(str, "ROWS ");
		else if (window_def->frameOptions & FRAMEOPTION_GROUPS)
			appendStringInfoString(str, "GROUPS ");
	
		if (window_def->frameOptions & FRAMEOPTION_BETWEEN)
			appendStringInfoString(str, "BETWEEN ");

		// frame_start
		if (window_def->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
		{
			appendStringInfoString(str, "UNBOUNDED PRECEDING ");
		}
		else if (window_def->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
		{
			Assert(false); // disallowed
		}
		else if (window_def->frameOptions & FRAMEOPTION_START_CURRENT_ROW)
		{
			appendStringInfoString(str, "CURRENT ROW ");
		}
		else if (window_def->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
		{
			Assert(window_def->startOffset != NULL);
			deparseExpr(str, window_def->startOffset);
			appendStringInfoString(str, " PRECEDING ");
		}
		else if (window_def->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
		{
			Assert(window_def->startOffset != NULL);
			deparseExpr(str, window_def->startOffset);
			appendStringInfoString(str, " FOLLOWING ");
		}

		if (window_def->frameOptions & FRAMEOPTION_BETWEEN)
		{
			appendStringInfoString(str, "AND ");

			// frame_end
			if (window_def->frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
			{
				Assert(false); // disallowed
			}
			else if (window_def->frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING)
			{
				appendStringInfoString(str, "UNBOUNDED FOLLOWING ");
			}
			else if (window_def->frameOptions & FRAMEOPTION_END_CURRENT_ROW)
			{
				appendStringInfoString(str, "CURRENT ROW ");
			}
			else if (window_def->frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING)
			{
				Assert(window_def->endOffset != NULL);
				deparseExpr(str, window_def->endOffset);
				appendStringInfoString(str, " PRECEDING ");
			}
			else if (window_def->frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING)
			{
				Assert(window_def->endOffset != NULL);
				deparseExpr(str, window_def->endOffset);
				appendStringInfoString(str, " FOLLOWING ");
			}
		}

		if (window_def->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
			appendStringInfoString(str, "EXCLUDE CURRENT ROW ");
		else if (window_def->frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
			appendStringInfoString(str, "EXCLUDE GROUP ");
		else if (window_def->frameOptions & FRAMEOPTION_EXCLUDE_TIES)
			appendStringInfoString(str, "EXCLUDE TIES ");
	}

	removeTrailingSpace(str);
	appendStringInfoChar(str, ')');
}