|
| 1 | +/* |
| 2 | + * IGinX - the polystore system with high performance |
| 3 | + * Copyright (C) Tsinghua University |
| 4 | + |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package cn.edu.tsinghua.iginx.engine.physical.memory.execute.compute.join; |
| 21 | + |
| 22 | +import cn.edu.tsinghua.iginx.engine.physical.memory.execute.compute.scalar.expression.ScalarExpression; |
| 23 | +import cn.edu.tsinghua.iginx.engine.physical.memory.execute.compute.scalar.expression.ScalarExpressionUtils; |
| 24 | +import cn.edu.tsinghua.iginx.engine.physical.memory.execute.compute.util.*; |
| 25 | +import cn.edu.tsinghua.iginx.engine.physical.memory.execute.compute.util.exception.ComputeException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import javax.annotation.WillClose; |
| 30 | +import javax.annotation.WillCloseWhenClosed; |
| 31 | +import org.apache.arrow.memory.BufferAllocator; |
| 32 | +import org.apache.arrow.util.Preconditions; |
| 33 | +import org.apache.arrow.vector.*; |
| 34 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 35 | + |
| 36 | +public class JoinArrayList implements AutoCloseable { |
| 37 | + |
| 38 | + private final BufferAllocator allocator; |
| 39 | + private final List<ScalarExpression<?>> outputExpressions; |
| 40 | + private final Schema probeSideSchema; |
| 41 | + private final VectorSchemaRoot buildSideSingleBatch; |
| 42 | + private final ResultConsumer resultConsumer; |
| 43 | + |
| 44 | + JoinArrayList( |
| 45 | + BufferAllocator allocator, |
| 46 | + List<ScalarExpression<?>> outputExpressions, |
| 47 | + Schema probeSideSchema, |
| 48 | + @WillCloseWhenClosed VectorSchemaRoot buildSideSingleBatch, |
| 49 | + ResultConsumer resultConsumer) { |
| 50 | + this.allocator = allocator; |
| 51 | + this.outputExpressions = outputExpressions; |
| 52 | + this.probeSideSchema = probeSideSchema; |
| 53 | + this.buildSideSingleBatch = buildSideSingleBatch; |
| 54 | + this.resultConsumer = resultConsumer; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void close() { |
| 59 | + buildSideSingleBatch.close(); |
| 60 | + } |
| 61 | + |
| 62 | + public void probe(VectorSchemaRoot probeSideBatch) throws ComputeException { |
| 63 | + Preconditions.checkArgument(probeSideBatch.getSchema().equals(probeSideSchema)); |
| 64 | + |
| 65 | + try (SelectionBuilder buildSideCandidateIndicesBuilder = |
| 66 | + new SelectionBuilder(allocator, "tempBuildSideIndices", probeSideBatch.getRowCount()); |
| 67 | + SelectionBuilder probeSideCandidateIndicesBuilder = |
| 68 | + new SelectionBuilder(allocator, "tempProbeSideIndices", probeSideBatch.getRowCount())) { |
| 69 | + |
| 70 | + for (int probeSideIndex = 0; |
| 71 | + probeSideIndex < probeSideBatch.getRowCount(); |
| 72 | + probeSideIndex++) { |
| 73 | + for (int buildSideIndex = 0; |
| 74 | + buildSideIndex < buildSideSingleBatch.getRowCount(); |
| 75 | + buildSideIndex++) { |
| 76 | + buildSideCandidateIndicesBuilder.append(buildSideIndex); |
| 77 | + probeSideCandidateIndicesBuilder.append(probeSideIndex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + try (ArrayDictionaryProvider outputDictionaryProvider = |
| 82 | + ArrayDictionaryProvider.of(allocator, buildSideSingleBatch, probeSideBatch); |
| 83 | + IntVector buildSideIndices = buildSideCandidateIndicesBuilder.build(); |
| 84 | + IntVector probeSideIndices = probeSideCandidateIndicesBuilder.build()) { |
| 85 | + output(outputDictionaryProvider, buildSideIndices, probeSideIndices); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void output( |
| 91 | + ArrayDictionaryProvider dictionaryProvider, |
| 92 | + BaseIntVector buildSideIndices, |
| 93 | + BaseIntVector probeSideIndices) |
| 94 | + throws ComputeException { |
| 95 | + Preconditions.checkArgument( |
| 96 | + buildSideIndices.getValueCount() == probeSideIndices.getValueCount()); |
| 97 | + |
| 98 | + int buildSideColumnCount = buildSideSingleBatch.getFieldVectors().size(); |
| 99 | + int probeSideColumnCount = probeSideSchema.getFields().size(); |
| 100 | + |
| 101 | + List<FieldVector> vectors = new ArrayList<>(); |
| 102 | + for (int i = 0; i < buildSideColumnCount; i++) { |
| 103 | + vectors.add(ValueVectors.slice(allocator, buildSideIndices, dictionaryProvider.lookup(i))); |
| 104 | + } |
| 105 | + for (int i = 0; i < probeSideColumnCount; i++) { |
| 106 | + vectors.add( |
| 107 | + ValueVectors.slice( |
| 108 | + allocator, probeSideIndices, dictionaryProvider.lookup(i + buildSideColumnCount))); |
| 109 | + } |
| 110 | + |
| 111 | + try (VectorSchemaRoot result = |
| 112 | + VectorSchemaRoots.create(vectors, probeSideIndices.getValueCount()); |
| 113 | + VectorSchemaRoot output = |
| 114 | + ScalarExpressionUtils.evaluate( |
| 115 | + allocator, dictionaryProvider, result, null, outputExpressions)) { |
| 116 | + resultConsumer.consume( |
| 117 | + dictionaryProvider.slice(allocator), VectorSchemaRoots.transfer(allocator, output), null); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static class Builder implements AutoCloseable { |
| 122 | + |
| 123 | + private final BufferAllocator allocator; |
| 124 | + private final Schema buildSideSchema; |
| 125 | + private final List<VectorSchemaRoot> buildSideBatches; |
| 126 | + |
| 127 | + public Builder(BufferAllocator allocator, Schema buildSideSchema) { |
| 128 | + this.allocator = Objects.requireNonNull(allocator); |
| 129 | + this.buildSideSchema = Objects.requireNonNull(buildSideSchema); |
| 130 | + this.buildSideBatches = new ArrayList<>(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void close() { |
| 135 | + buildSideBatches.forEach(VectorSchemaRoot::close); |
| 136 | + buildSideBatches.clear(); |
| 137 | + } |
| 138 | + |
| 139 | + public Builder add(@WillClose VectorSchemaRoot buildSideBatch) throws ComputeException { |
| 140 | + buildSideBatches.add(buildSideBatch); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public JoinArrayList build( |
| 145 | + BufferAllocator allocator, |
| 146 | + List<ScalarExpression<?>> outputExpressions, |
| 147 | + Schema probeSideSchema, |
| 148 | + ResultConsumer resultConsumer) |
| 149 | + throws ComputeException { |
| 150 | + Preconditions.checkNotNull(allocator); |
| 151 | + Preconditions.checkNotNull(probeSideSchema); |
| 152 | + Preconditions.checkNotNull(resultConsumer); |
| 153 | + |
| 154 | + try (VectorSchemaRoot buildSideSingleBatch = |
| 155 | + VectorSchemaRoots.concat(allocator, buildSideSchema, buildSideBatches)) { |
| 156 | + return new JoinArrayList( |
| 157 | + allocator, |
| 158 | + outputExpressions, |
| 159 | + probeSideSchema, |
| 160 | + VectorSchemaRoots.transfer(allocator, buildSideSingleBatch), |
| 161 | + resultConsumer); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments