From 36d7b3cc65629acc772d42493465cf8cbdc99ed9 Mon Sep 17 00:00:00 2001 From: Zoltan Haindrich Date: Thu, 5 Oct 2023 20:37:49 +0200 Subject: [PATCH] Add CalciteSysQueryTest to enable some testing of bindable plans. (#15070) --- .../sql/calcite/CalciteSysQueryTest.java | 63 +++++++++++++++++++ .../druid/sql/calcite/DecoupledIgnore.java | 3 +- .../druid/sql/calcite/util/CalciteTests.java | 41 ++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 sql/src/test/java/org/apache/druid/sql/calcite/CalciteSysQueryTest.java diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteSysQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteSysQueryTest.java new file mode 100644 index 000000000000..5a66383194cf --- /dev/null +++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteSysQueryTest.java @@ -0,0 +1,63 @@ +/* + * 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.sql.calcite; + +import com.google.common.collect.ImmutableList; +import org.apache.druid.sql.calcite.DecoupledIgnore.DecoupledIgnoreProcessor; +import org.apache.druid.sql.calcite.DecoupledIgnore.Modes; +import org.junit.Rule; +import org.junit.Test; + +public class CalciteSysQueryTest extends BaseCalciteQueryTest +{ + @Rule(order = 0) + public DecoupledIgnoreProcessor decoupledIgnoreProcessor = new DecoupledIgnoreProcessor(); + + @Test + public void testTasksSum() + { + notMsqCompatible(); + + testBuilder() + .sql("select datasource, sum(duration) from sys.tasks group by datasource") + .expectedResults(ImmutableList.of( + new Object[]{"foo", 11L}, + new Object[]{"foo2", 22L})) + .expectedLogicalPlan("LogicalAggregate(group=[{0}], EXPR$1=[SUM($1)])\n" + + " LogicalProject(exprs=[[$3, $8]])\n" + + " LogicalTableScan(table=[[sys, tasks]])\n") + .run(); + } + + @DecoupledIgnore(mode = Modes.EXPRESSION_NOT_GROUPED) + @Test + public void testTasksSumOver() + { + notMsqCompatible(); + + testBuilder() + .sql("select datasource, sum(duration) over () from sys.tasks group by datasource") + .expectedResults(ImmutableList.of( + new Object[]{"foo", 11L}, + new Object[]{"foo2", 22L})) + // please add expectedLogicalPlan if this test starts passing! + .run(); + } +} diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/DecoupledIgnore.java b/sql/src/test/java/org/apache/druid/sql/calcite/DecoupledIgnore.java index 0c30432d3d67..029b41f54995 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/DecoupledIgnore.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/DecoupledIgnore.java @@ -52,7 +52,8 @@ enum Modes PLAN_MISMATCH(AssertionError.class, "AssertionError: query #"), NOT_ENOUGH_RULES(DruidException.class, "not enough rules"), CANNOT_CONVERT(DruidException.class, "Cannot convert query parts"), - ERROR_HANDLING(AssertionError.class, "(is was |is was |with message a string containing)"); + ERROR_HANDLING(AssertionError.class, "(is was |is was |with message a string containing)"), + EXPRESSION_NOT_GROUPED(DruidException.class, "Expression '[a-z]+' is not being grouped"); public Class throwableClass; public String regex; diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/util/CalciteTests.java b/sql/src/test/java/org/apache/druid/sql/calcite/util/CalciteTests.java index 17f2ed106377..15568092ad80 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/util/CalciteTests.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/util/CalciteTests.java @@ -39,7 +39,14 @@ import org.apache.druid.discovery.DruidNodeDiscoveryProvider; import org.apache.druid.discovery.NodeRole; import org.apache.druid.guice.annotations.Json; +import org.apache.druid.indexer.RunnerTaskState; +import org.apache.druid.indexer.TaskLocation; +import org.apache.druid.indexer.TaskState; +import org.apache.druid.indexer.TaskStatusPlus; +import org.apache.druid.java.util.common.CloseableIterators; +import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.Pair; +import org.apache.druid.java.util.common.parsers.CloseableIterator; import org.apache.druid.java.util.http.client.HttpClient; import org.apache.druid.java.util.http.client.Request; import org.apache.druid.java.util.http.client.response.HttpResponseHandler; @@ -82,9 +89,11 @@ import java.io.File; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Executor; @@ -368,6 +377,38 @@ public ListenableFuture findCurrentLeader() throw new RuntimeException(e); } } + + @Override + public ListenableFuture> taskStatuses( + @Nullable String state, + @Nullable String dataSource, + @Nullable Integer maxCompletedTasks + ) + { + List tasks = new ArrayList(); + tasks.add(createTaskStatus("id1", DATASOURCE1, 10L)); + tasks.add(createTaskStatus("id1", DATASOURCE1, 1L)); + tasks.add(createTaskStatus("id2", DATASOURCE2, 20L)); + tasks.add(createTaskStatus("id2", DATASOURCE2, 2L)); + return Futures.immediateFuture(CloseableIterators.withEmptyBaggage(tasks.iterator())); + } + + private TaskStatusPlus createTaskStatus(String id, String datasource, Long duration) + { + return new TaskStatusPlus( + id, + "testGroupId", + "testType", + DateTimes.nowUtc(), + DateTimes.nowUtc(), + TaskState.RUNNING, + RunnerTaskState.RUNNING, + duration, + TaskLocation.create("testHost", 1010, -1), + datasource, + null + ); + } }; return new SystemSchema(