|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.streaming.state |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{GenericInternalRow, UnsafeProjection} |
| 22 | +import org.apache.spark.sql.types._ |
| 23 | +import org.apache.spark.unsafe.types.UTF8String |
| 24 | + |
| 25 | +class RangeScanBoundaryUtilsSuite extends SparkFunSuite { |
| 26 | + |
| 27 | + test("defaultInternalRow / defaultUnsafeRow accept schemas with common types") { |
| 28 | + val schema = new StructType() |
| 29 | + .add("ts", LongType) |
| 30 | + .add("s", StringType) |
| 31 | + .add("i", IntegerType) |
| 32 | + .add("nested", new StructType().add("b", BinaryType).add("d", DoubleType)) |
| 33 | + .add("arr", ArrayType(IntegerType)) |
| 34 | + .add("m", MapType(StringType, IntegerType)) |
| 35 | + |
| 36 | + val row = RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 37 | + assert(row.numFields === schema.length) |
| 38 | + |
| 39 | + val unsafe = RangeScanBoundaryUtils.defaultUnsafeRow(schema) |
| 40 | + assert(unsafe.numFields === schema.length) |
| 41 | + } |
| 42 | + |
| 43 | + test("CharType field uses n zero-bytes, which is byte-wise <= any legitimate value") { |
| 44 | + val n = 5 |
| 45 | + val schema = new StructType().add("ts", LongType).add("c", CharType(n)) |
| 46 | + val boundary = RangeScanBoundaryUtils.defaultUnsafeRow(schema) |
| 47 | + val boundaryStr = boundary.getUTF8String(1) |
| 48 | + |
| 49 | + // UTF-8 encoding of n U+0000 code points is exactly n bytes of 0x00. |
| 50 | + assert(boundaryStr.numBytes() === n) |
| 51 | + (0 until n).foreach { i => |
| 52 | + assert(boundaryStr.getByte(i) === 0.toByte, s"byte $i is not 0x00") |
| 53 | + } |
| 54 | + |
| 55 | + // Sanity: a legitimate CharType(5) value written through UnsafeProjection |
| 56 | + // (e.g. "abcde") should produce a row whose raw bytes are >= the boundary row's. |
| 57 | + def encode(value: UTF8String): Array[Byte] = { |
| 58 | + val input = new GenericInternalRow(2) |
| 59 | + input.setLong(0, 0L) |
| 60 | + input.update(1, value) |
| 61 | + UnsafeProjection.create(schema).apply(input).getBytes |
| 62 | + } |
| 63 | + |
| 64 | + val boundaryBytes = boundary.getBytes |
| 65 | + val realBytes = encode(UTF8String.fromString("abcde")) |
| 66 | + assert(compareBytes(boundaryBytes, realBytes) <= 0, |
| 67 | + "boundary bytes should be <= 'abcde' bytes") |
| 68 | + |
| 69 | + // Also <= a value with legitimate low code points (e.g. all U+0001 padded). |
| 70 | + val lowValue = UTF8String.fromBytes(Array.fill[Byte](n)(1.toByte)) |
| 71 | + assert(compareBytes(boundaryBytes, encode(lowValue)) <= 0, |
| 72 | + "boundary bytes should be <= all-0x01 bytes") |
| 73 | + } |
| 74 | + |
| 75 | + test("CharType nested inside a struct is still handled") { |
| 76 | + val inner = new StructType().add("c", CharType(3)) |
| 77 | + val schema = new StructType().add("ts", LongType).add("nested", inner) |
| 78 | + // Should not throw, and the nested struct's char field should be 3 zero-bytes. |
| 79 | + val row = RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 80 | + val nested = row.getStruct(1, inner.length) |
| 81 | + val str = nested.getUTF8String(0) |
| 82 | + assert(str.numBytes() === 3) |
| 83 | + (0 until 3).foreach(i => assert(str.getByte(i) === 0.toByte)) |
| 84 | + } |
| 85 | + |
| 86 | + test("VarcharType default (empty string) is still used and is byte-wise smallest") { |
| 87 | + val schema = new StructType().add("ts", LongType).add("v", VarcharType(10)) |
| 88 | + // Should not throw; VarcharType's Literal.default is empty string (no padding). |
| 89 | + val row = RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 90 | + val s = row.getUTF8String(1) |
| 91 | + assert(s.numBytes() === 0) |
| 92 | + } |
| 93 | + |
| 94 | + test("defaultInternalRow rejects schemas containing VariantType at top level") { |
| 95 | + val schema = new StructType().add("ts", LongType).add("v", VariantType) |
| 96 | + val e = intercept[AssertionError] { |
| 97 | + RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 98 | + } |
| 99 | + assert(e.getMessage.contains("VariantType")) |
| 100 | + } |
| 101 | + |
| 102 | + test("defaultInternalRow rejects schemas containing VariantType nested in array") { |
| 103 | + val schema = new StructType() |
| 104 | + .add("ts", LongType) |
| 105 | + .add("arr", ArrayType(VariantType)) |
| 106 | + val e = intercept[AssertionError] { |
| 107 | + RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 108 | + } |
| 109 | + assert(e.getMessage.contains("VariantType")) |
| 110 | + } |
| 111 | + |
| 112 | + test("defaultInternalRow rejects schemas containing VariantType nested in struct") { |
| 113 | + val schema = new StructType() |
| 114 | + .add("ts", LongType) |
| 115 | + .add("nested", new StructType().add("v", VariantType)) |
| 116 | + val e = intercept[AssertionError] { |
| 117 | + RangeScanBoundaryUtils.defaultInternalRow(schema) |
| 118 | + } |
| 119 | + assert(e.getMessage.contains("VariantType")) |
| 120 | + } |
| 121 | + |
| 122 | + test("defaultUnsafeRow also triggers the schema check") { |
| 123 | + val schema = new StructType().add("ts", LongType).add("v", VariantType) |
| 124 | + val e = intercept[AssertionError] { |
| 125 | + RangeScanBoundaryUtils.defaultUnsafeRow(schema) |
| 126 | + } |
| 127 | + assert(e.getMessage.contains("VariantType")) |
| 128 | + } |
| 129 | + |
| 130 | + // Byte-wise unsigned lexicographic comparison, matching the RocksDB key order. |
| 131 | + private def compareBytes(a: Array[Byte], b: Array[Byte]): Int = { |
| 132 | + val len = math.min(a.length, b.length) |
| 133 | + var i = 0 |
| 134 | + while (i < len) { |
| 135 | + val ai = a(i) & 0xFF |
| 136 | + val bi = b(i) & 0xFF |
| 137 | + if (ai != bi) return ai - bi |
| 138 | + i += 1 |
| 139 | + } |
| 140 | + a.length - b.length |
| 141 | + } |
| 142 | +} |
0 commit comments