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.sql.parser.opengauss.visitor.statement;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.antlr.v4.runtime.ParserRuleContext;
23  import org.antlr.v4.runtime.misc.Interval;
24  import org.antlr.v4.runtime.tree.ParseTree;
25  import org.antlr.v4.runtime.tree.TerminalNode;
26  import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
27  import org.apache.shardingsphere.sql.parser.api.ASTNode;
28  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementBaseVisitor;
29  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AExprContext;
30  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AexprConstContext;
31  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AliasClauseContext;
32  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AnyNameContext;
33  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AscDescContext;
34  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AssignmentContext;
35  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AttrNameContext;
36  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AttrsContext;
37  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.BExprContext;
38  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CExprContext;
39  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CaseExprContext;
40  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColIdContext;
41  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColumnNameContext;
42  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColumnNamesContext;
43  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColumnrefContext;
44  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ConstraintNameContext;
45  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeContext;
46  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeLengthContext;
47  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeNameContext;
48  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DeleteContext;
49  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExecuteStmtContext;
50  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExprListContext;
51  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExtractArgContext;
52  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ForLockingClauseContext;
53  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.FromClauseContext;
54  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.FromListContext;
55  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.FuncApplicationContext;
56  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.FuncExprContext;
57  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.FunctionExprCommonSubexprContext;
58  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.GroupByItemContext;
59  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.GroupClauseContext;
60  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.HavingClauseContext;
61  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IdentifierContext;
62  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InExprContext;
63  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndexNameContext;
64  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndirectionContext;
65  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertColumnItemContext;
66  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertColumnListContext;
67  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertContext;
68  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertRestContext;
69  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertTargetContext;
70  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IntoClauseContext;
71  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.JoinQualContext;
72  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.JoinedTableContext;
73  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.LimitClauseContext;
74  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.NameListContext;
75  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.NaturalJoinTypeContext;
76  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.NullsOrderContext;
77  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.NumberLiteralsContext;
78  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.OptOnDuplicateKeyContext;
79  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.OuterJoinTypeContext;
80  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.OwnerContext;
81  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ParameterMarkerContext;
82  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.QualifiedNameContext;
83  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.QualifiedNameListContext;
84  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.RelationExprOptAliasContext;
85  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SchemaNameContext;
86  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectClauseNContext;
87  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectContext;
88  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectFetchValueContext;
89  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectLimitContext;
90  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectLimitValueContext;
91  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectNoParensContext;
92  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectOffsetValueContext;
93  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SelectWithParensContext;
94  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SetClauseContext;
95  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SetClauseListContext;
96  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SetTargetContext;
97  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SignedIconstContext;
98  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SimpleSelectContext;
99  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SortClauseContext;
100 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SortbyContext;
101 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableNameContext;
102 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableNamesContext;
103 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableReferenceContext;
104 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TargetElContext;
105 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TargetListContext;
106 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.UnreservedWordContext;
107 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.UpdateContext;
108 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ValuesClauseContext;
109 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WhenClauseContext;
110 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WhereClauseContext;
111 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WhereOrCurrentClauseContext;
112 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowClauseContext;
113 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowDefinitionContext;
114 import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.WindowDefinitionListContext;
115 import org.apache.shardingsphere.sql.parser.sql.common.enums.AggregationType;
116 import org.apache.shardingsphere.sql.parser.sql.common.enums.CombineType;
117 import org.apache.shardingsphere.sql.parser.sql.common.enums.JoinType;
118 import org.apache.shardingsphere.sql.parser.sql.common.enums.OrderDirection;
119 import org.apache.shardingsphere.sql.parser.sql.common.enums.ParameterMarkerType;
120 import org.apache.shardingsphere.sql.parser.sql.common.enums.SubqueryType;
121 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
122 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexNameSegment;
123 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
124 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.ReturningSegment;
125 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
126 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
127 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
128 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
129 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
130 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
131 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
132 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
133 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
134 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.CaseWhenExpression;
135 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExistsSubqueryExpression;
136 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
137 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExtractArgExpression;
138 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
139 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
140 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
141 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.TypeCastExpression;
142 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
143 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
144 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
145 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
146 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
147 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
148 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
149 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
150 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
151 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
152 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
153 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
154 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
155 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
156 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
157 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
158 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
159 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
160 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
161 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
162 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitValueSegment;
163 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
164 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
165 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
166 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
167 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
168 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasAvailable;
169 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
170 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeLengthSegment;
171 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
172 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.NameSegment;
173 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
174 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
175 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowItemSegment;
176 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
177 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
178 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
179 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
180 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
181 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
182 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
183 import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
184 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
185 import org.apache.shardingsphere.sql.parser.sql.common.value.keyword.KeywordValue;
186 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.LiteralValue;
187 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.BooleanLiteralValue;
188 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.NullLiteralValue;
189 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.NumberLiteralValue;
190 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.OtherLiteralValue;
191 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.StringLiteralValue;
192 import org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker.ParameterMarkerValue;
193 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.ddl.OpenGaussExecuteStatement;
194 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussDeleteStatement;
195 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussInsertStatement;
196 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussSelectStatement;
197 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussUpdateStatement;
198 
199 import java.util.ArrayList;
200 import java.util.Collection;
201 import java.util.Collections;
202 import java.util.LinkedList;
203 import java.util.List;
204 import java.util.Optional;
205 
206 /**
207  * Statement visitor for openGauss.
208  */
209 @Getter(AccessLevel.PROTECTED)
210 public abstract class OpenGaussStatementVisitor extends OpenGaussStatementBaseVisitor<ASTNode> {
211     
212     private final Collection<ParameterMarkerSegment> parameterMarkerSegments = new LinkedList<>();
213     
214     @Override
215     public final ASTNode visitParameterMarker(final ParameterMarkerContext ctx) {
216         if (null == ctx.DOLLAR_()) {
217             return new ParameterMarkerValue(parameterMarkerSegments.size(), ParameterMarkerType.QUESTION);
218         }
219         return new ParameterMarkerValue(new NumberLiteralValue(ctx.NUMBER_().getText()).getValue().intValue() - 1, ParameterMarkerType.DOLLAR);
220     }
221     
222     @Override
223     public final ASTNode visitNumberLiterals(final NumberLiteralsContext ctx) {
224         return new NumberLiteralValue(ctx.NUMBER_().getText());
225     }
226     
227     @Override
228     public final ASTNode visitIdentifier(final IdentifierContext ctx) {
229         UnreservedWordContext unreservedWord = ctx.unreservedWord();
230         return null == unreservedWord ? new IdentifierValue(ctx.getText()) : visit(unreservedWord);
231     }
232     
233     @Override
234     public final ASTNode visitUnreservedWord(final UnreservedWordContext ctx) {
235         return new IdentifierValue(ctx.getText());
236     }
237     
238     @Override
239     public final ASTNode visitSchemaName(final SchemaNameContext ctx) {
240         return visit(ctx.identifier());
241     }
242     
243     @Override
244     public final ASTNode visitTableName(final TableNameContext ctx) {
245         SimpleTableSegment result = new SimpleTableSegment(new TableNameSegment(ctx.name().getStart().getStartIndex(), ctx.name().getStop().getStopIndex(), (IdentifierValue) visit(ctx.name())));
246         OwnerContext owner = ctx.owner();
247         if (null != owner) {
248             result.setOwner(new OwnerSegment(owner.getStart().getStartIndex(), owner.getStop().getStopIndex(), (IdentifierValue) visit(owner.identifier())));
249         }
250         return result;
251     }
252     
253     @Override
254     public final ASTNode visitColumnName(final ColumnNameContext ctx) {
255         ColumnSegment result = new ColumnSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (IdentifierValue) visit(ctx.name()));
256         OwnerContext owner = ctx.owner();
257         if (null != owner) {
258             result.setOwner(new OwnerSegment(owner.getStart().getStartIndex(), owner.getStop().getStopIndex(), (IdentifierValue) visit(owner.identifier())));
259         }
260         return result;
261     }
262     
263     @Override
264     public final ASTNode visitIndexName(final IndexNameContext ctx) {
265         IndexNameSegment indexName = new IndexNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.identifier()));
266         return new IndexSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), indexName);
267     }
268     
269     @Override
270     public final ASTNode visitConstraintName(final ConstraintNameContext ctx) {
271         return new ConstraintSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (IdentifierValue) visit(ctx.identifier()));
272     }
273     
274     @Override
275     public final ASTNode visitTableNames(final TableNamesContext ctx) {
276         CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
277         for (TableNameContext each : ctx.tableName()) {
278             result.getValue().add((SimpleTableSegment) visit(each));
279         }
280         return result;
281     }
282     
283     @Override
284     public final ASTNode visitColumnNames(final ColumnNamesContext ctx) {
285         CollectionValue<ColumnSegment> result = new CollectionValue<>();
286         for (ColumnNameContext each : ctx.columnName()) {
287             result.getValue().add((ColumnSegment) visit(each));
288         }
289         return result;
290     }
291     
292     @Override
293     public ASTNode visitAExpr(final AExprContext ctx) {
294         if (null != ctx.cExpr()) {
295             return visit(ctx.cExpr());
296         }
297         if (null != ctx.TYPE_CAST_()) {
298             return new TypeCastExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText(), (ExpressionSegment) visit(ctx.aExpr(0)), ctx.typeName().getText());
299         }
300         if (null != ctx.BETWEEN()) {
301             return createBetweenSegment(ctx);
302         }
303         if (null != ctx.IN()) {
304             return createInSegment(ctx);
305         }
306         if (null != ctx.patternMatchingOperator()) {
307             return createPatternMatchingOperationSegment(ctx);
308         }
309         Optional<String> binaryOperator = findBinaryOperator(ctx);
310         if (binaryOperator.isPresent()) {
311             return createBinaryOperationSegment(ctx, binaryOperator.get());
312         }
313         super.visitAExpr(ctx);
314         String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
315         return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), text);
316     }
317     
318     private Optional<String> findBinaryOperator(final AExprContext ctx) {
319         if (null != ctx.IS()) {
320             return Optional.of(ctx.IS().getText());
321         }
322         if (null != ctx.ISNULL()) {
323             return Optional.of("IS");
324         }
325         if (1 == ctx.aExpr().size()) {
326             return Optional.empty();
327         }
328         if (null != ctx.comparisonOperator()) {
329             return Optional.of(ctx.comparisonOperator().getText());
330         }
331         if (null != ctx.andOperator()) {
332             return Optional.of(ctx.andOperator().getText());
333         }
334         if (null != ctx.orOperator()) {
335             return Optional.of(ctx.orOperator().getText());
336         }
337         if (null != ctx.PLUS_()) {
338             return Optional.of(ctx.PLUS_().getText());
339         }
340         if (null != ctx.MINUS_()) {
341             return Optional.of(ctx.MINUS_().getText());
342         }
343         if (null != ctx.ASTERISK_()) {
344             return Optional.of(ctx.ASTERISK_().getText());
345         }
346         if (null != ctx.SLASH_()) {
347             return Optional.of(ctx.SLASH_().getText());
348         }
349         return Optional.empty();
350     }
351     
352     private BinaryOperationExpression createPatternMatchingOperationSegment(final AExprContext ctx) {
353         String operator = getOriginalText(ctx.patternMatchingOperator()).toUpperCase();
354         ExpressionSegment left = (ExpressionSegment) visit(ctx.aExpr(0));
355         ListExpression right = new ListExpression(ctx.aExpr(1).start.getStartIndex(), ctx.aExpr().get(ctx.aExpr().size() - 1).stop.getStopIndex());
356         for (int i = 1; i < ctx.aExpr().size(); i++) {
357             right.getItems().add((ExpressionSegment) visit(ctx.aExpr().get(i)));
358         }
359         String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
360         return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
361     }
362     
363     private BinaryOperationExpression createBinaryOperationSegment(final AExprContext ctx, final String operator) {
364         if ("IS".equalsIgnoreCase(operator)) {
365             ExpressionSegment left = (ExpressionSegment) visit(ctx.aExpr(0));
366             String rightText;
367             ExpressionSegment right;
368             if (null != ctx.IS()) {
369                 rightText = ctx.start.getInputStream().getText(new Interval(ctx.IS().getSymbol().getStopIndex() + 2, ctx.stop.getStopIndex())).trim();
370                 right = new LiteralExpressionSegment(ctx.IS().getSymbol().getStopIndex() + 2, ctx.stop.getStopIndex(), rightText);
371             } else {
372                 rightText = ctx.start.getInputStream().getText(new Interval(ctx.ISNULL().getSymbol().getStartIndex() + 2, ctx.stop.getStopIndex())).trim();
373                 right = new LiteralExpressionSegment(ctx.ISNULL().getSymbol().getStartIndex() + 2, ctx.stop.getStopIndex(), rightText);
374             }
375             return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, "IS",
376                     ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex())));
377         }
378         ExpressionSegment left = (ExpressionSegment) visit(ctx.aExpr(0));
379         ExpressionSegment right = (ExpressionSegment) visit(ctx.aExpr(1));
380         String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
381         return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
382     }
383     
384     @Override
385     public ASTNode visitCExpr(final CExprContext ctx) {
386         if (null != ctx.columnref()) {
387             return visit(ctx.columnref());
388         }
389         if (null != ctx.parameterMarker()) {
390             ParameterMarkerValue parameterMarker = (ParameterMarkerValue) visit(ctx.parameterMarker());
391             ParameterMarkerExpressionSegment result = new ParameterMarkerExpressionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), parameterMarker.getValue(), parameterMarker.getType());
392             parameterMarkerSegments.add(result);
393             return result;
394         }
395         if (null != ctx.aexprConst()) {
396             return visit(ctx.aexprConst());
397         }
398         if (null != ctx.aExpr()) {
399             return visit(ctx.aExpr());
400         }
401         if (null != ctx.funcExpr()) {
402             return visit(ctx.funcExpr());
403         }
404         if (null != ctx.selectWithParens()) {
405             return createSubqueryExpressionSegment(ctx);
406         }
407         if (null != ctx.caseExpr()) {
408             return visit(ctx.caseExpr());
409         }
410         super.visitCExpr(ctx);
411         String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
412         return new CommonExpressionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), text);
413     }
414     
415     private ExpressionSegment createSubqueryExpressionSegment(final CExprContext ctx) {
416         SubquerySegment subquerySegment = new SubquerySegment(ctx.selectWithParens().getStart().getStartIndex(),
417                 ctx.selectWithParens().getStop().getStopIndex(), (OpenGaussSelectStatement) visit(ctx.selectWithParens()), getOriginalText(ctx.selectWithParens()));
418         if (null != ctx.EXISTS()) {
419             subquerySegment.setSubqueryType(SubqueryType.EXISTS_SUBQUERY);
420             return new ExistsSubqueryExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquerySegment);
421         }
422         return new SubqueryExpressionSegment(subquerySegment);
423     }
424     
425     @Override
426     public ASTNode visitCaseExpr(final CaseExprContext ctx) {
427         Collection<ExpressionSegment> whenExprs = new LinkedList<>();
428         Collection<ExpressionSegment> thenExprs = new LinkedList<>();
429         for (WhenClauseContext each : ctx.whenClauseList().whenClause()) {
430             whenExprs.add((ExpressionSegment) visit(each.aExpr(0)));
431             thenExprs.add((ExpressionSegment) visit(each.aExpr(1)));
432         }
433         ExpressionSegment caseExpr = null == ctx.caseArg() ? null : (ExpressionSegment) visit(ctx.caseArg().aExpr());
434         ExpressionSegment elseExpr = null == ctx.caseDefault() ? null : (ExpressionSegment) visit(ctx.caseDefault().aExpr());
435         return new CaseWhenExpression(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), caseExpr, whenExprs, thenExprs, elseExpr);
436     }
437     
438     @Override
439     public ASTNode visitFuncExpr(final FuncExprContext ctx) {
440         if (null != ctx.functionExprCommonSubexpr()) {
441             return visit(ctx.functionExprCommonSubexpr());
442         }
443         Collection<ExpressionSegment> expressionSegments = getExpressionSegments(getTargetRuleContextFromParseTree(ctx, AExprContext.class));
444         // TODO replace aggregation segment
445         String aggregationType = ctx.funcApplication().funcName().getText();
446         if (AggregationType.isAggregationType(aggregationType)) {
447             return createAggregationSegment(ctx.funcApplication(), aggregationType, expressionSegments);
448         }
449         FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.funcApplication().funcName().getText(), getOriginalText(ctx));
450         result.getParameters().addAll(expressionSegments);
451         return result;
452     }
453     
454     @Override
455     public ASTNode visitFunctionExprCommonSubexpr(final FunctionExprCommonSubexprContext ctx) {
456         if (null != ctx.CAST()) {
457             return new TypeCastExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText(), (ExpressionSegment) visit(ctx.aExpr(0)), ctx.typeName().getText());
458         }
459         FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getChild(0).getText(), getOriginalText(ctx));
460         Collection<ExpressionSegment> expressionSegments = getExpressionSegments(getTargetRuleContextFromParseTree(ctx, AExprContext.class));
461         if ("EXTRACT".equalsIgnoreCase(ctx.getChild(0).getText())) {
462             result.getParameters().add((ExpressionSegment) visit(getTargetRuleContextFromParseTree(ctx, ExtractArgContext.class).iterator().next()));
463         }
464         result.getParameters().addAll(expressionSegments);
465         return result;
466     }
467     
468     @Override
469     public ASTNode visitExtractArg(final ExtractArgContext ctx) {
470         return new ExtractArgExpression(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getChild(0).getText());
471     }
472     
473     private <T extends ParseTree> Collection<T> getTargetRuleContextFromParseTree(final ParseTree parseTree, final Class<? extends T> clazz) {
474         Collection<T> result = new LinkedList<>();
475         for (int index = 0; index < parseTree.getChildCount(); index++) {
476             ParseTree child = parseTree.getChild(index);
477             if (clazz.isInstance(child)) {
478                 result.add(clazz.cast(child));
479             } else {
480                 result.addAll(getTargetRuleContextFromParseTree(child, clazz));
481             }
482         }
483         return result;
484     }
485     
486     private Collection<ExpressionSegment> getExpressionSegments(final Collection<AExprContext> aExprContexts) {
487         Collection<ExpressionSegment> result = new LinkedList<>();
488         for (AExprContext each : aExprContexts) {
489             result.add((ExpressionSegment) visit(each));
490         }
491         return result;
492     }
493     
494     @Override
495     public ASTNode visitAexprConst(final AexprConstContext ctx) {
496         LiteralValue<?> value;
497         if (null != ctx.numberConst()) {
498             value = new NumberLiteralValue(ctx.numberConst().getText());
499         } else if (null != ctx.STRING_()) {
500             value = new StringLiteralValue(ctx.STRING_().getText());
501         } else if (null != ctx.FALSE()) {
502             value = new BooleanLiteralValue(ctx.FALSE().getText());
503         } else if (null != ctx.TRUE()) {
504             value = new BooleanLiteralValue(ctx.TRUE().getText());
505         } else if (null != ctx.NULL()) {
506             value = new NullLiteralValue(ctx.getText());
507         } else {
508             value = new OtherLiteralValue(ctx.getText());
509         }
510         if (null != ctx.constTypeName() || null != ctx.funcName() && null == ctx.LP_()) {
511             LiteralExpressionSegment expression = new LiteralExpressionSegment(ctx.STRING_().getSymbol().getStartIndex(), ctx.STRING_().getSymbol().getStopIndex(), value.getValue().toString());
512             String dataType = null == ctx.constTypeName() ? ctx.funcName().getText() : ctx.constTypeName().getText();
513             return new TypeCastExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText(), expression, dataType);
514         }
515         return SQLUtils.createLiteralExpression(value, ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText());
516     }
517     
518     @Override
519     public ASTNode visitColumnref(final ColumnrefContext ctx) {
520         if (null != ctx.indirection()) {
521             AttrNameContext attrName = ctx.indirection().indirectionEl().attrName();
522             ColumnSegment result = new ColumnSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(attrName.getText()));
523             OwnerSegment owner = new OwnerSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
524             result.setOwner(owner);
525             return result;
526         }
527         return new ColumnSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
528     }
529     
530     private InExpression createInSegment(final AExprContext ctx) {
531         ExpressionSegment left = (ExpressionSegment) visit(ctx.aExpr(0));
532         ExpressionSegment right = createInExpressionSegment(ctx.inExpr());
533         boolean not = null != ctx.NOT();
534         return new InExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, not);
535     }
536     
537     @SuppressWarnings("unchecked")
538     private ExpressionSegment createInExpressionSegment(final InExprContext ctx) {
539         if (null != ctx.selectWithParens()) {
540             OpenGaussSelectStatement select = (OpenGaussSelectStatement) visit(ctx.selectWithParens());
541             SubquerySegment subquerySegment = new SubquerySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), select, getOriginalText(ctx.selectWithParens()));
542             return new SubqueryExpressionSegment(subquerySegment);
543         }
544         ListExpression result = new ListExpression(ctx.LP_().getSymbol().getStartIndex(), ctx.RP_().getSymbol().getStopIndex());
545         result.getItems().addAll(((CollectionValue<ExpressionSegment>) visit(ctx.exprList())).getValue());
546         return result;
547     }
548     
549     @SuppressWarnings("unchecked")
550     @Override
551     public ASTNode visitExprList(final ExprListContext ctx) {
552         CollectionValue<ExpressionSegment> result = new CollectionValue<>();
553         if (null != ctx.exprList()) {
554             result.combine((CollectionValue<ExpressionSegment>) visitExprList(ctx.exprList()));
555         }
556         result.getValue().add((ExpressionSegment) visit(ctx.aExpr()));
557         return result;
558     }
559     
560     private BetweenExpression createBetweenSegment(final AExprContext ctx) {
561         ExpressionSegment left = (ExpressionSegment) visit(ctx.aExpr(0));
562         ExpressionSegment between = (ExpressionSegment) visit(ctx.bExpr());
563         ExpressionSegment and = (ExpressionSegment) visit(ctx.aExpr(1));
564         boolean not = null != ctx.NOT();
565         return new BetweenExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, between, and, not);
566     }
567     
568     @Override
569     public ASTNode visitBExpr(final BExprContext ctx) {
570         if (null != ctx.cExpr()) {
571             return visit(ctx.cExpr());
572         }
573         if (null != ctx.TYPE_CAST_()) {
574             return new TypeCastExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText(), (ExpressionSegment) visit(ctx.bExpr(0)), ctx.typeName().getText());
575         }
576         if (null != ctx.qualOp()) {
577             ExpressionSegment left = (ExpressionSegment) visit(ctx.bExpr(0));
578             ExpressionSegment right = (ExpressionSegment) visit(ctx.bExpr(1));
579             String operator = ctx.qualOp().getText();
580             String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
581             return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
582         }
583         for (BExprContext each : ctx.bExpr()) {
584             visit(each);
585         }
586         return new LiteralExpressionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText());
587     }
588     
589     private ProjectionSegment createAggregationSegment(final FuncApplicationContext ctx, final String aggregationType, final Collection<ExpressionSegment> expressionSegments) {
590         AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase());
591         if (null == ctx.DISTINCT()) {
592             AggregationProjectionSegment result = new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, getOriginalText(ctx));
593             result.getParameters().addAll(expressionSegments);
594             return result;
595         }
596         AggregationDistinctProjectionSegment result =
597                 new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, getOriginalText(ctx), getDistinctExpression(ctx));
598         result.getParameters().addAll(expressionSegments);
599         return result;
600     }
601     
602     private String getDistinctExpression(final FuncApplicationContext ctx) {
603         StringBuilder result = new StringBuilder();
604         result.append(ctx.funcArgList().getText());
605         if (null != ctx.sortClause()) {
606             result.append(ctx.sortClause().getText());
607         }
608         return result.toString();
609     }
610     
611     @Override
612     public final ASTNode visitDataTypeName(final DataTypeNameContext ctx) {
613         IdentifierContext identifierContext = ctx.identifier();
614         if (null != identifierContext) {
615             return new KeywordValue(identifierContext.getText());
616         }
617         Collection<String> dataTypeNames = new LinkedList<>();
618         for (int i = 0; i < ctx.getChildCount(); i++) {
619             dataTypeNames.add(ctx.getChild(i).getText());
620         }
621         return new KeywordValue(String.join(" ", dataTypeNames));
622     }
623     
624     @Override
625     public final ASTNode visitSortClause(final SortClauseContext ctx) {
626         Collection<OrderByItemSegment> items = new LinkedList<>();
627         for (SortbyContext each : ctx.sortbyList().sortby()) {
628             items.add((OrderByItemSegment) visit(each));
629         }
630         return new OrderBySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), items);
631     }
632     
633     @Override
634     public final ASTNode visitSortby(final SortbyContext ctx) {
635         OrderDirection orderDirection = null == ctx.ascDesc() ? OrderDirection.ASC : generateOrderDirection(ctx.ascDesc());
636         NullsOrderType nullsOrderType = generateNullsOrderType(ctx.nullsOrder());
637         ASTNode expr = visit(ctx.aExpr());
638         if (expr instanceof ColumnSegment) {
639             ColumnSegment column = (ColumnSegment) expr;
640             return new ColumnOrderByItemSegment(column, orderDirection, nullsOrderType);
641         }
642         if (expr instanceof LiteralExpressionSegment) {
643             LiteralExpressionSegment index = (LiteralExpressionSegment) expr;
644             return new IndexOrderByItemSegment(index.getStartIndex(), index.getStopIndex(), Integer.parseInt(index.getLiterals().toString()), orderDirection, nullsOrderType);
645         }
646         if (expr instanceof ExpressionSegment) {
647             return new ExpressionOrderByItemSegment(ctx.aExpr().getStart().getStartIndex(),
648                     ctx.aExpr().getStop().getStopIndex(), getOriginalText(ctx.aExpr()), orderDirection, nullsOrderType, (ExpressionSegment) expr);
649         }
650         return new ExpressionOrderByItemSegment(ctx.aExpr().getStart().getStartIndex(), ctx.aExpr().getStop().getStopIndex(), getOriginalText(ctx.aExpr()), orderDirection, nullsOrderType);
651     }
652     
653     private NullsOrderType generateNullsOrderType(final NullsOrderContext ctx) {
654         if (null == ctx) {
655             return null;
656         }
657         return null == ctx.FIRST() ? NullsOrderType.LAST : NullsOrderType.FIRST;
658     }
659     
660     private OrderDirection generateOrderDirection(final AscDescContext ctx) {
661         return null == ctx.DESC() ? OrderDirection.ASC : OrderDirection.DESC;
662     }
663     
664     @Override
665     public final ASTNode visitDataType(final DataTypeContext ctx) {
666         DataTypeSegment result = new DataTypeSegment();
667         result.setDataTypeName(((KeywordValue) visit(ctx.dataTypeName())).getValue());
668         result.setStartIndex(ctx.start.getStartIndex());
669         result.setStopIndex(ctx.stop.getStopIndex());
670         if (null != ctx.dataTypeLength()) {
671             DataTypeLengthSegment dataTypeLengthSegment = (DataTypeLengthSegment) visit(ctx.dataTypeLength());
672             result.setDataLength(dataTypeLengthSegment);
673         }
674         return result;
675     }
676     
677     @Override
678     public final ASTNode visitDataTypeLength(final DataTypeLengthContext ctx) {
679         DataTypeLengthSegment result = new DataTypeLengthSegment();
680         result.setStartIndex(ctx.start.getStartIndex());
681         result.setStopIndex(ctx.stop.getStartIndex());
682         List<TerminalNode> numbers = ctx.NUMBER_();
683         if (1 == numbers.size()) {
684             result.setPrecision(Integer.parseInt(numbers.get(0).getText()));
685         }
686         if (2 == numbers.size()) {
687             result.setPrecision(Integer.parseInt(numbers.get(0).getText()));
688             result.setScale(Integer.parseInt(numbers.get(1).getText()));
689         }
690         return result;
691     }
692     
693     @Override
694     public ASTNode visitInsert(final InsertContext ctx) {
695         // TODO :FIXME, since there is no segment for insertValuesClause, InsertStatement is created by sub rule.
696         // TODO :deal with insert select
697         OpenGaussInsertStatement result = (OpenGaussInsertStatement) visit(ctx.insertRest());
698         result.setTable((SimpleTableSegment) visit(ctx.insertTarget()));
699         if (null != ctx.optOnDuplicateKey()) {
700             result.setOnDuplicateKeyColumnsSegment((OnDuplicateKeyColumnsSegment) visit(ctx.optOnDuplicateKey()));
701         }
702         if (null != ctx.returningClause()) {
703             result.setReturningSegment((ReturningSegment) visit(ctx.returningClause()));
704         }
705         result.addParameterMarkerSegments(getParameterMarkerSegments());
706         return result;
707     }
708     
709     @Override
710     public ASTNode visitInsertTarget(final InsertTargetContext ctx) {
711         SimpleTableSegment result = (SimpleTableSegment) visit(ctx.qualifiedName());
712         if (null != ctx.AS()) {
713             ColIdContext colId = ctx.colId();
714             result.setAlias(new AliasSegment(colId.start.getStartIndex(), colId.stop.getStopIndex(), new IdentifierValue(colId.getText())));
715         }
716         return result;
717     }
718     
719     @SuppressWarnings({"unchecked", "rawtypes"})
720     @Override
721     public ASTNode visitQualifiedNameList(final QualifiedNameListContext ctx) {
722         CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
723         if (null != ctx.qualifiedName()) {
724             result.getValue().add((SimpleTableSegment) visit(ctx.qualifiedName()));
725         }
726         if (null != ctx.qualifiedNameList()) {
727             result.combine((CollectionValue) visit(ctx.qualifiedNameList()));
728         }
729         return result;
730     }
731     
732     @Override
733     public ASTNode visitQualifiedName(final QualifiedNameContext ctx) {
734         if (null == ctx.indirection()) {
735             return new SimpleTableSegment(new TableNameSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText())));
736         }
737         AttrNameContext attrName = ctx.indirection().indirectionEl().attrName();
738         TableNameSegment tableName = new TableNameSegment(attrName.start.getStartIndex(), attrName.stop.getStopIndex(), new IdentifierValue(attrName.getText()));
739         OwnerSegment owner = new OwnerSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
740         SimpleTableSegment result = new SimpleTableSegment(tableName);
741         if (null == ctx.indirection().indirection()) {
742             result.setOwner(owner);
743         } else {
744             OwnerSegment tableOwner = createTableOwner(ctx.indirection().indirection());
745             tableOwner.setOwner(owner);
746             result.setOwner(tableOwner);
747         }
748         return result;
749     }
750     
751     @SuppressWarnings("unchecked")
752     @Override
753     public ASTNode visitInsertRest(final InsertRestContext ctx) {
754         OpenGaussInsertStatement result = new OpenGaussInsertStatement();
755         ValuesClauseContext valuesClause = ctx.select().selectNoParens().selectClauseN().simpleSelect().valuesClause();
756         if (null == valuesClause) {
757             OpenGaussSelectStatement selectStatement = (OpenGaussSelectStatement) visit(ctx.select());
758             result.setInsertSelect(new SubquerySegment(ctx.select().start.getStartIndex(), ctx.select().stop.getStopIndex(), selectStatement, getOriginalText(ctx.select())));
759         } else {
760             result.getValues().addAll(createInsertValuesSegments(valuesClause));
761         }
762         if (null == ctx.insertColumnList()) {
763             result.setInsertColumns(new InsertColumnsSegment(ctx.start.getStartIndex() - 1, ctx.start.getStartIndex() - 1, Collections.emptyList()));
764         } else {
765             InsertColumnListContext insertColumns = ctx.insertColumnList();
766             CollectionValue<ColumnSegment> columns = (CollectionValue<ColumnSegment>) visit(insertColumns);
767             InsertColumnsSegment insertColumnsSegment = new InsertColumnsSegment(insertColumns.start.getStartIndex() - 1, insertColumns.stop.getStopIndex() + 1, columns.getValue());
768             result.setInsertColumns(insertColumnsSegment);
769         }
770         return result;
771     }
772     
773     @Override
774     public ASTNode visitOptOnDuplicateKey(final OptOnDuplicateKeyContext ctx) {
775         if (null != ctx.NOTHING()) {
776             return new OnDuplicateKeyColumnsSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), Collections.emptyList());
777         }
778         Collection<ColumnAssignmentSegment> columns = new LinkedList<>();
779         for (AssignmentContext each : ctx.assignment()) {
780             columns.add((ColumnAssignmentSegment) visit(each));
781         }
782         return new OnDuplicateKeyColumnsSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), columns);
783     }
784     
785     @Override
786     public ASTNode visitAssignment(final AssignmentContext ctx) {
787         List<ColumnSegment> columnSegments = Collections.singletonList((ColumnSegment) visit(ctx.setTarget()));
788         ExpressionSegment expressionSegment;
789         if (null == ctx.aExpr()) {
790             String value = ctx.start.getInputStream().getText(new Interval(ctx.VALUES().getSymbol().getStartIndex(), ctx.stop.getStopIndex()));
791             FunctionSegment functionSegment = new FunctionSegment(ctx.VALUES().getSymbol().getStartIndex(), ctx.getStop().getStopIndex(), ctx.VALUES().getText(), value);
792             functionSegment.getParameters().add(new ColumnSegment(ctx.name().getStart().getStartIndex(), ctx.name().getStop().getStopIndex(), new IdentifierValue(ctx.name().getText())));
793             expressionSegment = functionSegment;
794         } else {
795             expressionSegment = (ExpressionSegment) visit(ctx.aExpr());
796         }
797         return new ColumnAssignmentSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), columnSegments, expressionSegment);
798     }
799     
800     @SuppressWarnings("unchecked")
801     @Override
802     public ASTNode visitInsertColumnList(final InsertColumnListContext ctx) {
803         CollectionValue<ColumnSegment> result = new CollectionValue<>();
804         if (null != ctx.insertColumnList()) {
805             result.getValue().addAll(((CollectionValue<ColumnSegment>) visit(ctx.insertColumnList())).getValue());
806         }
807         result.getValue().add((ColumnSegment) visit(ctx.insertColumnItem()));
808         return result;
809     }
810     
811     @Override
812     public ASTNode visitInsertColumnItem(final InsertColumnItemContext ctx) {
813         if (null == ctx.optIndirection().indirectionEl()) {
814             return new ColumnSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
815         }
816         ColumnSegment result = new ColumnSegment(
817                 ctx.colId().start.getStartIndex(), ctx.optIndirection().stop.getStopIndex(), new IdentifierValue(ctx.optIndirection().indirectionEl().attrName().getText()));
818         result.setOwner(new OwnerSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText())));
819         return result;
820     }
821     
822     private Collection<InsertValuesSegment> createInsertValuesSegments(final ValuesClauseContext ctx) {
823         Collection<InsertValuesSegment> result = new LinkedList<>();
824         if (null != ctx.valuesClause()) {
825             Collection<InsertValuesSegment> expressions = createInsertValuesSegments(ctx.valuesClause());
826             result.addAll(expressions);
827         }
828         Collection<ExpressionSegment> expressions = createInsertValuesSegments(ctx.exprList());
829         InsertValuesSegment insertValuesSegment = new InsertValuesSegment(ctx.LP_().getSymbol().getStartIndex(), ctx.RP_().getSymbol().getStopIndex(), (List<ExpressionSegment>) expressions);
830         result.add(insertValuesSegment);
831         return result;
832     }
833     
834     private Collection<ExpressionSegment> createInsertValuesSegments(final ExprListContext ctx) {
835         Collection<ExpressionSegment> result = new LinkedList<>();
836         if (null != ctx.exprList()) {
837             Collection<ExpressionSegment> tmpResult = createInsertValuesSegments(ctx.exprList());
838             result.addAll(tmpResult);
839         }
840         ExpressionSegment expr = (ExpressionSegment) visit(ctx.aExpr());
841         result.add(expr);
842         return result;
843     }
844     
845     private Collection<ColumnAssignmentSegment> generateAssignmentSegments(final SetClauseListContext ctx) {
846         Collection<ColumnAssignmentSegment> result = new LinkedList<>();
847         if (null != ctx.setClauseList()) {
848             Collection<ColumnAssignmentSegment> tmpResult = generateAssignmentSegments(ctx.setClauseList());
849             result.addAll(tmpResult);
850         }
851         ColumnAssignmentSegment assignmentSegment = (ColumnAssignmentSegment) visit(ctx.setClause());
852         result.add(assignmentSegment);
853         return result;
854     }
855     
856     @Override
857     public ASTNode visitSetClause(final SetClauseContext ctx) {
858         ColumnSegment columnSegment = (ColumnSegment) visit(ctx.setTarget());
859         List<ColumnSegment> columnSegments = new LinkedList<>();
860         columnSegments.add(columnSegment);
861         ExpressionSegment expressionSegment = (ExpressionSegment) visit(ctx.aExpr());
862         return new ColumnAssignmentSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), columnSegments, expressionSegment);
863     }
864     
865     @Override
866     public ASTNode visitSetTarget(final SetTargetContext ctx) {
867         List<ColIdContext> colIdContexts = ctx.colId();
868         if (2 == colIdContexts.size()) {
869             ColIdContext columnName = ctx.colId().get(1);
870             ColumnSegment result = new ColumnSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(columnName.getText()));
871             ColIdContext ownerName = ctx.colId().get(0);
872             result.setOwner(new OwnerSegment(ownerName.start.getStartIndex(), ownerName.stop.getStopIndex(), new IdentifierValue(ownerName.getText())));
873             return result;
874         }
875         return new ColumnSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(ctx.getText()));
876     }
877     
878     @Override
879     public ASTNode visitRelationExprOptAlias(final RelationExprOptAliasContext ctx) {
880         SimpleTableSegment result = (SimpleTableSegment) visit(ctx.relationExpr().qualifiedName());
881         if (null != ctx.colId()) {
882             result.setAlias(new AliasSegment(ctx.colId().start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(ctx.colId().getText())));
883         }
884         return result;
885     }
886     
887     @Override
888     public ASTNode visitUpdate(final UpdateContext ctx) {
889         OpenGaussUpdateStatement result = new OpenGaussUpdateStatement();
890         SimpleTableSegment tableSegment = (SimpleTableSegment) visit(ctx.relationExprOptAlias());
891         result.setTable(tableSegment);
892         result.setSetAssignment((SetAssignmentSegment) visit(ctx.setClauseList()));
893         if (null != ctx.whereOrCurrentClause()) {
894             result.setWhere((WhereSegment) visit(ctx.whereOrCurrentClause()));
895         }
896         result.addParameterMarkerSegments(getParameterMarkerSegments());
897         return result;
898     }
899     
900     @Override
901     public ASTNode visitSetClauseList(final SetClauseListContext ctx) {
902         Collection<ColumnAssignmentSegment> assignments = generateAssignmentSegments(ctx);
903         return new SetAssignmentSegment(ctx.start.getStartIndex() - 4, ctx.stop.getStopIndex(), assignments);
904     }
905     
906     @Override
907     public ASTNode visitDelete(final DeleteContext ctx) {
908         OpenGaussDeleteStatement result = new OpenGaussDeleteStatement();
909         SimpleTableSegment tableSegment = (SimpleTableSegment) visit(ctx.relationExprOptAlias());
910         result.setTable(tableSegment);
911         if (null != ctx.whereOrCurrentClause()) {
912             result.setWhere((WhereSegment) visit(ctx.whereOrCurrentClause()));
913         }
914         result.addParameterMarkerSegments(getParameterMarkerSegments());
915         return result;
916     }
917     
918     @Override
919     public ASTNode visitWhereOrCurrentClause(final WhereOrCurrentClauseContext ctx) {
920         return visit(ctx.whereClause());
921     }
922     
923     @Override
924     public ASTNode visitSelect(final SelectContext ctx) {
925         // TODO :Unsupported for withClause.
926         OpenGaussSelectStatement result = (OpenGaussSelectStatement) visit(ctx.selectNoParens());
927         result.addParameterMarkerSegments(getParameterMarkerSegments());
928         return result;
929     }
930     
931     @Override
932     public ASTNode visitSelectNoParens(final SelectNoParensContext ctx) {
933         OpenGaussSelectStatement result = (OpenGaussSelectStatement) visit(ctx.selectClauseN());
934         if (null != ctx.sortClause()) {
935             OrderBySegment orderBySegment = (OrderBySegment) visit(ctx.sortClause());
936             result.setOrderBy(orderBySegment);
937         }
938         if (null != ctx.selectLimit()) {
939             LimitSegment limitSegment = (LimitSegment) visit(ctx.selectLimit());
940             result.setLimit(limitSegment);
941         }
942         if (null != ctx.forLockingClause()) {
943             LockSegment lockSegment = (LockSegment) visit(ctx.forLockingClause());
944             result.setLock(lockSegment);
945         }
946         return result;
947     }
948     
949     @Override
950     public ASTNode visitForLockingClause(final ForLockingClauseContext ctx) {
951         return new LockSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
952     }
953     
954     @Override
955     public ASTNode visitSelectWithParens(final SelectWithParensContext ctx) {
956         if (null != ctx.selectWithParens()) {
957             return visit(ctx.selectWithParens());
958         }
959         return visit(ctx.selectNoParens());
960     }
961     
962     @Override
963     public ASTNode visitSelectClauseN(final SelectClauseNContext ctx) {
964         if (null != ctx.simpleSelect()) {
965             return visit(ctx.simpleSelect());
966         }
967         if (null != ctx.selectClauseN() && !ctx.selectClauseN().isEmpty()) {
968             OpenGaussSelectStatement result = new OpenGaussSelectStatement();
969             OpenGaussSelectStatement left = (OpenGaussSelectStatement) visit(ctx.selectClauseN(0));
970             result.setProjections(left.getProjections());
971             left.getFrom().ifPresent(result::setFrom);
972             CombineSegment combineSegment = new CombineSegment(((TerminalNode) ctx.getChild(1)).getSymbol().getStartIndex(), ctx.getStop().getStopIndex(),
973                     createSubquerySegment(ctx.selectClauseN(0), left), getCombineType(ctx), createSubquerySegment(ctx.selectClauseN(1), (OpenGaussSelectStatement) visit(ctx.selectClauseN(1))));
974             result.setCombine(combineSegment);
975             return result;
976         }
977         return visit(ctx.selectWithParens());
978     }
979     
980     private SubquerySegment createSubquerySegment(final SelectClauseNContext ctx, final OpenGaussSelectStatement selectStatement) {
981         return new SubquerySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), selectStatement, getOriginalText(ctx));
982     }
983     
984     private CombineType getCombineType(final SelectClauseNContext ctx) {
985         boolean isDistinct = null == ctx.allOrDistinct() || null != ctx.allOrDistinct().DISTINCT();
986         if (null != ctx.UNION()) {
987             return isDistinct ? CombineType.UNION : CombineType.UNION_ALL;
988         }
989         if (null != ctx.INTERSECT()) {
990             return isDistinct ? CombineType.INTERSECT : CombineType.INTERSECT_ALL;
991         }
992         if (null != ctx.MINUS()) {
993             return isDistinct ? CombineType.MINUS : CombineType.MINUS_ALL;
994         }
995         return isDistinct ? CombineType.EXCEPT : CombineType.EXCEPT_ALL;
996     }
997     
998     @Override
999     public ASTNode visitSimpleSelect(final SimpleSelectContext ctx) {
1000         OpenGaussSelectStatement result = new OpenGaussSelectStatement();
1001         if (null != ctx.targetList()) {
1002             ProjectionsSegment projects = (ProjectionsSegment) visit(ctx.targetList());
1003             if (null != ctx.distinctClause()) {
1004                 projects.setDistinctRow(true);
1005             }
1006             result.setProjections(projects);
1007         } else {
1008             result.setProjections(new ProjectionsSegment(-1, -1));
1009         }
1010         if (null != ctx.intoClause()) {
1011             result.setIntoSegment((TableSegment) visit(ctx.intoClause()));
1012         }
1013         if (null != ctx.fromClause()) {
1014             TableSegment tableSegment = (TableSegment) visit(ctx.fromClause());
1015             result.setFrom(tableSegment);
1016         }
1017         if (null != ctx.whereClause()) {
1018             result.setWhere((WhereSegment) visit(ctx.whereClause()));
1019         }
1020         if (null != ctx.groupClause()) {
1021             result.setGroupBy((GroupBySegment) visit(ctx.groupClause()));
1022         }
1023         if (null != ctx.havingClause()) {
1024             result.setHaving((HavingSegment) visit(ctx.havingClause()));
1025         }
1026         if (null != ctx.windowClause()) {
1027             result.setWindow((WindowSegment) visit(ctx.windowClause()));
1028         }
1029         return result;
1030     }
1031     
1032     @Override
1033     public ASTNode visitIntoClause(final IntoClauseContext ctx) {
1034         return visit(ctx.optTempTableName().qualifiedName());
1035     }
1036     
1037     @Override
1038     public ASTNode visitHavingClause(final HavingClauseContext ctx) {
1039         ExpressionSegment expr = (ExpressionSegment) visit(ctx.aExpr());
1040         return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
1041     }
1042     
1043     @Override
1044     public ASTNode visitWindowClause(final WindowClauseContext ctx) {
1045         WindowSegment result = new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
1046         appendWindowItems(ctx.windowDefinitionList(), result.getItemSegments());
1047         return result;
1048     }
1049     
1050     private void appendWindowItems(final WindowDefinitionListContext ctx, final Collection<WindowItemSegment> windowItems) {
1051         if (null != ctx.windowDefinitionList()) {
1052             appendWindowItems(ctx.windowDefinitionList(), windowItems);
1053             windowItems.add((WindowItemSegment) visit(ctx.windowDefinition()));
1054             return;
1055         }
1056         windowItems.add((WindowItemSegment) visit(ctx.windowDefinition()));
1057     }
1058     
1059     @SuppressWarnings("unchecked")
1060     @Override
1061     public ASTNode visitWindowDefinition(final WindowDefinitionContext ctx) {
1062         WindowItemSegment result = new WindowItemSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
1063         result.setWindowName(new IdentifierValue(ctx.colId().getText()));
1064         if (null != ctx.windowSpecification().partitionClause()) {
1065             CollectionValue<ExpressionSegment> value = (CollectionValue<ExpressionSegment>) visit(ctx.windowSpecification().partitionClause().exprList());
1066             result.setPartitionListSegments(value.getValue());
1067         }
1068         if (null != ctx.windowSpecification().sortClause()) {
1069             OrderBySegment orderBySegment = (OrderBySegment) visit(ctx.windowSpecification().sortClause());
1070             result.setOrderBySegment(orderBySegment);
1071         }
1072         if (null != ctx.windowSpecification().frameClause()) {
1073             result.setFrameClause(new CommonExpressionSegment(ctx.windowSpecification().frameClause().start.getStartIndex(), ctx.windowSpecification().frameClause().stop.getStopIndex(),
1074                     ctx.windowSpecification().frameClause().getText()));
1075         }
1076         return result;
1077     }
1078     
1079     @Override
1080     public ASTNode visitGroupClause(final GroupClauseContext ctx) {
1081         Collection<OrderByItemSegment> items = new LinkedList<>();
1082         for (GroupByItemContext each : ctx.groupByList().groupByItem()) {
1083             items.add((OrderByItemSegment) visit(each));
1084         }
1085         return new GroupBySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), items);
1086     }
1087     
1088     @Override
1089     public ASTNode visitGroupByItem(final GroupByItemContext ctx) {
1090         if (null != ctx.aExpr()) {
1091             ASTNode astNode = visit(ctx.aExpr());
1092             if (astNode instanceof ColumnSegment) {
1093                 return new ColumnOrderByItemSegment((ColumnSegment) astNode, OrderDirection.ASC, null);
1094             }
1095             if (astNode instanceof LiteralExpressionSegment) {
1096                 LiteralExpressionSegment index = (LiteralExpressionSegment) astNode;
1097                 return new IndexOrderByItemSegment(index.getStartIndex(), index.getStopIndex(),
1098                         Integer.parseInt(index.getLiterals().toString()), OrderDirection.ASC, null);
1099             }
1100             return new ExpressionOrderByItemSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getOriginalText(ctx), OrderDirection.ASC, null, (ExpressionSegment) visit(ctx.aExpr()));
1101         }
1102         return new ExpressionOrderByItemSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getOriginalText(ctx), OrderDirection.ASC, null);
1103     }
1104     
1105     @Override
1106     public ASTNode visitTargetList(final TargetListContext ctx) {
1107         ProjectionsSegment result = new ProjectionsSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
1108         if (null != ctx.targetList()) {
1109             ProjectionsSegment projections = (ProjectionsSegment) visit(ctx.targetList());
1110             result.getProjections().addAll(projections.getProjections());
1111         }
1112         ProjectionSegment projection = (ProjectionSegment) visit(ctx.targetEl());
1113         result.getProjections().add(projection);
1114         return result;
1115     }
1116     
1117     @Override
1118     public ASTNode visitTargetEl(final TargetElContext ctx) {
1119         ProjectionSegment result = createProjectionSegment(ctx, ctx.aExpr());
1120         if (null != ctx.identifier()) {
1121             ((AliasAvailable) result).setAlias(new AliasSegment(ctx.identifier().start.getStartIndex(), ctx.identifier().stop.getStopIndex(), new IdentifierValue(ctx.identifier().getText())));
1122         }
1123         return result;
1124     }
1125     
1126     private ProjectionSegment createProjectionSegment(final TargetElContext ctx, final AExprContext expr) {
1127         if (null != ctx.ASTERISK_()) {
1128             return new ShorthandProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
1129         }
1130         if (null != ctx.DOT_ASTERISK_()) {
1131             ShorthandProjectionSegment result = new ShorthandProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
1132             result.setOwner(new OwnerSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText())));
1133             return result;
1134         }
1135         if (null != ctx.aExpr()) {
1136             ASTNode projection = visit(ctx.aExpr());
1137             return createProjectionSegment(ctx, expr, projection);
1138         }
1139         return new ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getOriginalText(expr), null);
1140     }
1141     
1142     private ProjectionSegment createProjectionSegment(final TargetElContext ctx, final AExprContext expr, final ASTNode projection) {
1143         if (projection instanceof ColumnSegment) {
1144             return new ColumnProjectionSegment((ColumnSegment) projection);
1145         }
1146         if (projection instanceof AggregationProjectionSegment) {
1147             return (AggregationProjectionSegment) projection;
1148         }
1149         if (projection instanceof SubqueryExpressionSegment) {
1150             SubqueryExpressionSegment subqueryExpression = (SubqueryExpressionSegment) projection;
1151             String text = ctx.start.getInputStream().getText(new Interval(subqueryExpression.getStartIndex(), subqueryExpression.getStopIndex()));
1152             return new SubqueryProjectionSegment(subqueryExpression.getSubquery(), text);
1153         }
1154         if (projection instanceof ExistsSubqueryExpression) {
1155             ExistsSubqueryExpression existsSubqueryExpression = (ExistsSubqueryExpression) projection;
1156             String text = ctx.start.getInputStream().getText(new Interval(existsSubqueryExpression.getStartIndex(), existsSubqueryExpression.getStopIndex()));
1157             return new SubqueryProjectionSegment(existsSubqueryExpression.getSubquery(), text);
1158         }
1159         if (projection instanceof ExpressionSegment) {
1160             return new ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getOriginalText(expr), (ExpressionSegment) projection);
1161         }
1162         return new ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), getOriginalText(expr), null);
1163     }
1164     
1165     @Override
1166     public ASTNode visitFromClause(final FromClauseContext ctx) {
1167         return visit(ctx.fromList());
1168     }
1169     
1170     @Override
1171     public ASTNode visitFromList(final FromListContext ctx) {
1172         if (null != ctx.fromList()) {
1173             JoinTableSegment result = new JoinTableSegment();
1174             result.setStartIndex(ctx.start.getStartIndex());
1175             result.setStopIndex(ctx.stop.getStopIndex());
1176             result.setLeft((TableSegment) visit(ctx.fromList()));
1177             result.setRight((TableSegment) visit(ctx.tableReference()));
1178             result.setJoinType(JoinType.COMMA.name());
1179             return result;
1180         }
1181         return visit(ctx.tableReference());
1182     }
1183     
1184     @Override
1185     public ASTNode visitTableReference(final TableReferenceContext ctx) {
1186         if (null != ctx.relationExpr()) {
1187             return getSimpleTableSegment(ctx);
1188         }
1189         if (null != ctx.selectWithParens()) {
1190             return getSubqueryTableSegment(ctx);
1191         }
1192         if (null != ctx.tableReference()) {
1193             return getJoinTableSegment(ctx);
1194         }
1195         // TODO deal with functionTable and xmlTable
1196         return new SimpleTableSegment(new TableNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue("not support")));
1197     }
1198     
1199     private SimpleTableSegment getSimpleTableSegment(final TableReferenceContext ctx) {
1200         SimpleTableSegment result = (SimpleTableSegment) visit(ctx.relationExpr().qualifiedName());
1201         if (null != ctx.aliasClause()) {
1202             result.setAlias((AliasSegment) visit(ctx.aliasClause()));
1203         }
1204         return result;
1205     }
1206     
1207     private SubqueryTableSegment getSubqueryTableSegment(final TableReferenceContext ctx) {
1208         OpenGaussSelectStatement select = (OpenGaussSelectStatement) visit(ctx.selectWithParens());
1209         SubquerySegment subquery = new SubquerySegment(ctx.selectWithParens().start.getStartIndex(), ctx.selectWithParens().stop.getStopIndex(), select, getOriginalText(ctx.selectWithParens()));
1210         SubqueryTableSegment result = new SubqueryTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), subquery);
1211         if (null != ctx.aliasClause()) {
1212             result.setAlias((AliasSegment) visit(ctx.aliasClause()));
1213         }
1214         return result;
1215     }
1216     
1217     private JoinTableSegment getJoinTableSegment(final TableReferenceContext ctx) {
1218         JoinTableSegment result = new JoinTableSegment();
1219         result.setLeft((TableSegment) visit(ctx.tableReference()));
1220         int startIndex = null == ctx.LP_() ? ctx.tableReference().start.getStartIndex() : ctx.LP_().getSymbol().getStartIndex();
1221         int stopIndex = 0;
1222         AliasSegment alias = null;
1223         if (null == ctx.aliasClause()) {
1224             stopIndex = null == ctx.RP_() ? ctx.tableReference().start.getStopIndex() : ctx.RP_().getSymbol().getStopIndex();
1225         } else {
1226             alias = (AliasSegment) visit(ctx.aliasClause());
1227             startIndex = null == ctx.RP_() ? ctx.joinedTable().stop.getStopIndex() : ctx.RP_().getSymbol().getStopIndex();
1228         }
1229         result.setStartIndex(startIndex);
1230         result.setStopIndex(stopIndex);
1231         visitJoinedTable(ctx.joinedTable(), result);
1232         result.setAlias(alias);
1233         return result;
1234     }
1235     
1236     private JoinTableSegment visitJoinedTable(final JoinedTableContext ctx, final JoinTableSegment tableSegment) {
1237         TableSegment right = (TableSegment) visit(ctx.tableReference());
1238         tableSegment.setRight(right);
1239         tableSegment.setJoinType(getJoinType(ctx));
1240         tableSegment.setNatural(null != ctx.naturalJoinType());
1241         return null == ctx.joinQual() ? tableSegment : visitJoinQual(ctx.joinQual(), tableSegment);
1242     }
1243     
1244     private String getJoinType(final JoinedTableContext ctx) {
1245         if (null != ctx.crossJoinType()) {
1246             return JoinType.CROSS.name();
1247         }
1248         if (null != ctx.innerJoinType()) {
1249             return JoinType.INNER.name();
1250         }
1251         if (null != ctx.outerJoinType()) {
1252             return getOutJoinType(ctx.outerJoinType());
1253         }
1254         if (null != ctx.naturalJoinType()) {
1255             return getNaturalJoinType(ctx.naturalJoinType());
1256         }
1257         return JoinType.COMMA.name();
1258     }
1259     
1260     private String getOutJoinType(final OuterJoinTypeContext ctx) {
1261         if (null != ctx.FULL()) {
1262             return JoinType.FULL.name();
1263         }
1264         return null == ctx.LEFT() ? JoinType.RIGHT.name() : JoinType.LEFT.name();
1265     }
1266     
1267     private String getNaturalJoinType(final NaturalJoinTypeContext ctx) {
1268         if (null != ctx.INNER()) {
1269             return JoinType.INNER.name();
1270         }
1271         if (null != ctx.FULL()) {
1272             return JoinType.FULL.name();
1273         }
1274         if (null != ctx.LEFT()) {
1275             return JoinType.LEFT.name();
1276         }
1277         if (null != ctx.RIGHT()) {
1278             return JoinType.RIGHT.name();
1279         }
1280         return JoinType.INNER.name();
1281     }
1282     
1283     private JoinTableSegment visitJoinQual(final JoinQualContext ctx, final JoinTableSegment joinTableSource) {
1284         if (null != ctx.aExpr()) {
1285             ExpressionSegment condition = (ExpressionSegment) visit(ctx.aExpr());
1286             joinTableSource.setCondition(condition);
1287         }
1288         if (null != ctx.USING()) {
1289             joinTableSource.setUsing(generateUsingColumn(ctx.nameList()));
1290         }
1291         return joinTableSource;
1292     }
1293     
1294     private List<ColumnSegment> generateUsingColumn(final NameListContext ctx) {
1295         List<ColumnSegment> result = new ArrayList<>();
1296         if (null != ctx.nameList()) {
1297             result.addAll(generateUsingColumn(ctx.nameList()));
1298         }
1299         if (null != ctx.name()) {
1300             result.add(new ColumnSegment(ctx.name().start.getStartIndex(), ctx.name().stop.getStopIndex(), new IdentifierValue(ctx.name().getText())));
1301         }
1302         return result;
1303     }
1304     
1305     @Override
1306     public ASTNode visitAliasClause(final AliasClauseContext ctx) {
1307         StringBuilder aliasName = new StringBuilder(ctx.colId().getText());
1308         if (null != ctx.nameList()) {
1309             aliasName.append(ctx.LP_().getText());
1310             aliasName.append(ctx.nameList().getText());
1311             aliasName.append(ctx.RP_().getText());
1312         }
1313         return new AliasSegment(ctx.colId().start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(aliasName.toString()));
1314     }
1315     
1316     private OwnerSegment createTableOwner(final IndirectionContext ctx) {
1317         AttrNameContext attrName = ctx.indirectionEl().attrName();
1318         return new OwnerSegment(attrName.start.getStartIndex(), attrName.stop.getStopIndex(), new IdentifierValue(attrName.getText()));
1319     }
1320     
1321     @Override
1322     public ASTNode visitWhereClause(final WhereClauseContext ctx) {
1323         ExpressionSegment expr = (ExpressionSegment) visit(ctx.aExpr());
1324         return new WhereSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
1325     }
1326     
1327     @Override
1328     public ASTNode visitSelectLimit(final SelectLimitContext ctx) {
1329         if (null != ctx.limitClause() && null != ctx.offsetClause()) {
1330             return createLimitSegmentWhenLimitAndOffset(ctx);
1331         }
1332         return createLimitSegmentWhenRowCountOrOffsetAbsent(ctx);
1333     }
1334     
1335     @Override
1336     public ASTNode visitSelectLimitValue(final SelectLimitValueContext ctx) {
1337         if (null != ctx.ALL()) {
1338             return null;
1339         }
1340         ASTNode astNode = visit(ctx.cExpr());
1341         if (astNode instanceof ParameterMarkerExpressionSegment) {
1342             return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerExpressionSegment) astNode).getParameterMarkerIndex());
1343         }
1344         return new NumberLiteralLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), Long.parseLong(((LiteralExpressionSegment) astNode).getLiterals().toString()));
1345     }
1346     
1347     @Override
1348     public ASTNode visitSelectOffsetValue(final SelectOffsetValueContext ctx) {
1349         ASTNode astNode = visit(ctx.cExpr());
1350         if (astNode instanceof ParameterMarkerExpressionSegment) {
1351             return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerExpressionSegment) astNode).getParameterMarkerIndex());
1352         }
1353         return new NumberLiteralLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), Long.parseLong(((LiteralExpressionSegment) astNode).getLiterals().toString()));
1354     }
1355     
1356     @Override
1357     public ASTNode visitSelectFetchValue(final SelectFetchValueContext ctx) {
1358         ASTNode astNode = visit(ctx.cExpr());
1359         if (astNode instanceof ParameterMarkerExpressionSegment) {
1360             return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerExpressionSegment) astNode).getParameterMarkerIndex());
1361         }
1362         return new NumberLiteralLimitValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), Long.parseLong(((LiteralExpressionSegment) astNode).getLiterals().toString()));
1363     }
1364     
1365     private LimitSegment createLimitSegmentWhenLimitAndOffset(final SelectLimitContext ctx) {
1366         ParseTree astNode0 = ctx.getChild(0);
1367         LimitValueSegment rowCount = null;
1368         LimitValueSegment offset = null;
1369         if (astNode0 instanceof LimitClauseContext) {
1370             rowCount = null == ctx.limitClause().selectLimitValue() ? null : (LimitValueSegment) visit(ctx.limitClause().selectLimitValue());
1371         } else {
1372             offset = (LimitValueSegment) visit(ctx.offsetClause().selectOffsetValue());
1373         }
1374         ParseTree astNode1 = ctx.getChild(1);
1375         if (astNode1 instanceof LimitClauseContext) {
1376             rowCount = null == ctx.limitClause().selectLimitValue() ? null : (LimitValueSegment) visit(ctx.limitClause().selectLimitValue());
1377         } else {
1378             offset = (LimitValueSegment) visit(ctx.offsetClause().selectOffsetValue());
1379         }
1380         return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, rowCount);
1381     }
1382     
1383     private LimitSegment createLimitSegmentWhenRowCountOrOffsetAbsent(final SelectLimitContext ctx) {
1384         if (null != ctx.limitClause()) {
1385             if (null != ctx.limitClause().selectOffsetValue()) {
1386                 LimitValueSegment limit = (LimitValueSegment) visit(ctx.limitClause().selectLimitValue());
1387                 LimitValueSegment offset = (LimitValueSegment) visit(ctx.limitClause().selectOffsetValue());
1388                 return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, limit);
1389             }
1390             if (null != ctx.limitClause().selectFetchValue()) {
1391                 LimitValueSegment limit = (LimitValueSegment) visit(ctx.limitClause().selectFetchValue());
1392                 return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null, limit);
1393             }
1394             LimitValueSegment limit = (LimitValueSegment) visit(ctx.limitClause().selectLimitValue());
1395             return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null, limit);
1396         }
1397         LimitValueSegment offset = (LimitValueSegment) visit(ctx.offsetClause().selectOffsetValue());
1398         return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, null);
1399     }
1400     
1401     @Override
1402     public ASTNode visitExecuteStmt(final ExecuteStmtContext ctx) {
1403         return new OpenGaussExecuteStatement();
1404     }
1405     
1406     /**
1407      * Get original text.
1408      *
1409      * @param ctx context
1410      * @return original text
1411      */
1412     protected String getOriginalText(final ParserRuleContext ctx) {
1413         return ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
1414     }
1415     
1416     @Override
1417     @SuppressWarnings("unchecked")
1418     public ASTNode visitAnyName(final AnyNameContext ctx) {
1419         CollectionValue<NameSegment> result = new CollectionValue<>();
1420         if (null != ctx.attrs()) {
1421             result.combine((CollectionValue<NameSegment>) visit(ctx.attrs()));
1422         }
1423         result.getValue().add(new NameSegment(ctx.colId().getStart().getStartIndex(), ctx.colId().getStop().getStopIndex(), new IdentifierValue(ctx.colId().getText())));
1424         return result;
1425     }
1426     
1427     @Override
1428     @SuppressWarnings("unchecked")
1429     public ASTNode visitAttrs(final AttrsContext ctx) {
1430         CollectionValue<NameSegment> result = new CollectionValue<>();
1431         result.getValue().add(new NameSegment(ctx.attrName().getStart().getStartIndex(), ctx.attrName().getStop().getStopIndex(), new IdentifierValue(ctx.attrName().getText())));
1432         if (null != ctx.attrs()) {
1433             result.combine((CollectionValue<NameSegment>) visit(ctx.attrs()));
1434         }
1435         return result;
1436     }
1437     
1438     @Override
1439     public ASTNode visitSignedIconst(final SignedIconstContext ctx) {
1440         return new NumberLiteralValue(ctx.getText());
1441     }
1442 }