Commit f8c9bec
[SPARK-14394][SQL] Generate AggregateHashMap class for LongTypes during TungstenAggregate codegen
## What changes were proposed in this pull request?
This PR adds support for generating the `AggregateHashMap` class in `TungstenAggregate` if the aggregate group by keys/value are of `LongType`. Note that currently this generate aggregate is not actually used.
NB: This currently only supports `LongType` keys/values (please see `isAggregateHashMapSupported` in `TungstenAggregate`) and will be generalized to other data types in a subsequent PR.
## How was this patch tested?
Manually inspected the generated code. This is what the generated map looks like for 2 keys:
```java
/* 068 */ public class agg_GeneratedAggregateHashMap {
/* 069 */ private org.apache.spark.sql.execution.vectorized.ColumnarBatch batch;
/* 070 */ private int[] buckets;
/* 071 */ private int numBuckets;
/* 072 */ private int maxSteps;
/* 073 */ private int numRows = 0;
/* 074 */ private org.apache.spark.sql.types.StructType schema =
/* 075 */ new org.apache.spark.sql.types.StructType()
/* 076 */ .add("k1", org.apache.spark.sql.types.DataTypes.LongType)
/* 077 */ .add("k2", org.apache.spark.sql.types.DataTypes.LongType)
/* 078 */ .add("sum", org.apache.spark.sql.types.DataTypes.LongType);
/* 079 */
/* 080 */ public agg_GeneratedAggregateHashMap(int capacity, double loadFactor, int maxSteps) {
/* 081 */ assert (capacity > 0 && ((capacity & (capacity - 1)) == 0));
/* 082 */ this.maxSteps = maxSteps;
/* 083 */ numBuckets = (int) (capacity / loadFactor);
/* 084 */ batch = org.apache.spark.sql.execution.vectorized.ColumnarBatch.allocate(schema,
/* 085 */ org.apache.spark.memory.MemoryMode.ON_HEAP, capacity);
/* 086 */ buckets = new int[numBuckets];
/* 087 */ java.util.Arrays.fill(buckets, -1);
/* 088 */ }
/* 089 */
/* 090 */ public agg_GeneratedAggregateHashMap() {
/* 091 */ new agg_GeneratedAggregateHashMap(1 << 16, 0.25, 5);
/* 092 */ }
/* 093 */
/* 094 */ public org.apache.spark.sql.execution.vectorized.ColumnarBatch.Row findOrInsert(long agg_key, long agg_key1) {
/* 095 */ long h = hash(agg_key, agg_key1);
/* 096 */ int step = 0;
/* 097 */ int idx = (int) h & (numBuckets - 1);
/* 098 */ while (step < maxSteps) {
/* 099 */ // Return bucket index if it's either an empty slot or already contains the key
/* 100 */ if (buckets[idx] == -1) {
/* 101 */ batch.column(0).putLong(numRows, agg_key);
/* 102 */ batch.column(1).putLong(numRows, agg_key1);
/* 103 */ batch.column(2).putLong(numRows, 0);
/* 104 */ buckets[idx] = numRows++;
/* 105 */ return batch.getRow(buckets[idx]);
/* 106 */ } else if (equals(idx, agg_key, agg_key1)) {
/* 107 */ return batch.getRow(buckets[idx]);
/* 108 */ }
/* 109 */ idx = (idx + 1) & (numBuckets - 1);
/* 110 */ step++;
/* 111 */ }
/* 112 */ // Didn't find it
/* 113 */ return null;
/* 114 */ }
/* 115 */
/* 116 */ private boolean equals(int idx, long agg_key, long agg_key1) {
/* 117 */ return batch.column(0).getLong(buckets[idx]) == agg_key && batch.column(1).getLong(buckets[idx]) == agg_key1;
/* 118 */ }
/* 119 */
/* 120 */ // TODO: Improve this Hash Function
/* 121 */ private long hash(long agg_key, long agg_key1) {
/* 122 */ return agg_key ^ agg_key1;
/* 123 */ }
/* 124 */
/* 125 */ }
```
Author: Sameer Agarwal <[email protected]>
Closes #12161 from sameeragarwal/tungsten-aggregate.1 parent 0275753 commit f8c9bec
File tree
2 files changed
+210
-3
lines changed- sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate
2 files changed
+210
-3
lines changedsql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/ColumnarAggMapCodeGenerator.scala
Lines changed: 193 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
Lines changed: 17 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
437 | 437 | | |
438 | 438 | | |
439 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
440 | 453 | | |
441 | 454 | | |
442 | 455 | | |
| |||
452 | 465 | | |
453 | 466 | | |
454 | 467 | | |
| 468 | + | |
455 | 469 | | |
456 | 470 | | |
457 | 471 | | |
| |||
0 commit comments