You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2023/01/03 06:21:57 UTC

[GitHub] [druid] LakshSingla opened a new pull request, #13625: Port Calcite's tests to run with MSQ

LakshSingla opened a new pull request, #13625:
URL: https://github.com/apache/druid/pull/13625

   ### Description
   
   Calcite's test framework has been revamped recently to reduce the dependence on statically created objects and inject the dependency via Guice. This unlocks the ability to inject MSQ-specific dependencies and test out the existing Calcite's tests using the MSQ engine.
   
   Changes accomplished in the PR:
   1. Inject MSQ's specific objects using Guice so that the code path to run the tests via Guice exists.
   2. Refactor the existing test runner
     a. Run steps are now chained together so that one run step can use the results obtained from the previous run step and the final run step's results are used in the verification
     b. The query context is now not taken into account while verifying the queries
     c. Add the support to add custom verifications
   3. The MSQ has an extract step that allows the query results to be extracted from the reports before they are verified.
   
   <hr>
   
   ##### Key changed/added classes in this PR
    * `MSQTestBase`
    * `CalciteSelectQueryTestMSQ`
    * `QueryTestRunner`
   
   <hr>
   
   <!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
   
   This PR has:
   
   - [ ] been self-reviewed.
      - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] a release note entry in the PR description.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster.
   


-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] LakshSingla commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
LakshSingla commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1065312243


