@Test public void testMassageSql() { { String sql = "select ( date '2001-09-28' + interval floor(1.2) day) from test_kylin_fact"; String s = QueryUtil.massageSql(sql, "default", 0, 0, "DEFAULT"); Assert.assertEquals("select ( date '2001-09-28' + interval '1' day) from test_kylin_fact", s); } { String sql = "select ( date '2001-09-28' + interval floor(2) month) from test_kylin_fact group by ( date '2001-09-28' + interval floor(2) month)"; String s = QueryUtil.massageSql(sql, "default", 0, 0, "DEFAULT"); Assert.assertEquals( "select ( date '2001-09-28' + interval '2' month) from test_kylin_fact group by ( date '2001-09-28' + interval '2' month)", s); } { String sql = "select count(*) test_limit from test_kylin_fact where price > 10.0"; String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_limit from test_kylin_fact where price > 10.0) limit 50000", s); } { String sql = "select count(*) test_offset from test_kylin_fact where price > 10.0"; String s = QueryUtil.massageSql(sql, "default", 0, 50, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_offset from test_kylin_fact where price > 10.0) offset 50", s); } { String sql = "select count(*) test_limit_and_offset from test_kylin_fact where price > 10.0"; String s = QueryUtil.massageSql(sql, "default", 50000, 50, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_limit_and_offset from test_kylin_fact where price > 10.0) limit 50000 offset 50", s); } { String newLine = System.getProperty("line.separator"); String sql = "select count(*) test_limit from " + newLine + "test_kylin_fact where price > 10.0"; newLine = newLine.replace("\r", " ").replace("\n", newLine); String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_limit from " + newLine + "test_kylin_fact where price > 10.0) limit 50000", s); } { String newLine = System.getProperty("line.separator"); String sql = "select count(*) test_offset from " + newLine + "test_kylin_fact where price > 10.0"; newLine = newLine.replace("\r", " ").replace("\n", newLine); String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_offset from " + newLine + "test_kylin_fact where price > 10.0) limit 50000", s); } { String newLine = System.getProperty("line.separator"); String sql = "select count(*) test_limit_and_offset from " + newLine + "test_kylin_fact where price > 10.0"; newLine = newLine.replace("\r", " ").replace("\n", newLine); String s = QueryUtil.massageSql(sql, "default", 50000, 0, "DEFAULT"); Assert.assertEquals( "select * from (select count(*) test_limit_and_offset from " + newLine + "test_kylin_fact where price > 10.0) limit 50000", s); } }
@Deprecated public static String massageSql(String sql, String project, int limit, int offset, String defaultSchema) { sql = sql.trim(); sql = sql.replace("\r", " ").replace("\n", System.getProperty("line.separator")); ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv()); ProjectInstance projectInstance = projectManager.getProject(project); KylinConfig kylinConfig = projectInstance.getConfig(); sql = removeCommentInSql(sql); while (sql.endsWith(";")) sql = sql.substring(0, sql.length() - 1); String sql1 = sql; final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$"; sql = sql.replaceAll("\\s+", " "); Pattern pattern = Pattern.compile(suffixPattern); Matcher matcher = pattern.matcher(sql.toLowerCase(Locale.ROOT) + " "); int toAppendLimit = 0; int toAppendOffset = 0; if (matcher.find()) { if (limit > 0 && matcher.group(1) == null) { toAppendLimit = limit; } if (offset > 0 && matcher.group(2) == null) { toAppendOffset = offset; } } if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1) == null && sql1.toLowerCase(Locale.ROOT).matches("^select\\s+\\*\\p{all}*")) { toAppendLimit = kylinConfig.getForceLimit(); } sql1 = appendLimitOffsetToSql(sql1, toAppendLimit, toAppendOffset); if (queryTransformers == null) { initQueryTransformers(); } for (IQueryTransformer t : queryTransformers) { sql1 = t.transform(sql1, project, defaultSchema); } return sql1; }
QueryUtil { @Deprecated public static String massageSql(String sql, String project, int limit, int offset, String defaultSchema) { sql = sql.trim(); sql = sql.replace("\r", " ").replace("\n", System.getProperty("line.separator")); ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv()); ProjectInstance projectInstance = projectManager.getProject(project); KylinConfig kylinConfig = projectInstance.getConfig(); sql = removeCommentInSql(sql); while (sql.endsWith(";")) sql = sql.substring(0, sql.length() - 1); String sql1 = sql; final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$"; sql = sql.replaceAll("\\s+", " "); Pattern pattern = Pattern.compile(suffixPattern); Matcher matcher = pattern.matcher(sql.toLowerCase(Locale.ROOT) + " "); int toAppendLimit = 0; int toAppendOffset = 0; if (matcher.find()) { if (limit > 0 && matcher.group(1) == null) { toAppendLimit = limit; } if (offset > 0 && matcher.group(2) == null) { toAppendOffset = offset; } } if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1) == null && sql1.toLowerCase(Locale.ROOT).matches("^select\\s+\\*\\p{all}*")) { toAppendLimit = kylinConfig.getForceLimit(); } sql1 = appendLimitOffsetToSql(sql1, toAppendLimit, toAppendOffset); if (queryTransformers == null) { initQueryTransformers(); } for (IQueryTransformer t : queryTransformers) { sql1 = t.transform(sql1, project, defaultSchema); } return sql1; } }
QueryUtil { @Deprecated public static String massageSql(String sql, String project, int limit, int offset, String defaultSchema) { sql = sql.trim(); sql = sql.replace("\r", " ").replace("\n", System.getProperty("line.separator")); ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv()); ProjectInstance projectInstance = projectManager.getProject(project); KylinConfig kylinConfig = projectInstance.getConfig(); sql = removeCommentInSql(sql); while (sql.endsWith(";")) sql = sql.substring(0, sql.length() - 1); String sql1 = sql; final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$"; sql = sql.replaceAll("\\s+", " "); Pattern pattern = Pattern.compile(suffixPattern); Matcher matcher = pattern.matcher(sql.toLowerCase(Locale.ROOT) + " "); int toAppendLimit = 0; int toAppendOffset = 0; if (matcher.find()) { if (limit > 0 && matcher.group(1) == null) { toAppendLimit = limit; } if (offset > 0 && matcher.group(2) == null) { toAppendOffset = offset; } } if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1) == null && sql1.toLowerCase(Locale.ROOT).matches("^select\\s+\\*\\p{all}*")) { toAppendLimit = kylinConfig.getForceLimit(); } sql1 = appendLimitOffsetToSql(sql1, toAppendLimit, toAppendOffset); if (queryTransformers == null) { initQueryTransformers(); } for (IQueryTransformer t : queryTransformers) { sql1 = t.transform(sql1, project, defaultSchema); } return sql1; } private  QueryUtil(); }
QueryUtil { @Deprecated public static String massageSql(String sql, String project, int limit, int offset, String defaultSchema) { sql = sql.trim(); sql = sql.replace("\r", " ").replace("\n", System.getProperty("line.separator")); ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv()); ProjectInstance projectInstance = projectManager.getProject(project); KylinConfig kylinConfig = projectInstance.getConfig(); sql = removeCommentInSql(sql); while (sql.endsWith(";")) sql = sql.substring(0, sql.length() - 1); String sql1 = sql; final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$"; sql = sql.replaceAll("\\s+", " "); Pattern pattern = Pattern.compile(suffixPattern); Matcher matcher = pattern.matcher(sql.toLowerCase(Locale.ROOT) + " "); int toAppendLimit = 0; int toAppendOffset = 0; if (matcher.find()) { if (limit > 0 && matcher.group(1) == null) { toAppendLimit = limit; } if (offset > 0 && matcher.group(2) == null) { toAppendOffset = offset; } } if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1) == null && sql1.toLowerCase(Locale.ROOT).matches("^select\\s+\\*\\p{all}*")) { toAppendLimit = kylinConfig.getForceLimit(); } sql1 = appendLimitOffsetToSql(sql1, toAppendLimit, toAppendOffset); if (queryTransformers == null) { initQueryTransformers(); } for (IQueryTransformer t : queryTransformers) { sql1 = t.transform(sql1, project, defaultSchema); } return sql1; } private  QueryUtil(); static String appendLimitOffsetToSql(String sql, int limit, int offset); @Deprecated static String massageSql(String sql, String project, int limit, int offset, String defaultSchema); static String massageSql(String sql, String project, int limit, int offset, String defaultSchema,
            String catalog); static String makeErrorMsgUserFriendly(Throwable e); static String makeErrorMsgUserFriendly(String errorMsg); static boolean isSelectStatement(String sql); static String removeCommentInSql(String sql1); }
QueryUtil { @Deprecated public static String massageSql(String sql, String project, int limit, int offset, String defaultSchema) { sql = sql.trim(); sql = sql.replace("\r", " ").replace("\n", System.getProperty("line.separator")); ProjectManager projectManager = ProjectManager.getInstance(KylinConfig.getInstanceFromEnv()); ProjectInstance projectInstance = projectManager.getProject(project); KylinConfig kylinConfig = projectInstance.getConfig(); sql = removeCommentInSql(sql); while (sql.endsWith(";")) sql = sql.substring(0, sql.length() - 1); String sql1 = sql; final String suffixPattern = "^.+?\\s(limit\\s\\d+)?\\s(offset\\s\\d+)?\\s*$"; sql = sql.replaceAll("\\s+", " "); Pattern pattern = Pattern.compile(suffixPattern); Matcher matcher = pattern.matcher(sql.toLowerCase(Locale.ROOT) + " "); int toAppendLimit = 0; int toAppendOffset = 0; if (matcher.find()) { if (limit > 0 && matcher.group(1) == null) { toAppendLimit = limit; } if (offset > 0 && matcher.group(2) == null) { toAppendOffset = offset; } } if (kylinConfig.getForceLimit() > 0 && limit <= 0 && matcher.group(1) == null && sql1.toLowerCase(Locale.ROOT).matches("^select\\s+\\*\\p{all}*")) { toAppendLimit = kylinConfig.getForceLimit(); } sql1 = appendLimitOffsetToSql(sql1, toAppendLimit, toAppendOffset); if (queryTransformers == null) { initQueryTransformers(); } for (IQueryTransformer t : queryTransformers) { sql1 = t.transform(sql1, project, defaultSchema); } return sql1; } private  QueryUtil(); static String appendLimitOffsetToSql(String sql, int limit, int offset); @Deprecated static String massageSql(String sql, String project, int limit, int offset, String defaultSchema); static String massageSql(String sql, String project, int limit, int offset, String defaultSchema,
            String catalog); static String makeErrorMsgUserFriendly(Throwable e); static String makeErrorMsgUserFriendly(String errorMsg); static boolean isSelectStatement(String sql); static String removeCommentInSql(String sql1);  }