Skip to content

Commit

Permalink
Describe Table with catalog name.
Browse files Browse the repository at this point in the history
Signed-off-by: Vamsi Manohar <[email protected]>
  • Loading branch information
vmmusings committed Oct 29, 2022
1 parent e3fe086 commit 4f34e0d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
26 changes: 25 additions & 1 deletion docs/user/ppl/cmd/describe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ Description

Syntax
============
describe <index>
describe <catalog>.<index>

* 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
Expand Down Expand Up @@ -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 |
+-----------------+----------------+--------------------------------+---------------+-------------+
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> parts = new ArrayList<>(tableQualifiedName.getParts());
parts.set(parts.size() - 1, mappingTable(parts.get(parts.size() - 1)));
return new Relation(new QualifiedName(parts));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +81,9 @@ public Map<String, List<MetricMetadata>> getAllMetrics() throws IOException {
private List<String> toListOfStrings(JSONArray array) {
List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ void testGetLabel() {
mockWebServer.enqueue(mockResponse);
List<String> response = prometheusClient.getLabels(METRIC_NAME);
assertEquals(new ArrayList<String>() {{
add("__name__");
add("call");
add("code");
}
Expand Down

0 comments on commit 4f34e0d

Please sign in to comment.