KylinInterpreter extends Interpreter { String formatResult(String msg) { StringBuilder res = new StringBuilder("%table "); Matcher ml = KYLIN_TABLE_FORMAT_REGEX_LABEL.matcher(msg); while (!ml.hitEnd() && ml.find()) { res.append(ml.group(1) + " \t"); } res.append(" \n"); Matcher mr = KYLIN_TABLE_FORMAT_REGEX_RESULTS.matcher(msg); String table = null; while (!mr.hitEnd() && mr.find()) { table = mr.group(1); } if (table != null && !table.isEmpty()) { String[] row = table.split("],\\["); for (int i = 0; i < row.length; i++) { String[] col = row[i].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); for (int j = 0; j < col.length; j++) { if (col[j] != null) { col[j] = col[j].replaceAll("^\"|\"$", ""); } res.append(col[j] + " \t"); } res.append(" \n"); } } return res.toString(); } KylinInterpreter(Properties property); @Override void open(); @Override void close(); @Override InterpreterResult interpret(String st, InterpreterContext context); @Override void cancel(InterpreterContext context); @Override FormType getFormType(); @Override int getProgress(InterpreterContext context); @Override List<InterpreterCompletion> completion(String buf, int cursor,
      InterpreterContext interpreterContext); HttpResponse prepareRequest(String sql); String getProject(String cmd); String getSQL(String cmd);  }
@Test public void testParseResult() { String msg = "{\"columnMetas\":[{\"isNullable\":1,\"displaySize\":256,\"label\":\"COUNTRY\"," + "\"name\":\"COUNTRY\",\"schemaName\":\"DEFAULT\",\"catelogName\":null," + "\"tableName\":\"SALES_TABLE\",\"precision\":256,\"scale\":0,\"columnType\":12," + "\"columnTypeName\":\"VARCHAR\",\"writable\":false,\"readOnly\":true," + "\"definitelyWritable\":false,\"autoIncrement\":false,\"caseSensitive\":true," + "\"searchable\":false,\"currency\":false,\"signed\":true},{\"isNullable\":1," + "\"displaySize\":256,\"label\":\"CURRENCY\",\"name\":\"CURRENCY\"," + "\"schemaName\":\"DEFAULT\",\"catelogName\":null,\"tableName\":\"SALES_TABLE\"," + "\"precision\":256,\"scale\":0,\"columnType\":12,\"columnTypeName\":\"VARCHAR\"," + "\"writable\":false,\"readOnly\":true,\"definitelyWritable\":false," + "\"autoIncrement\":false,\"caseSensitive\":true,\"searchable\":false," + "\"currency\":false,\"signed\":true},{\"isNullable\":0,\"displaySize\":19," + "\"label\":\"COUNT__\",\"name\":\"COUNT__\",\"schemaName\":\"DEFAULT\"," + "\"catelogName\":null,\"tableName\":\"SALES_TABLE\",\"precision\":19,\"scale\":0," + "\"columnType\":-5,\"columnTypeName\":\"BIGINT\",\"writable\":false," + "\"readOnly\":true,\"definitelyWritable\":false,\"autoIncrement\":false," + "\"caseSensitive\":true,\"searchable\":false,\"currency\":false,\"signed\":true}]," + "\"results\":[[\"AMERICA\",\"USD\",null],[null,\"RMB\",0],[\"KOR\",null,100]," + "[\"\\\"abc\\\"\",\"a,b,c\",-1]],\"cube\":\"Sample_Cube\",\"affectedRowCount\":0," + "\"isException\":false,\"exceptionMessage\":null,\"duration\":134," + "\"totalScanCount\":1,\"hitExceptionCache\":false,\"storageCacheUsed\":false," + "\"partial\":false}"; String expected = "%table COUNTRY \tCURRENCY \tCOUNT__ \t \n" + "AMERICA \tUSD \tnull \t \n" + "null \tRMB \t0 \t \n" + "KOR \tnull \t100 \t \n" + "\\\"abc\\\" \ta,b,c \t-1 \t \n"; KylinInterpreter t = new MockKylinInterpreter(getDefaultProperties()); String actual = t.formatResult(msg); Assert.assertEquals(expected, actual); }
@Test public void testParseEmptyResult() { String msg = "{\"columnMetas\":[{\"isNullable\":1,\"displaySize\":256,\"label\":\"COUNTRY\"," + "\"name\":\"COUNTRY\",\"schemaName\":\"DEFAULT\",\"catelogName\":null," + "\"tableName\":\"SALES_TABLE\",\"precision\":256,\"scale\":0,\"columnType\":12," + "\"columnTypeName\":\"VARCHAR\",\"writable\":false,\"readOnly\":true," + "\"definitelyWritable\":false,\"autoIncrement\":false,\"caseSensitive\":true," + "\"searchable\":false,\"currency\":false,\"signed\":true},{\"isNullable\":1," + "\"displaySize\":256,\"label\":\"CURRENCY\",\"name\":\"CURRENCY\"," + "\"schemaName\":\"DEFAULT\",\"catelogName\":null,\"tableName\":\"SALES_TABLE\"," + "\"precision\":256,\"scale\":0,\"columnType\":12,\"columnTypeName\":\"VARCHAR\"," + "\"writable\":false,\"readOnly\":true,\"definitelyWritable\":false," + "\"autoIncrement\":false,\"caseSensitive\":true,\"searchable\":false," + "\"currency\":false,\"signed\":true},{\"isNullable\":0,\"displaySize\":19," + "\"label\":\"COUNT__\",\"name\":\"COUNT__\",\"schemaName\":\"DEFAULT\"," + "\"catelogName\":null,\"tableName\":\"SALES_TABLE\",\"precision\":19,\"scale\":0," + "\"columnType\":-5,\"columnTypeName\":\"BIGINT\",\"writable\":false," + "\"readOnly\":true,\"definitelyWritable\":false,\"autoIncrement\":false," + "\"caseSensitive\":true,\"searchable\":false,\"currency\":false,\"signed\":true}]," + "\"results\":[]," + "\"cube\":\"Sample_Cube\",\"affectedRowCount\":0," + "\"isException\":false,\"exceptionMessage\":null,\"duration\":134," + "\"totalScanCount\":1,\"hitExceptionCache\":false,\"storageCacheUsed\":false," + "\"partial\":false}"; String expected = "%table COUNTRY \tCURRENCY \tCOUNT__ \t \n"; KylinInterpreter t = new MockKylinInterpreter(getDefaultProperties()); String actual = t.formatResult(msg); Assert.assertEquals(expected, actual); }