imply-cheddar commented on code in PR #13268: URL: https://github.com/apache/druid/pull/13268#discussion_r1037676413
########## processing/src/main/java/org/apache/druid/query/UnnestDataSource.java: ########## @@ -0,0 +1,200 @@ +/* + * 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.query; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.ImmutableList; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.segment.SegmentReference; +import org.apache.druid.segment.UnnestSegmentReference; +import org.apache.druid.utils.JvmUtils; + +import javax.annotation.Nullable; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Function; + +public class UnnestDataSource implements DataSource +{ + private final DataSource base; + private final String column; + private final String outputName; + private final LinkedHashSet<String> allowList; + + private UnnestDataSource( + DataSource dataSource, + String columnName, + String outputName, + LinkedHashSet<String> allowList + ) + { + this.base = dataSource; + this.column = columnName; + this.outputName = outputName; + this.allowList = allowList; + } + + @JsonCreator + public static UnnestDataSource create( + @JsonProperty("base") DataSource base, + @JsonProperty("column") String columnName, + @JsonProperty("outputName") String outputName, + @Nullable @JsonProperty("allowList") LinkedHashSet<String> allowList + ) + { + return new UnnestDataSource(base, columnName, outputName, allowList); + } + + @JsonProperty("base") + public DataSource getBase() + { + return base; + } + + @JsonProperty("column") + public String getColumn() + { + return column; + } + + @JsonProperty("outputName") + public String getOutputName() + { + return outputName; + } + + @JsonProperty("allowList") + public LinkedHashSet<String> getAllowList() + { + return allowList; + } + + @Override + public Set<String> getTableNames() + { + return base.getTableNames(); + } + + @Override + public List<DataSource> getChildren() + { + return ImmutableList.of(base); + } + + @Override + public DataSource withChildren(List<DataSource> children) + { + if (children.size() != 1) { + throw new IAE("Expected [1] child, got [%d]", children.size()); + } + return new UnnestDataSource(children.get(0), column, outputName, allowList); + } + + @Override + public boolean isCacheable(boolean isBroker) + { + return base.isCacheable(isBroker); + } + + @Override + public boolean isGlobal() + { + return base.isGlobal(); + } + + @Override + public boolean isConcrete() + { + return base.isConcrete(); + } + + @Override + public Function<SegmentReference, SegmentReference> createSegmentMapFunction( + Query query, + AtomicLong cpuTimeAccumulator + ) + { + final Function<SegmentReference, SegmentReference> segmentMapFn = base.createSegmentMapFunction( + query, + cpuTimeAccumulator + ); + return JvmUtils.safeAccumulateThreadCpuTime( + cpuTimeAccumulator, + () -> { + if (column == null) { + return segmentMapFn; + } else if (column.isEmpty()) { + return segmentMapFn; + } else { + return + segmentMapFn.andThen( + baseSegment -> + new UnnestSegmentReference( + baseSegment, + column, + outputName, + allowList + ) + ); + } + } + ); + + } + + @Override + public DataSource withUpdatedDataSource(DataSource newSource) + { + return new UnnestDataSource(newSource, column, outputName, allowList); + } + + @Override + public byte[] getCacheKey() + { + return null; Review Comment: getCacheKey is documented as ``` /** * Compute a cache key prefix for a data source. This includes the data sources that participate in the RHS of a * join as well as any query specific constructs associated with join data source such as base table filter. This key prefix * can be used in segment level cache or result level cache. The function can return following * - Non-empty byte array - If there is join datasource involved and caching is possible. The result includes * join condition expression, join type and cache key returned by joinable factory for each {@link PreJoinableClause} * - NULL - There is a join but caching is not possible. It may happen if one of the participating datasource * in the JOIN is not cacheable. * * @return the cache key to be used as part of query cache key */ ``` Meaning that a null return type should disable caching. We should likely be even more explicit and set `isCachable` to return false. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
