{"record":{"id":"8106e7c017bd1563","repo":"stanfordnlp/CoreNLP","slug":"getting-table-count-is-not-working","errorCode":null,"errorMessage":"getting table count is not working!","messagePattern":"getting table count is not working!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/GoogleNGramsSQLBacked.java","lineNumber":188,"sourceCode":"        stmt.execute(q);\n      }\n    }\n  }\n\n  /** Note that this is really really slow for ngram > 1\n   * TODO: make this fast (if we had been using mysql we could have)\n   * **/\n  public static int getTotalCount(int ngram){\n    try{\n      connect();\n      Statement stmt = connection.createStatement();\n      String table = tablenamePrefix + ngram;\n      String q = \"select count(*) from \" + table+\";\";\n      ResultSet s = stmt.executeQuery(q);\n      if(s.next()){\n        return s.getInt(1);\n      } else\n        throw new RuntimeException(\"getting table count is not working!\");\n    }\n    catch(SQLException e){\n      throw new RuntimeException(\"getting table count is not working! \" + e);\n    }\n  }\n\n  /** Return rank of 1 gram in google ngeams if it is less than 20k. Otherwise -1. */\n  public static int get1GramRank(String str) {\n    String query = null;\n    try{\n      connect();\n      str = str.trim();\n      if(str.contains(\"'\")){\n        str = StringUtils.escapeString(str, new char[]{'\\''},'\\'');\n      }\n\n      int ngram = str.split(\"\\\\s+\").length;\n      if(ngram > 1)","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/GoogleNGramsSQLBacked.java#L170-L206","documentation":"getTotalCount runs 'select count(*) from GoogleNgrams_<n>' and returns the first row's value. If the ResultSet has no rows (unexpected for a count query), it throws this RuntimeException, indicating the query unexpectedly returned no result.","triggerScenarios":"Calling getTotalCount on a table for which the SELECT returns an empty ResultSet — practically only when the query was rewritten, the table is a view behaving oddly, or the JDBC driver misbehaves.","commonSituations":"Modifying the query to something other than count(*); driver or connection issues yielding an unusable ResultSet.","solutions":["Verify the table exists and the query is still 'select count(*) from <table>' (a count query always returns one row)","Recreate the table per the class docs if it is corrupted or empty in an unexpected state","Check for JDBC driver problems; try the query manually in psql"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// confirm the table exists before counting\nselect to_regclass('GoogleNgrams_1') is not null as ok;","typeGuard":null,"tryCatchPattern":"try { int c = GoogleNGramsSQLBacked.getTotalCount(n); } catch (RuntimeException e) { log(\"count unavailable: \" + e.getMessage()); }","preventionTips":["Ensure count(*) queries are left unmodified (they always return one row)","Check DB connectivity before batch queries","Manually run the query in psql to sanity-check"],"tags":["sql","postgres","empty-result"],"backgroundTag":"empty-result-set","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}