diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java index baa235aa89..c749c12758 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java @@ -37,7 +37,9 @@ public static String build(String metricName, Expression filterCondition) { SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor = new SeriesSelectionExpressionNodeVisitor(); String selectorQuery = filterCondition.accept(seriesSelectionExpressionNodeVisitor, null); - return metricName + "{" + selectorQuery + "}"; + if (selectorQuery != null) { + return metricName + "{" + selectorQuery + "}"; + } } return metricName; } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java index 7e2de95604..7af75ad661 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java @@ -303,7 +303,7 @@ void testTimeRangeResolverWithOutEndTimeInFilter() { new PrometheusMetricTable(client, "prometheus_http_total_requests"); - //Both endTime and startTime are set. + //Only endTime is set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); @@ -724,6 +724,35 @@ void testImplementWithRelationAndFilter() { assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } + @Test + void testImplementWithRelationAndTimestampFilter() { + List finalProjectList = new ArrayList<>(); + finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); + finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Long endTime = new Date(System.currentTimeMillis()).getTime(); + PrometheusMetricTable prometheusMetricTable = + new PrometheusMetricTable(client, "prometheus_http_total_requests"); + LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", + dsl.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue(dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP))) + ), finalProjectList, null); + PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); + assertTrue(physicalPlan instanceof ProjectOperator); + assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); + PrometheusQueryRequest request + = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + assertEquals((3600 / 250) + "s", request.getStep()); + assertEquals("prometheus_http_total_requests", + request.getPromQl()); + List projectList = ((ProjectOperator) physicalPlan).getProjectList(); + List outputFields + = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + assertEquals(List.of(VALUE, TIMESTAMP), outputFields); + } + @Test void testOptimize() { PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest();