Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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.analysis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Java equivalent of the Thrift {@code TColumnAccessPath} struct.
* Merges {@code TDataAccessPath} and {@code TMetaAccessPath} into a single class
* since both are structurally identical ({@code List<String> path}).
* The {@link ColumnAccessPathType} discriminates between data and metadata access.
*
* <p>This class is immutable and implements {@link Comparable} so it can be used
* in sorted collections such as {@code TreeSet}.
*/
public final class ColumnAccessPath implements Comparable<ColumnAccessPath> {
private final ColumnAccessPathType type;
private final List<String> path;

public ColumnAccessPath(ColumnAccessPathType type, List<String> path) {
this.type = Objects.requireNonNull(type, "type must not be null");
this.path = Collections.unmodifiableList(new ArrayList<>(
Objects.requireNonNull(path, "path must not be null")));
}

/** Creates a DATA access path. */
public static ColumnAccessPath data(List<String> path) {
return new ColumnAccessPath(ColumnAccessPathType.DATA, path);
}

/** Creates a META access path. */
public static ColumnAccessPath meta(List<String> path) {
return new ColumnAccessPath(ColumnAccessPathType.META, path);
}

public ColumnAccessPathType getType() {
return type;
}

public List<String> getPath() {
return path;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ColumnAccessPath)) {
return false;
}
ColumnAccessPath that = (ColumnAccessPath) o;
return type == that.type && Objects.equals(path, that.path);
}

@Override
public int hashCode() {
return Objects.hash(type, path);
}

@Override
public int compareTo(ColumnAccessPath other) {
int cmp = type.compareTo(other.type);
if (cmp != 0) {
return cmp;
}
int minLen = Math.min(path.size(), other.path.size());
for (int i = 0; i < minLen; i++) {
cmp = path.get(i).compareTo(other.path.get(i));
if (cmp != 0) {
return cmp;
}
}
return Integer.compare(path.size(), other.path.size());
}

