Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions be/src/exec/rowid_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,28 @@ struct IteratorItem {
StorageReadOptions storage_read_options;
};

static void set_slot_access_paths(const SlotDescriptor& slot, const TabletSchema& schema,
StorageReadOptions& storage_read_options) {
int32_t unique_id = slot.col_unique_id();
const int field_index =
unique_id >= 0 ? schema.field_index(unique_id) : schema.field_index(slot.col_name());
if (field_index >= 0) {
const auto& column = schema.column(field_index);
unique_id = column.unique_id() >= 0 ? column.unique_id() : column.parent_unique_id();
}
if (unique_id < 0) {
return;
}

if (!slot.all_access_paths().empty()) {
storage_read_options.all_access_paths[unique_id] = slot.all_access_paths();
}

if (!slot.predicate_access_paths().empty()) {
storage_read_options.predicate_access_paths[unique_id] = slot.predicate_access_paths();
}
}

struct SegItem {
BaseTabletSPtr tablet;
BetaRowsetSharedPtr rowset;
Expand Down Expand Up @@ -450,6 +472,7 @@ Status RowIdStorageReader::read_by_rowids(const PMultiGetRequest& request,
iterator_item.storage_read_options.io_ctx.reader_type = ReaderType::READER_QUERY;
}
segment = iterator_item.segment;
set_slot_access_paths(slots[x], full_read_schema, iterator_item.storage_read_options);
RETURN_IF_ERROR(segment->seek_and_read_by_rowid(
full_read_schema, &slots[x], row_id, column, iterator_item.storage_read_options,
iterator_item.iterator));
Expand Down Expand Up @@ -1071,6 +1094,7 @@ Status RowIdStorageReader::read_doris_format_row(
iterator_item.storage_read_options.stats = &stats;
iterator_item.storage_read_options.io_ctx.reader_type = ReaderType::READER_QUERY;
}
set_slot_access_paths(slots[x], full_read_schema, iterator_item.storage_read_options);
for (auto row_id : row_ids) {
RETURN_IF_ERROR(segment->seek_and_read_by_rowid(
full_read_schema, &slots[x], row_id, column,
Expand Down
1 change: 0 additions & 1 deletion be/src/storage/segment/column_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,6 @@ Status StructFileColumnIterator::set_access_paths(
}

if (!need_to_read) {
set_reading_flag(ReadingFlag::SKIP_READING);
sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP_READING";
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2917,7 +2917,32 @@ private boolean shouldUseRowStore(Relation rel, List<Slot> lazySlots) {
useRowStore = olapTable.storeRowColumn()
&& CollectionUtils.isEmpty(olapTable.getTableProperty().getCopiedRowStoreColumns());
}
return useRowStore && canUseRowStoreForLazySlots(lazySlots);
return useRowStore && canUseRowStoreForLazySlots(lazySlots)
&& !hasNestedAccessPaths(rel, lazySlots);
}

private boolean hasNestedAccessPaths(Relation rel, List<Slot> lazySlots) {
Set<Integer> lazyColumnUniqueIds = new HashSet<>();
for (Slot lazySlot : lazySlots) {
SlotReference slotReference = (SlotReference) lazySlot;
lazyColumnUniqueIds.add(slotReference.getOriginalColumn().get().getUniqueId());
}
for (Slot outputSlot : rel.getOutput()) {
if (outputSlot instanceof SlotReference) {
SlotReference slotReference = (SlotReference) outputSlot;
if (slotReference.getOriginalColumn().isPresent()
&& lazyColumnUniqueIds.contains(slotReference.getOriginalColumn().get().getUniqueId())
&& hasNestedAccessPaths(slotReference)) {
return true;
}
}
}
return false;
}

private boolean hasNestedAccessPaths(SlotReference slotReference) {
return slotReference.getAllAccessPaths().map(paths -> !paths.isEmpty()).orElse(false)
|| slotReference.getPredicateAccessPaths().map(paths -> !paths.isEmpty()).orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public Optional<MaterializeSource> visitPhysicalFilter(PhysicalFilter<? extends
return Optional.empty();
}
if (filter.getInputSlots().contains(context.slot)) {
return Optional.of(new MaterializeSource((Relation) filter.child(), context.slot));
Relation relation = (Relation) filter.child();
return Optional.of(new MaterializeSource(
relation, findRelationOutputSlot(relation, context.slot).orElse(context.slot)));
} else {
return filter.child().accept(this, context);
}
Expand Down Expand Up @@ -152,7 +154,8 @@ public Optional<MaterializeSource> visitPhysicalOlapScan(PhysicalOlapScan scan,
if (scan.getOperativeSlots().contains(context.slot)) {
return Optional.empty();
}
return Optional.of(new MaterializeSource(scan, context.slot));
return Optional.of(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still does not get the nested access paths to BE. The visitor now returns the relation output slot, but the slot that becomes the materialization tuple descriptor is rebuilt later from the original lazy slot: PhysicalLazyMaterialize does ((SlotReference) lazySlot).withColumn(originalColumn), and withColumn() does not copy allAccessPaths/predicateAccessPaths. generateTupleDesc(materialize.getOutput(), ...) therefore creates a SlotDescriptor with empty access paths, so the new set_slot_access_paths() calls in BE are a no-op for the targeted rowid fetch. Please carry MaterializeSource.baseSlot's access paths onto the lazy output slot that is serialized for the fetch request, and add a test that checks the materialization tuple descriptor contains those paths.

new MaterializeSource(scan, findRelationOutputSlot(scan, context.slot).orElse(context.slot)));
}

@Override
Expand All @@ -163,7 +166,8 @@ public Optional<MaterializeSource> visitPhysicalCatalogRelation(
&& !relation.getOperativeSlots().contains(context.slot)) {
// lazy materialize slot must be a passive slot
if (context.slot.getOriginalColumn().isPresent()) {
return Optional.of(new MaterializeSource(relation, context.slot));
return Optional.of(new MaterializeSource(
relation, findRelationOutputSlot(relation, context.slot).orElse(context.slot)));
} else {
LOG.info("lazy materialize {} failed, because its column is empty", context.slot);
}
Expand All @@ -178,7 +182,8 @@ public Optional<MaterializeSource> visitPhysicalTVFRelation(
&& !tvfRelation.getOperativeSlots().contains(context.slot)) {
// lazy materialize slot must be a passive slot
if (context.slot.getOriginalColumn().isPresent()) {
return Optional.of(new MaterializeSource(tvfRelation, context.slot));
return Optional.of(new MaterializeSource(
tvfRelation, findRelationOutputSlot(tvfRelation, context.slot).orElse(context.slot)));
} else {
LOG.info("lazy materialize {} failed, because its column is empty", context.slot);
}
Expand Down Expand Up @@ -225,4 +230,11 @@ public Optional<MaterializeSource> visitPhysicalProject(
}
}

private Optional<SlotReference> findRelationOutputSlot(Relation relation, SlotReference contextSlot) {
return relation.getOutput().stream()
.filter(slot -> slot instanceof SlotReference && slot.equals(contextSlot))
.map(slot -> (SlotReference) slot)
.findFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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.doris.nereids.processor.post.materialize;

import org.apache.doris.catalog.KeysType;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter;
import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TAccessPathType;
import org.apache.doris.thrift.TColumnAccessPath;
import org.apache.doris.thrift.TDataAccessPath;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Optional;

public class MaterializeProbeVisitorTest {

@Test
public void testOlapScanUsesRelationSlotWithAccessPaths() {
SlotReference contextSlot = new SlotReference("a", IntegerType.INSTANCE);
SlotReference relationSlot = contextSlot.withAccessPaths(
ImmutableList.of(dataPath("nested")), ImmutableList.of());
contextSlot = (SlotReference) contextSlot.withNullable(false);
PhysicalOlapScan scan = mockBaseOlapScan(relationSlot);

MaterializeProbeVisitor.ProbeContext context = new MaterializeProbeVisitor.ProbeContext(contextSlot);
Optional<MaterializeSource> source = new MaterializeProbeVisitor().visitPhysicalOlapScan(scan, context);

Assertions.assertTrue(source.isPresent());
Assertions.assertSame(relationSlot, source.get().baseSlot);
Assertions.assertEquals(relationSlot.getAllAccessPaths(), source.get().baseSlot.getAllAccessPaths());
}

@Test
@SuppressWarnings("unchecked")
public void testFilterUsingIndexUsesRelationSlotWithAccessPaths() {
ConnectContext oldContext = ConnectContext.get();
ConnectContext context = new ConnectContext();
context.getSessionVariable().topNLazyMaterializationUsingIndex = true;
context.setThreadLocalInfo();
try {
SlotReference contextSlot = new SlotReference("a", IntegerType.INSTANCE);
SlotReference relationSlot = contextSlot.withAccessPaths(
ImmutableList.of(dataPath("nested")), ImmutableList.of());
contextSlot = (SlotReference) contextSlot.withNullable(false);
PhysicalOlapScan scan = mockBaseOlapScan(relationSlot);

PhysicalFilter<PhysicalOlapScan> filter = Mockito.mock(PhysicalFilter.class);
Mockito.when(filter.child()).thenReturn(scan);
Mockito.when(filter.getInputSlots()).thenReturn(ImmutableSet.of(contextSlot));

MaterializeProbeVisitor.ProbeContext probeContext = new MaterializeProbeVisitor.ProbeContext(contextSlot);
Optional<MaterializeSource> source =
new MaterializeProbeVisitor().visitPhysicalFilter(filter, probeContext);

Assertions.assertTrue(source.isPresent());
Assertions.assertSame(relationSlot, source.get().baseSlot);
Assertions.assertEquals(relationSlot.getAllAccessPaths(), source.get().baseSlot.getAllAccessPaths());
} finally {
if (oldContext == null) {
ConnectContext.remove();
} else {
oldContext.setThreadLocalInfo();
}
}
}

private TColumnAccessPath dataPath(String... path) {
TColumnAccessPath accessPath = new TColumnAccessPath(TAccessPathType.DATA);
accessPath.data_access_path = new TDataAccessPath(ImmutableList.copyOf(path));
return accessPath;
}

private PhysicalOlapScan mockBaseOlapScan(SlotReference outputSlot) {
OlapTable table = Mockito.mock(OlapTable.class);
Mockito.when(table.getBaseIndexId()).thenReturn(1L);
Mockito.when(table.getKeysType()).thenReturn(KeysType.DUP_KEYS);
PhysicalOlapScan scan = Mockito.mock(PhysicalOlapScan.class);
Mockito.when(scan.getSelectedIndexId()).thenReturn(1L);
Mockito.when(scan.getTable()).thenReturn(table);
Mockito.when(scan.getOutput()).thenReturn(ImmutableList.of(outputSlot));
Mockito.when(scan.getOperativeSlots()).thenReturn(ImmutableList.of());
return scan;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !struct_result --
1 \N

-- !variant_result --
1 aaa 张三
2 bbb 李四
3 ccc 王五

-- !map_result --
1 1

-- !array_result --
1 1

-- !variant_nested_result --
4 上海
5 北京

-- !map_using_index_result --
1 1

-- !array_using_index_result --
1 1

-- !struct_col_order_result --
\N 1

-- !variant_col_order_result --
张三 1 aaa
李四 2 bbb
王五 3 ccc

-- !map_col_order_result --
1 1

-- !project_under_topn_consumed_slot --
1 hello {"city":null, "zip":10001} [1, 2, 3] {"a":1, "b":2} 1 \N

-- !sparse_struct_map_array_result --
3 9 9 3 false
1 7 7 3 false
Loading
Loading