Skip to content

Commit 73040b8

Browse files
committed
Mask timefield argument in anonymizer
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 9318f62 commit 73040b8

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.opensearch.sql.calcite.utils.PlanUtils.transformPlanToAttachChild;
1010
import static org.opensearch.sql.utils.QueryStringUtils.MASK_COLUMN;
1111
import static org.opensearch.sql.utils.QueryStringUtils.MASK_LITERAL;
12+
import static org.opensearch.sql.utils.QueryStringUtils.MASK_TIMESTAMP_COLUMN;
1213
import static org.opensearch.sql.utils.QueryStringUtils.maskField;
1314

1415
import com.google.common.base.Strings;
@@ -18,7 +19,9 @@
1819
import java.util.List;
1920
import java.util.Locale;
2021
import java.util.Objects;
22+
import java.util.Optional;
2123
import java.util.stream.Collectors;
24+
import org.apache.commons.lang3.NotImplementedException;
2225
import org.apache.commons.lang3.tuple.ImmutablePair;
2326
import org.apache.commons.lang3.tuple.Pair;
2427
import org.opensearch.sql.ast.AbstractNodeVisitor;
@@ -97,6 +100,7 @@
97100
import org.opensearch.sql.ast.tree.UnresolvedPlan;
98101
import org.opensearch.sql.ast.tree.Values;
99102
import org.opensearch.sql.ast.tree.Window;
103+
import org.opensearch.sql.calcite.plan.OpenSearchConstants;
100104
import org.opensearch.sql.common.setting.Settings;
101105
import org.opensearch.sql.common.utils.StringUtils;
102106
import org.opensearch.sql.planner.logical.LogicalAggregation;
@@ -512,10 +516,14 @@ public String visitChart(Chart node, String context) {
512516
if ("top".equals(argName)) {
513517
continue;
514518
}
515-
if ("limit".equals(argName) || "useother".equals(argName) || "usenull".equals(argName)) {
516-
chartCommand.append(" ").append(argName).append("=").append(MASK_LITERAL);
517-
} else if ("otherstr".equals(argName) || "nullstr".equals(argName)) {
518-
chartCommand.append(" ").append(argName).append("=").append(MASK_LITERAL);
519+
chartCommand.append(" ").append(argName).append("=");
520+
switch (argName) {
521+
case "limit", "useother", "usenull", "otherstr", "nullstr" ->
522+
chartCommand.append(MASK_LITERAL);
523+
case "timefield" -> chartCommand.append(MASK_TIMESTAMP_COLUMN);
524+
default ->
525+
throw new NotImplementedException(
526+
StringUtils.format("Please implement anonymizer for arg: %s", argName));
519527
}
520528
}
521529

@@ -542,8 +550,12 @@ private boolean isTimechartNode(Chart node) {
542550
Alias alias = (Alias) node.getRowSplit();
543551
if (alias.getDelegated() instanceof Span) {
544552
Span span = (Span) alias.getDelegated();
553+
String timeFieldName =
554+
Optional.ofNullable(ArgumentMap.of(node.getArguments()).get("timefield"))
555+
.map(Literal::toString)
556+
.orElse(OpenSearchConstants.IMPLICIT_FIELD_TIMESTAMP);
545557
return span.getField() instanceof Field
546-
&& "@timestamp".equals(((Field) span.getField()).getField().toString());
558+
&& timeFieldName.equals(((Field) span.getField()).getField().toString());
547559
}
548560
}
549561
return false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ public void testTimechartCommand() {
259259
anonymize("source=t | timechart count() by host"));
260260

261261
assertEquals(
262-
"source=table | timechart max(identifier)",
263-
anonymize("source=t | timechart max(revenue)"));
262+
"source=table | timechart timefield=time_identifier max(identifier)",
263+
anonymize("source=t | timechart timefield=month max(revenue)"));
264264
}
265265

266266
@Test

0 commit comments

Comments
 (0)