diff --git a/docs/user/ppl/cmd/describe.rst b/docs/user/ppl/cmd/describe.rst index 0abd569684..1a2fc82285 100644 --- a/docs/user/ppl/cmd/describe.rst +++ b/docs/user/ppl/cmd/describe.rst @@ -16,9 +16,12 @@ Description Syntax ============ -describe +describe . * index: mandatory. describe command must specify which index to query from. +* catalog: optional. If catalog is not provided, it defaults to opensearch catalog. + + Example 1: Fetch all the metadata @@ -63,3 +66,24 @@ PPL query:: | age | +----------------+ + +Example 3: Fetch metadata for table in prometheus catalog +========================================================= + +The example retrieves table info for ``prometheus_http_requests_total`` metric in prometheus catalog. + +PPL query:: + + os> describe my_prometheus.prometheus_http_requests_total; + fetched rows / total rows = 7/7 + +-----------------+----------------+--------------------------------+---------------+-------------+ + | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | DATA_TYPE | + |-----------------+----------------+--------------------------------+---------------+-------------| + | my_prometheus | default | prometheus_http_requests_total | @labels | keyword | + | my_prometheus | default | prometheus_http_requests_total | handler | keyword | + | my_prometheus | default | prometheus_http_requests_total | code | keyword | + | my_prometheus | default | prometheus_http_requests_total | instance | keyword | + | my_prometheus | default | prometheus_http_requests_total | @timestamp | timestamp | + | my_prometheus | default | prometheus_http_requests_total | @value | double | + | my_prometheus | default | prometheus_http_requests_total | job | keyword | + +-----------------+----------------+--------------------------------+---------------+-------------+ diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/DescribeCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/DescribeCommandIT.java index c06ef3bc21..88ef028fbe 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/DescribeCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/DescribeCommandIT.java @@ -15,6 +15,7 @@ import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOG; import static org.opensearch.sql.util.MatcherUtils.columnName; +import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.verifyColumn; import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; @@ -87,4 +88,26 @@ public void describeCommandWithoutIndexShouldFailToParse() throws IOException { assertTrue(e.getMessage().contains("Failed to parse query due to offending symbol")); } } + + @Test + public void testDescribeCommandWithPrometheusCatalog() throws IOException { + JSONObject result = executeQuery("describe my_prometheus.prometheus_http_requests_total"); + verifyColumn( + result, + columnName("TABLE_CATALOG"), + columnName("TABLE_SCHEMA"), + columnName("TABLE_NAME"), + columnName("COLUMN_NAME"), + columnName("DATA_TYPE") + ); + verifyDataRows(result, + rows("my_prometheus", "default", "prometheus_http_requests_total", "@labels", "keyword"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "handler", "keyword"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "code", "keyword"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "instance", "keyword"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "@value", "double"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "@timestamp", + "timestamp"), + rows("my_prometheus", "default", "prometheus_http_requests_total", "job", "keyword")); + } } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java index b0d17940a4..4bd70c453e 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java @@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -44,6 +45,7 @@ import org.opensearch.sql.ast.expression.Literal; import org.opensearch.sql.ast.expression.Map; import org.opensearch.sql.ast.expression.ParseMethod; +import org.opensearch.sql.ast.expression.QualifiedName; import org.opensearch.sql.ast.expression.UnresolvedArgument; import org.opensearch.sql.ast.expression.UnresolvedExpression; import org.opensearch.sql.ast.tree.AD; @@ -121,7 +123,10 @@ public UnresolvedPlan visitSearchFilterFrom(SearchFilterFromContext ctx) { @Override public UnresolvedPlan visitDescribeCommand(DescribeCommandContext ctx) { final Relation table = (Relation) visitTableSourceClause(ctx.tableSourceClause()); - return new Relation(qualifiedName(mappingTable(table.getTableName()))); + QualifiedName tableQualifiedName = table.getTableQualifiedName(); + ArrayList parts = new ArrayList<>(tableQualifiedName.getParts()); + parts.set(parts.size() - 1, mappingTable(parts.get(parts.size() - 1))); + return new Relation(new QualifiedName(parts)); } /** diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java index cd9aed4d18..a39cc38dd2 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java @@ -14,7 +14,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -82,7 +81,9 @@ public Map> getAllMetrics() throws IOException { private List toListOfStrings(JSONArray array) { List result = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { - result.add(array.optString(i)); + if (!"__name__".equals(array.optString(i))) { + result.add(array.optString(i)); + } } return result; } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java index 5f4715fca7..f49672a882 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java @@ -106,7 +106,6 @@ void testGetLabel() { mockWebServer.enqueue(mockResponse); List response = prometheusClient.getLabels(METRIC_NAME); assertEquals(new ArrayList() {{ - add("__name__"); add("call"); add("code"); }