Skip to content

Commit

Permalink
Allow metadata fields in PPL query
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Jin <ltjin@amazon.com>
  • Loading branch information
LantaoJin committed Jul 9, 2024
1 parent f241f34 commit 9c8ba3d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ public Expression visitWhen(When node, AnalysisContext context) {

@Override
public Expression visitField(Field node, AnalysisContext context) {
String attr = node.getField().toString();
return visitIdentifier(attr, context);
return visitQualifiedName((QualifiedName) node.getField(), context);
}

@Override
Expand Down
25 changes: 25 additions & 0 deletions docs/user/ppl/general/identifiers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,28 @@ Query delimited multiple indices seperated by ``,``::
| 5 |
+-----------+

Metadata Identifiers
====================

Description
-----------

One can also provide meta-field name(s) to retrieve reserved-fields (beginning with underscore) from OpenSearch documents. Meta-fields are not output
as default field list (`search source=<index>`) and must be explicitly included to be returned.

Examples
---------

Query metadata fields::

os> source=accounts | fields firstname, lastname, _index, _sort;
fetched rows / total rows = 4/4
+-------------+------------+----------+---------+
| firstname | lastname | _index | _sort |
|-------------+------------+----------+---------|
| Amber | Duke | accounts | -2 |
| Hattie | Bond | accounts | -2 |
| Nanette | Bates | accounts | -2 |
| Dale | Adams | accounts | -2 |
+-------------+------------+----------+---------+

Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,19 @@ public void testSelectDateTypeField() throws IOException {
rows("2018-08-19 00:00:00"),
rows("2018-08-11 00:00:00"));
}

@Test
public void testMetadataFields() throws IOException {
JSONObject result =
executeQuery(String.format("source=%s | fields firstname, _index", TEST_INDEX_ACCOUNT));
verifyColumn(result, columnName("firstname"), columnName("_index"));
}

@Test
public void testDelimitedMetadataFields() throws IOException {
JSONObject result =
executeQuery(
String.format("source=%s | fields firstname, `_id`, `_index`", TEST_INDEX_ACCOUNT));
verifyColumn(result, columnName("firstname"), columnName("_id"), columnName("_index"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ public void testIsNotNullFunction() throws IOException {
TEST_INDEX_BANK_WITH_NULL_VALUES));
verifyDataRows(result, rows("Amber JOHnny"));
}

@Test
public void testWhereWithMetadataFields() throws IOException {
JSONObject result =
executeQuery(
String.format("source=%s | where _id='1' | fields firstname", TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"));
}
}
5 changes: 4 additions & 1 deletion ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,16 @@ INTEGER_LITERAL: DEC_DIGIT+;
DECIMAL_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+;

fragment DATE_SUFFIX: ([\-.][*0-9]+)+;
fragment ID_LITERAL: [@*A-Z]+?[*A-Z_\-0-9]*;
fragment CLUSTER_PREFIX_LITERAL: [*A-Z]+?[*A-Z_\-0-9]* COLON;
ID_DATE_SUFFIX: CLUSTER_PREFIX_LITERAL? ID_LITERAL DATE_SUFFIX;
DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';
SQUOTA_STRING: '\'' ('\\'. | '\'\'' | ~('\'' | '\\'))* '\'';
BQUOTA_STRING: '`' ( '\\'. | '``' | ~('`'|'\\'))* '`';
fragment DEC_DIGIT: [0-9];

// Identifiers cannot start with a single '_' since this an OpenSearch reserved
// metadata field. Two underscores (or more) is acceptable, such as '__field'.
fragment ID_LITERAL: ([@*A-Z_])+?[*A-Z_\-0-9]*;


ERROR_RECOGNITION: . -> channel(ERRORCHANNEL);
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,32 @@ public void canBuildKeywordsAsIdentInQualifiedName() {
projectWithArg(relation("test"), defaultFieldsArgs(), field("timestamp")));
}

@Test
public void canBuildMetaDataFieldAsQualifiedName() {
assertEqual(
"source=test | fields _id, _index, _sort, _maxscore",
projectWithArg(
relation("test"),
defaultFieldsArgs(),
field("_id"),
field("_index"),
field("_sort"),
field("_maxscore")));
}

@Test
public void canBuildNonMetaDataFieldAsQualifiedName() {
assertEqual(
"source=test | fields id, __id, _routing, ___field",
projectWithArg(
relation("test"),
defaultFieldsArgs(),
field("id"),
field("__id"),
field("_routing"),
field("___field")));
}

@Test
public void canBuildMatchRelevanceFunctionWithArguments() {
assertEqual(
Expand Down

0 comments on commit 9c8ba3d

Please sign in to comment.