rubenada commented on code in PR #5294: URL: https://github.com/apache/calcite/pull/5294#discussion_r4154639980
########## core/src/main/java/org/apache/calcite/sql/validate/CycleRewriter.java: ########## @@ -0,0 +1,395 @@ +/* + * 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.calcite.sql.validate; + +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.sql.JoinConditionType; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlCycleClause; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlJoin; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlLiteral; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.SqlSelect; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.SqlWith; +import org.apache.calcite.sql.SqlWithItem; +import org.apache.calcite.sql.fun.SqlCase; +import org.apache.calcite.sql.fun.SqlInternalOperators; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.sql.util.SqlBasicVisitor; +import org.apache.calcite.sql.util.SqlShuttle; +import org.apache.calcite.util.NlsString; +import org.apache.calcite.util.Util; + +import com.google.common.collect.ImmutableList; + +import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.apache.calcite.sql.validate.SqlValidatorUtil.EXPR_SUGGESTER; +import static org.apache.calcite.util.Static.RESOURCE; + +import static java.util.Objects.requireNonNull; + +/** Lowers CYCLE to a recursive UNION with a mark and an array of key tuples. + * + * <p>Each row has its own path. The row closing a cycle is emitted, but is + * filtered out before evaluating the next recursive step. Fresh syntax nodes + * are used because the original query has already been validated. */ +final class CycleRewriter { + private static final SqlParserPos POS = SqlParserPos.ZERO; + private static final String INPUT = "$CYCLE_INPUT"; + + private final SqlValidatorImpl validator; + private final SqlWithItem item; + private final SqlCycleClause cycle; + private final SqlNameMatcher matcher; + + private CycleRewriter(SqlValidatorImpl validator, SqlWithItem item) { + this.validator = validator; + this.item = item; + this.cycle = requireNonNull(item.cycleClause); + this.matcher = validator.getCatalogReader().nameMatcher(); + } + + static SqlWithItem rewrite(SqlValidatorImpl validator, SqlValidatorScope scope, + SqlWithItem item) { + return new CycleRewriter(validator, item).rewrite(scope); + } + + private SqlWithItem rewrite(SqlValidatorScope scope) { Review Comment: would it be possible to add a comment here with some high-level description or pseudo code example of what this rewrite is doing? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
