{"record":{"id":"c3e66e23f2b4f519","repo":"OtterMind/Chat2DB","slug":"count-error","errorCode":"count error","errorMessage":"count error","messagePattern":"count error","errorType":"exception","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbDlTemplateServiceImpl.java","lineNumber":154,"sourceCode":"                    .build());\n            List<List<String>> dataList = executeResult.getDisplayDataList();\n            if (CollectionUtils.isEmpty(dataList)) {\n                return 0L;\n            }\n            String count = EasyCollectionUtils.stream(dataList)\n                    .findFirst()\n                    .orElse(Collections.emptyList())\n                    .stream()\n                    .findFirst()\n                    .orElse(\"0\");\n            return Long.valueOf(count);\n        } catch (Exception e) {\n            log.warn(\"Failed to execute SQL: {}\", sql, e);\n            try {\n                Long count = executor.count(sql, Chat2DBContext.getConnection());\n                return count;\n            } catch (Exception e1) {\n                throw new BusinessException(\"count error\", new Object[]{sql, e1.getMessage()}, e1);\n            }\n        }\n    }\n\n    @Override\n    public String updateSelectResult(DbSelectResultUpdateRequest param) {\n        ISqlBuilder sqlBuilder = Chat2DBContext.getSqlBuilder();\n        QueryResponse queryResult = commandConverter.updateSelectResult2query(param);\n        return sqlBuilder.dml().buildByQueryResult(queryResult);\n    }\n\n    @Override\n    public String copySelectResult(DbSelectResultUpdateRequest param) {\n\n        ISqlBuilder sqlBuilder = Chat2DBContext.getSqlBuilder();\n        QueryResponse queryResult = commandConverter.updateSelectResult2query(param);\n        return sqlBuilder.dml().buildCopyByQueryResult(queryResult);\n    }","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbDlTemplateServiceImpl.java#L136-L172","documentation":"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.","triggerScenarios":"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].","commonSituations":"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.","solutions":["Inspect the SQL in the error args and the chained cause for the DB-native error.","Verify the table exists and the user has SELECT permission.","If SqlUtils.count() mangled the SQL, test the count query manually against the DB.","Handle null from SqlUtils.count() separately — that path uses executor.count() directly."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Long total = service.count(req);\n} catch (BusinessException e) {\n    if (\"count error\".equals(e.getCode())) {\n        Object[] args = e.getArgs(); // [sql, fallbackMessage]\n        // log args[0] (sql) and inspect e.getCause() for the DB error\n    } else { throw e; }\n}","preventionTips":["Validate the table exists and the user has SELECT permission before counting.","Test the generated count SQL manually for complex queries.","Note SqlUtils.count() returning null takes a different (uncaught) path."],"tags":["count","sql-execution","fallback-exhausted"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}