Skip to content

Commit ee949cc

Browse files
authored
Fixed error with single timestamp query (opensearch-project#1244)
Signed-off-by: vamsi-amazon <reddyvam@amazon.com> Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
1 parent aae57a0 commit ee949cc

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public static String build(String metricName, Expression filterCondition) {
3737
SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor
3838
= new SeriesSelectionExpressionNodeVisitor();
3939
String selectorQuery = filterCondition.accept(seriesSelectionExpressionNodeVisitor, null);
40-
return metricName + "{" + selectorQuery + "}";
40+
if (selectorQuery != null) {
41+
return metricName + "{" + selectorQuery + "}";
42+
}
4143
}
4244
return metricName;
4345
}

prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void testTimeRangeResolverWithOutEndTimeInFilter() {
303303
new PrometheusMetricTable(client, "prometheus_http_total_requests");
304304

305305

306-
//Both endTime and startTime are set.
306+
//Only endTime is set.
307307
List<NamedExpression> finalProjectList = new ArrayList<>();
308308
finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING)));
309309
finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP)));
@@ -724,6 +724,35 @@ void testImplementWithRelationAndFilter() {
724724
assertEquals(List.of(VALUE, TIMESTAMP), outputFields);
725725
}
726726

727+
@Test
728+
void testImplementWithRelationAndTimestampFilter() {
729+
List<NamedExpression> finalProjectList = new ArrayList<>();
730+
finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING)));
731+
finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP)));
732+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
733+
Long endTime = new Date(System.currentTimeMillis()).getTime();
734+
PrometheusMetricTable prometheusMetricTable =
735+
new PrometheusMetricTable(client, "prometheus_http_total_requests");
736+
LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests",
737+
DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP),
738+
DSL.literal(
739+
fromObjectValue(dateFormat.format(new Date(endTime)),
740+
ExprCoreType.TIMESTAMP)))
741+
), finalProjectList, null);
742+
PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan);
743+
assertTrue(physicalPlan instanceof ProjectOperator);
744+
assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan);
745+
PrometheusQueryRequest request
746+
= ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest();
747+
assertEquals((3600 / 250) + "s", request.getStep());
748+
assertEquals("prometheus_http_total_requests",
749+
request.getPromQl());
750+
List<NamedExpression> projectList = ((ProjectOperator) physicalPlan).getProjectList();
751+
List<String> outputFields
752+
= projectList.stream().map(NamedExpression::getName).collect(Collectors.toList());
753+
assertEquals(List.of(VALUE, TIMESTAMP), outputFields);
754+
}
755+
727756
@Test
728757
void testOptimize() {
729758
PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest();

0 commit comments

Comments
 (0)