public static WindowFrameDef wdwFrame(int p, int f) { BoundaryDef start, end; if (p == 0) { start = new BoundaryDef(Direction.CURRENT, 0); } else { start = new BoundaryDef(Direction.PRECEDING, p); } if (f == 0) { end = new BoundaryDef(Direction.CURRENT, 0); } else { end = new BoundaryDef(Direction.FOLLOWING, f); } return new WindowFrameDef(WindowType.ROWS, start, end); }
public boolean isUnbounded() { return this.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT; }
public WindowFrameDef(WindowType windowType, BoundaryDef start, BoundaryDef end) { this.windowType = windowType; this.start = start; this.end = end; // Calculate window size if (start.getDirection() == end.getDirection()) { windowSize = Math.abs(end.getAmt() - start.getAmt()) + 1; } else { windowSize = end.getAmt() + start.getAmt() + 1; } }
public int compareTo(BoundaryDef other) { int c = getDirection().compareTo(other.getDirection()); if (c != 0) { return c; } return this.direction == Direction.PRECEDING ? other.amt - this.amt : this.amt - other.amt; }
/** * Gets the next row index that the data within the window are available and can be processed * @param wFrameDef * @return */ public int rowToProcess(WindowFrameDef wFrameDef) { int rowToProcess = numRowsReceived - 1 - Math.max(0, wFrameDef.getEnd().getRelativeOffset()); return rowToProcess >= 0 ? rowToProcess : -1; }
BoundaryDef end = wdwFrame.getEnd(); if (wdwFrame.getWindowType() == WindowType.ROWS) { if (!end.isUnbounded() && !start.isUnbounded()) { startPos = Math.min(startPos, wdwFrame.getStart().getRelativeOffset()); endPos = Math.max(endPos, wdwFrame.getEnd().getRelativeOffset()); continue;
public boolean isStartUnbounded() { return start.isUnbounded(); }
if (!windowFrameDef.getEnd().isCurrentRow()) { setOperatorIssue(functionName + " only CURRENT ROW end frame is supported for RANGE"); return false; (windowFrameDef.getEnd().isCurrentRow() && (supportedFunctionType == SupportedFunctionType.AVG || supportedFunctionType == SupportedFunctionType.MAX ||
public int compareTo(BoundaryDef other) { int c = getDirection().compareTo(other.getDirection()); if (c != 0) { return c; } return this.direction == Direction.PRECEDING ? other.amt - this.amt : this.amt - other.amt; }
/** * Gets the next row index that the data within the window are available and can be processed * @param wFrameDef * @return */ public int rowToProcess(WindowFrameDef wFrameDef) { int rowToProcess = numRowsReceived - 1 - Math.max(0, wFrameDef.getEnd().getRelativeOffset()); return rowToProcess >= 0 ? rowToProcess : -1; }
BoundaryDef end = wdwFrame.getEnd(); if (wdwFrame.getWindowType() == WindowType.ROWS) { if (!end.isUnbounded() && !start.isUnbounded()) { startPos = Math.min(startPos, wdwFrame.getStart().getRelativeOffset()); endPos = Math.max(endPos, wdwFrame.getEnd().getRelativeOffset()); continue;
public boolean isEndUnbounded() { return end.isUnbounded(); }
windowFrameDef.getEnd().isCurrentRow());
public boolean isUnbounded() { return this.getAmt() == BoundarySpec.UNBOUNDED_AMOUNT; }
public WindowFrameDef(WindowType windowType, BoundaryDef start, BoundaryDef end) { this.windowType = windowType; this.start = start; this.end = end; // Calculate window size if (start.getDirection() == end.getDirection()) { windowSize = Math.abs(end.getAmt() - start.getAmt()) + 1; } else { windowSize = end.getAmt() + start.getAmt() + 1; } }
@Override public int computeStart(int rowIdx, PTFPartition p) throws HiveException { switch(start.getDirection()) { case PRECEDING: return computeStartPreceding(rowIdx, p); case CURRENT: return computeStartCurrentRow(rowIdx, p); case FOLLOWING: default: return computeStartFollowing(rowIdx, p); } }
@Override public Object terminate(AggregationBuffer agg) throws HiveException { State s = (State) agg; Object[] r = s.maxChain.isEmpty() ? null : s.maxChain.getFirst(); // After all the rows are processed, continue to generate results for the rows that results haven't generated. // For the case: X following and Y following, process first Y-X results and then insert X nulls. // For the case X preceding and Y following, process Y results. for (int i = Math.max(0, wFrameDef.getStart().getRelativeOffset()); i < wFrameDef.getEnd().getRelativeOffset(); i++) { if (s.hasResultReady()) { s.results.add(r == null ? null : r[0]); } s.numRows++; if (r != null) { int fIdx = (Integer) r[1]; if (!wFrameDef.isStartUnbounded() && s.numRows >= fIdx + wFrameDef.getWindowSize() && !s.maxChain.isEmpty()) { s.maxChain.removeFirst(); r = !s.maxChain.isEmpty() ? s.maxChain.getFirst() : null; } } } for (int i = 0; i < wFrameDef.getStart().getRelativeOffset(); i++) { if (s.hasResultReady()) { s.results.add(null); } s.numRows++; } return null; }
private WindowFrameDef translate(ShapeDetails inpShape, WindowFrameSpec spec, List<OrderExpression> orderExpressions) throws SemanticException { if (spec == null) { return null; } BoundarySpec s = spec.getStart(); BoundarySpec e = spec.getEnd(); int cmp = s.compareTo(e); if (cmp > 0) { throw new SemanticException(String.format( "Window range invalid, start boundary is greater than end boundary: %s", spec)); } WindowFrameDef winFrame = new WindowFrameDef( spec.getWindowType(), new BoundaryDef(s.direction, s.getAmt()), new BoundaryDef(e.direction, e.getAmt())); if (winFrame.getWindowType() == WindowType.RANGE) { winFrame.setOrderDef(buildOrderExpressions(inpShape, orderExpressions)); } return winFrame; }
public boolean isEndUnbounded() { return end.isUnbounded(); }