ggangadharan opened a new pull request, #6997:
URL: https://github.com/apache/hive/pull/6997

   ### What changes were proposed in this pull request?
   
   HIVE-25123 asks for vectorized lead/lag evaluators that stream their results 
where possible. Today both are buffered evaluators: every row goes through 
`runOnRange`, and each value is boxed and stored in `PartitionResults` before 
it is unboxed into the output column.
   
   **Evaluators** (package `org.apache.hadoop.hive.ql.exec.vector.ptf`)
   
   ```
   VectorPTFEvaluatorBase
   └── VectorPTFEvaluatorAbstractLeadLag   amt, typed default value, bulk copy 
helpers
       ├── VectorPTFEvaluatorLag           pure streaming
       └── VectorPTFEvaluatorLead          group-aggregated streaming (like 
cume_dist / ntile)
   ```
   
   - **`lag()` is a pure streaming evaluator.** It writes its output while 
batches are streamed. It keeps only the last `amt` input values of the 
partition in a ring buffer, which is allocated lazily and capped at `amt` rows, 
so it can look back into earlier batches. Each batch is handled in three parts:
     - rows with no row `amt` back get the default value;
     - rows whose lag row is in an earlier batch read the ring buffer;
     - rows whose lag row is in the same batch are filled with one 
`System.arraycopy`.
   
     When every window function of the PTF streams (lag, row_number, rank, 
first_value, running sum/min/max/avg), the partition is no longer buffered or 
spilled.
   - **`lead()` cannot stream, because it needs rows that arrive later.** While 
the partition is buffered, it collects its input values into a primitive column 
vector through a new hook, `VectorPTFEvaluatorBase#onBufferGroupBatch`. It 
skips the first `amt` rows, which are nobody's lead. When the batches are 
forwarded, it writes its results with at most two bulk copies per batch.
   - Both evaluators support LONG, DOUBLE and DECIMAL column vectors, so 
tinyint, smallint, int, bigint, boolean, date, float, double and decimal 
arguments are covered. Copies are type-specialised, with no per-row allocation 
or boxing.
   
   **Default value (3rd argument)**
   
   - A constant default is converted to the type of the first argument at 
compile time in `Vectorizer`. It uses the same `ObjectInspectorConverters` 
conversion as row mode (`GenericUDAFLeadLag`), and a missing default means NULL.
   - A default of the same type as the first argument may be a column or an 
expression. This includes a DECIMAL_64 column, which is wrapped in 
`ConvertDecimal64ToDecimal`. The default is evaluated on every incoming batch 
together with the input expressions, before the batch is buffered. This uses 
the new `VectorPTFEvaluatorBase#getInputVecExprs`, which `VectorPTFOperator` 
uses for evaluation, transient init and buffering of the scratch columns.
   - Any other default, such as a column or expression of another type, is not 
vectorized. It shows the reason `lag default value must be a constant or have 
the same type as its first argument`.
   
   **Other changes**
   
   - `Vectorizer`: lag/lead are exempt from the "argument references 
partition-only column" restriction, like ntile. They read their arguments only 
from incoming or forwarded batches, and both carry the partition columns.
   - `VectorPTFOperator#process`: the evaluator reset after the last group 
batch of a PARTITION BY-only window now runs only when all evaluators stream. 
Buffered evaluators are reset in `cleanupPartition` after the partition is 
forwarded. Resetting them earlier would drop the values lead collected while 
buffering.
   - `VectorPTFGroupBatches.PartitionResults` no longer allocates a per-row 
`Object[]` for streaming evaluators, since they write into the forwarded 
batches directly. This helps lead/lag as well as cume_dist, percent_rank and 
ntile.
   
   Example:
   ```sql
   -- lag only: no partition buffering any more (EXPLAIN shows 
allEvaluatorsAreStreaming: true)
   select p, idx, lag(i) over w, lag(i, 3, -1) over w, row_number() over w
   from t window w as (partition by p order by idx);
   
   -- lead with a constant default of another type, converted like in row mode
   select p, idx, lead(d, 2, 2) over w, lag(dc, 1, 2.345) over w
   from t window w as (partition by p order by idx);
   ```
   
   ### Why are the changes needed?
   
   - **Performance.** lag no longer forces the whole partition to be buffered, 
