SamzaSqlValidator { public static String formatErrorString(String query, Exception e) { Pattern pattern = Pattern.compile("line [0-9]+, column [0-9]+"); Matcher matcher = pattern.matcher(e.getMessage()); String[] queryLines = query.split("\\n"); StringBuilder result = new StringBuilder(); int startColIdx; int endColIdx; int startLineIdx; int endLineIdx; try { if (matcher.find()) { String match = matcher.group(); LOG.info(match); startLineIdx = getIdxFromString(match, "line "); startColIdx = getIdxFromString(match, "column "); if (matcher.find()) { match = matcher.group(); LOG.info(match); endLineIdx = getIdxFromString(match, "line "); endColIdx = getIdxFromString(match, "column "); } else { endColIdx = startColIdx; endLineIdx = startLineIdx; } int lineLen = endLineIdx - startLineIdx; int colLen = endColIdx - startColIdx + 1; if (lineLen > 0) { throw new SamzaException("lineLen formatting validation error: error cannot span across multiple lines."); } int lineIdx = 0; for (String line : queryLines) { result.append(line) .append("\n"); if (lineIdx == startLineIdx) { String lineStr = getStringWithRepeatedChars('-', line.length() - 1); String pointerStr = getStringWithRepeatedChars('^', colLen); String errorMarkerStr = new StringBuilder(lineStr).replace(startColIdx, endColIdx, pointerStr).toString(); result.append(errorMarkerStr) .append("\n"); } lineIdx++; } } String[] errorMsgParts = e.getMessage().split("Exception:"); result.append("\n") .append(errorMsgParts[errorMsgParts.length - 1].trim()); return String.format("Sql syntax error:\n\n%s\n", result); } catch (Exception ex) { LOG.error("Formatting error (Not the actual error. Look for the logs for actual error)", ex); return String.format("Failed with formatting exception (not the actual error) for the following sql" + " statement:\n\"%s\"\n\n%s", query, e.getMessage()); } } SamzaSqlValidator(Config config); void validate(List<String> sqlStmts); static String formatErrorString(String query, Exception e);  }
@Test public void testFormatErrorString() { String sql = "select 'SampleJob' as jobName, pv.pageKey, count(*) as `count`\n" + "from testavro.PAGEVIEW as pv\n" + "where pv.pageKey = 'job' or pv.pageKey = 'inbox'\n" + "group bys (pv.pageKey)"; String errorStr = "org.apache.calcite.tools.ValidationException: org.apache.calcite.runtime.CalciteContextException: " + "From line 3, column 7 to line 3, column 16: Column 'pv.pageKey' not found in any table"; String formattedErrStr = SamzaSqlValidator.formatErrorString(sql, new Exception(errorStr)); LOG.info(formattedErrStr); }
@Test public void testExceptionInFormatErrorString() { String sql = "select 'SampleJob' as jobName, pv.pageKey, count(*) as `count`\n" + "from testavro.PAGEVIEW as pv\n" + "where pv.pageKey = 'job' or pv.pageKey = 'inbox'\n" + "group bys (pv.pageKey)"; String errorStr = "org.apache.calcite.tools.ValidationException: org.apache.calcite.runtime.CalciteContextException: " + "From line 3, column 7 to line 3, column 16: Column 'pv.pageKey' not found in any table"; String formattedErrStr = SamzaSqlValidator.formatErrorString(sql, new Exception(errorStr)); LOG.info(formattedErrStr); }