Source:
public SQLQuery resolveTemplate(Query clientQuery, SQLQueryTemplate template, Sorting sorting, FilterExpression whereClause, FilterExpression havingClause) { SQLAnalyticView sqlAnalyticView = (SQLAnalyticView) clientQuery.getTable(); Table elideTable = clientQuery.getTable(); Class<?> tableCls = clientQuery.getTable().getCls(); String tableAlias = getClassAlias(tableCls); SQLQuery.SQLQueryBuilder builder = SQLQuery.builder().clientQuery(clientQuery); Set<Path.PathElement> joinPredicates = new HashSet<>(); String tableStatement = tableCls.isAnnotationPresent(FromSubquery.class) ? "(" + tableCls.getAnnotation(FromSubquery.class).sql() + ")" : tableCls.isAnnotationPresent(FromTable.class) ? tableCls.getAnnotation(FromTable.class).name() : elideTable.getName(); builder.fromClause(String.format("%s AS %s", tableStatement, tableAlias)); builder.projectionClause(referenceProject(template, sqlAnalyticView)); Set<ColumnProjection> groupByDimensions = template.getGroupByDimensions(); if (!groupByDimensions.isEmpty()) { builder.groupByClause(referenceProjectGroupBy(groupByDimensions, sqlAnalyticView)); joinPredicates.addAll(extractPathElements(groupByDimensions, sqlAnalyticView)); } if (whereClause != null) { joinPredicates.addAll(extractPathElements(whereClause)); builder.whereClause("WHERE " + translateFilterExpression( whereClause, this::generateColumnReference)); } if (havingClause != null) { joinPredicates.addAll(extractPathElements(havingClause)); builder.havingClause("HAVING " + translateFilterExpression( havingClause, (predicate) -> referenceProjectHavingFilter(predicate, elideTable, template))); } if (sorting != null) { Map<Path, Sorting.SortOrder> sortClauses = sorting.getValidSortingRules(tableCls, dictionary); builder.orderByClause(extractOrderBy(tableCls, sortClauses)); joinPredicates.addAll(extractPathElements(sortClauses)); } String joinClause = joinPredicates.stream() .map(this::extractJoin) .collect(Collectors.joining(" ")); builder.joinClause(joinClause); return builder.build(); }

Target:
public SQLQuery resolveTemplate(Query clientQuery, SQLQueryTemplate template, Sorting sorting, FilterExpression whereClause, FilterExpression havingClause) { SQLAnalyticView queriedTable = (SQLAnalyticView) clientQuery.getAnalyticView(); Class<?> tableCls = clientQuery.getAnalyticView().getCls(); String tableAlias = getClassAlias(tableCls); SQLQuery.SQLQueryBuilder builder = SQLQuery.builder().clientQuery(clientQuery); Set<Path.PathElement> joinPredicates = new HashSet<>(); String tableStatement = tableCls.isAnnotationPresent(FromSubquery.class) ? "(" + tableCls.getAnnotation(FromSubquery.class).sql() + ")" : tableCls.isAnnotationPresent(FromTable.class) ? tableCls.getAnnotation(FromTable.class).name() : queriedTable.getName(); builder.fromClause(String.format("%s AS %s", tableStatement, tableAlias)); builder.projectionClause(constructProjectionWithReference(template, queriedTable)); Set<ColumnProjection> groupByDimensions = template.getGroupByDimensions(); if (!groupByDimensions.isEmpty()) { builder.groupByClause(constructGroupByWithReference(groupByDimensions, queriedTable)); joinPredicates.addAll(extractPathElements(groupByDimensions, queriedTable)); } if (whereClause != null) { joinPredicates.addAll(extractPathElements(whereClause)); builder.whereClause("WHERE " + translateFilterExpression( whereClause, this::generateColumnReference)); } if (havingClause != null) { joinPredicates.addAll(extractPathElements(havingClause)); builder.havingClause("HAVING " + translateFilterExpression( havingClause, (predicate) -> constructHavingClauseWithReference(predicate, queriedTable, template))); } if (sorting != null) { Map<Path, Sorting.SortOrder> sortClauses = sorting.getValidSortingRules(tableCls, dictionary); builder.orderByClause(extractOrderBy(tableCls, sortClauses)); joinPredicates.addAll(extractPathElements(sortClauses)); } String joinClause = joinPredicates.stream() .map(this::extractJoin) .collect(Collectors.joining(" ")); builder.joinClause(joinClause); return builder.build(); }