From e3daa9580a498ddc19ce1c7b318cc4e3a1c3da01 Mon Sep 17 00:00:00 2001 From: EmmyMiao87 <522274284@qq.com> Date: Fri, 8 Apr 2022 09:08:55 +0800 Subject: [PATCH] [Fix](Lateral View) The Error expr type when exploding a function result of inline view (#8851) Fixed #8850 The column in inline view maybe a function instead of slotRef. So when this column is used as the input of explode function, it can't be converted to slotRef. The correct way is to treat it as an Expr and extract the required slotRef for materialization. For example: ``` with d as (select k1+k1 as k1_plus from table) select k1_plus from d explode_split(k1_plus, ",") ``` FnExp: SlorRef SubstituteFnExpr: functionCallExpr originSlotRefList: SlotRef --- .../org/apache/doris/analysis/LateralViewRef.java | 12 ++++++------ .../apache/doris/planner/TableFunctionPlanTest.java | 11 +++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java index bee7032c03..3af8b5481c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java @@ -26,7 +26,6 @@ import org.apache.doris.common.UserException; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; -import java.util.ArrayList; import java.util.List; /** @@ -42,7 +41,7 @@ public class LateralViewRef extends TableRef { // after analyzed private FunctionCallExpr fnExpr; - private ArrayList originSlotRefList = Lists.newArrayList(); + private List originSlotRefList = Lists.newArrayList(); private InlineView view; private SlotRef explodeSlotRef; @@ -98,7 +97,6 @@ public class LateralViewRef extends TableRef { for (Expr expr : fnExpr.getChildren()) { checkScalarFunction(expr); } - fnExpr.collect(SlotRef.class, originSlotRefList); } @Override @@ -116,11 +114,13 @@ public class LateralViewRef extends TableRef { } public void materializeRequiredSlots(ExprSubstitutionMap baseTblSmap, Analyzer analyzer) throws AnalysisException { + Expr substituteFnExpr = fnExpr; if (relatedTableRef instanceof InlineViewRef) { - originSlotRefList = Expr.trySubstituteList(originSlotRefList, baseTblSmap, analyzer, false); + substituteFnExpr = fnExpr.trySubstitute(baseTblSmap, analyzer, false); } - for (Expr originSlotRef : originSlotRefList) { - ((SlotRef) originSlotRef).getDesc().setIsMaterialized(true); + substituteFnExpr.collect(SlotRef.class, originSlotRefList); + for (SlotRef originSlotRef : originSlotRefList) { + originSlotRef.getDesc().setIsMaterialized(true); } explodeSlotRef.getDesc().setIsMaterialized(true); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java b/fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java index f203c8dbf2..58e4c9de86 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java @@ -527,4 +527,15 @@ public class TableFunctionPlanTest { String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, true); Assert.assertTrue(!explainString.contains("Unknown column 'e1' in 'table list'")); } + + + // The 'k1' column in 'd' view should be materialized + // Fix #8850 + @Test + public void testLateralViewWithInlineViewBug() throws Exception { + String sql = "with d as (select k1+k1 as k1 from db1.table_for_view ) " + + "select k1 from d lateral view explode_split(k1,',') tmp as e1;"; + String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, true); + Assert.assertTrue(!explainString.contains("Unexpected exception: org.apache.doris.analysis.FunctionCallExpr cannot be cast to org.apache.doris.analysis.SlotRef")); + } }