Skip to content

Commit d7949ef

Browse files
committed
Add anonymize test to chart command
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent 1fe81b3 commit d7949ef

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.opensearch.sql.ast.tree.Append;
5757
import org.opensearch.sql.ast.tree.AppendCol;
5858
import org.opensearch.sql.ast.tree.Bin;
59+
import org.opensearch.sql.ast.tree.Chart;
5960
import org.opensearch.sql.ast.tree.CountBin;
6061
import org.opensearch.sql.ast.tree.Dedupe;
6162
import org.opensearch.sql.ast.tree.DefaultBin;
@@ -520,6 +521,42 @@ public String visitTimechart(Timechart node, String context) {
520521
return StringUtils.format("%s%s", child, timechartCommand.toString());
521522
}
522523

524+
@Override
525+
public String visitChart(Chart node, String context) {
526+
String child = node.getChild().get(0).accept(this, context);
527+
StringBuilder chartCommand = new StringBuilder();
528+
chartCommand.append(" | chart");
529+
530+
for (Argument arg : node.getArguments()) {
531+
String argName = arg.getArgName();
532+
// Skip the auto-generated "top" parameter that's added when limit is specified
533+
if ("top".equals(argName)) {
534+
continue;
535+
}
536+
if ("limit".equals(argName) || "useother".equals(argName) || "usenull".equals(argName)) {
537+
chartCommand.append(" ").append(argName).append("=").append(MASK_LITERAL);
538+
} else if ("otherstr".equals(argName) || "nullstr".equals(argName)) {
539+
chartCommand.append(" ").append(argName).append("=").append(MASK_LITERAL);
540+
}
541+
}
542+
543+
chartCommand.append(" ").append(visitExpression(node.getAggregationFunction()));
544+
545+
if (node.getRowSplit() != null && node.getColumnSplit() != null) {
546+
chartCommand
547+
.append(" by ")
548+
.append(visitExpression(node.getRowSplit()))
549+
.append(" ")
550+
.append(visitExpression(node.getColumnSplit()));
551+
} else if (node.getRowSplit() != null) {
552+
chartCommand.append(" by ").append(visitExpression(node.getRowSplit()));
553+
} else if (node.getColumnSplit() != null) {
554+
chartCommand.append(" by ").append(visitExpression(node.getColumnSplit()));
555+
}
556+
557+
return StringUtils.format("%s%s", child, chartCommand.toString());
558+
}
559+
523560
public String visitRex(Rex node, String context) {
524561
String child = node.getChild().get(0).accept(this, context);
525562
String field = visitExpression(node.getField());

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,34 @@ public void testTimechartCommand() {
232232
anonymize("source=t | timechart count() by host"));
233233
}
234234

235+
@Test
236+
public void testChartCommand() {
237+
assertEquals(
238+
"source=table | chart count(identifier) by identifier identifier",
239+
anonymize("source=t | chart count(age) by gender country"));
240+
}
241+
242+
@Test
243+
public void testChartCommandWithParameters() {
244+
assertEquals(
245+
"source=table | chart limit=*** useother=*** avg(identifier) by identifier",
246+
anonymize("source=t | chart limit=5 useother=false avg(balance) by state"));
247+
}
248+
249+
@Test
250+
public void testChartCommandOver() {
251+
assertEquals(
252+
"source=table | chart avg(identifier) by identifier",
253+
anonymize("source=t | chart avg(balance) over gender"));
254+
}
255+
256+
@Test
257+
public void testChartCommandOverBy() {
258+
assertEquals(
259+
"source=table | chart sum(identifier) by identifier identifier",
260+
anonymize("source=t | chart sum(amount) over gender by age"));
261+
}
262+
235263
// todo, sort order is ignored, it doesn't impact the log analysis.
236264
@Test
237265
public void testSortCommandWithOptions() {

0 commit comments

Comments
 (0)