gianm commented on code in PR #17564: URL: https://github.com/apache/druid/pull/17564#discussion_r1905819571
########## processing/src/main/java/org/apache/druid/segment/RestrictedSegment.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.segment; + +import org.apache.druid.query.filter.DimFilter; +import org.apache.druid.timeline.SegmentId; +import org.joda.time.Interval; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.io.Closeable; +import java.io.IOException; +import java.util.Optional; + +/** + * A wrapped {@link SegmentReference} with a {@link DimFilter} restriction. The restriction must be applied on the + * segment. + */ +public class RestrictedSegment implements SegmentReference +{ + protected final SegmentReference delegate; + @Nullable + private final DimFilter filter; Review Comment: This should be a `List<Policy>` just like in `RestrictedDataSource`, and for the same reasons: https://github.com/apache/druid/pull/17564#discussion_r1905784938 ########## processing/src/main/java/org/apache/druid/segment/RestrictedSegment.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.segment; + +import org.apache.druid.query.filter.DimFilter; +import org.apache.druid.timeline.SegmentId; +import org.joda.time.Interval; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.io.Closeable; +import java.io.IOException; +import java.util.Optional; + +/** + * A wrapped {@link SegmentReference} with a {@link DimFilter} restriction. The restriction must be applied on the + * segment. + */ +public class RestrictedSegment implements SegmentReference +{ + protected final SegmentReference delegate; + @Nullable + private final DimFilter filter; + + public RestrictedSegment( + SegmentReference delegate, + @Nullable DimFilter filter + ) + { + this.delegate = delegate; + this.filter = filter; + } + + @Override + public Optional<Closeable> acquireReferences() + { + return delegate.acquireReferences(); + } + + @Override + public SegmentId getId() + { + return delegate.getId(); + } + + @Override + public Interval getDataInterval() + { + return delegate.getDataInterval(); + } + + @Override + public CursorFactory asCursorFactory() + { + return new RestrictedCursorFactory(delegate.asCursorFactory(), filter); + } + + @Nullable + @Override + public QueryableIndex asQueryableIndex() + { + return null; + } + + @Nullable + @Override + public <T> T as(@Nonnull Class<T> clazz) Review Comment: There's a problem here: some query engines use `QueryableIndex` for direct access to columns and indexes, and we can't really make a clean restricted wrapper around `QueryableIndex` (we can't anticipate what columns and indexes may occur in extensions). So we need some way for query engines to say "give me the QueryableIndex, I know what I'm doing and I will apply the policies directly". IMO, a good way to do that is to offer an `as(BypassRestrictedSegment.class)` which returns one of these: ```java class BypassRestrictedSegment implements SegmentReference { private final Segment segment; private final List<Policy> policies; public List<Policy> getPolicies() { return policies; } @Override public CursorFactory asCursorFactory() { // Bypass policies return delegate.asCursorFactory(); } @Override public <T> T as(Class<T> clazz) { // Bypass policies return segment.as(clazz); } } ``` Policy-aware query engines that need to use `QueryableIndex` directly are expected to: - Call `as(QueryableIndex.class)` first. - If that returns null, try `as(BypassRestrictedSegment.class)` followed by `as(QueryableIndex.class)`. - Call `getPolicies()` and apply the policies when using the `QueryableIndex`. - Throw an exception if any unrecognized policies are found. ########## processing/src/main/java/org/apache/druid/query/policy/Policy.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.policy; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.druid.query.filter.DimFilter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Objects; + +/** + * Represents a granular-level (e.x. row filter) restriction on read-table access. + */ +public class Policy Review Comment: For reasons in https://github.com/apache/druid/pull/17564/files#r1905784938, this should be an `interface` rather than a `class`. -- 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]
