move stuff around / prepare to unglobalize

This commit is contained in:
Zoltan Haindrich 2024-07-30 10:22:28 +00:00
parent d227029b6b
commit ce667eeb5e
9 changed files with 111 additions and 13 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<T>
{
HookKey<RelNode> CONVERTED_PLAN = new HookKey<>("converted", RelNode.class);
HookKey<RelNode> LOGICAL_PLAN = new HookKey<>("logicalPlan", RelNode.class);
HookKey<RelNode> DRUID_PLAN = new HookKey<>("druidPlan", RelNode.class);
HookKey<String> SQL = new HookKey<>("sql", String.class);
@Immutable
class HookKey<T>
{
@ -66,14 +71,8 @@ public interface DruidHook<T>
HookKey<?> other = (HookKey<?>) obj;
return Objects.equals(label, other.label) && Objects.equals(type, other.type);
}
}
HookKey<RelNode> CONVERTED_PLAN = new HookKey<>("converted", RelNode.class);
HookKey<RelNode> LOGICAL_PLAN = new HookKey<>("logicalPlan", RelNode.class);
HookKey<RelNode> DRUID_PLAN = new HookKey<>("druidPlan", RelNode.class);
HookKey<String> SQL = new HookKey<>("sql", String.class);
void invoke(HookKey<T> key, T object);
@SuppressFBWarnings({"MS_OOI_PKGPROTECT"})

View File

@ -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<T>
{
}

View File

@ -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<HookKey<?>, List<DruidHook<?>>> 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);
}
<T> Closeable withHook(HookKey<T> key, DruidHook<T> hook)
{
register(key, hook);
return new Closeable()
{
@Override
public void close()
{
unregister(key, hook);
}
};
}
@SuppressWarnings({"rawtypes", "unchecked"})
<T> void dispatch(HookKey<T> key, T object)
{
List<DruidHook<?>> hooks = GLOBAL.get(key);
if (hooks != null) {
for (DruidHook hook : hooks) {
hook.invoke(key, object);
}
}
}
}

View File

@ -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;

View File

@ -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