and spilled when it is large. lead and lag no longer compute a range, look up 
the row, box the value, store it in `PartitionResults` and unbox it again for 
every row. Both now do a few bulk copies per batch.
   - **Correctness of default values in the old code path:**
     - Every constant default was read with 
`ConstantVectorExpression#getLongValue()`, whatever its type, so constants were 
not converted to the argument type the way row mode does. Examples: 
`lag(double_col, 1, 1.5)`, `lag(dec_col, 1, 2.345)`, a `NULL` default, or a 
string default like `'12'`.
     - A default that is neither an `IdentityExpression` nor a constant threw 
`RuntimeException` at runtime. That includes a DECIMAL_64 column such as 
`lag(dc, 1, dc)` on a `decimal(10,2)` column, which is wrapped in 
`ConvertDecimal64ToDecimal`.
     - A default column of another type was cast to the output type in 
`fillGroupResults`, which can fail with a `ClassCastException`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No change in query results, except that vectorized lead/lag default values 
now match row mode. Other changes are visible in plans:
   - EXPLAIN VECTORIZATION shows lead/lag output columns in `streamingColumns`. 
PTFs whose window functions all stream now show `allEvaluatorsAreStreaming: 
true`.
   - lag/lead whose argument or default is a partition-only column are now 
vectorized.
   - lag/lead whose default is neither a constant nor of the argument's type 
now run in row mode, with the not-vectorized reason above.
   
   ### How was this patch tested?
   
   New qtest `vector_ptf_lead_lag_streaming.q` (TestMiniLlapLocalCliDriver). 
Every scenario runs first with `hive.vectorized.execution.ptf.enabled=false` as 
the row-mode reference, then vectorized, and the results must be identical:
   - **Streaming modes:**
     - lag with other streaming functions, where all evaluators stream;
     - lead only;
     - lag/lead mixed with non-streaming functions (sum, avg with a bounded 
start, cume_dist);
     - S7: an argument or default that is a partition-only column.
   - **Types and defaults:**
     - all supported types with constant defaults that need conversion 
(int/decimal/string/date literals, NULL);
     - same-type column and expression defaults, including DECIMAL_64;
     - expressions inside and around lag/lead.
   - **Windows and ordering:**
     - `amt` of 0, larger than a batch, and larger than the partition;
     - NULLs at the edges of partitions, a single-row partition, and a NULL 
partition key;
     - no PARTITION BY, DESC ordering, and NULLS FIRST/LAST;
     - PARTITION BY only, where row order is arbitrary, checked through 
aggregates.
   - **Execution settings:**
     - reducer batch sizes 1, 2, 3 and the default;
     - spilling with `hive.vectorized.ptf.max.memory.buffering.batch.count=1`;
     - the two fallback cases (default of another type), checked with EXPLAIN.
   - **5,000-row table:** `amt` up to 2,000, with heavy spilling, then default 
settings. The row-mode and vectorized results are written to tables and 
compared row by row with a null-safe full outer join, which returns 0 
mismatches, plus checksum aggregates.
   
   Regenerated q files , with EXPLAIN-only diffs (`streamingColumns`, 
`allEvaluatorsAreStreaming`, scratch column types of the converted constants): 
`vector_ptf_bounded_start`, `vector_ptf_lead_lag`, `vector_windowing`, 
`vector_windowing_expressions`, `vector_windowing_navfn`, `vectorized_ptf`.
   
   These pass without regeneration:
   - all `vector_ptf_*`, `vector_windowing*` and `vectorized_ptf` qtests;
   - `ptf`, `ptf_streaming`, `windowing`, `windowing_navfn`, `leadlag` and 
`leadlag_queries`;
   - the other lead/lag qtests (`cbo_*windowing*`, `ptf_decimal`, `semijoin*`, 
`explainuser_1`, …), which are unchanged;
   - `TestVectorPTFGroupBatches`.
   
   ```
   mvn test -Dtest=TestMiniLlapLocalCliDriver 
-Dqfile=vector_ptf_lead_lag_streaming.q -pl itests/qtest
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to