Source:
<code>public SQLQuery resolveTemplate(Query clientQuery, SQLQueryTemplate template, Sorting sorting, FilterExpression whereClause, FilterExpression havingClause) { <START> SQLAnalyticView sqlAnalyticView = (SQLAnalyticView) clientQuery.getTable(); <END> 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(); }</code><technical_language>clientQuery store AnalyticView (since is query) instead of a Table</technical_language>

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(); }