danny0405 commented on a change in pull request #13050: URL: https://github.com/apache/flink/pull/13050#discussion_r471256512
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/utils/Expander.java ########## @@ -0,0 +1,126 @@ +/* + * 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.flink.table.planner.utils; + +import org.apache.flink.table.planner.calcite.FlinkPlannerImpl; + +import org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableMap; + +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParser; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.util.SqlBasicVisitor; +import org.apache.calcite.sql.util.SqlShuttle; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; + +/** + * Utility that expand SQL identifiers from a SQL query. + * + * <p>Simple use: + * + * <blockquote><code> + * final String sql =<br> + * "select ename from emp where deptno < 10";<br> + * final Expander.Expanded expanded =<br> + * Expander.create(planner).expanded(sql);<br> + * print(expanded); // "select `emp`.`ename` from `catalog`.`db`.`emp` where `emp`.`deptno` < 10" + * </code></blockquote> + * + * <p>Calling {@link Expanded#toString()} generates a string that is similar to + * SQL where a user has manually converted all identifiers as expanded, and + * which could then be persisted as expanded query of a Catalog view. + * + * <p>For more advanced formatting, use {@link Expanded#substitute(Function)}. + * + * <p>Adjust {@link SqlParser.Config} to use a different parser or parsing options. + */ +public class Expander { + private final FlinkPlannerImpl planner; + + private Expander(FlinkPlannerImpl planner) { + this.planner = Objects.requireNonNull(planner); + } + + /** Creates an Expander. **/ + public static Expander create(FlinkPlannerImpl planner) { + return new Expander(planner); + } + + /** Expands identifiers in a given SQL string, returning a {@link Expanded}. */ + public Expanded expanded(String ori) { + final Map<SqlParserPos, SqlIdentifier> identifiers = new HashMap<>(); + final SqlNode oriNode; + final SqlNode validated; + oriNode = planner.parser().parse(ori); + // parse again because validation is stateful, that means the node tree was probably + // mutated. + validated = planner.validate(planner.parser().parse(ori)); + validated.accept(new SqlBasicVisitor<Void>() { + @Override public Void visit(SqlIdentifier identifier) { + identifiers.putIfAbsent(identifier.getParserPosition(), identifier); + return null; + } + }); + return new Expanded(planner.config().getParserConfig(), oriNode, identifiers); + } + + /** Result of expanding. */ + public static class Expanded { + public final SqlParser.Config parserConf; + public final SqlNode oriNode; + public final Map<SqlParserPos, SqlIdentifier> identifiersMap; + + Expanded(SqlParser.Config parserConf, SqlNode oriNode, + Map<SqlParserPos, SqlIdentifier> identifiers) { + this.oriNode = oriNode; + this.parserConf = parserConf; + this.identifiersMap = ImmutableMap.copyOf(identifiers); + } + + @Override + public String toString() { + return substitute(SqlNode::toString); + } + + /** Returns the SQL string with identifiers replaced according to the + * given unparse function. */ + public String substitute(Function<SqlNode, String> fn) { + final SqlShuttle shuttle = new SqlShuttle() { + @Override + public SqlNode visit(SqlIdentifier id) { + if (id.isStar()) { + return id; + } + final SqlIdentifier toReplace = identifiersMap.get(id.getParserPosition()); + if (toReplace != null && id.names.size() >= toReplace.names.size()) { Review comment: Yes, nice catch ~ ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org