aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/test/java/org/apache/drill/exec/store/parquet/columnreaders/TestBatchSizingMemoryUtil.java
blob: 4cf150372e38aecb13cd466488c550ad602f6bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.drill.exec.store.parquet.columnreaders;

import java.math.BigDecimal;
import org.apache.drill.common.types.TypeProtos;
import org.apache.drill.exec.record.VectorContainer;
import org.apache.drill.exec.record.metadata.SchemaBuilder;
import org.apache.drill.exec.record.metadata.TupleMetadata;
import org.apache.drill.exec.store.parquet.columnreaders.batchsizing.BatchSizingMemoryUtil;
import org.apache.drill.exec.store.parquet.columnreaders.batchsizing.BatchSizingMemoryUtil.ColumnMemoryUsageInfo;
import org.apache.drill.exec.store.parquet.columnreaders.batchsizing.RecordBatchSizerManager.ColumnMemoryQuota;
import org.apache.drill.test.PhysicalOpUnitTestBase;
import org.apache.drill.test.rowSet.RowSet;
import org.apache.drill.test.rowSet.RowSetBuilder;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestBatchSizingMemoryUtil extends PhysicalOpUnitTestBase {
//  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TestBatchSizingMemoryUtil.class);

  // Batch schema
  private static TupleMetadata schema;
  private static TupleMetadata nullableSchema;

  // Row set
  private RowSet.SingleRowSet rowSet;

  // Column memory usage information
  private final ColumnMemoryUsageInfo[] columnMemoryInfo = new ColumnMemoryUsageInfo[3];

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    schema = new SchemaBuilder()
      .add("name_vchar", TypeProtos.MinorType.VARCHAR, TypeProtos.DataMode.REQUIRED)
      .add("name_vbinary", TypeProtos.MinorType.VARBINARY, TypeProtos.DataMode.REQUIRED)
      .add("name_vdecimal", TypeProtos.MinorType.VARDECIMAL, TypeProtos.DataMode.REQUIRED)
      .buildSchema();

    nullableSchema = new SchemaBuilder()
      .add("name_vchar", TypeProtos.MinorType.VARCHAR, TypeProtos.DataMode.OPTIONAL)
      .add("name_vbinary", TypeProtos.MinorType.VARBINARY, TypeProtos.DataMode.OPTIONAL)
      .add("name_vdecimal", TypeProtos.MinorType.VARDECIMAL, TypeProtos.DataMode.OPTIONAL)
      .buildSchema();
  }

  @Test
  public void testCanAddNewData() {
   try {
     testCanAddNewData(false);

    } finally {
      if (rowSet != null) {
        rowSet.clear();
      }
    }
  }

  @Test
  public void testCanAddNewNullabalData() {
   try {
     testCanAddNewData(true);

    } finally {
      if (rowSet != null) {
        rowSet.clear();
      }
    }
  }

  private void testCanAddNewData(boolean isOptional) {
    final Object[] data = {"0123456789", new byte[10], new BigDecimal(Long.MAX_VALUE) };
    final int numRows = 1;

    // Load the test data into the associated Value Vectors
    loadTestData(numRows, isOptional, data);

    for (int columnIdx = 0; columnIdx < 3; columnIdx++) {
      final ColumnMemoryUsageInfo columnInfo = columnMemoryInfo[columnIdx];
      final long remainingBitsCapacity = getRemainingBitsCapacity(columnInfo);
      final long remainingOffsetsCapacity = getRemainingOffsetsCapacity(columnInfo);
      final long remainingDataCapacity = getRemainingDataCapacity(columnInfo);

      // Test current VV is within quota (since we are not adding new entries)
      Assert.assertTrue(BatchSizingMemoryUtil.canAddNewData(columnInfo, 0, 0, 0));

      if (isOptional) {
        // Test add the maximum offsets and data
        Assert.assertTrue(BatchSizingMemoryUtil.canAddNewData(columnInfo, remainingBitsCapacity, remainingOffsetsCapacity, remainingDataCapacity));

        // Test VV overflow: for bits, offsets, data, and then all
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, remainingBitsCapacity + 1, remainingOffsetsCapacity, remainingDataCapacity));
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, remainingBitsCapacity, remainingOffsetsCapacity + 1, remainingDataCapacity));
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, remainingBitsCapacity, remainingOffsetsCapacity, remainingDataCapacity + 1));
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, remainingBitsCapacity + 1, remainingOffsetsCapacity + 1, remainingDataCapacity + 1));
      } else {
        // Test add the maximum offsets and data
        Assert.assertTrue(BatchSizingMemoryUtil.canAddNewData(columnInfo, 0, remainingOffsetsCapacity, remainingDataCapacity));

        // Test VV overflow: for offsets, data, and then both
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, 0, remainingOffsetsCapacity + 1, remainingDataCapacity));
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, 0, remainingOffsetsCapacity, remainingDataCapacity + 1));
        Assert.assertFalse(BatchSizingMemoryUtil.canAddNewData(columnInfo, 0, remainingOffsetsCapacity + 1, remainingDataCapacity + 1));
      }
    }
  }

  private void loadTestData(int numRows, boolean isOptional, Object...data) {
    // First, lets create a row set
    rowSet = null;
    final TupleMetadata targetSchema = isOptional ? nullableSchema : schema;
    final RowSetBuilder builder = operatorFixture.rowSetBuilder(targetSchema);

    for (int rowIdx = 0; rowIdx < numRows; ++rowIdx) {
      builder.addRow(data);
    }
    rowSet = builder.build();

    // Now load the column memory information
    for (int columnIdx = 0; columnIdx < columnMemoryInfo.length; columnIdx++) {
      columnMemoryInfo[columnIdx] = getColumnMemoryUsageInfo(columnIdx);
    }
  }

  private ColumnMemoryUsageInfo getColumnMemoryUsageInfo(int columnIdx) {
    final VectorContainer vectorContainer = rowSet.container();
    final ColumnMemoryUsageInfo result = new ColumnMemoryUsageInfo();

    result.vector = vectorContainer.getValueVector(columnIdx).getValueVector();
    result.currValueCount = vectorContainer.getRecordCount();
    result.memoryQuota = new ColumnMemoryQuota(result.vector.getAllocatedSize());

    // Load the VV memory usage information
    BatchSizingMemoryUtil.getMemoryUsage(result.vector, result.currValueCount, result.vectorMemoryUsage);

    return result;
  }

  private static long getRemainingBitsCapacity(ColumnMemoryUsageInfo columnInfo) {
    return columnInfo.vectorMemoryUsage.bitsBytesCapacity - columnInfo.vectorMemoryUsage.bitsBytesUsed;
  }

  private static long getRemainingOffsetsCapacity(ColumnMemoryUsageInfo columnInfo) {
    return columnInfo.vectorMemoryUsage.offsetsByteCapacity - columnInfo.vectorMemoryUsage.offsetsBytesUsed;
  }

  private static long getRemainingDataCapacity(ColumnMemoryUsageInfo columnInfo) {
    return columnInfo.vectorMemoryUsage.dataByteCapacity - columnInfo.vectorMemoryUsage.dataBytesUsed;
  }

}