diff --git a/quidem-it/src/main/java/org/apache/druid/quidem/QuidemRecorder.java b/quidem-it/src/main/java/org/apache/druid/quidem/QuidemRecorder.java index 8a06815d651..a05f4fec819 100644 --- a/quidem-it/src/main/java/org/apache/druid/quidem/QuidemRecorder.java +++ b/quidem-it/src/main/java/org/apache/druid/quidem/QuidemRecorder.java @@ -19,7 +19,7 @@ package org.apache.druid.quidem; -import org.apache.druid.sql.calcite.run.DruidHook; +import org.apache.druid.sql.hook.DruidHook; import java.io.File; import java.io.FileNotFoundException; diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java b/sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java index 8b39be932e0..a1855288cd2 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/planner/CalciteRulesManager.java @@ -68,8 +68,8 @@ import org.apache.druid.sql.calcite.rule.RewriteFirstValueLastValueRule; import org.apache.druid.sql.calcite.rule.SortCollapseRule; import org.apache.druid.sql.calcite.rule.logical.DruidAggregateRemoveRedundancyRule; import org.apache.druid.sql.calcite.rule.logical.DruidLogicalRules; -import org.apache.druid.sql.calcite.run.DruidHook; import org.apache.druid.sql.calcite.run.EngineFeature; +import org.apache.druid.sql.hook.DruidHook; import java.util.ArrayList; import java.util.Collections; diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerFactory.java b/sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerFactory.java index 61bf4ec8f7e..ac369fd3f08 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerFactory.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerFactory.java @@ -46,10 +46,10 @@ import org.apache.druid.server.security.AuthorizerMapper; import org.apache.druid.server.security.NoopEscalator; import org.apache.druid.sql.calcite.parser.DruidSqlParserImplFactory; import org.apache.druid.sql.calcite.planner.convertlet.DruidConvertletTable; -import org.apache.druid.sql.calcite.run.DruidHook; import org.apache.druid.sql.calcite.run.SqlEngine; import org.apache.druid.sql.calcite.schema.DruidSchemaCatalog; import org.apache.druid.sql.calcite.schema.DruidSchemaName; +import org.apache.druid.sql.hook.DruidHook; import java.util.Map; import java.util.Properties; diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/planner/QueryHandler.java b/sql/src/main/java/org/apache/druid/sql/calcite/planner/QueryHandler.java index 41b03760c1c..ed21e929f1f 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/planner/QueryHandler.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/planner/QueryHandler.java @@ -72,10 +72,10 @@ import org.apache.druid.sql.calcite.rel.DruidRel; import org.apache.druid.sql.calcite.rel.DruidUnionRel; import org.apache.druid.sql.calcite.rel.logical.DruidLogicalConvention; import org.apache.druid.sql.calcite.rel.logical.DruidLogicalNode; -import org.apache.druid.sql.calcite.run.DruidHook; import org.apache.druid.sql.calcite.run.EngineFeature; import org.apache.druid.sql.calcite.run.QueryMaker; import org.apache.druid.sql.calcite.table.DruidTable; +import org.apache.druid.sql.hook.DruidHook; import org.apache.druid.utils.Throwables; import javax.annotation.Nullable; diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/run/DruidHook.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java similarity index 98% rename from sql/src/main/java/org/apache/druid/sql/calcite/run/DruidHook.java rename to sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java index e23d7a2c3a3..bc9226ed3f4 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/run/DruidHook.java +++ b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.druid.sql.calcite.run; +package org.apache.druid.sql.hook; import com.google.errorprone.annotations.Immutable; import org.apache.calcite.rel.RelNode; @@ -33,6 +33,11 @@ import java.util.Objects; @FunctionalInterface public interface DruidHook { + HookKey CONVERTED_PLAN = new HookKey<>("converted", RelNode.class); + HookKey LOGICAL_PLAN = new HookKey<>("logicalPlan", RelNode.class); + HookKey DRUID_PLAN = new HookKey<>("druidPlan", RelNode.class); + HookKey SQL = new HookKey<>("sql", String.class); + @Immutable class HookKey { @@ -66,14 +71,8 @@ public interface DruidHook HookKey other = (HookKey) obj; return Objects.equals(label, other.label) && Objects.equals(type, other.type); } - } - HookKey CONVERTED_PLAN = new HookKey<>("converted", RelNode.class); - HookKey LOGICAL_PLAN = new HookKey<>("logicalPlan", RelNode.class); - HookKey DRUID_PLAN = new HookKey<>("druidPlan", RelNode.class); - HookKey SQL = new HookKey<>("sql", String.class); - void invoke(HookKey key, T object); @SuppressFBWarnings({"MS_OOI_PKGPROTECT"}) diff --git a/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java new file mode 100644 index 00000000000..e1bf52b74a8 --- /dev/null +++ b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java @@ -0,0 +1,24 @@ +/* + * 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.hook; + +public interface DruidHook2 +{ +} diff --git a/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java new file mode 100644 index 00000000000..cfc37523a02 --- /dev/null +++ b/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java @@ -0,0 +1,74 @@ +/* + * 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.hook; + +import com.google.inject.Inject; +import org.apache.druid.sql.hook.DruidHook.HookKey; + +import java.io.Closeable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DruidHookDispatcher +{ + @Inject + public DruidHookDispatcher() + { + } + + Map, List>> GLOBAL = new HashMap<>(); + + void register(HookKey label, DruidHook hook) + { + GLOBAL.computeIfAbsent(label, k -> new ArrayList<>()).add(hook); + } + + void unregister(HookKey key, DruidHook hook) + { + GLOBAL.get(key).remove(hook); + } + + Closeable withHook(HookKey key, DruidHook hook) + { + register(key, hook); + return new Closeable() + { + @Override + public void close() + { + unregister(key, hook); + } + }; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + void dispatch(HookKey key, T object) + { + List> hooks = GLOBAL.get(key); + if (hooks != null) { + for (DruidHook hook : hooks) { + hook.invoke(key, object); + } + } + } + +} diff --git a/sql/src/test/java/org/apache/druid/quidem/DruidQuidemCommandHandler.java b/sql/src/test/java/org/apache/druid/quidem/DruidQuidemCommandHandler.java index 86876cb90ee..58ab62a24f9 100644 --- a/sql/src/test/java/org/apache/druid/quidem/DruidQuidemCommandHandler.java +++ b/sql/src/test/java/org/apache/druid/quidem/DruidQuidemCommandHandler.java @@ -34,9 +34,9 @@ import org.apache.calcite.util.Util; import org.apache.druid.query.Query; import org.apache.druid.sql.calcite.BaseCalciteQueryTest; import org.apache.druid.sql.calcite.rel.DruidRel; -import org.apache.druid.sql.calcite.run.DruidHook; -import org.apache.druid.sql.calcite.run.DruidHook.HookKey; import org.apache.druid.sql.calcite.util.QueryLogHook; +import org.apache.druid.sql.hook.DruidHook; +import org.apache.druid.sql.hook.DruidHook.HookKey; import java.io.Closeable; import java.io.IOException; diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/run/DruidHookTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/run/DruidHookTest.java index f8bfb7bdfe0..62190fb0c69 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/run/DruidHookTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/run/DruidHookTest.java @@ -20,6 +20,7 @@ package org.apache.druid.sql.calcite.run; import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.druid.sql.hook.DruidHook; import org.junit.Test; public class DruidHookTest