Skip to content

Commit

Permalink
feat: sql (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangwanpeng authored Aug 3, 2023
1 parent 0d17138 commit f928ead
Show file tree
Hide file tree
Showing 6 changed files with 465 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class QueryDataSource {
// whether the APM metrics is obtained by materializing them to the metric storage in advance
// instead of post-calculation
public boolean apmMaterialized;
public String ql;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.ceresdb.common.parser.SqlParser;
import io.ceresdb.common.parser.SqlParserFactoryProvider;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -104,16 +106,28 @@ public Mono<Void> write(WriteMetricsParam writeMetricsParam) {

@Override
public List<Result> queryData(QueryParam queryParam) {
String whereStatement = parseWhere(queryParam);
String fromStatement = parseFrom(queryParam);
if (StringUtils.isBlank(fromStatement)) {
LOGGER.warn("fromStatement is empty, queryParam:{}", queryParam);
return Lists.newArrayList();
String sql = queryParam.getQl();
String[] tableNames;
if (StringUtils.isNotBlank(sql)) {
SqlParser parser = SqlParserFactoryProvider.getSqlParserFactory().getParser(sql);
List<String> tables = parser.tableNames();
tableNames = tables.stream().map(this::fixName).collect(Collectors.toList())
.toArray(new String[tables.size()]);
sql = StringUtils.replaceEach(sql, tables.toArray(new String[tables.size()]), tableNames);
} else {
String whereStatement = parseWhere(queryParam);
String fromStatement = parseFrom(queryParam);
if (StringUtils.isBlank(fromStatement)) {
LOGGER.warn("fromStatement is empty, queryParam:{}", queryParam);
return Lists.newArrayList();
}
tableNames = new String[] {fixName(queryParam.getMetric())};
sql = genSqlWithGroupBy(queryParam, fromStatement, whereStatement);

}
String sql = genSqlWithGroupBy(queryParam, fromStatement, whereStatement);
LOGGER.info("queryData queryparam:{}, sql:{}", queryParam, sql);
final SqlQueryRequest queryRequest =
SqlQueryRequest.newBuilder().forTables(fixName(queryParam.getMetric())).sql(sql).build();
SqlQueryRequest.newBuilder().forTables(tableNames).sql(sql).build();
long begin = System.currentTimeMillis();
try {
CompletableFuture<io.ceresdb.models.Result<SqlQueryOk, Err>> qf = Context.ROOT.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class QueryParam {
String downsample;
SlidingWindow slidingWindow;
List<String> groupBy;
String ql;

@Data
public static class SlidingWindow {
Expand Down
Loading

0 comments on commit f928ead

Please sign in to comment.