Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GEOT-7248] fix query for multiple columns #4135

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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