|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +package dev.vortex.api; |
| 5 | + |
| 6 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import dev.vortex.arrow.ArrowAllocation; |
| 11 | +import dev.vortex.jni.NativeLoader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import org.apache.arrow.c.ArrowArray; |
| 17 | +import org.apache.arrow.c.ArrowSchema; |
| 18 | +import org.apache.arrow.c.Data; |
| 19 | +import org.apache.arrow.memory.BufferAllocator; |
| 20 | +import org.apache.arrow.vector.IntVector; |
| 21 | +import org.apache.arrow.vector.VarCharVector; |
| 22 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 23 | +import org.apache.arrow.vector.ipc.ArrowReader; |
| 24 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 25 | +import org.apache.arrow.vector.types.pojo.Field; |
| 26 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | + |
| 31 | +/** |
| 32 | + * End-to-end coverage for {@link Expression#listContains(Expression, Expression)} as a scan filter. |
| 33 | + * |
| 34 | + * <p>Constructing a list-contains expression proves nothing on its own: the element type is only checked when the |
| 35 | + * expression is bound to a schema, so a list literal built from the wrong element type builds fine and fails later, |
| 36 | + * inside a scan. These tests therefore write a small file and read it back through the filter, asserting on the rows |
| 37 | + * that survive. |
| 38 | + * |
| 39 | + * <p>The {@code maybe} column is nullable and null on every odd id, and the sets below hold a null element, to pin the |
| 40 | + * two null rules apart: under SQL's, which {@link Expression#in} and {@link Expression#notIn} use, a null element makes |
| 41 | + * every non-match null, so {@code NOT IN} keeps nothing; under Vortex's it is simply never a match. |
| 42 | + * |
| 43 | + * <p>The large-set case is the reason the binding exists. A caller without it has to expand {@code IN} into a chain of |
| 44 | + * equality comparisons, which is why callers cap the set size and fall back to a bounding range; a single |
| 45 | + * {@code list_contains} node carries the whole set, and the stats rewrite falsifies it for a zone only when every |
| 46 | + * element misses that zone's bounds. |
| 47 | + */ |
| 48 | +public final class ListContainsFilterTest { |
| 49 | + private static final int ROW_COUNT = 6; |
| 50 | + |
| 51 | + @TempDir |
| 52 | + static Path tempDir; |
| 53 | + |
| 54 | + private static Session session; |
| 55 | + private static String filePath; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + public static void loadLibrary() { |
| 59 | + NativeLoader.loadJni(); |
| 60 | + } |
| 61 | + |
| 62 | + @BeforeAll |
| 63 | + static void writeFile() throws IOException { |
| 64 | + session = Session.create(); |
| 65 | + filePath = |
| 66 | + tempDir.resolve("list_contains.vortex").toAbsolutePath().toUri().toString(); |
| 67 | + |
| 68 | + BufferAllocator allocator = ArrowAllocation.rootAllocator(); |
| 69 | + Schema schema = new Schema(List.of( |
| 70 | + Field.notNullable("id", new ArrowType.Int(32, true)), |
| 71 | + Field.notNullable("name", new ArrowType.Utf8()), |
| 72 | + Field.nullable("maybe", new ArrowType.Int(32, true)))); |
| 73 | + |
| 74 | + try (VortexWriter writer = VortexWriter.builder(session, filePath, schema, allocator) |
| 75 | + .build(); |
| 76 | + VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) { |
| 77 | + IntVector id = (IntVector) root.getVector("id"); |
| 78 | + VarCharVector name = (VarCharVector) root.getVector("name"); |
| 79 | + IntVector maybe = (IntVector) root.getVector("maybe"); |
| 80 | + id.allocateNew(ROW_COUNT); |
| 81 | + name.allocateNew(ROW_COUNT); |
| 82 | + maybe.allocateNew(ROW_COUNT); |
| 83 | + for (int i = 0; i < ROW_COUNT; i++) { |
| 84 | + id.setSafe(i, i + 1); |
| 85 | + name.setSafe(i, ("row-" + (i + 1)).getBytes(UTF_8)); |
| 86 | + // Every other row is null; the rest carry their id. |
| 87 | + if (i % 2 == 0) { |
| 88 | + maybe.setNull(i); |
| 89 | + } else { |
| 90 | + maybe.setSafe(i, i + 1); |
| 91 | + } |
| 92 | + } |
| 93 | + root.setRowCount(ROW_COUNT); |
| 94 | + |
| 95 | + try (ArrowArray array = ArrowArray.allocateNew(allocator); |
| 96 | + ArrowSchema arrowSchema = ArrowSchema.allocateNew(allocator)) { |
| 97 | + Data.exportVectorSchemaRoot(allocator, root, null, array, arrowSchema); |
| 98 | + writer.writeBatch(array.memoryAddress(), arrowSchema.memoryAddress()); |
| 99 | + } |
| 100 | + writer.finish(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void inKeepsOnlyTheMatchingRows() { |
| 106 | + Expression set = Expression.literalList( |
| 107 | + i32(), false, Expression.literal(2), Expression.literal(4), Expression.literal(99)); |
| 108 | + assertEquals(List.of(2, 4), scanIds(Expression.in(Expression.column("id"), set))); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void notInKeepsTheComplement() { |
| 113 | + Expression set = Expression.literalList( |
| 114 | + i32(), false, Expression.literal(2), Expression.literal(4), Expression.literal(99)); |
| 115 | + assertEquals(List.of(1, 3, 5, 6), scanIds(Expression.notIn(Expression.column("id"), set))); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void listContainsTakesTheListFirst() { |
| 120 | + // in(value, list) is listContains(list, value) with the operands the other way round; both spellings have to |
| 121 | + // agree, and passing the column as the list would be a type error rather than a silently different filter. |
| 122 | + Expression set = Expression.literalList(i32(), false, Expression.literal(3)); |
| 123 | + assertEquals(List.of(3), scanIds(Expression.listContains(set, Expression.column("id")))); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void inWithANullElementKeepsOnlyMatches() { |
| 128 | + // maybe = [null, 2, null, 4, null, 6]; the set holds 2 and a null. |
| 129 | + Expression set = Expression.literalList( |
| 130 | + i32(), true, Expression.literal(2), Expression.nullLiteral(Expression.DType.I32)); |
| 131 | + assertEquals(List.of(2), scanIds(Expression.in(Expression.column("maybe"), set))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void notInWithANullElementKeepsNothing() { |
| 136 | + // SQL: x NOT IN (2, NULL) is false for 2 and null for everything else, so no row survives. |
| 137 | + Expression set = Expression.literalList( |
| 138 | + i32(), true, Expression.literal(2), Expression.nullLiteral(Expression.DType.I32)); |
| 139 | + assertEquals(List.of(), scanIds(Expression.notIn(Expression.column("maybe"), set))); |
| 140 | + // Without the null element NOT IN keeps the non-null non-matches. |
| 141 | + assertEquals( |
| 142 | + List.of(4, 6), |
| 143 | + scanIds(Expression.notIn( |
| 144 | + Expression.column("maybe"), Expression.literalList(i32(), false, Expression.literal(2))))); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void vortexNullRulesTreatANullElementAsNoMatch() { |
| 149 | + // listContains without SQL semantics: the null element never matches, so a non-match stays false and its |
| 150 | + // negation keeps the non-null non-matches. |
| 151 | + Expression set = Expression.literalList( |
| 152 | + i32(), true, Expression.literal(2), Expression.nullLiteral(Expression.DType.I32)); |
| 153 | + assertEquals(List.of(2), scanIds(Expression.listContains(set, Expression.column("maybe")))); |
| 154 | + assertEquals(List.of(4, 6), scanIds(Expression.not(Expression.listContains(set, Expression.column("maybe"))))); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void aStringSetFiltersOnUtf8() { |
| 159 | + Expression set = |
| 160 | + Expression.literalList(utf8(), false, Expression.literal("row-1"), Expression.literal("row-6")); |
| 161 | + assertEquals(List.of(1, 6), scanIds(Expression.in(Expression.column("name"), set))); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void aSetLargerThanAnyOrChainCapStillPushesDown() { |
| 166 | + // The whole point of the binding: 1000 literals stay one expression node. Only 5 is present in the file. |
| 167 | + Expression[] elements = new Expression[1000]; |
| 168 | + for (int i = 0; i < elements.length; i++) { |
| 169 | + elements[i] = Expression.literal(i == 0 ? 5 : ROW_COUNT + i); |
| 170 | + } |
| 171 | + assertEquals( |
| 172 | + List.of(5), |
| 173 | + scanIds(Expression.in(Expression.column("id"), Expression.literalList(i32(), false, elements)))); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void anEmptySetMatchesNothing() { |
| 178 | + Expression empty = Expression.literalList(i32(), true); |
| 179 | + assertTrue(scanIds(Expression.in(Expression.column("id"), empty)).isEmpty()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void aNullSetMatchesNothing() { |
| 184 | + // A null list yields null rather than false, which filters the row out just the same. |
| 185 | + Expression nullList = Expression.nullLiteralList(i32(), true); |
| 186 | + assertTrue(scanIds(Expression.in(Expression.column("id"), nullList)).isEmpty()); |
| 187 | + } |
| 188 | + |
| 189 | + /** The element type prototype of an {@code i32} set. */ |
| 190 | + private static Expression i32() { |
| 191 | + return Expression.nullLiteral(Expression.DType.I32); |
| 192 | + } |
| 193 | + |
| 194 | + /** The element type prototype of a UTF-8 set. */ |
| 195 | + private static Expression utf8() { |
| 196 | + return Expression.nullLiteral(Expression.DType.UTF8); |
| 197 | + } |
| 198 | + |
| 199 | + /** Reads the {@code id} column of every row that survives {@code filter}, in file order. */ |
| 200 | + private static List<Integer> scanIds(Expression filter) { |
| 201 | + BufferAllocator allocator = ArrowAllocation.rootAllocator(); |
| 202 | + DataSource dataSource = DataSource.open(session, filePath); |
| 203 | + Scan scan = dataSource.scan( |
| 204 | + ScanOptions.builder().filter(filter).ordered(true).build()); |
| 205 | + |
| 206 | + List<Integer> ids = new ArrayList<>(); |
| 207 | + while (scan.hasNext()) { |
| 208 | + Partition partition = scan.next(); |
| 209 | + try (ArrowReader reader = partition.scanArrow(allocator)) { |
| 210 | + while (reader.loadNextBatch()) { |
| 211 | + VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 212 | + IntVector id = (IntVector) root.getVector("id"); |
| 213 | + for (int i = 0; i < root.getRowCount(); i++) { |
| 214 | + ids.add(id.get(i)); |
| 215 | + } |
| 216 | + } |
| 217 | + } catch (IOException e) { |
| 218 | + throw new AssertionError("failed reading partition", e); |
| 219 | + } |
| 220 | + } |
| 221 | + return ids; |
| 222 | + } |
| 223 | +} |
0 commit comments