Skip to content

Commit f56c04d

Browse files
committed
add test cases for metadata and timestamp identifier
Signed-off-by: xinyual <xinyual@amazon.com>
1 parent 44d7591 commit f56c04d

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

core/src/main/java/org/opensearch/sql/ast/expression/SearchComparison.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package org.opensearch.sql.ast.expression;
77

8+
import static org.opensearch.sql.utils.QueryStringUtils.maskField;
9+
810
import java.util.Arrays;
911
import java.util.List;
1012
import lombok.EqualsAndHashCode;
@@ -65,7 +67,8 @@ public String toQueryString() {
6567

6668
@Override
6769
public String toAnonymizedString() {
68-
return "identifier " + operator.symbol + " ***";
70+
String fieldName = QueryStringUtils.escapeFieldName(field.getField().toString());
71+
return maskField(fieldName) + " " + operator.symbol + " ***";
6972
}
7073

7174
@Override

core/src/main/java/org/opensearch/sql/utils/QueryStringUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88
/** Utility class for query_string syntax operations. */
99
public class QueryStringUtils {
1010

11+
private static final String INTERNAL_TIMESTAMP = "@timestamp";
12+
13+
public static final String MASK_LITERAL = "***";
14+
15+
public static final String MASK_COLUMN = "identifier";
16+
17+
public static final String MASK_TIMESTAMP_COLUMN = "timestamp_identifier";
18+
19+
public static final String MASK_METADATA_COLUMN = "metadata_identifier";
20+
21+
public static String maskField(String fieldName) {
22+
if (fieldName.equals(INTERNAL_TIMESTAMP)) {
23+
return MASK_TIMESTAMP_COLUMN;
24+
}
25+
if (fieldName.startsWith("_")) {
26+
return MASK_METADATA_COLUMN;
27+
}
28+
return MASK_COLUMN;
29+
}
30+
1131
// For field names, we typically don't escape dots as they're used for nested fields
1232
// But we escape other special characters
1333
public static final String LUCENE_SPECIAL_CHARS = "+-&|!(){}[]^\"~:/";

ppl/src/main/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import static org.opensearch.sql.calcite.utils.PlanUtils.getRelation;
99
import static org.opensearch.sql.calcite.utils.PlanUtils.transformPlanToAttachChild;
10+
import static org.opensearch.sql.utils.QueryStringUtils.MASK_COLUMN;
11+
import static org.opensearch.sql.utils.QueryStringUtils.MASK_LITERAL;
12+
import static org.opensearch.sql.utils.QueryStringUtils.maskField;
1013

1114
import com.google.common.base.Strings;
1215
import com.google.common.collect.ImmutableList;
@@ -108,10 +111,6 @@
108111
/** Utility class to mask sensitive information in incoming PPL queries. */
109112
public class PPLQueryDataAnonymizer extends AbstractNodeVisitor<String, String> {
110113

111-
public static final String MASK_LITERAL = "***";
112-
113-
public static final String MASK_COLUMN = "identifier";
114-
115114
public static final String MASK_TABLE = "table";
116115

117116
private final AnonymizerExpressionAnalyzer expressionAnalyzer;
@@ -916,7 +915,8 @@ public String visitIn(In node, String context) {
916915

917916
@Override
918917
public String visitField(Field node, String context) {
919-
return MASK_COLUMN;
918+
String fieldName = node.getField().toString();
919+
return maskField(fieldName);
920920
}
921921

922922
@Override

ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public void testReverseCommand() {
255255
@Test
256256
public void testTimechartCommand() {
257257
assertEquals(
258-
"source=table | timechart limit=*** useother=*** count() by span(identifier, *** m)"
259-
+ " identifier",
258+
"source=table | timechart limit=*** useother=*** count() by span(timestamp_identifier, ***"
259+
+ " m) identifier",
260260
anonymize("source=t | timechart count() by host"));
261261
}
262262

@@ -388,6 +388,13 @@ public void testAndExpression() {
388388
anonymize("source=t | where a=1 and b=2"));
389389
}
390390

391+
@Test
392+
public void testAndExpressionWithMetaData() {
393+
assertEquals(
394+
"source=table | where metadata_identifier = *** and identifier = ***",
395+
anonymize("source=t | where _id=1 and b=2"));
396+
}
397+
391398
@Test
392399
public void testOrExpression() {
393400
assertEquals(
@@ -879,7 +886,7 @@ private String anonymizeStatement(String query, boolean isExplain) {
879886
@Test
880887
public void testSearchWithAbsoluteTimeRange() {
881888
assertEquals(
882-
"source=table (identifier >= ***) AND (identifier <= ***)",
889+
"source=table (timestamp_identifier >= ***) AND (timestamp_identifier <= ***)",
883890
anonymize("search source=t earliest='2012-12-10 15:00:00' latest=now"));
884891
}
885892

@@ -906,7 +913,7 @@ public void testSearchWithGroup() {
906913
@Test
907914
public void testSearchWithOr() {
908915
assertEquals(
909-
"source=table (identifier >= *** OR identifier <= ***)",
916+
"source=table (timestamp_identifier >= *** OR timestamp_identifier <= ***)",
910917
anonymize("search source=t earliest='2012-12-10 15:00:00' or latest=now"));
911918
}
912919

0 commit comments

Comments
 (0)