yashmayya commented on code in PR #19759: URL: https://github.com/apache/pinot/pull/19759#discussion_r4212878219
########## pinot-core/src/test/java/org/apache/pinot/core/operator/transform/function/InIdSetTransformFunctionTest.java: ########## @@ -0,0 +1,132 @@ +/** + * 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.pinot.core.operator.transform.function; + +import java.io.IOException; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; +import org.apache.pinot.common.request.context.ExpressionContext; +import org.apache.pinot.common.request.context.RequestContextUtils; +import org.apache.pinot.core.operator.ColumnContext; +import org.apache.pinot.core.query.request.context.QueryContext; +import org.apache.pinot.core.query.request.context.utils.QueryContextConverterUtils; +import org.apache.pinot.core.query.utils.idset.IdSet; +import org.apache.pinot.core.query.utils.idset.IdSets; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotSame; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertSame; +import static org.testng.Assert.assertTrue; + + +/// Tests the IN_ID_SET transform function on a segment; `IdSetQueriesTest` runs it in queries. +public class InIdSetTransformFunctionTest extends BaseTransformFunctionTest { + + @Test + public void testSharesTheIdSetAcrossTheSegmentsOfAQuery() + throws IOException { + IdSet idSet = createIdSet(0, NUM_ROWS / 2); + ExpressionContext expression = createExpression(idSet); + QueryContext queryContext = createQueryContext(); + + // Each segment of a query builds its own transform function, but they share one deserialized IdSet + InIdSetTransformFunction segment1Function = createTransformFunction(expression, queryContext); + InIdSetTransformFunction segment2Function = createTransformFunction(expression, queryContext); + assertNotSame(segment2Function, segment1Function); + assertSame(segment2Function.getIdSet(), segment1Function.getIdSet()); + assertResults(segment2Function, idSet); + + // Another IdSet of the same query is deserialized on its own + IdSet otherIdSet = createIdSet(NUM_ROWS / 2, NUM_ROWS); + InIdSetTransformFunction otherIdSetFunction = createTransformFunction(createExpression(otherIdSet), queryContext); + assertNotSame(otherIdSetFunction.getIdSet(), segment1Function.getIdSet()); + assertResults(otherIdSetFunction, otherIdSet); + + // Another query deserializes its own IdSet + InIdSetTransformFunction otherQueryFunction = createTransformFunction(expression, createQueryContext()); + assertNotSame(otherQueryFunction.getIdSet(), segment1Function.getIdSet()); + } + + /// The query context that shares the IdSet must not keep it after the query. + @Test + public void testIdSetDoesNotOutliveTheQuery() + throws Exception { + ReferenceQueue<IdSet> queue = new ReferenceQueue<>(); + WeakReference<IdSet> idSetRef = new WeakReference<>(createIdSetForQuery(), queue); + assertTrue(awaitCleared(queue), "The IdSet must be collected after the query"); + // Also keeps the reference reachable until it is enqueued + assertNull(idSetRef.get()); + } + + /// Returns the IdSet that a query deserializes, and drops the query. + private IdSet createIdSetForQuery() + throws IOException { + return createTransformFunction(createExpression(createIdSet(0, NUM_ROWS / 2)), createQueryContext()).getIdSet(); + } + + /// Waits for the reference to be enqueued, collecting garbage until then or for up to 5 seconds. + private static boolean awaitCleared(ReferenceQueue<?> queue) + throws InterruptedException { + long deadlineMs = System.currentTimeMillis() + 5_000L; + do { + System.gc(); Review Comment: Removed the test altogether, looked unnecessary to me. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
