Skip to content

Commit

Permalink
[GEOT-7248] fix query for multiple columns (#4135)
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Jan 10, 2023
1 parent fe840bf commit 91733ce
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class JoiningJDBCFeatureSource extends JDBCFeatureSource {
public static final String PRIMARY_KEY = "PARENT_TABLE_PKEY";

private static final String COUNT_TABLE_ALIAS = "COUNT_TABLE";
private static final String DISTINCT_TABLE_ALIAS = "DISTINCT_TABLE";

public JoiningJDBCFeatureSource(JDBCFeatureSource featureSource) throws IOException {
super(featureSource);
Expand Down Expand Up @@ -1547,7 +1548,7 @@ private String createCountQuery(
Set<String> idColumnNames,
AtomicReference<PreparedFilterToSQL> toSQLRef)
throws FilterToSQLException, SQLException {
StringBuffer countSQL = new StringBuffer("SELECT COUNT(").append("DISTINCT ");
StringBuffer countSQL = new StringBuffer("SELECT COUNT(*) FROM (SELECT DISTINCT ");
boolean first = true;
for (String idColumnName : idColumnNames) {
if (!first) {
Expand All @@ -1558,13 +1559,15 @@ private String createCountQuery(
dialect.encodeColumnName(null, idColumnName, countSQL);
first = false;
}
countSQL.append(")").append(" FROM ");
countSQL.append(" FROM ");
getDataStore().encodeTableName(querySchema.getTypeName(), countSQL, query.getHints());
if (!query.getFilter().equals(Filter.INCLUDE)) {
FilterToSQL toSql = createFilterToSQL(querySchema);
countSQL.append(toSql.encodeToString(query.getFilter()));
if (toSql instanceof PreparedFilterToSQL) toSQLRef.set((PreparedFilterToSQL) toSql);
}
countSQL.append(")");
dialect.encodeTableName(DISTINCT_TABLE_ALIAS, countSQL);
String countQuery = countSQL.toString();
if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine(countQuery);
return countQuery;
Expand All @@ -1577,7 +1580,7 @@ private String createJoiningCountQuery(
Set<String> idColumnNames,
AtomicReference<PreparedFilterToSQL> toSQLRef)
throws IOException, SQLException, FilterToSQLException {
StringBuffer countSQL = new StringBuffer("SELECT COUNT(").append("DISTINCT ");
StringBuffer countSQL = new StringBuffer("SELECT COUNT(*) FROM (SELECT DISTINCT ");
boolean first = true;
for (String idColumnName : idColumnNames) {
if (!first) {
Expand All @@ -1586,10 +1589,12 @@ private String createJoiningCountQuery(
dialect.encodeColumnName(COUNT_TABLE_ALIAS, idColumnName, countSQL);
first = false;
}
countSQL.append(")").append(" FROM (");
countSQL.append(" FROM (");
String sql = selectSQL(querySchema, query, toSQLRef, true);
countSQL.append(sql).append(") ");
dialect.encodeTableName(COUNT_TABLE_ALIAS, countSQL);
countSQL.append(") ");
dialect.encodeTableName(DISTINCT_TABLE_ALIAS, countSQL);
String countQuery = countSQL.toString();
if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine(countQuery);
return countQuery;
Expand Down

0 comments on commit 91733ce

Please sign in to comment.