|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.sql.comet.util |
| 21 | + |
| 22 | +import org.apache.spark.sql.CometTestBase |
| 23 | +import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector} |
| 24 | + |
| 25 | +class UtilsSuite extends CometTestBase { |
| 26 | + |
| 27 | + test("serializeBatches preserves row count for a zero-column batch") { |
| 28 | + val numRows = 5 |
| 29 | + val batch = new ColumnarBatch(Array.empty[ColumnVector], numRows) |
| 30 | + |
| 31 | + val (rowCount, buf) = Utils.serializeBatches(Iterator(batch)).next() |
| 32 | + assert(rowCount == numRows) |
| 33 | + |
| 34 | + val decoded = Utils.decodeBatches(buf, "test").toSeq |
| 35 | + assert(decoded.map(_.numRows()).sum == numRows) |
| 36 | + } |
| 37 | + |
| 38 | + test("coalesceBroadcastBatches preserves row count across zero-column inputs") { |
| 39 | + val numRows = 5 |
| 40 | + val numBatches = 3 |
| 41 | + val batches = |
| 42 | + (0 until numBatches).map(_ => new ColumnarBatch(Array.empty[ColumnVector], numRows)) |
| 43 | + |
| 44 | + val bufs = Utils.serializeBatches(batches.iterator).map(_._2).toSeq.iterator |
| 45 | + val (coalesced, batchCount, totalRows) = Utils.coalesceBroadcastBatches(bufs) |
| 46 | + |
| 47 | + val expected = numRows.toLong * numBatches |
| 48 | + assert(batchCount == numBatches) |
| 49 | + assert(totalRows == expected) |
| 50 | + |
| 51 | + val decoded = coalesced.iterator.flatMap(b => Utils.decodeBatches(b, "test")).toSeq |
| 52 | + assert(decoded.map(_.numRows()).sum == expected) |
| 53 | + } |
| 54 | +} |
0 commit comments