in fastmodel-parser/src/main/java/com/aliyun/fastmodel/parser/visitor/QueryVisitor.java [150:254]
public Node visitQueryNoWith(QueryNoWithContext ctx) {
BaseQueryBody term = (BaseQueryBody)visit(ctx.queryTerm());
Optional<OrderBy> orderBy = Optional.empty();
if (ctx.KW_ORDER() != null) {
orderBy = Optional.of(new OrderBy(visit(ctx.sortItem(), SortItem.class)));
}
ClusterBy clusterBy = null;
if (ctx.clusterByClause() != null) {
clusterBy = (ClusterBy)visit(ctx.clusterByClause());
}
DistributeBy distributeBy = null;
if (ctx.distributeByClause() != null) {
distributeBy = (DistributeBy)visit(ctx.distributeByClause());
}
SortBy sortBy = null;
if (ctx.sortByClause() != null) {
sortBy = (SortBy)visit(ctx.sortByClause());
}
Optional<Offset> offset = Optional.empty();
if (ctx.KW_OFFSET() != null) {
BaseExpression rowCount;
if (ctx.offset.INTEGER_VALUE() != null) {
rowCount = new LongLiteral(ctx.offset.getText());
} else {
rowCount = new Parameter(ParserHelper.getLocation(ctx.offset.QUESTION()),
getOrigin(ctx.offset.QUESTION()), parameterPosition);
parameterPosition++;
}
offset = Optional.of(new Offset(ParserHelper.getLocation(ctx.KW_OFFSET()), rowCount));
}
Optional<Node> limit = Optional.empty();
if (ctx.KW_FETCH() != null) {
Optional<BaseExpression> rowCount = Optional.empty();
if (ctx.fetchFirst != null) {
if (ctx.fetchFirst.INTEGER_VALUE() != null) {
rowCount = Optional.of(new LongLiteral(ctx.fetchFirst.getText()));
} else {
rowCount = Optional.of(
new Parameter(ParserHelper.getLocation(ctx.fetchFirst.QUESTION()),
getOrigin(ctx.fetchFirst.QUESTION()), parameterPosition));
parameterPosition++;
}
}
limit = Optional.of(
new FetchFirst(ParserHelper.getLocation(ctx.KW_FETCH()), rowCount.orElse(null), ctx.KW_TIES() != null));
} else if (ctx.KW_LIMIT() != null) {
if (ctx.limit == null) {
throw new IllegalStateException("Missing LIMIT value");
}
BaseExpression rowCount;
if (ctx.limit.KW_ALL() != null) {
rowCount = new AllRows(ParserHelper.getLocation(ctx.limit.KW_ALL()), getOrigin(ctx.limit.KW_ALL()));
} else if (ctx.limit.rowCount().INTEGER_VALUE() != null) {
rowCount = new LongLiteral(ctx.limit.getText());
} else {
rowCount = new Parameter(ParserHelper.getLocation(ctx.limit.rowCount().QUESTION()),
getOrigin(ctx.limit.rowCount().QUESTION()), parameterPosition);
parameterPosition++;
}
limit = Optional.of(new Limit(ParserHelper.getLocation(ctx.KW_LIMIT()), rowCount));
}
if (term instanceof QuerySpecification) {
// When we have a simple query specification
// followed by order by, offset, limit or fetch,
// fold the order by, limit, offset or fetch clauses
// into the query specification (analyzer/planner
// expects this structure to resolve references with respect
// to columns defined in the query specification)
QuerySpecification query = (QuerySpecification)term;
return new Query(
null,
new QuerySpecification(
query.getSelect(),
query.getHints(),
query.getFrom(),
query.getWhere(),
query.getGroupBy(),
query.getHaving(),
orderBy.orElse(null),
clusterBy,
distributeBy,
sortBy,
offset.orElse(null),
limit.orElse(null)),
null,
null,
null);
}
return new Query(
null,
term,
orderBy.orElse(null),
offset.orElse(null),
limit.orElse(null));
}