morrySnow commented on code in PR #33193: URL: https://github.com/apache/doris/pull/33193#discussion_r1604311389
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java: ########## @@ -328,13 +333,57 @@ private Plan parseAndAnalyzeHiveView( return hiveViewPlan; } + /** + * Support get column from hive view with column name like '_c0', '_c1' etc for hive. + */ + public void addHiveViewDerivedColumn(Plan hiveViewPlan) { + if (hiveViewPlan instanceof LogicalProject) { + List<NamedExpression> projects = ((LogicalProject) hiveViewPlan).getProjects(); + int counter = -1; + for (NamedExpression project : projects) { + counter++; + // only set inner name for derived column + if (project instanceof UnboundAlias) { + UnboundAlias aliasProject = (UnboundAlias) project; + if (aliasProject.getAlias().isPresent()) { + continue; + } + if (aliasProject.child() instanceof UnboundFunction) { + aliasProject.setAlias(Optional.of("_c" + counter)); + } + } + } + } + + if (hiveViewPlan instanceof LogicalAggregate) { + int counter = -1; + List<NamedExpression> expressions = ((LogicalAggregate) hiveViewPlan).getOutputExpressions(); + for (NamedExpression expression : expressions) { + counter++; + if (expression instanceof UnboundAlias) { + UnboundAlias aliasProject = (UnboundAlias) expression; + if (aliasProject.getAlias().isPresent()) { + continue; + } + aliasProject.setAlias(Optional.of("_c" + counter)); + } + } + } + + // Recursively init subquery + for (Plan subPlan : hiveViewPlan.children()) { + addHiveViewDerivedColumn(subPlan); + } + } + private Plan parseAndAnalyzeView(TableIf view, String ddlSql, CascadesContext parentContext) { parentContext.getStatementContext().addViewDdlSql(ddlSql); Optional<SqlCacheContext> sqlCacheContext = parentContext.getStatementContext().getSqlCacheContext(); if (sqlCacheContext.isPresent()) { sqlCacheContext.get().addUsedView(view, ddlSql); } LogicalPlan parsedViewPlan = new NereidsParser().parseSingle(ddlSql); + addHiveViewDerivedColumn(parsedViewPlan); Review Comment: this function use for both doris view and hive view. so also process doris' view? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java: ########## @@ -89,4 +89,8 @@ public UnboundAlias withChildren(List<Expression> children) { public Optional<String> getAlias() { return alias; } + + public void setAlias(Optional<String> alias) { + this.alias = alias; + } Review Comment: this is a mistake of `UnboundAlias`. In Nereids all Expression and plan should be Immutable. So we should let alias be final. and add a function `withAlias` like `withChildren` to generate a new `UnboundAlias` object with new alias ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java: ########## @@ -328,13 +333,57 @@ private Plan parseAndAnalyzeHiveView( return hiveViewPlan; } + /** + * Support get column from hive view with column name like '_c0', '_c1' etc for hive. + */ + public void addHiveViewDerivedColumn(Plan hiveViewPlan) { + if (hiveViewPlan instanceof LogicalProject) { + List<NamedExpression> projects = ((LogicalProject) hiveViewPlan).getProjects(); + int counter = -1; + for (NamedExpression project : projects) { + counter++; + // only set inner name for derived column + if (project instanceof UnboundAlias) { + UnboundAlias aliasProject = (UnboundAlias) project; + if (aliasProject.getAlias().isPresent()) { + continue; + } + if (aliasProject.child() instanceof UnboundFunction) { + aliasProject.setAlias(Optional.of("_c" + counter)); Review Comment: if we repalce all alias with '_cx', what will happen for this sql ```sql SELECT a1, a2 FROM (SELECT id AS a1, count(*) AS a2 FROM t1 GROUP BY id) inline_view ``` -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org