Source:
public LogicalPlan visitQuerySpecification(QuerySpecificationContext ctx) { LogicalPlan query; if (ctx.fromClause() == null) { query = new LocalRelation(source(ctx), new SingletonExecutable()); } else { query = plan(ctx.fromClause()); } if (ctx.where != null) { query = new Filter(source(ctx), query, expression(ctx.where)); } List<NamedExpression> selectTarget = ctx.selectItems().isEmpty() ? emptyList() : visitList(ctx.selectItems().selectItem(), NamedExpression.class); GroupByContext groupByCtx = ctx.groupBy(); if (groupByCtx != null) { SetQuantifierContext setQualifierContext = groupByCtx.setQuantifier(); TerminalNode groupByAll = setQualifierContext == null ? null : setQualifierContext.ALL(); if (groupByAll != null) { throw new ParsingException(source(groupByAll), "GROUP BY ALL is not supported"); } List<GroupingElementContext> groupingElement = groupByCtx.groupingElement(); List<Expression> groupBy = expressions(groupingElement); ParserRuleContext endSource = groupingElement.isEmpty() ? groupByCtx : groupingElement.get(groupingElement.size() - 1); query = new Aggregate(source(ctx.GROUP(), endSource), query, groupBy, selectTarget); } else if (!selectTarget.isEmpty()) { query = new Project(source(ctx.selectItems()), query, selectTarget); } if (ctx.having != null) { query = new Filter(source(ctx.having), query, expression(ctx.having)); } if (ctx.setQuantifier() != null && ctx.setQuantifier().DISTINCT() != null) { query = new Distinct(source(ctx.setQuantifier()), query); } SqlBaseParser.TopClauseContext topClauseContext = ctx.topClause(); if (topClauseContext != null) { if (topClauseContext.top != null && topClauseContext.INTEGER_VALUE() != null) { query = limit(query, source(topClauseContext), topClauseContext.top); } } return query; }

Target:
public LogicalPlan visitQuerySpecification(QuerySpecificationContext ctx) { LogicalPlan query; if (ctx.fromClause() == null) { query = new LocalRelation(source(ctx), new SingletonExecutable()); } else { query = plan(ctx.fromClause()); } if (ctx.where != null) { query = new Filter(source(ctx), query, expression(ctx.where)); } List<NamedExpression> selectTarget = ctx.selectItems().isEmpty() ? emptyList() : visitList(ctx.selectItems().selectItem(), NamedExpression.class); GroupByContext groupByCtx = ctx.groupBy(); if (groupByCtx != null) { SetQuantifierContext setQualifierContext = groupByCtx.setQuantifier(); TerminalNode groupByAll = setQualifierContext == null ? null : setQualifierContext.ALL(); if (groupByAll != null) { throw new ParsingException(source(groupByAll), "GROUP BY ALL is not supported"); } List<GroupingElementContext> groupingElement = groupByCtx.groupingElement(); List<Expression> groupBy = expressions(groupingElement); ParserRuleContext endSource = groupingElement.isEmpty() ? groupByCtx : groupingElement.get(groupingElement.size() - 1); query = new Aggregate(source(ctx.GROUP(), endSource), query, groupBy, selectTarget); } else if (!selectTarget.isEmpty()) { query = new Project(source(ctx.selectItems()), query, selectTarget); } if (ctx.having != null) { query = new Filter(source(ctx.having), query, expression(ctx.having)); } if (ctx.setQuantifier() != null && ctx.setQuantifier().DISTINCT() != null) { query = new Distinct(source(ctx.setQuantifier()), query); } SqlBaseParser.TopClauseContext topClauseContext = ctx.topClause(); if (topClauseContext != null && topClauseContext.top != null && topClauseContext.INTEGER_VALUE() != null) { query = limit(query, source(topClauseContext), topClauseContext.top); } return query; }