OtterMind/Chat2DB · error · BusinessException

count error

count error

Error message

count error

What it means

Thrown by count() after BOTH the optimized count-query path and the fallback executor.count() path fail. The primary path builds a count SQL via SqlUtils.count() and executes it; if that throws, the catch block retries with executor.count(sql, connection). Only if that fallback also throws is this error raised, carrying the SQL and the fallback exception message. Note: if SqlUtils.count() returns null, the primary path calls executor.count() directly and any failure there is NOT caught by this error.

Source

Thrown at chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbDlTemplateServiceImpl.java:154

                    .build());
            List<List<String>> dataList = executeResult.getDisplayDataList();
            if (CollectionUtils.isEmpty(dataList)) {
                return 0L;
            }
            String count = EasyCollectionUtils.stream(dataList)
                    .findFirst()
                    .orElse(Collections.emptyList())
                    .stream()
                    .findFirst()
                    .orElse("0");
            return Long.valueOf(count);
        } catch (Exception e) {
            log.warn("Failed to execute SQL: {}", sql, e);
            try {
                Long count = executor.count(sql, Chat2DBContext.getConnection());
                return count;
            } catch (Exception e1) {
                throw new BusinessException("count error", new Object[]{sql, e1.getMessage()}, e1);
            }
        }
    }

    @Override
    public String updateSelectResult(DbSelectResultUpdateRequest param) {
        ISqlBuilder sqlBuilder = Chat2DBContext.getSqlBuilder();
        QueryResponse queryResult = commandConverter.updateSelectResult2query(param);
        return sqlBuilder.dml().buildByQueryResult(queryResult);
    }

    @Override
    public String copySelectResult(DbSelectResultUpdateRequest param) {

        ISqlBuilder sqlBuilder = Chat2DBContext.getSqlBuilder();
        QueryResponse queryResult = commandConverter.updateSelectResult2query(param);
        return sqlBuilder.dml().buildCopyByQueryResult(queryResult);
    }

View on GitHub (pinned to 5ee1e990e7)

Solutions

  1. Inspect the SQL in the error args and the chained cause for the DB-native error.
  2. Verify the table exists and the user has SELECT permission.
  3. If SqlUtils.count() mangled the SQL, test the count query manually against the DB.
  4. Handle null from SqlUtils.count() separately — that path uses executor.count() directly.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    Long total = service.count(req);
} catch (BusinessException e) {
    if ("count error".equals(e.getCode())) {
        Object[] args = e.getArgs(); // [sql, fallbackMessage]
        // log args[0] (sql) and inspect e.getCause() for the DB error
    } else { throw e; }
}

Prevention

When it happens

Trigger: count() is called with a SQL/table that fails on both the optimized count query (e.g., executor.execute throws) and the fallback executor.count(). The error args are [sql, fallbackExceptionMessage].

Common situations: The count SQL is syntactically invalid for the dialect; the table doesn't exist; the connection drops mid-query; permissions insufficient for the count query; SqlUtils.count() produced a malformed wrapper query.

Related errors


AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14). Data as JSON: /api/errors/c3e66e23f2b4f519. Report an issue: GitHub.