Skip to content

Commit

Permalink
[fix](Nereids) retrieve the table using SlotReference in the Match (a…
Browse files Browse the repository at this point in the history
…pache#39652)

## Proposed changes

Sometimes, there is no table in SlotRef, so we need to use SlotReference
to get the table.
  • Loading branch information
csun5285 authored Aug 29, 2024
1 parent 9dbd4d9 commit 664ca8a
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
import org.apache.doris.analysis.LambdaFunctionExpr;
import org.apache.doris.analysis.MatchPredicate;
import org.apache.doris.analysis.OrderByElement;
import org.apache.doris.analysis.SlotDescriptor;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.TimestampArithmeticExpr;
import org.apache.doris.analysis.TupleDescriptor;
import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Function;
import org.apache.doris.catalog.Function.NullableMode;
import org.apache.doris.catalog.Index;
Expand Down Expand Up @@ -189,19 +188,11 @@ public Expr visitLessThanEqual(LessThanEqual lessThanEqual, PlanTranslatorContex
return le;
}

private OlapTable getOlapTableFromSlotDesc(SlotDescriptor slotDesc) {
if (slotDesc != null && slotDesc.isScanSlot()) {
TupleDescriptor slotParent = slotDesc.getParent();
return (OlapTable) slotParent.getTable();
}
return null;
}

private OlapTable getOlapTableDirectly(SlotRef left) {
if (left.getTableDirect() instanceof OlapTable) {
return (OlapTable) left.getTableDirect();
}
return null;
private OlapTable getOlapTableDirectly(SlotReference slot) {
return slot.getTable()
.filter(OlapTable.class::isInstance)
.map(OlapTable.class::cast)
.orElse(null);
}

@Override
Expand All @@ -213,20 +204,28 @@ public Expr visitElementAt(ElementAt elementAt, PlanTranslatorContext context) {
public Expr visitMatch(Match match, PlanTranslatorContext context) {
Index invertedIndex = null;
// Get the first slot from match's left expr
SlotRef left = (SlotRef) match.left().getInputSlots().stream().findFirst().get().accept(this, context);
OlapTable olapTbl = Optional.ofNullable(getOlapTableFromSlotDesc(left.getDesc()))
.orElse(getOlapTableDirectly(left));

SlotReference slot = match.getInputSlots().stream()
.findFirst()
.filter(SlotReference.class::isInstance)
.map(SlotReference.class::cast)
.orElseThrow(() -> new AnalysisException(
"No SlotReference found in Match, SQL is " + match.toSql()));

Column column = slot.getColumn()
.orElseThrow(() -> new AnalysisException(
"SlotReference in Match failed to get Column, SQL is " + match.toSql()));

OlapTable olapTbl = getOlapTableDirectly(slot);
if (olapTbl == null) {
throw new AnalysisException("slotRef in matchExpression failed to get OlapTable");
throw new AnalysisException("SlotReference in Match failed to get OlapTable, SQL is " + match.toSql());
}

List<Index> indexes = olapTbl.getIndexes();
if (indexes != null) {
for (Index index : indexes) {
if (index.getIndexType() == IndexDef.IndexType.INVERTED) {
List<String> columns = index.getColumns();
if (columns != null && !columns.isEmpty() && left.getColumnName().equals(columns.get(0))) {
if (columns != null && !columns.isEmpty() && column.getName().equals(columns.get(0))) {
invertedIndex = index;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
810

Loading

0 comments on commit 664ca8a

Please sign in to comment.