|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.compute.operator; |
| 9 | + |
| 10 | +import org.apache.lucene.util.BytesRef; |
| 11 | +import org.elasticsearch.compute.data.Block; |
| 12 | +import org.elasticsearch.compute.data.BooleanBlock; |
| 13 | +import org.elasticsearch.compute.data.BytesRefBlock; |
| 14 | +import org.elasticsearch.compute.data.DoubleBlock; |
| 15 | +import org.elasticsearch.compute.data.FloatBlock; |
| 16 | +import org.elasticsearch.compute.data.IntBlock; |
| 17 | +import org.elasticsearch.compute.data.LongBlock; |
| 18 | +import org.elasticsearch.compute.data.Page; |
| 19 | +import org.elasticsearch.core.Releasables; |
| 20 | +import org.elasticsearch.test.ESTestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * A {@link SourceOperator} that inserts random garbage into data from another |
| 24 | + * {@link SourceOperator}. It also inserts an extra channel at the end of the page |
| 25 | + * containing a {@code boolean} column. If it is {@code true} then the data came |
| 26 | + * from the original operator. If it's {@code false} then the data is random |
| 27 | + * garbage inserted by this operator. |
| 28 | + */ |
| 29 | +public class AddGarbageRowsSourceOperator extends SourceOperator { |
| 30 | + public static EvalOperator.ExpressionEvaluator.Factory filterFactory() { |
| 31 | + /* |
| 32 | + * Grabs the filter from the last block. That's where we put it. |
| 33 | + */ |
| 34 | + return ctx -> new EvalOperator.ExpressionEvaluator() { |
| 35 | + @Override |
| 36 | + public Block eval(Page page) { |
| 37 | + Block block = page.getBlock(page.getBlockCount() - 1); |
| 38 | + block.incRef(); |
| 39 | + return block; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void close() {} |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + private final SourceOperator next; |
| 48 | + |
| 49 | + public AddGarbageRowsSourceOperator(SourceOperator next) { |
| 50 | + this.next = next; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void finish() { |
| 55 | + next.finish(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean isFinished() { |
| 60 | + return next.isFinished(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Page getOutput() { |
| 65 | + Page page = next.getOutput(); |
| 66 | + if (page == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + Block.Builder[] newBlocks = new Block.Builder[page.getBlockCount() + 1]; |
| 70 | + try { |
| 71 | + for (int b = 0; b < page.getBlockCount(); b++) { |
| 72 | + Block block = page.getBlock(b); |
| 73 | + newBlocks[b] = block.elementType().newBlockBuilder(page.getPositionCount(), block.blockFactory()); |
| 74 | + } |
| 75 | + newBlocks[page.getBlockCount()] = page.getBlock(0).blockFactory().newBooleanBlockBuilder(page.getPositionCount()); |
| 76 | + |
| 77 | + for (int p = 0; p < page.getPositionCount(); p++) { |
| 78 | + if (ESTestCase.randomBoolean()) { |
| 79 | + insertGarbageRows(newBlocks, page); |
| 80 | + } |
| 81 | + copyPosition(newBlocks, page, p); |
| 82 | + if (ESTestCase.randomBoolean()) { |
| 83 | + insertGarbageRows(newBlocks, page); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return new Page(Block.Builder.buildAll(newBlocks)); |
| 88 | + } finally { |
| 89 | + Releasables.close(Releasables.wrap(newBlocks), page::releaseBlocks); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void copyPosition(Block.Builder[] newBlocks, Page page, int p) { |
| 94 | + for (int b = 0; b < page.getBlockCount(); b++) { |
| 95 | + Block block = page.getBlock(b); |
| 96 | + newBlocks[b].copyFrom(block, p, p + 1); |
| 97 | + } |
| 98 | + signalKeep(newBlocks, true); |
| 99 | + } |
| 100 | + |
| 101 | + private void insertGarbageRows(Block.Builder[] newBlocks, Page page) { |
| 102 | + int count = ESTestCase.between(1, 5); |
| 103 | + for (int c = 0; c < count; c++) { |
| 104 | + insertGarbageRow(newBlocks, page); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void insertGarbageRow(Block.Builder[] newBlocks, Page page) { |
| 109 | + for (int b = 0; b < page.getBlockCount(); b++) { |
| 110 | + Block block = page.getBlock(b); |
| 111 | + switch (block.elementType()) { |
| 112 | + case BOOLEAN -> ((BooleanBlock.Builder) newBlocks[b]).appendBoolean(ESTestCase.randomBoolean()); |
| 113 | + case BYTES_REF -> ((BytesRefBlock.Builder) newBlocks[b]).appendBytesRef(new BytesRef(ESTestCase.randomAlphaOfLength(5))); |
| 114 | + case COMPOSITE, DOC, UNKNOWN -> throw new UnsupportedOperationException(); |
| 115 | + case INT -> ((IntBlock.Builder) newBlocks[b]).appendInt(ESTestCase.randomInt()); |
| 116 | + case LONG -> ((LongBlock.Builder) newBlocks[b]).appendLong(ESTestCase.randomLong()); |
| 117 | + case NULL -> newBlocks[b].appendNull(); |
| 118 | + case DOUBLE -> ((DoubleBlock.Builder) newBlocks[b]).appendDouble(ESTestCase.randomDouble()); |
| 119 | + case FLOAT -> ((FloatBlock.Builder) newBlocks[b]).appendFloat(ESTestCase.randomFloat()); |
| 120 | + } |
| 121 | + } |
| 122 | + signalKeep(newBlocks, false); |
| 123 | + } |
| 124 | + |
| 125 | + private void signalKeep(Block.Builder[] newBlocks, boolean shouldKeep) { |
| 126 | + ((BooleanBlock.Builder) newBlocks[newBlocks.length - 1]).appendBoolean(shouldKeep); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void close() { |
| 131 | + next.close(); |
| 132 | + } |
| 133 | +} |
0 commit comments