Github user davies commented on a diff in the pull request:
https://github.com/apache/spark/pull/7821#discussion_r36028383
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateRowConcat.scala
---
@@ -0,0 +1,241 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.expressions.codegen
+
+import org.apache.spark.sql.catalyst.expressions.{UnsafeRow, Attribute}
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.unsafe.PlatformDependent
+
+
+abstract class UnsafeRowConcat {
+ def concat(row1: UnsafeRow, row2: UnsafeRow): UnsafeRow
+}
+
+
+/**
+ * A code generator for concatenating two [[UnsafeRow]]s into a single
[[UnsafeRow]].
+ *
+ * The high level algorithm is:
+ *
+ * 1. Concatenate the two bitsets together into a single one, taking
padding into account.
+ * 2. Move fixed-length data.
+ * 3. Move variable-length data.
+ * 4. Update the offset position (i.e. the upper 32 bits in the fixed
length part) for all
+ * variable-length data.
+ */
+object GenerateRowConcat extends CodeGenerator[(StructType, StructType),
UnsafeRowConcat] {
+
+ def dump(word: Long): String = {
+ Seq.tabulate(64) { i => if ((word >> i) % 2 == 0) "0" else "1"
}.reverse.mkString
+ }
+
+ override protected def create(in: (StructType, StructType)):
UnsafeRowConcat = {
+ create(in._1, in._2)
+ }
+
+ override protected def canonicalize(in: (StructType, StructType)):
(StructType, StructType) = in
+
+ override protected def bind(in: (StructType, StructType), inputSchema:
Seq[Attribute])
+ : (StructType, StructType) = {
+ in
+ }
+
+ def create(schema1: StructType, schema2: StructType): UnsafeRowConcat = {
+ val ctx = newCodeGenContext()
+ val offset = PlatformDependent.BYTE_ARRAY_OFFSET
+
+ val bitset1Words = (schema1.size + 63) / 64
+ val bitset2Words = (schema2.size + 63) / 64
+ val outputBitsetWords = (schema1.size + schema2.size + 63) / 64
+ val bitset1Remainder = schema1.size % 64
+ val bitset2Remainder = schema2.size % 64
+
+ // The number of words we can reduce when we concat two rows together.
+ // The only reduction comes from merging the bitset portion of the two
rows, saving 1 word.
+ val sizeReduction = bitset1Words + bitset2Words - outputBitsetWords
+
+ // --------------------- copy bitset from row 1
----------------------- //
+ val copyBitset1 = Seq.tabulate(bitset1Words) { i =>
+ s"""
+ |PlatformDependent.UNSAFE.putLong(buf, ${offset + i * 8},
+ | PlatformDependent.UNSAFE.getLong(obj1, ${offset + i * 8}));
+ """.stripMargin
+ }.mkString
+
+
+ // --------------------- copy bitset from row 2
----------------------- //
--- End diff --
We could do something like this
```
Seq.tabulate(outputBitsetWords) { i =>
val bitset = if (i< bitset1Words) {
getLong(obj1, i * 8)
} else if (i == bitset1Words && bitset1Remainder > 0) {
getLong(obj1, i* 8) | getLong(obj2, 0) >>> bitset1Remainder
} else {
getLong(obj2, i - bitset1Words) <<< bitset1Remainder | getLong(obj2 i -
bitset1Words + 1) >>> bitset1Remainder
}
putLong(buf, i * 8, bitset)
}
```
---
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]