@Override
public String toString() {
return type + ":" + String.join(".", path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.analysis;

/**
* Java equivalent of the Thrift {@code TAccessPathType} enum.
* Represents whether a column access path reads data or metadata.
*/
public enum ColumnAccessPathType {
DATA,
META
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

package org.apache.doris.analysis;

import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.IdGenerator;
import org.apache.doris.thrift.TDescriptorTable;

import com.google.common.collect.Maps;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Repository for tuple (and slot) descriptors.
Expand All @@ -39,7 +37,6 @@ public class DescriptorTable {
private final IdGenerator<TupleId> tupleIdGenerator = TupleId.createGenerator();
private final IdGenerator<SlotId> slotIdGenerator = SlotId.createGenerator();
private final HashMap<SlotId, SlotDescriptor> slotDescs = Maps.newHashMap();
private TDescriptorTable thriftDescTable = null; // serialized version of this

public DescriptorTable() {
}
Expand All @@ -61,33 +58,8 @@ public TupleDescriptor getTupleDesc(TupleId id) {
return tupleDescs.get(id);
}

public TDescriptorTable toThrift() {
if (thriftDescTable != null) {
return thriftDescTable;
}

TDescriptorTable result = new TDescriptorTable();
Map<Long, TableIf> referencedTbls = Maps.newHashMap();
for (TupleDescriptor tupleD : tupleDescs.values()) {
// inline view of a non-constant select has a non-materialized tuple descriptor
// in the descriptor table just for type checking, which we need to skip
result.addToTupleDescriptors(tupleD.toThrift());
// an inline view of a constant select has a materialized tuple
// but its table has no id
if (tupleD.getTable() != null
&& tupleD.getTable().getId() >= 0) {
referencedTbls.put(tupleD.getTable().getId(), tupleD.getTable());
}
for (SlotDescriptor slotD : tupleD.getSlots()) {
result.addToSlotDescriptors(slotD.toThrift());
}
}

for (TableIf tbl : referencedTbls.values()) {
result.addToTableDescriptors(tbl.toThrift());
}
thriftDescTable = result;
return result;
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.

getTupleDescs() exposes a live tupleDescs.values() view as public API. That collection supports remove()/clear(), so external callers can now mutate DescriptorTable without updating slotDescs or the ID generators, leaving the object internally inconsistent. Since DescriptorToThriftConverter is in the same package, this helper can stay package-private, or it should at least return an unmodifiable view.

public Collection<TupleDescriptor> getTupleDescs() {
return tupleDescs.values();
}

public String getExplainString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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.analysis;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.thrift.TAccessPathType;
import org.apache.doris.thrift.TColumnAccessPath;
import org.apache.doris.thrift.TDataAccessPath;
import org.apache.doris.thrift.TDescriptorTable;
import org.apache.doris.thrift.TMetaAccessPath;
import org.apache.doris.thrift.TSlotDescriptor;
import org.apache.doris.thrift.TTupleDescriptor;

import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Converts {@link SlotDescriptor}, {@link TupleDescriptor}, and {@link DescriptorTable}
* to their Thrift representations.
*/
public final class DescriptorToThriftConverter {
private static final Logger LOG = LogManager.getLogger(DescriptorToThriftConverter.class);

private DescriptorToThriftConverter() {
}

/**
* Converts a {@link SlotDescriptor} to its Thrift representation.
*/
public static TSlotDescriptor toThrift(SlotDescriptor slotDesc) {
String materializedColumnName = slotDesc.getMaterializedColumnName();
Column column = slotDesc.getColumn();
String colName = materializedColumnName != null ? materializedColumnName :
((column != null) ? column.getNonShadowName() : "");
TSlotDescriptor tSlotDescriptor = new TSlotDescriptor(slotDesc.getId().asInt(),
slotDesc.getParentId().asInt(), slotDesc.getType().toThrift(), -1,
0, 0, slotDesc.getIsNullable() ? 0 : -1, colName, -1,
true);
tSlotDescriptor.setIsAutoIncrement(slotDesc.isAutoInc());
if (column != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("column name:{}, column unique id:{}", column.getNonShadowName(), column.getUniqueId());
}
tSlotDescriptor.setColUniqueId(column.getUniqueId());
tSlotDescriptor.setPrimitiveType(column.getDataType().toThrift());
tSlotDescriptor.setIsKey(column.isKey());
tSlotDescriptor.setColDefaultValue(column.getDefaultValue());
}
if (slotDesc.getSubColLables() != null) {
tSlotDescriptor.setColumnPaths(slotDesc.getSubColLables());
}
if (slotDesc.getVirtualColumn() != null) {
tSlotDescriptor.setVirtualColumnExpr(ExprToThriftVisitor.treeToThrift(slotDesc.getVirtualColumn()));
}
if (slotDesc.getAllAccessPaths() != null) {
tSlotDescriptor.setAllAccessPaths(toThrift(slotDesc.getAllAccessPaths()));
}
if (slotDesc.getPredicateAccessPaths() != null) {
tSlotDescriptor.setPredicateAccessPaths(toThrift(slotDesc.getPredicateAccessPaths()));
}
return tSlotDescriptor;
}

/**
* Converts a {@link ColumnAccessPath} to its Thrift representation.
*/
public static TColumnAccessPath toThrift(ColumnAccessPath accessPath) {
TColumnAccessPath result = new TColumnAccessPath(
accessPath.getType() == ColumnAccessPathType.DATA ? TAccessPathType.DATA : TAccessPathType.META);
if (accessPath.getType() == ColumnAccessPathType.DATA) {
result.setDataAccessPath(new TDataAccessPath(accessPath.getPath()));
} else {
result.setMetaAccessPath(new TMetaAccessPath(accessPath.getPath()));
}
return result;
}

/**
* Converts a list of {@link ColumnAccessPath} to Thrift representations.
*/
public static List<TColumnAccessPath> toThrift(List<ColumnAccessPath> accessPaths) {
List<TColumnAccessPath> result = new ArrayList<>(accessPaths.size());
for (ColumnAccessPath accessPath : accessPaths) {
result.add(toThrift(accessPath));
}
return result;
}

/**
* Converts a {@link TupleDescriptor} to its Thrift representation.
*/
public static TTupleDescriptor toThrift(TupleDescriptor tupleDesc) {
TTupleDescriptor tTupleDesc = new TTupleDescriptor(tupleDesc.getId().asInt(), 0, 0);
if (tupleDesc.getTable() != null && tupleDesc.getTable().getId() >= 0) {
tTupleDesc.setTableId((int) tupleDesc.getTable().getId());
}
return tTupleDesc;
}

/**
* Converts a {@link DescriptorTable} to its Thrift representation.
*/
public static TDescriptorTable toThrift(DescriptorTable descTable) {
TDescriptorTable result = new TDescriptorTable();
Map<Long, TableIf> referencedTbls = Maps.newHashMap();
for (TupleDescriptor tupleD : descTable.getTupleDescs()) {
result.addToTupleDescriptors(toThrift(tupleD));
if (tupleD.getTable() != null && tupleD.getTable().getId() >= 0) {
referencedTbls.put(tupleD.getTable().getId(), tupleD.getTable());
}
for (SlotDescriptor slotD : tupleD.getSlots()) {
result.addToSlotDescriptors(toThrift(slotD));
}
}

for (TableIf tbl : referencedTbls.values()) {
result.addToTableDescriptors(tbl.toThrift());
}
return result;
}
}
Loading
Loading