View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.calcite.sql.SqlNode;
23  import org.apache.calcite.sql.SqlNodeList;
24  import org.apache.calcite.sql.parser.SqlParserPos;
25  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
29  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
30  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
31  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
32  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
33  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.ParameterMarkerExpressionConverter;
34  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.AggregationProjectionConverter;
35  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.ColumnProjectionConverter;
36  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.ExpressionProjectionConverter;
37  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.ShorthandProjectionConverter;
38  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.SubqueryProjectionConverter;
39  
40  import java.util.Collection;
41  import java.util.LinkedList;
42  import java.util.Optional;
43  
44  /**
45   * Projection converter.
46   */
47  @NoArgsConstructor(access = AccessLevel.PRIVATE)
48  public final class ProjectionsConverter {
49      
50      /**
51       * Convert projections segment to sql node list.
52       * 
53       * @param segment projections segment
54       * @return sql node list
55       */
56      public static Optional<SqlNodeList> convert(final ProjectionsSegment segment) {
57          Collection<SqlNode> projectionSQLNodes = new LinkedList<>();
58          for (ProjectionSegment each : segment.getProjections()) {
59              getProjectionSQLNode(each).ifPresent(projectionSQLNodes::add);
60          }
61          return Optional.of(new SqlNodeList(projectionSQLNodes, SqlParserPos.ZERO));
62      }
63      
64      private static Optional<SqlNode> getProjectionSQLNode(final ProjectionSegment segment) {
65          if (segment instanceof ColumnProjectionSegment) {
66              return ColumnProjectionConverter.convert((ColumnProjectionSegment) segment);
67          }
68          if (segment instanceof ExpressionProjectionSegment) {
69              return ExpressionProjectionConverter.convert((ExpressionProjectionSegment) segment);
70          }
71          if (segment instanceof ShorthandProjectionSegment) {
72              return ShorthandProjectionConverter.convert((ShorthandProjectionSegment) segment);
73          }
74          if (segment instanceof SubqueryProjectionSegment) {
75              return SubqueryProjectionConverter.convert((SubqueryProjectionSegment) segment);
76          }
77          if (segment instanceof AggregationProjectionSegment) {
78              return AggregationProjectionConverter.convert((AggregationProjectionSegment) segment);
79          }
80          if (segment instanceof ParameterMarkerExpressionSegment) {
81              return ParameterMarkerExpressionConverter.convert((ParameterMarkerExpressionSegment) segment);
82          }
83          // TODO process other projection
84          return Optional.empty();
85      }
86  }