* row_is_in_frame * Determine whether a row is in the current row's window frame according * to our window framing rule * * The caller must have already determined that the row is in the partition * and fetched it into a slot. This function just encapsulates the framing * rules. * * Returns: * -1, if the row is out of frame and no succeeding rows can be in frame * 0, if the row is out o
| 1621 | * May clobber winstate->temp_slot_2. |
| 1622 | */ |
| 1623 | static int |
| 1624 | row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot) |
| 1625 | { |
| 1626 | int frameOptions = winstate->frameOptions; |
| 1627 | |
| 1628 | compute_start_end_offsets(winstate); |
| 1629 | |
| 1630 | Assert(pos >= 0); /* else caller error */ |
| 1631 | |
| 1632 | /* |
| 1633 | * First, check frame starting conditions. We might as well delegate this |
| 1634 | * to update_frameheadpos always; it doesn't add any notable cost. |
| 1635 | */ |
| 1636 | update_frameheadpos(winstate); |
| 1637 | if (pos < winstate->frameheadpos) |
| 1638 | return 0; |
| 1639 | |
| 1640 | /* |
| 1641 | * Okay so far, now check frame ending conditions. Here, we avoid calling |
| 1642 | * update_frametailpos in simple cases, so as not to spool tuples further |
| 1643 | * ahead than necessary. |
| 1644 | */ |
| 1645 | if (frameOptions & FRAMEOPTION_END_CURRENT_ROW) |
| 1646 | { |
| 1647 | if (frameOptions & FRAMEOPTION_ROWS) |
| 1648 | { |
| 1649 | /* rows after current row are out of frame */ |
| 1650 | if (pos > winstate->currentpos) |
| 1651 | return -1; |
| 1652 | } |
| 1653 | else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS)) |
| 1654 | { |
| 1655 | /* following row that is not peer is out of frame */ |
| 1656 | if (pos > winstate->currentpos && |
| 1657 | !are_peers(winstate, slot, winstate->ss.ss_ScanTupleSlot)) |
| 1658 | return -1; |
| 1659 | } |
| 1660 | else |
| 1661 | Assert(false); |
| 1662 | } |
| 1663 | else if (frameOptions & FRAMEOPTION_END_OFFSET) |
| 1664 | { |
| 1665 | if (frameOptions & FRAMEOPTION_ROWS) |
| 1666 | { |
| 1667 | int64 offset = DatumGetInt64(winstate->endOffsetValue); |
| 1668 | |
| 1669 | /* rows after current row + offset are out of frame */ |
| 1670 | if (frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING) |
| 1671 | offset = -offset; |
| 1672 | |
| 1673 | if (pos > winstate->currentpos + offset) |
| 1674 | return -1; |
| 1675 | } |
| 1676 | else if (frameOptions & (FRAMEOPTION_RANGE | FRAMEOPTION_GROUPS)) |
| 1677 | { |
| 1678 | /* hard cases, so delegate to update_frametailpos */ |
| 1679 | update_frametailpos(winstate); |
| 1680 | if (pos >= winstate->frametailpos) |
no test coverage detected