|
20 | 20 | package org.apache.iotdb.db.queryengine.execution.operator; |
21 | 21 |
|
22 | 22 | import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory; |
| 23 | +import org.apache.iotdb.commons.path.PartialPath; |
| 24 | +import org.apache.iotdb.commons.udf.service.UDFClassLoaderManager; |
23 | 25 | import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; |
| 26 | +import org.apache.iotdb.db.queryengine.common.NodeRef; |
24 | 27 | import org.apache.iotdb.db.queryengine.common.PlanFragmentId; |
25 | 28 | import org.apache.iotdb.db.queryengine.common.QueryId; |
26 | 29 | import org.apache.iotdb.db.queryengine.execution.driver.DriverContext; |
27 | 30 | import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext; |
28 | 31 | import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine; |
29 | 32 | import org.apache.iotdb.db.queryengine.execution.operator.process.TransformOperator; |
30 | 33 | import org.apache.iotdb.db.queryengine.execution.operator.source.SeriesScanOperator; |
| 34 | +import org.apache.iotdb.db.queryengine.plan.expression.Expression; |
| 35 | +import org.apache.iotdb.db.queryengine.plan.expression.leaf.TimeSeriesOperand; |
31 | 36 | import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNodeId; |
| 37 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.parameter.InputLocation; |
32 | 38 | import org.apache.iotdb.db.queryengine.transformation.api.LayerReader; |
33 | 39 | import org.apache.iotdb.db.queryengine.transformation.dag.input.QueryDataSetInputLayer; |
34 | 40 | import org.apache.iotdb.db.queryengine.transformation.dag.input.TsBlockInputDataSet; |
35 | 41 |
|
36 | 42 | import com.google.common.collect.ImmutableList; |
| 43 | +import com.google.common.collect.ImmutableMap; |
| 44 | +import org.apache.tsfile.common.conf.TSFileDescriptor; |
37 | 45 | import org.apache.tsfile.enums.TSDataType; |
38 | 46 | import org.apache.tsfile.read.common.block.TsBlock; |
39 | 47 | import org.apache.tsfile.read.common.block.column.LongColumn; |
40 | 48 | import org.apache.tsfile.read.common.block.column.TimeColumn; |
| 49 | +import org.junit.Assert; |
41 | 50 | import org.junit.Test; |
42 | 51 |
|
| 52 | +import java.time.ZoneId; |
| 53 | +import java.util.HashMap; |
| 54 | +import java.util.List; |
| 55 | +import java.util.Map; |
43 | 56 | import java.util.Optional; |
44 | 57 |
|
45 | 58 | import static org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext.createFragmentInstanceContext; |
@@ -139,4 +152,127 @@ public long ramBytesUsed() { |
139 | 152 | reader.yield(); |
140 | 153 | reader.consumedAll(); |
141 | 154 | } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testTransformResultLimit() throws Exception { |
| 158 | + UDFClassLoaderManager.setupAndGetInstance(); |
| 159 | + int savedMaxLine = TSFileDescriptor.getInstance().getConfig().getMaxTsBlockLineNumber(); |
| 160 | + try { |
| 161 | + int rowCount = 2001; |
| 162 | + int maxLine = 200; |
| 163 | + TSFileDescriptor.getInstance().getConfig().setMaxTsBlockLineNumber(200); |
| 164 | + QueryId queryId = new QueryId("stub_query_chunk"); |
| 165 | + FragmentInstanceId instanceId = |
| 166 | + new FragmentInstanceId(new PlanFragmentId(queryId, 0), "stub-instance"); |
| 167 | + FragmentInstanceStateMachine stateMachine = |
| 168 | + new FragmentInstanceStateMachine( |
| 169 | + instanceId, |
| 170 | + IoTDBThreadPoolFactory.newFixedThreadPool(1, "test-instance-notification")); |
| 171 | + FragmentInstanceContext fragmentInstanceContext = |
| 172 | + createFragmentInstanceContext(instanceId, stateMachine); |
| 173 | + DriverContext driverContext = new DriverContext(fragmentInstanceContext, 0); |
| 174 | + PlanNodeId scanNodeId = new PlanNodeId("scan"); |
| 175 | + driverContext.addOperatorContext(1, scanNodeId, SeriesScanOperator.class.getSimpleName()); |
| 176 | + PlanNodeId transformNodeId = new PlanNodeId("transform"); |
| 177 | + driverContext.addOperatorContext(2, transformNodeId, TransformOperator.class.getSimpleName()); |
| 178 | + |
| 179 | + long[] times = new long[rowCount]; |
| 180 | + long[] values = new long[rowCount]; |
| 181 | + for (int i = 0; i < rowCount; i++) { |
| 182 | + times[i] = i; |
| 183 | + values[i] = i * 10L; |
| 184 | + } |
| 185 | + TsBlock oneBatch = |
| 186 | + new TsBlock( |
| 187 | + new TimeColumn(rowCount, times), new LongColumn(rowCount, Optional.empty(), values)); |
| 188 | + |
| 189 | + Operator childOperator = |
| 190 | + new Operator() { |
| 191 | + boolean consumed = false; |
| 192 | + |
| 193 | + @Override |
| 194 | + public OperatorContext getOperatorContext() { |
| 195 | + return driverContext.getOperatorContexts().get(0); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public TsBlock next() { |
| 200 | + if (!consumed) { |
| 201 | + consumed = true; |
| 202 | + return oneBatch; |
| 203 | + } |
| 204 | + return null; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public boolean hasNext() { |
| 209 | + return !consumed; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void close() {} |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean isFinished() { |
| 217 | + return consumed; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public long calculateMaxPeekMemory() { |
| 222 | + return oneBatch.getSizeInBytes(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public long calculateMaxReturnSize() { |
| 227 | + return oneBatch.getSizeInBytes(); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public long calculateRetainedSizeAfterCallingNext() { |
| 232 | + return 0; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public long ramBytesUsed() { |
| 237 | + return 0; |
| 238 | + } |
| 239 | + }; |
| 240 | + |
| 241 | + TimeSeriesOperand s1 = |
| 242 | + new TimeSeriesOperand(new PartialPath("root.sg.d1.s1"), TSDataType.INT64); |
| 243 | + Map<String, List<InputLocation>> inputLocations = |
| 244 | + ImmutableMap.of(s1.getExpressionString(), ImmutableList.of(new InputLocation(0, 0))); |
| 245 | + Map<NodeRef<Expression>, TSDataType> expressionTypes = new HashMap<>(); |
| 246 | + expressionTypes.put(NodeRef.of(s1), TSDataType.INT64); |
| 247 | + |
| 248 | + TransformOperator transform = |
| 249 | + new TransformOperator( |
| 250 | + driverContext.getOperatorContexts().get(1), |
| 251 | + childOperator, |
| 252 | + ImmutableList.of(TSDataType.INT64), |
| 253 | + inputLocations, |
| 254 | + new Expression[] {s1}, |
| 255 | + true, |
| 256 | + ZoneId.systemDefault(), |
| 257 | + expressionTypes, |
| 258 | + true); |
| 259 | + |
| 260 | + int totalOutRows = 0; |
| 261 | + int nonNullNextCount = 0; |
| 262 | + while (transform.hasNext()) { |
| 263 | + TsBlock out = transform.next(); |
| 264 | + if (out != null) { |
| 265 | + nonNullNextCount++; |
| 266 | + Assert.assertTrue( |
| 267 | + "Each batch must be at most " + maxLine + " rows", out.getPositionCount() <= maxLine); |
| 268 | + totalOutRows += out.getPositionCount(); |
| 269 | + } |
| 270 | + } |
| 271 | + Assert.assertEquals(rowCount, totalOutRows); |
| 272 | + System.out.println(nonNullNextCount); |
| 273 | + Assert.assertTrue(nonNullNextCount >= 11); |
| 274 | + } finally { |
| 275 | + TSFileDescriptor.getInstance().getConfig().setMaxTsBlockLineNumber(savedMaxLine); |
| 276 | + } |
| 277 | + } |
142 | 278 | } |
0 commit comments