##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.LongDimensionSchema;
+import org.apache.druid.data.input.impl.StringDimensionSchema;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.guice.GuiceInjectors;
+import org.apache.druid.guice.IndexingServiceTuningConfigModule;
+import org.apache.druid.guice.JoinableFactoryModule;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.indexing.common.SegmentCacheManagerFactory;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.math.expr.ExprMacroTable;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.guice.MSQExternalDataSourceModule;
+import org.apache.druid.msq.guice.MSQIndexingModule;
+import org.apache.druid.msq.querykit.DataSegmentProvider;
+import org.apache.druid.msq.querykit.LazyResourceHolder;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.DruidProcessingConfig;
+import org.apache.druid.query.ForwardingQueryProcessingPool;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryProcessingPool;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FloatSumAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
+import org.apache.druid.query.lookup.LookupReferencesManager;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexStorageAdapter;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusherConfig;
+import org.apache.druid.segment.loading.SegmentCacheManager;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorsManager;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.server.SegmentManager;
+import org.apache.druid.server.coordination.DataSegmentAnnouncer;
+import org.apache.druid.server.coordination.NoopDataSegmentAnnouncer;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.rel.DruidQuery;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.easymock.EasyMock;
+import org.joda.time.Interval;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE1;
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE2;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS1;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS2;
+
+public class CalciteSelectQueryTestMSQ extends BaseCalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)
+  {
+    super.configureGuice(builder);
+    builder.addModule(binder -> {
+      final LookupReferencesManager lookupReferencesManager =
+          EasyMock.createStrictMock(LookupReferencesManager.class);
+      EasyMock.expect(lookupReferencesManager.getAllLookupNames()).andReturn(Collections.emptySet()).anyTimes();
+      EasyMock.replay(lookupReferencesManager);
+      binder.bind(LookupReferencesManager.class).toInstance(lookupReferencesManager);
+      binder.bind(AppenderatorsManager.class).toProvider(() -> null);
+
+      // Requirements of JoinableFactoryModule
+      binder.bind(SegmentManager.class).toInstance(EasyMock.createMock(SegmentManager.class));
+
+      binder.bind(new TypeLiteral<Set<NodeRole>>()
+      {
+      }).annotatedWith(Self.class).toInstance(ImmutableSet.of(NodeRole.PEON));
+
+      DruidProcessingConfig druidProcessingConfig = new DruidProcessingConfig()
+      {
+        @Override
+        public String getFormatString()
+        {
+          return "test";
+        }
+      };
+      binder.bind(DruidProcessingConfig.class).toInstance(druidProcessingConfig);
+      binder.bind(QueryProcessingPool.class)
+            .toInstance(new ForwardingQueryProcessingPool(Execs.singleThreaded("Test-runner-processing-pool")));
+
+      // Select queries donot require this
+      Injector dummyInjector = GuiceInjectors.makeStartupInjectorWithModules(
+          ImmutableList.of(
+              binder1 -> {
+                binder1.bind(ExprMacroTable.class).toInstance(CalciteTests.createExprMacroTable());
+                binder1.bind(DataSegment.PruneSpecsHolder.class).toInstance(DataSegment.PruneSpecsHolder.DEFAULT);
+              }
+          )
+      );
+      ObjectMapper testMapper = MSQTestBase.setupObjectMapper(dummyInjector);
+      IndexIO indexIO = new IndexIO(testMapper, () -> 0);
+      SegmentCacheManager segmentCacheManager = null;
+      try {
+        segmentCacheManager = new SegmentCacheManagerFactory(testMapper).manufacturate(temporaryFolder.newFolder("test"));
+      }
+      catch (IOException e) {
+        e.printStackTrace();
+      }
+      LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
+      MSQTestSegmentManager segmentManager = new MSQTestSegmentManager(segmentCacheManager, indexIO);
+      try {
+        config.storageDirectory = temporaryFolder.newFolder("localsegments");
+      }
+      catch (IOException e) {
+        throw new ISE(e, "Unable to create folder");
+      }
+      binder.bind(DataSegmentPusher.class).toProvider(() -> new MSQTestDelegateDataSegmentPusher(
+          new LocalDataSegmentPusher(config),
+          segmentManager
+      ));
+      binder.bind(DataSegmentAnnouncer.class).toInstance(new NoopDataSegmentAnnouncer());
+      binder.bind(DataSegmentProvider.class)
+            .toInstance((dataSegment, channelCounters) ->
+                            new LazyResourceHolder<>(getSupplierForSegment(dataSegment)));
+    });
+
+    builder.addModule(new IndexingServiceTuningConfigModule());
+    builder.addModule(new JoinableFactoryModule());
+    builder.addModule(new MSQExternalDataSourceModule());
+    builder.addModule(new MSQIndexingModule());
+
+  }
+
+  @Override
+  public SqlEngine createEngine(
+      QueryLifecycleFactory qlf,
+      ObjectMapper queryJsonMapper,
+      Injector injector
+  )
+  {
+    final WorkerMemoryParameters workerMemoryParameters = Mockito.spy(
+        WorkerMemoryParameters.createInstance(
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            2,
+            10,
+            2
+        )
+    );
+    indexingServiceClient = new MSQTestOverlordServiceClient(
+        queryJsonMapper,
+        injector,
+        new MSQTestTaskActionClient(queryJsonMapper),
+        workerMemoryParameters
+    );
+    return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper);
+  }
+
+  Supplier<Pair<Segment, Closeable>> getSupplierForSegment(SegmentId segmentId)
+  {
+    final QueryableIndex index;
+    try {
+      switch (segmentId.getDataSource()) {
+        case DATASOURCE1:
+          IncrementalIndexSchema foo1Schema = new IncrementalIndexSchema.Builder()
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new FloatSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(foo1Schema)
+              .rows(ROWS1)
+              .buildMMappedIndex();
+          break;
+        case DATASOURCE2:
+          final IncrementalIndexSchema indexSchemaDifferentDim3M1Types = new IncrementalIndexSchema.Builder()
+              .withDimensionsSpec(
+                  new DimensionsSpec(
+                      ImmutableList.of(
+                          new StringDimensionSchema("dim1"),
+                          new StringDimensionSchema("dim2"),
+                          new LongDimensionSchema("dim3")
+                      )
+                  )
+              )
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new LongSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(indexSchemaDifferentDim3M1Types)
+              .rows(ROWS2)
+              .buildMMappedIndex();
+          break;
+        default:
+          throw new ISE("Cannot query segment %s in test runner", segmentId);
+
+      }
+    }
+    catch (IOException e) {
+      throw new ISE(e, "Unable to load index for segment %s", segmentId);
+    }
+    Segment segment = new Segment()
+    {
+      @Override
+      public SegmentId getId()
+      {
+        return segmentId;
+      }
+
+      @Override
+      public Interval getDataInterval()
+      {
+        return segmentId.getInterval();
+      }
+
+      @Nullable
+      @Override
+      public QueryableIndex asQueryableIndex()
+      {
+        return index;
+      }
+
+      @Override
+      public StorageAdapter asStorageAdapter()
+      {
+        return new QueryableIndexStorageAdapter(index);
+      }
+
+      @Override
+      public void close()
+      {
+      }
+    };
+    return new Supplier<Pair<Segment, Closeable>>()
+    {
+      @Override
+      public Pair<Segment, Closeable> get()
+      {
+        return new Pair<>(segment, Closer.create());
+      }
+    };
+  }
+
+  protected Map<String, Object> defaultScanQueryContext(final RowSignature signature)
+  {
+    try {
+      return ImmutableMap.<String, Object>builder()
+                         .putAll(MSQTestBase.DEFAULT_MSQ_CONTEXT)
+                         .put(
+                             DruidQuery.CTX_SCAN_SIGNATURE,
+                             queryFramework().queryJsonMapper().writeValueAsString(signature)
+                         )
+                         .build();
+    }
+    catch (JsonProcessingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+
+
+  @Test
+  public void testOrderThenLimitThenFilter()
+  {
+    RowSignature resultSignature = RowSignature.builder()
+                                               .add("dim1", ColumnType.STRING)
+                                               .build();
+
+    testQueryWithMSQ(
+        "SELECT dim1 FROM "
+        + "(SELECT __time, dim1 FROM druid.foo ORDER BY __time DESC LIMIT 4) "
+        + "WHERE dim1 IN ('abc', 'def')",
+        ImmutableList.of(
+            newScanQueryBuilder()
+                .dataSource(
+                    new QueryDataSource(
+                        newScanQueryBuilder()
+                            .dataSource(CalciteTests.DATASOURCE1)
+                            .intervals(querySegmentSpec(Filtration.eternity()))
+                            .columns(ImmutableList.of("__time", "dim1"))
+                            .limit(4)
+                            .order(ScanQuery.Order.DESCENDING)
+                            .resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
+                            .context(defaultScanQueryContext(resultSignature))
+                            .build()
+                    )
+                )
+                .intervals(querySegmentSpec(Filtration.eternity()))
+                .columns(ImmutableList.of("dim1"))
+                .filters(in("dim1", Arrays.asList("abc", "def"), null))
+                .resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
+                .context(defaultScanQueryContext(resultSignature))
+                .build()
+        ),
+        ImmutableList.of(
+            new Object[]{"abc"},
+            new Object[]{"def"}
+        ),
+        new MSQTestBase.ExtractResults(indexingServiceClient)

Review Comment:
   Updated



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] LakshSingla commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
LakshSingla commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1065335587


##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.LongDimensionSchema;
+import org.apache.druid.data.input.impl.StringDimensionSchema;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.guice.GuiceInjectors;
+import org.apache.druid.guice.IndexingServiceTuningConfigModule;
+import org.apache.druid.guice.JoinableFactoryModule;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.indexing.common.SegmentCacheManagerFactory;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.math.expr.ExprMacroTable;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.guice.MSQExternalDataSourceModule;
+import org.apache.druid.msq.guice.MSQIndexingModule;
+import org.apache.druid.msq.querykit.DataSegmentProvider;
+import org.apache.druid.msq.querykit.LazyResourceHolder;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.DruidProcessingConfig;
+import org.apache.druid.query.ForwardingQueryProcessingPool;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryProcessingPool;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FloatSumAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
+import org.apache.druid.query.lookup.LookupReferencesManager;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexStorageAdapter;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusherConfig;
+import org.apache.druid.segment.loading.SegmentCacheManager;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorsManager;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.server.SegmentManager;
+import org.apache.druid.server.coordination.DataSegmentAnnouncer;
+import org.apache.druid.server.coordination.NoopDataSegmentAnnouncer;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.rel.DruidQuery;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.easymock.EasyMock;
+import org.joda.time.Interval;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE1;
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE2;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS1;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS2;
+
+public class CalciteSelectQueryTestMSQ extends BaseCalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)
+  {
+    super.configureGuice(builder);
+    builder.addModule(binder -> {
+      final LookupReferencesManager lookupReferencesManager =
+          EasyMock.createStrictMock(LookupReferencesManager.class);
+      EasyMock.expect(lookupReferencesManager.getAllLookupNames()).andReturn(Collections.emptySet()).anyTimes();
+      EasyMock.replay(lookupReferencesManager);
+      binder.bind(LookupReferencesManager.class).toInstance(lookupReferencesManager);
+      binder.bind(AppenderatorsManager.class).toProvider(() -> null);
+
+      // Requirements of JoinableFactoryModule
+      binder.bind(SegmentManager.class).toInstance(EasyMock.createMock(SegmentManager.class));
+
+      binder.bind(new TypeLiteral<Set<NodeRole>>()
+      {
+      }).annotatedWith(Self.class).toInstance(ImmutableSet.of(NodeRole.PEON));
+
+      DruidProcessingConfig druidProcessingConfig = new DruidProcessingConfig()
+      {
+        @Override
+        public String getFormatString()
+        {
+          return "test";
+        }
+      };
+      binder.bind(DruidProcessingConfig.class).toInstance(druidProcessingConfig);
+      binder.bind(QueryProcessingPool.class)
+            .toInstance(new ForwardingQueryProcessingPool(Execs.singleThreaded("Test-runner-processing-pool")));
+
+      // Select queries donot require this
+      Injector dummyInjector = GuiceInjectors.makeStartupInjectorWithModules(
+          ImmutableList.of(
+              binder1 -> {
+                binder1.bind(ExprMacroTable.class).toInstance(CalciteTests.createExprMacroTable());
+                binder1.bind(DataSegment.PruneSpecsHolder.class).toInstance(DataSegment.PruneSpecsHolder.DEFAULT);
+              }
+          )
+      );
+      ObjectMapper testMapper = MSQTestBase.setupObjectMapper(dummyInjector);
+      IndexIO indexIO = new IndexIO(testMapper, () -> 0);
+      SegmentCacheManager segmentCacheManager = null;
+      try {
+        segmentCacheManager = new SegmentCacheManagerFactory(testMapper).manufacturate(temporaryFolder.newFolder("test"));
+      }
+      catch (IOException e) {
+        e.printStackTrace();
+      }
+      LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
+      MSQTestSegmentManager segmentManager = new MSQTestSegmentManager(segmentCacheManager, indexIO);
+      try {
+        config.storageDirectory = temporaryFolder.newFolder("localsegments");
+      }
+      catch (IOException e) {
+        throw new ISE(e, "Unable to create folder");
+      }
+      binder.bind(DataSegmentPusher.class).toProvider(() -> new MSQTestDelegateDataSegmentPusher(
+          new LocalDataSegmentPusher(config),
+          segmentManager
+      ));
+      binder.bind(DataSegmentAnnouncer.class).toInstance(new NoopDataSegmentAnnouncer());
+      binder.bind(DataSegmentProvider.class)
+            .toInstance((dataSegment, channelCounters) ->
+                            new LazyResourceHolder<>(getSupplierForSegment(dataSegment)));
+    });
+
+    builder.addModule(new IndexingServiceTuningConfigModule());
+    builder.addModule(new JoinableFactoryModule());
+    builder.addModule(new MSQExternalDataSourceModule());
+    builder.addModule(new MSQIndexingModule());
+
+  }
+
+  @Override
+  public SqlEngine createEngine(
+      QueryLifecycleFactory qlf,
+      ObjectMapper queryJsonMapper,
+      Injector injector
+  )
+  {
+    final WorkerMemoryParameters workerMemoryParameters = Mockito.spy(
+        WorkerMemoryParameters.createInstance(
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            2,
+            10,
+            2
+        )
+    );
+    indexingServiceClient = new MSQTestOverlordServiceClient(
+        queryJsonMapper,
+        injector,
+        new MSQTestTaskActionClient(queryJsonMapper),
+        workerMemoryParameters
+    );
+    return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper);
+  }
+
+  Supplier<Pair<Segment, Closeable>> getSupplierForSegment(SegmentId segmentId)
+  {
+    final QueryableIndex index;
+    try {
+      switch (segmentId.getDataSource()) {
+        case DATASOURCE1:
+          IncrementalIndexSchema foo1Schema = new IncrementalIndexSchema.Builder()
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new FloatSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(foo1Schema)
+              .rows(ROWS1)
+              .buildMMappedIndex();
+          break;
+        case DATASOURCE2:
+          final IncrementalIndexSchema indexSchemaDifferentDim3M1Types = new IncrementalIndexSchema.Builder()
+              .withDimensionsSpec(
+                  new DimensionsSpec(
+                      ImmutableList.of(
+                          new StringDimensionSchema("dim1"),
+                          new StringDimensionSchema("dim2"),
+                          new LongDimensionSchema("dim3")
+                      )
+                  )
+              )
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new LongSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(indexSchemaDifferentDim3M1Types)
+              .rows(ROWS2)
+              .buildMMappedIndex();
+          break;
+        default:
+          throw new ISE("Cannot query segment %s in test runner", segmentId);
+
+      }
+    }
+    catch (IOException e) {
+      throw new ISE(e, "Unable to load index for segment %s", segmentId);
+    }
+    Segment segment = new Segment()
+    {
+      @Override
+      public SegmentId getId()
+      {
+        return segmentId;
+      }
+
+      @Override
+      public Interval getDataInterval()
+      {
+        return segmentId.getInterval();
+      }
+
+      @Nullable
+      @Override
+      public QueryableIndex asQueryableIndex()
+      {
+        return index;
+      }
+
+      @Override
+      public StorageAdapter asStorageAdapter()
+      {
+        return new QueryableIndexStorageAdapter(index);
+      }
+
+      @Override
+      public void close()
+      {
+      }
+    };
+    return new Supplier<Pair<Segment, Closeable>>()
+    {
+      @Override
+      public Pair<Segment, Closeable> get()
+      {
+        return new Pair<>(segment, Closer.create());
+      }
+    };
+  }
+
+  protected Map<String, Object> defaultScanQueryContext(final RowSignature signature)
+  {
+    try {
+      return ImmutableMap.<String, Object>builder()
+                         .putAll(MSQTestBase.DEFAULT_MSQ_CONTEXT)
+                         .put(
+                             DruidQuery.CTX_SCAN_SIGNATURE,
+                             queryFramework().queryJsonMapper().writeValueAsString(signature)
+                         )
+                         .build();
+    }
+    catch (JsonProcessingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+
+
+  @Test
+  public void testOrderThenLimitThenFilter()
+  {
+    RowSignature resultSignature = RowSignature.builder()
+                                               .add("dim1", ColumnType.STRING)
+                                               .build();
+
+    testQueryWithMSQ(

Review Comment:
   Added a top level comment, and pushing out the code for the same



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] LakshSingla commented on pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
LakshSingla commented on PR #13625:
URL: https://github.com/apache/druid/pull/13625#issuecomment-1376737201

   One issue with the tests being written in `sql` module and the MSQ-related stuff being in the `druid-multi-stage-query` module has been that we are not able to setup the MSQ-related stuff in the `sql` module as `sql` does not contain MSQ as a dependency. After an offline discussion with @paul-rogers and @cryptoe, documented below are the few approaches that we can take up.
   
   1. Remove the dependency of `sql` on `druid-multi-stage-query` by breaking the coupling, add `druid-multi-stage-query` as a dependency of `sql` and proceed as mentioned above. However since currently MSQ is present as an extension, this dependency of MSQ on `sql` seems slightly weird. This approach however would be pretty clean.
   2. Use one of the test case definitions like the one proposed in https://github.com/apache/druid/pull/13106 to make the test cases independent from the execution which would also help the PR. This would also help prevent the duplication of tests in the Druid Catalog. 
   3. Add the `druid-multi-stage-query` as a runtime dependency to `sql` (not sure how this would have worked out, just an idea at this point).
   4. Inherit the tests from the sql in the `druid-multi-stage-query`, override the methods that will help add the msq tests and add the plumbing. The only drawback of this approach is that debugging the individual test cases would be difficult since there won't be a run configuration corresponding to them. However that's a minor dev discomfort. Therefore going down with this approach.


-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] LakshSingla commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
LakshSingla commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1065311612


##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/MSQTestBase.java:
##########
@@ -195,7 +197,8 @@
 
 /**
  * Base test runner for running MSQ unit tests. It sets up multi stage query execution environment
- * and populates data for the datasources. The runner does not go via the HTTP layer for communication between the
+ * and populates data for the datasources. The ruimport org.apache.druid.initialization.CoreInjectorBuilder;
+nner does not go via the HTTP layer for communication between the

Review Comment:
   Misclick and dragged and import in the comment. Thanks for pointing it out.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] LakshSingla commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
LakshSingla commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1065737984


##########
processing/src/main/java/org/apache/druid/query/QueryContext.java:
##########
@@ -54,7 +54,7 @@
 {
   private static final QueryContext EMPTY = new QueryContext(null);
 
-  private final Map<String, Object> context;
+  private Map<String, Object> context;

Review Comment:
   This instance replace is not easily possible as the current `Query` interface does not support replace, only override (where the keys are preserved). 
   To solve this currently, I am using the ObjectMapper to create an ObjectNode from the query object and manually clear the "context" key, which is working fine. I also tried creating a Mockito's spy object and overriding the context methods, however, that was failing the equals and the serde tests (presumably because the child classes won't be the same)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] github-code-scanning[bot] commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
github-code-scanning[bot] commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1068724761


##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/VerifyMSQSupportedNativeQueriesFactory.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.druid.msq.test;
+
+import org.apache.druid.query.DataSource;
+import org.apache.druid.query.Query;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.sql.calcite.QueryTestBuilder;
+import org.apache.druid.sql.calcite.QueryTestRunner;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Custom {@link org.apache.druid.sql.calcite.QueryTestRunner.QueryVerifyStep} for the MSQ run queries. Since all the
+ * query types are not supported therefore we skip the {@link org.apache.druid.sql.calcite.QueryTestRunner.VerifyNativeQueries}
+ * check for some query types
+ */
+public class VerifyMSQSupportedNativeQueriesFactory implements QueryTestRunner.QueryVerifyStepFactory
+{
+  @Override
+  public QueryTestRunner.QueryVerifyStep make(QueryTestRunner.BaseExecuteQuery execStep)
+  {
+    return new QueryTestRunner.QueryVerifyStep()
+    {
+      @Override
+      public void verify()
+      {
+        for (QueryTestRunner.QueryResults queryResults : execStep.results()) {

Review Comment:
   ## Unread local variable
   
   Variable 'QueryResults queryResults' is never read.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/3663)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] paul-rogers commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
paul-rogers commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1070698729


##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/MSQTestBase.java:
##########
@@ -637,7 +637,7 @@ public IngestTester testIngestQuery()
     return new IngestTester();
   }
 
-  private ObjectMapper setupObjectMapper(Injector injector)
+  public static ObjectMapper setupObjectMapper(Injector injector)

Review Comment:
   This method is deprecated. (I should add a `@Deprecated` annotation.). The preferred solution is to register the module that contains the item here, or create a "stub" module with the item needed, and use the methods to register Druid modules.



##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,741 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.groupby.TestGroupByBuffers;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.sql.calcite.CalciteQueryTest;
+import org.apache.druid.sql.calcite.QueryTestBuilder;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+
+/**
+ * Runs {@link CalciteQueryTest} but with MSQ engine
+ */
+public class CalciteSelectQueryTestMSQ extends CalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+  private TestGroupByBuffers groupByBuffers;
+
+  @Before
+  public void setup2()
+  {
+    groupByBuffers = TestGroupByBuffers.createDefault();
+  }
+
+  @After
+  public void teardown2()
+  {
+    groupByBuffers.close();
+  }
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)
+  {
+    super.configureGuice(builder);
+    builder.addModules(CalciteMSQTestsHelper.fetchModules(temporaryFolder, groupByBuffers).toArray(new Module[0]));
+  }
+
+
+  @Override
+  public SqlEngine createEngine(
+      QueryLifecycleFactory qlf,
+      ObjectMapper queryJsonMapper,
+      Injector injector
+  )
+  {
+    final WorkerMemoryParameters workerMemoryParameters =
+        WorkerMemoryParameters.createInstance(
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            2,
+            10,
+            2
+        );
+    indexingServiceClient = new MSQTestOverlordServiceClient(
+        queryJsonMapper,
+        injector,
+        new MSQTestTaskActionClient(queryJsonMapper),
+        workerMemoryParameters
+    );
+    return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper);
+  }
+
+  @Override
+  protected QueryTestBuilder testBuilder()
+  {
+    return new QueryTestBuilder(new CalciteTestConfig())
+        .addCustomVerification(new VerifyMSQSupportedNativeQueriesFactory())
+        .addCustomRunner(new ExtractResultsFactory(() -> (MSQTestOverlordServiceClient) ((MSQTaskSqlEngine) queryFramework().engine()).overlordClient()))
+        .skipVectorize(true)
+        .verifyNativeQueries(false);
+  }
+
+  @Ignore("Union datasource not supported by MSQ")
+  @Override
+  public void testUnionAllSameTableTwiceWithSameMapping()
+  {
+
+  }

Review Comment:
   This approach seems clunky. There are hundreds of test, each of which will need a stub here. Someone will modify the Calcite tests, not know about this, and will then encounter test failures on the build during MSQ tests that they can't easily understand.
   
   I wonder, can we do it this way? For a test that _does not_ support MSQ:
   
   ```java
     @Test
     public void someClassicOnlyCalciteTest()
     {
       // Actual test here
     }
   ```
   
   And, for tests that are validated to work with MSQ:
   
   ```java
     @Test
     public void someMSQCompatibleCalciteTest()
     {
       msqCompatible();
       // Actual test here
     }
   ```
   
   The `msqCompatible` function is defined as:
   
   ```java
   class BaseCalciteQueryTest
   {
     // ...
     private boolean msqCompatibleFlag;
   
     protected void msqCompabible()
     {
       msqCompatibleFlag = true;
     }
   ```
   
   Finally, the info is passed to the test builder:
   
   ```java
     protected QueryTestBuilder testBuilder()
     {
       return new QueryTestBuilder(new CalciteTestConfig())
           .cannotVectorize(cannotVectorize)
           .skipVectorize(skipVectorize)
           .msqCompatible(msqCompatibleFlag); // New line
     }
   ```
   
   Now, the test runner can check the per-test MSQ compatible flag (in the test builder) and skip the test if the framework is running MSQ and the test is not compatible. (To be clear: there are two flags: the one shown above that avoids the need to pass another argument to `testQuery()`, and a second one, hinted at above, in the test builder.)
   
   With this model, all Calcite tests can be run on MSQ, and will do nothing by default. We only need an entry (that one method call) for tests that _can_ run under MSQ.
   
   With this, adding a new test is safe: MSQ will ignore it by default. The MSQ team can occasionally look for new test cases, check if they can run under MSQ, and if so, add the needed marker.
   
   Note that the same could be done with an annotation, but that would require a custom test runner. The above is a crude-but-effective solution that avoids the need for any fancy stuff.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] paul-rogers merged pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
paul-rogers merged PR #13625:
URL: https://github.com/apache/druid/pull/13625


-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] paul-rogers commented on a diff in pull request #13625: Port Calcite's tests to run with MSQ

Posted by GitBox <gi...@apache.org>.
paul-rogers commented on code in PR #13625:
URL: https://github.com/apache/druid/pull/13625#discussion_r1061072754


##########
processing/src/main/java/org/apache/druid/query/QueryContext.java:
##########
@@ -54,7 +54,7 @@
 {
   private static final QueryContext EMPTY = new QueryContext(null);
 
-  private final Map<String, Object> context;
+  private Map<String, Object> context;

Review Comment:
   A large amount of code depends on the fact that this class is immutable. There was a big project a couple months back to clean up problems that arose when an earlier version was mutable. 
   
   If we wish to clear the context, we should do so by replacing one instance of this class with the `EMPTY` context.



##########
sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java:
##########
@@ -1005,28 +1030,31 @@ protected static boolean isRewriteJoinToFilter(final Map<String, Object> queryCo
     );
   }
 
+
   /**
    * Override not just the outer query context, but also the contexts of all subqueries.
    */
-  public static <T> Query<T> recursivelyOverrideContext(final Query<T> query, final Map<String, Object> context)
+  public static <T> Query<T> recursivelyClearContext(final Query<T> query)
   {
-    return query.withDataSource(recursivelyOverrideContext(query.getDataSource(), context))
-                .withOverriddenContext(context);
+    Query<T> newQuery = query.withDataSource(recursivelyClearContext(query.getDataSource()));
+    newQuery.context().clear();

Review Comment:
   We need to pass in an empty context to a copy of the native query if we want to clear the context.



##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/MSQTestBase.java:
##########
@@ -195,7 +197,8 @@
 
 /**
  * Base test runner for running MSQ unit tests. It sets up multi stage query execution environment
- * and populates data for the datasources. The runner does not go via the HTTP layer for communication between the
+ * and populates data for the datasources. The ruimport org.apache.druid.initialization.CoreInjectorBuilder;
+nner does not go via the HTTP layer for communication between the

Review Comment:
   "ruimport" rogue copy/paste?



##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.LongDimensionSchema;
+import org.apache.druid.data.input.impl.StringDimensionSchema;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.guice.GuiceInjectors;
+import org.apache.druid.guice.IndexingServiceTuningConfigModule;
+import org.apache.druid.guice.JoinableFactoryModule;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.indexing.common.SegmentCacheManagerFactory;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.math.expr.ExprMacroTable;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.guice.MSQExternalDataSourceModule;
+import org.apache.druid.msq.guice.MSQIndexingModule;
+import org.apache.druid.msq.querykit.DataSegmentProvider;
+import org.apache.druid.msq.querykit.LazyResourceHolder;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.DruidProcessingConfig;
+import org.apache.druid.query.ForwardingQueryProcessingPool;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryProcessingPool;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FloatSumAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
+import org.apache.druid.query.lookup.LookupReferencesManager;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexStorageAdapter;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusherConfig;
+import org.apache.druid.segment.loading.SegmentCacheManager;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorsManager;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.server.SegmentManager;
+import org.apache.druid.server.coordination.DataSegmentAnnouncer;
+import org.apache.druid.server.coordination.NoopDataSegmentAnnouncer;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.rel.DruidQuery;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.easymock.EasyMock;
+import org.joda.time.Interval;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE1;
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE2;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS1;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS2;
+
+public class CalciteSelectQueryTestMSQ extends BaseCalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)

Review Comment:
   I wonder how much of this code is specific to this test and how much is generic to each MSQ setup? It might be handy to factor out the MSQ setup somehow. One option is to pass into the `SqlTestFramework.Builder()` method some kind of extra object that, when invoked, will set up all the MSQ stuff. That way, we only need to set the option once (via a flag, etc.) and the test framework can build up the MSQ structure as required per test without the code having to be copy/pasted into each test class (or into some base class.)



##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.LongDimensionSchema;
+import org.apache.druid.data.input.impl.StringDimensionSchema;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.guice.GuiceInjectors;
+import org.apache.druid.guice.IndexingServiceTuningConfigModule;
+import org.apache.druid.guice.JoinableFactoryModule;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.indexing.common.SegmentCacheManagerFactory;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.math.expr.ExprMacroTable;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.guice.MSQExternalDataSourceModule;
+import org.apache.druid.msq.guice.MSQIndexingModule;
+import org.apache.druid.msq.querykit.DataSegmentProvider;
+import org.apache.druid.msq.querykit.LazyResourceHolder;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.DruidProcessingConfig;
+import org.apache.druid.query.ForwardingQueryProcessingPool;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryProcessingPool;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FloatSumAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
+import org.apache.druid.query.lookup.LookupReferencesManager;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexStorageAdapter;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusherConfig;
+import org.apache.druid.segment.loading.SegmentCacheManager;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorsManager;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.server.SegmentManager;
+import org.apache.druid.server.coordination.DataSegmentAnnouncer;
+import org.apache.druid.server.coordination.NoopDataSegmentAnnouncer;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.rel.DruidQuery;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.easymock.EasyMock;
+import org.joda.time.Interval;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE1;
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE2;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS1;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS2;
+
+public class CalciteSelectQueryTestMSQ extends BaseCalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)
+  {
+    super.configureGuice(builder);
+    builder.addModule(binder -> {
+      final LookupReferencesManager lookupReferencesManager =
+          EasyMock.createStrictMock(LookupReferencesManager.class);
+      EasyMock.expect(lookupReferencesManager.getAllLookupNames()).andReturn(Collections.emptySet()).anyTimes();
+      EasyMock.replay(lookupReferencesManager);
+      binder.bind(LookupReferencesManager.class).toInstance(lookupReferencesManager);
+      binder.bind(AppenderatorsManager.class).toProvider(() -> null);
+
+      // Requirements of JoinableFactoryModule
+      binder.bind(SegmentManager.class).toInstance(EasyMock.createMock(SegmentManager.class));
+
+      binder.bind(new TypeLiteral<Set<NodeRole>>()
+      {
+      }).annotatedWith(Self.class).toInstance(ImmutableSet.of(NodeRole.PEON));
+
+      DruidProcessingConfig druidProcessingConfig = new DruidProcessingConfig()
+      {
+        @Override
+        public String getFormatString()
+        {
+          return "test";
+        }
+      };
+      binder.bind(DruidProcessingConfig.class).toInstance(druidProcessingConfig);
+      binder.bind(QueryProcessingPool.class)
+            .toInstance(new ForwardingQueryProcessingPool(Execs.singleThreaded("Test-runner-processing-pool")));
+
+      // Select queries donot require this
+      Injector dummyInjector = GuiceInjectors.makeStartupInjectorWithModules(
+          ImmutableList.of(
+              binder1 -> {
+                binder1.bind(ExprMacroTable.class).toInstance(CalciteTests.createExprMacroTable());
+                binder1.bind(DataSegment.PruneSpecsHolder.class).toInstance(DataSegment.PruneSpecsHolder.DEFAULT);
+              }
+          )
+      );
+      ObjectMapper testMapper = MSQTestBase.setupObjectMapper(dummyInjector);
+      IndexIO indexIO = new IndexIO(testMapper, () -> 0);
+      SegmentCacheManager segmentCacheManager = null;
+      try {
+        segmentCacheManager = new SegmentCacheManagerFactory(testMapper).manufacturate(temporaryFolder.newFolder("test"));
+      }
+      catch (IOException e) {
+        e.printStackTrace();
+      }
+      LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
+      MSQTestSegmentManager segmentManager = new MSQTestSegmentManager(segmentCacheManager, indexIO);
+      try {
+        config.storageDirectory = temporaryFolder.newFolder("localsegments");
+      }
+      catch (IOException e) {
+        throw new ISE(e, "Unable to create folder");
+      }
+      binder.bind(DataSegmentPusher.class).toProvider(() -> new MSQTestDelegateDataSegmentPusher(
+          new LocalDataSegmentPusher(config),
+          segmentManager
+      ));
+      binder.bind(DataSegmentAnnouncer.class).toInstance(new NoopDataSegmentAnnouncer());
+      binder.bind(DataSegmentProvider.class)
+            .toInstance((dataSegment, channelCounters) ->
+                            new LazyResourceHolder<>(getSupplierForSegment(dataSegment)));
+    });
+
+    builder.addModule(new IndexingServiceTuningConfigModule());
+    builder.addModule(new JoinableFactoryModule());
+    builder.addModule(new MSQExternalDataSourceModule());
+    builder.addModule(new MSQIndexingModule());
+
+  }
+
+  @Override
+  public SqlEngine createEngine(
+      QueryLifecycleFactory qlf,
+      ObjectMapper queryJsonMapper,
+      Injector injector
+  )
+  {
+    final WorkerMemoryParameters workerMemoryParameters = Mockito.spy(
+        WorkerMemoryParameters.createInstance(
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            2,
+            10,
+            2
+        )
+    );
+    indexingServiceClient = new MSQTestOverlordServiceClient(
+        queryJsonMapper,
+        injector,
+        new MSQTestTaskActionClient(queryJsonMapper),
+        workerMemoryParameters
+    );
+    return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper);
+  }
+
+  Supplier<Pair<Segment, Closeable>> getSupplierForSegment(SegmentId segmentId)
+  {
+    final QueryableIndex index;
+    try {
+      switch (segmentId.getDataSource()) {
+        case DATASOURCE1:
+          IncrementalIndexSchema foo1Schema = new IncrementalIndexSchema.Builder()
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new FloatSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(foo1Schema)
+              .rows(ROWS1)
+              .buildMMappedIndex();
+          break;
+        case DATASOURCE2:
+          final IncrementalIndexSchema indexSchemaDifferentDim3M1Types = new IncrementalIndexSchema.Builder()
+              .withDimensionsSpec(
+                  new DimensionsSpec(
+                      ImmutableList.of(
+                          new StringDimensionSchema("dim1"),
+                          new StringDimensionSchema("dim2"),
+                          new LongDimensionSchema("dim3")
+                      )
+                  )
+              )
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new LongSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(indexSchemaDifferentDim3M1Types)
+              .rows(ROWS2)
+              .buildMMappedIndex();
+          break;
+        default:
+          throw new ISE("Cannot query segment %s in test runner", segmentId);
+
+      }
+    }
+    catch (IOException e) {
+      throw new ISE(e, "Unable to load index for segment %s", segmentId);
+    }
+    Segment segment = new Segment()
+    {
+      @Override
+      public SegmentId getId()
+      {
+        return segmentId;
+      }
+
+      @Override
+      public Interval getDataInterval()
+      {
+        return segmentId.getInterval();
+      }
+
+      @Nullable
+      @Override
+      public QueryableIndex asQueryableIndex()
+      {
+        return index;
+      }
+
+      @Override
+      public StorageAdapter asStorageAdapter()
+      {
+        return new QueryableIndexStorageAdapter(index);
+      }
+
+      @Override
+      public void close()
+      {
+      }
+    };
+    return new Supplier<Pair<Segment, Closeable>>()
+    {
+      @Override
+      public Pair<Segment, Closeable> get()
+      {
+        return new Pair<>(segment, Closer.create());
+      }
+    };
+  }
+
+  protected Map<String, Object> defaultScanQueryContext(final RowSignature signature)
+  {
+    try {
+      return ImmutableMap.<String, Object>builder()
+                         .putAll(MSQTestBase.DEFAULT_MSQ_CONTEXT)
+                         .put(
+                             DruidQuery.CTX_SCAN_SIGNATURE,
+                             queryFramework().queryJsonMapper().writeValueAsString(signature)
+                         )
+                         .build();
+    }
+    catch (JsonProcessingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+
+
+  @Test
+  public void testOrderThenLimitThenFilter()
+  {
+    RowSignature resultSignature = RowSignature.builder()
+                                               .add("dim1", ColumnType.STRING)
+                                               .build();
+
+    testQueryWithMSQ(

Review Comment:
   This test uses an MSQ-specific test function. The original goal was to reuse the existing tests. Is this an intermediate step along the way? Or, have we opted to copy/paste tests to the MSQ form?



##########
sql/src/test/java/org/apache/druid/sql/calcite/QueryTestBuilder.java:
##########
@@ -156,6 +157,24 @@ public QueryTestBuilder expectedResults(
     return this;
   }
 
+  public QueryTestBuilder addCustomRunner(
+      QueryTestRunner.QueryRunStepFactory factory
+  )
+  {
+    this.customRunners.add(factory);
+    return this;
+  }
+
+  public QueryTestBuilder setCustomRunners(
+      List<QueryTestRunner.QueryRunStepFactory> factories
+  )
+  {
+    this.customRunners = new ArrayList<>();
+    this.customRunners.addAll(factories);

Review Comment:
   I suspect IntelliJ inspections will tell you to do:
   
   ```
   this.customRunners = new ArrayList<>(factories);
   ```



##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/test/CalciteSelectQueryTestMSQ.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.druid.msq.test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.LongDimensionSchema;
+import org.apache.druid.data.input.impl.StringDimensionSchema;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.guice.DruidInjectorBuilder;
+import org.apache.druid.guice.GuiceInjectors;
+import org.apache.druid.guice.IndexingServiceTuningConfigModule;
+import org.apache.druid.guice.JoinableFactoryModule;
+import org.apache.druid.guice.annotations.Self;
+import org.apache.druid.indexing.common.SegmentCacheManagerFactory;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.math.expr.ExprMacroTable;
+import org.apache.druid.msq.exec.WorkerMemoryParameters;
+import org.apache.druid.msq.guice.MSQExternalDataSourceModule;
+import org.apache.druid.msq.guice.MSQIndexingModule;
+import org.apache.druid.msq.querykit.DataSegmentProvider;
+import org.apache.druid.msq.querykit.LazyResourceHolder;
+import org.apache.druid.msq.sql.MSQTaskSqlEngine;
+import org.apache.druid.query.DruidProcessingConfig;
+import org.apache.druid.query.ForwardingQueryProcessingPool;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryProcessingPool;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FloatSumAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory;
+import org.apache.druid.query.lookup.LookupReferencesManager;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexStorageAdapter;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.loading.DataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusher;
+import org.apache.druid.segment.loading.LocalDataSegmentPusherConfig;
+import org.apache.druid.segment.loading.SegmentCacheManager;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorsManager;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.QueryLifecycleFactory;
+import org.apache.druid.server.SegmentManager;
+import org.apache.druid.server.coordination.DataSegmentAnnouncer;
+import org.apache.druid.server.coordination.NoopDataSegmentAnnouncer;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.rel.DruidQuery;
+import org.apache.druid.sql.calcite.run.SqlEngine;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.easymock.EasyMock;
+import org.joda.time.Interval;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE1;
+import static org.apache.druid.sql.calcite.util.CalciteTests.DATASOURCE2;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS1;
+import static org.apache.druid.sql.calcite.util.TestDataBuilder.ROWS2;
+
+public class CalciteSelectQueryTestMSQ extends BaseCalciteQueryTest
+{
+
+  private MSQTestOverlordServiceClient indexingServiceClient;
+
+  @Override
+  public void configureGuice(DruidInjectorBuilder builder)
+  {
+    super.configureGuice(builder);
+    builder.addModule(binder -> {
+      final LookupReferencesManager lookupReferencesManager =
+          EasyMock.createStrictMock(LookupReferencesManager.class);
+      EasyMock.expect(lookupReferencesManager.getAllLookupNames()).andReturn(Collections.emptySet()).anyTimes();
+      EasyMock.replay(lookupReferencesManager);
+      binder.bind(LookupReferencesManager.class).toInstance(lookupReferencesManager);
+      binder.bind(AppenderatorsManager.class).toProvider(() -> null);
+
+      // Requirements of JoinableFactoryModule
+      binder.bind(SegmentManager.class).toInstance(EasyMock.createMock(SegmentManager.class));
+
+      binder.bind(new TypeLiteral<Set<NodeRole>>()
+      {
+      }).annotatedWith(Self.class).toInstance(ImmutableSet.of(NodeRole.PEON));
+
+      DruidProcessingConfig druidProcessingConfig = new DruidProcessingConfig()
+      {
+        @Override
+        public String getFormatString()
+        {
+          return "test";
+        }
+      };
+      binder.bind(DruidProcessingConfig.class).toInstance(druidProcessingConfig);
+      binder.bind(QueryProcessingPool.class)
+            .toInstance(new ForwardingQueryProcessingPool(Execs.singleThreaded("Test-runner-processing-pool")));
+
+      // Select queries donot require this
+      Injector dummyInjector = GuiceInjectors.makeStartupInjectorWithModules(
+          ImmutableList.of(
+              binder1 -> {
+                binder1.bind(ExprMacroTable.class).toInstance(CalciteTests.createExprMacroTable());
+                binder1.bind(DataSegment.PruneSpecsHolder.class).toInstance(DataSegment.PruneSpecsHolder.DEFAULT);
+              }
+          )
+      );
+      ObjectMapper testMapper = MSQTestBase.setupObjectMapper(dummyInjector);
+      IndexIO indexIO = new IndexIO(testMapper, () -> 0);
+      SegmentCacheManager segmentCacheManager = null;
+      try {
+        segmentCacheManager = new SegmentCacheManagerFactory(testMapper).manufacturate(temporaryFolder.newFolder("test"));
+      }
+      catch (IOException e) {
+        e.printStackTrace();
+      }
+      LocalDataSegmentPusherConfig config = new LocalDataSegmentPusherConfig();
+      MSQTestSegmentManager segmentManager = new MSQTestSegmentManager(segmentCacheManager, indexIO);
+      try {
+        config.storageDirectory = temporaryFolder.newFolder("localsegments");
+      }
+      catch (IOException e) {
+        throw new ISE(e, "Unable to create folder");
+      }
+      binder.bind(DataSegmentPusher.class).toProvider(() -> new MSQTestDelegateDataSegmentPusher(
+          new LocalDataSegmentPusher(config),
+          segmentManager
+      ));
+      binder.bind(DataSegmentAnnouncer.class).toInstance(new NoopDataSegmentAnnouncer());
+      binder.bind(DataSegmentProvider.class)
+            .toInstance((dataSegment, channelCounters) ->
+                            new LazyResourceHolder<>(getSupplierForSegment(dataSegment)));
+    });
+
+    builder.addModule(new IndexingServiceTuningConfigModule());
+    builder.addModule(new JoinableFactoryModule());
+    builder.addModule(new MSQExternalDataSourceModule());
+    builder.addModule(new MSQIndexingModule());
+
+  }
+
+  @Override
+  public SqlEngine createEngine(
+      QueryLifecycleFactory qlf,
+      ObjectMapper queryJsonMapper,
+      Injector injector
+  )
+  {
+    final WorkerMemoryParameters workerMemoryParameters = Mockito.spy(
+        WorkerMemoryParameters.createInstance(
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50,
+            2,
+            10,
+            2
+        )
+    );
+    indexingServiceClient = new MSQTestOverlordServiceClient(
+        queryJsonMapper,
+        injector,
+        new MSQTestTaskActionClient(queryJsonMapper),
+        workerMemoryParameters
+    );
+    return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper);
+  }
+
+  Supplier<Pair<Segment, Closeable>> getSupplierForSegment(SegmentId segmentId)
+  {
+    final QueryableIndex index;
+    try {
+      switch (segmentId.getDataSource()) {
+        case DATASOURCE1:
+          IncrementalIndexSchema foo1Schema = new IncrementalIndexSchema.Builder()
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new FloatSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(foo1Schema)
+              .rows(ROWS1)
+              .buildMMappedIndex();
+          break;
+        case DATASOURCE2:
+          final IncrementalIndexSchema indexSchemaDifferentDim3M1Types = new IncrementalIndexSchema.Builder()
+              .withDimensionsSpec(
+                  new DimensionsSpec(
+                      ImmutableList.of(
+                          new StringDimensionSchema("dim1"),
+                          new StringDimensionSchema("dim2"),
+                          new LongDimensionSchema("dim3")
+                      )
+                  )
+              )
+              .withMetrics(
+                  new CountAggregatorFactory("cnt"),
+                  new LongSumAggregatorFactory("m1", "m1"),
+                  new DoubleSumAggregatorFactory("m2", "m2"),
+                  new HyperUniquesAggregatorFactory("unique_dim1", "dim1")
+              )
+              .withRollup(false)
+              .build();
+          index = IndexBuilder
+              .create()
+              .tmpDir(new File(temporaryFolder.newFolder(), "1"))
+              .segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+              .schema(indexSchemaDifferentDim3M1Types)
+              .rows(ROWS2)
+              .buildMMappedIndex();
+          break;
+        default:
+          throw new ISE("Cannot query segment %s in test runner", segmentId);
+
+      }
+    }
+    catch (IOException e) {
+      throw new ISE(e, "Unable to load index for segment %s", segmentId);
+    }
+    Segment segment = new Segment()
+    {
+      @Override
+      public SegmentId getId()
+      {
+        return segmentId;
+      }
+
+      @Override
+      public Interval getDataInterval()
+      {
+        return segmentId.getInterval();
+      }
+
+      @Nullable
+      @Override
+      public QueryableIndex asQueryableIndex()
+      {
+        return index;
+      }
+
+      @Override
+      public StorageAdapter asStorageAdapter()
+      {
+        return new QueryableIndexStorageAdapter(index);
+      }
+
+      @Override
+      public void close()
+      {
+      }
+    };
+    return new Supplier<Pair<Segment, Closeable>>()
+    {
+      @Override
+      public Pair<Segment, Closeable> get()
+      {
+        return new Pair<>(segment, Closer.create());
+      }
+    };
+  }
+
+  protected Map<String, Object> defaultScanQueryContext(final RowSignature signature)
+  {
+    try {
+      return ImmutableMap.<String, Object>builder()
+                         .putAll(MSQTestBase.DEFAULT_MSQ_CONTEXT)
+                         .put(
+                             DruidQuery.CTX_SCAN_SIGNATURE,
+                             queryFramework().queryJsonMapper().writeValueAsString(signature)
+                         )
+                         .build();
+    }
+    catch (JsonProcessingException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+
+
+  @Test
+  public void testOrderThenLimitThenFilter()
+  {
+    RowSignature resultSignature = RowSignature.builder()
+                                               .add("dim1", ColumnType.STRING)
+                                               .build();
+
+    testQueryWithMSQ(
+        "SELECT dim1 FROM "
+        + "(SELECT __time, dim1 FROM druid.foo ORDER BY __time DESC LIMIT 4) "
+        + "WHERE dim1 IN ('abc', 'def')",
+        ImmutableList.of(
+            newScanQueryBuilder()
+                .dataSource(
+                    new QueryDataSource(
+                        newScanQueryBuilder()
+                            .dataSource(CalciteTests.DATASOURCE1)
+                            .intervals(querySegmentSpec(Filtration.eternity()))
+                            .columns(ImmutableList.of("__time", "dim1"))
+                            .limit(4)
+                            .order(ScanQuery.Order.DESCENDING)
+                            .resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
+                            .context(defaultScanQueryContext(resultSignature))
+                            .build()
+                    )
+                )
+                .intervals(querySegmentSpec(Filtration.eternity()))
+                .columns(ImmutableList.of("dim1"))
+                .filters(in("dim1", Arrays.asList("abc", "def"), null))
+                .resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
+                .context(defaultScanQueryContext(resultSignature))
+                .build()
+        ),
+        ImmutableList.of(
+            new Object[]{"abc"},
+            new Object[]{"def"}
+        ),
+        new MSQTestBase.ExtractResults(indexingServiceClient)

Review Comment:
   Getting the results seems pretty generic: the kind of thing that should be built into the query framework, not specified per-test.
   
   Since the test framework will need the indexing service client, one way to provide that is via the test config class which is passed into the builder, then onto the runner.



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org