-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unnest functionality for Druid #13268
Changes from 24 commits
4e686a8
f354dd7
f9ac767
9f98b12
31612fc
035c423
6a9b0d7
ba55890
7333da5
90073f6
f6fc1aa
bbc66f5
e146ebb
bb22a92
1908242
3de1161
d1a884a
401a9b2
bce6ffe
240afe2
9821c53
b7ab781
5fd3dd7
576dbcc
e56b0b2
bb66e59
8f71b81
e312f91
0e3ede4
edff9cd
321536f
5e8c38a
2ecf23b
81b674a
bd467dc
30c2897
44c9955
5659760
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does caching work for this data source? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have kept this null as of now. The caching can be turned on by setting this part to
This is similar to the other data sources that are involved in caching like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think the column being unnested would need to be part of the cache key since the reason table datasources can get away with an empty cache key is because that is part of the segmentId. However here the results are dependent on what is being unnested, so we can't rely on just the datasource name, so a cache key would need to be non-empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getCacheKey is documented as
Meaning that a null return type should disable caching. We should likely be even more explicit and set |
||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
UnnestDataSource that = (UnnestDataSource) o; | ||
return column.equals(that.column) | ||
&& outputName.equals(that.outputName) | ||
&& base.equals(that.base); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(base, column, outputName); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a style thing, but this sort of fluent style tends to produce hard-to-read stack traces if there are any errors. It creates stack traces with lines from the
Function
class rather than fromUnnestDataSource
. Generally speaking, only use a fluent style when the fluency doesn't go outside of the current scope of the code. If you are returning an object that is going to be used by someone else, create a closure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed, creating new object now