Github user davies commented on the pull request:

    https://github.com/apache/spark/pull/11130#issuecomment-181988539
  
    For query:
    ```
    val dim = broadcast(sqlContext.range(1 << 16).selectExpr("id as k", 
"cast(id as string) as v"))
    sqlContext.range(N).join(dim, (col("id") % 60000) === col("k"), 
"left").count()
    ```
    
    will generate:
    
    ```
    /* 001 */
    /* 002 */ public Object generate(Object[] references) {
    /* 003 */   return new GeneratedIterator(references);
    /* 004 */ }
    /* 005 */
    /* 006 */ class GeneratedIterator extends 
org.apache.spark.sql.execution.BufferedRowIterator {
    /* 007 */
    /* 008 */   private Object[] references;
    /* 009 */   private boolean agg_initAgg;
    /* 010 */   private boolean agg_bufIsNull;
    /* 011 */   private long agg_bufValue;
    /* 012 */   private org.apache.spark.broadcast.TorrentBroadcast 
join_broadcast;
    /* 013 */   private org.apache.spark.sql.execution.joins.LongArrayRelation 
join_relation;
    /* 014 */   private boolean range_initRange;
    /* 015 */   private long range_partitionEnd;
    /* 016 */   private long range_number;
    /* 017 */   private boolean range_overflow;
    /* 018 */   private UnsafeRow agg_result;
    /* 019 */   private 
org.apache.spark.sql.catalyst.expressions.codegen.BufferHolder agg_holder;
    /* 020 */   private 
org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter agg_rowWriter;
    /* 021 */
    /* 022 */   public GeneratedIterator(Object[] references) {
    /* 023 */     this.references = references;
    /* 024 */     agg_initAgg = false;
    /* 025 */
    /* 026 */
    /* 027 */     this.join_broadcast = 
(org.apache.spark.broadcast.TorrentBroadcast) references[0];
    /* 028 */
    /* 029 */     join_relation = 
(org.apache.spark.sql.execution.joins.LongArrayRelation) join_broadcast.value();
    /* 030 */     incPeakExecutionMemory(join_relation.getMemorySize());
    /* 031 */
    /* 032 */     range_initRange = false;
    /* 033 */     range_partitionEnd = 0L;
    /* 034 */     range_number = 0L;
    /* 035 */     range_overflow = false;
    /* 036 */     agg_result = new UnsafeRow(1);
    /* 037 */     this.agg_holder = new 
org.apache.spark.sql.catalyst.expressions.codegen.BufferHolder(agg_result, 0);
    /* 038 */     this.agg_rowWriter = new 
org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter(agg_holder, 
1);
    /* 039 */   }
    /* 040 */
    /* 041 */
    /* 042 */   private void agg_doAggregateWithoutKey() throws 
java.io.IOException {
    /* 043 */     // initialize aggregation buffer
    /* 044 */
    /* 045 */     agg_bufIsNull = false;
    /* 046 */     agg_bufValue = 0L;
    /* 047 */
    /* 048 */
    /* 049 */
    /* 050 */
    /* 051 */     // initialize Range
    /* 052 */     if (!range_initRange) {
    /* 053 */       range_initRange = true;
    /* 054 */       if (input.hasNext()) {
    /* 055 */         initRange(((InternalRow) input.next()).getInt(0));
    /* 056 */       } else {
    /* 057 */         return;
    /* 058 */       }
    /* 059 */     }
    /* 060 */
    /* 061 */     while (!range_overflow && range_number < range_partitionEnd) {
    /* 062 */       long range_value = range_number;
    /* 063 */       range_number += 1L;
    /* 064 */       if (range_number < range_value ^ 1L < 0) {
    /* 065 */         range_overflow = true;
    /* 066 */       }
    /* 067 */
    /* 068 */       // generate join key
    /* 069 */       /* (input[0, bigint] % 60000) */
    /* 070 */       boolean join_isNull = false;
    /* 071 */       long join_value = -1L;
    /* 072 */       if (false || 60000L == 0) {
    /* 073 */         join_isNull = true;
    /* 074 */       } else {
    /* 075 */
    /* 076 */         if (false) {
    /* 077 */           join_isNull = true;
    /* 078 */         } else {
    /* 079 */           join_value = (long)(range_value % 60000L);
    /* 080 */         }
    /* 081 */       }
    /* 082 */       // find matches from HashedRelation
    /* 083 */       UnsafeRow join_matched = join_isNull ? null: 
(UnsafeRow)join_relation.getValue(join_value);
    /* 084 */
    /* 085 */       boolean join_isNull4 = true;
    /* 086 */       long join_value4 = -1L;
    /* 087 */       if (join_matched != null) {
    /* 088 */         /* input[0, bigint] */
    /* 089 */         long join_value3 = join_matched.getLong(0);
    /* 090 */         join_isNull4 = false;
    /* 091 */         join_value4 = join_value3;
    /* 092 */       }
    /* 093 */
    /* 094 */       final boolean join_invalid = true;
    /* 095 */       if (!join_invalid) {
    /* 096 */         // reset to null
    /* 097 */         join_isNull4 = true;
    /* 098 */       }
    /* 099 */
    /* 100 */
    /* 101 */
    /* 102 */
    /* 103 */       // do aggregate
    /* 104 */       /* (input[0, bigint] + 1) */
    /* 105 */       long agg_value1 = -1L;
    /* 106 */       agg_value1 = agg_bufValue + 1L;
    /* 107 */       // update aggregation buffer
    /* 108 */       agg_bufIsNull = false;
    /* 109 */       agg_bufValue = agg_value1;
    /* 110 */
    /* 111 */
    /* 112 */
    /* 113 */
    /* 114 */       if (shouldStop()) return;
    /* 115 */     }
    /* 116 */
    /* 117 */
    /* 118 */   }
    /* 119 */
    /* 120 */
    /* 121 */   private void initRange(int idx) {
    /* 122 */     java.math.BigInteger index = 
java.math.BigInteger.valueOf(idx);
    /* 123 */     java.math.BigInteger numSlice = 
java.math.BigInteger.valueOf(1L);
    /* 124 */     java.math.BigInteger numElement = 
java.math.BigInteger.valueOf(102400L);
    /* 125 */     java.math.BigInteger step = java.math.BigInteger.valueOf(1L);
    /* 126 */     java.math.BigInteger start = java.math.BigInteger.valueOf(0L);
    /* 127 */
    /* 128 */     java.math.BigInteger st = 
index.multiply(numElement).divide(numSlice).multiply(step).add(start);
    /* 129 */     if 
(st.compareTo(java.math.BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
    /* 130 */       range_number = Long.MAX_VALUE;
    /* 131 */     } else if 
(st.compareTo(java.math.BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
    /* 132 */       range_number = Long.MIN_VALUE;
    /* 133 */     } else {
    /* 134 */       range_number = st.longValue();
    /* 135 */     }
    /* 136 */
    /* 137 */     java.math.BigInteger end = 
index.add(java.math.BigInteger.ONE).multiply(numElement).divide(numSlice)
    /* 138 */     .multiply(step).add(start);
    /* 139 */     if 
(end.compareTo(java.math.BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
    /* 140 */       range_partitionEnd = Long.MAX_VALUE;
    /* 141 */     } else if 
(end.compareTo(java.math.BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
    /* 142 */       range_partitionEnd = Long.MIN_VALUE;
    /* 143 */     } else {
    /* 144 */       range_partitionEnd = end.longValue();
    /* 145 */     }
    /* 146 */   }
    /* 147 */
    /* 148 */
    /* 149 */   protected void processNext() throws java.io.IOException {
    /* 150 */     if (!agg_initAgg) {
    /* 151 */       agg_initAgg = true;
    /* 152 */       agg_doAggregateWithoutKey();
    /* 153 */
    /* 154 */       // output the result
    /* 155 */
    /* 156 */
    /* 157 */       agg_rowWriter.zeroOutNullBytes();
    /* 158 */
    /* 159 */
    /* 160 */       if (agg_bufIsNull) {
    /* 161 */         agg_rowWriter.setNullAt(0);
    /* 162 */       } else {
    /* 163 */         agg_rowWriter.write(0, agg_bufValue);
    /* 164 */       }
    /* 165 */       currentRows.add(agg_result.copy());
    /* 166 */     }
    /* 167 */   }
    /* 168 */ }
    /* 169 */
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to