Skip to content

Commit f77ef7a

Browse files
committed
Unit test search with absolute time range
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 8fc6176 commit f77ef7a

4 files changed

Lines changed: 127 additions & 55 deletions

File tree

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLAbstractTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import static org.mockito.Mockito.mock;
1313
import static org.opensearch.sql.executor.QueryType.PPL;
1414

15+
import com.google.common.collect.ImmutableList;
1516
import java.io.PrintWriter;
1617
import java.io.StringWriter;
18+
import java.sql.Date;
1719
import java.sql.PreparedStatement;
1820
import java.sql.SQLException;
1921
import java.util.List;
@@ -176,4 +178,60 @@ public void verifyErrorMessageContains(Throwable t, String msg) {
176178
String stackTrace = getStackTrace(t);
177179
assertThat(String.format("Actual stack trace was:\n%s", stackTrace), stackTrace.contains(msg));
178180
}
181+
182+
/**
183+
* Add a test table with @timestamp and created_at fields with name {@code LOGS}
184+
*
185+
* <p>Note: @timestamp and created_at have different orderings to test explicit field usage
186+
*/
187+
protected Frameworks.ConfigBuilder configureTimestampLogSchema(
188+
CalciteAssert.SchemaSpec... schemaSpecs) {
189+
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
190+
final SchemaPlus schema = CalciteAssert.addSchema(rootSchema, schemaSpecs);
191+
192+
ImmutableList<Object[]> rows =
193+
ImmutableList.of(
194+
new Object[] {
195+
"server1",
196+
"ERROR",
197+
"Database connection failed",
198+
Date.valueOf("2023-01-01"),
199+
Date.valueOf("2023-01-05")
200+
},
201+
new Object[] {
202+
"server2",
203+
"INFO",
204+
"Service started",
205+
Date.valueOf("2023-01-02"),
206+
Date.valueOf("2023-01-04")
207+
},
208+
new Object[] {
209+
"server1",
210+
"WARN",
211+
"High memory usage",
212+
Date.valueOf("2023-01-03"),
213+
Date.valueOf("2023-01-03")
214+
},
215+
new Object[] {
216+
"server3",
217+
"ERROR",
218+
"Disk space low",
219+
Date.valueOf("2023-01-04"),
220+
Date.valueOf("2023-01-02")
221+
},
222+
new Object[] {
223+
"server2",
224+
"INFO",
225+
"Backup completed",
226+
Date.valueOf("2023-01-05"),
227+
Date.valueOf("2023-01-01")
228+
});
229+
schema.add("LOGS", new CalcitePPLEarliestLatestTest.LogsTable(rows));
230+
231+
return Frameworks.newConfigBuilder()
232+
.parserConfig(SqlParser.Config.DEFAULT)
233+
.defaultSchema(schema)
234+
.traitDefs((List<RelTraitDef>) null)
235+
.programs(Programs.heuristicJoinOrder(Programs.RULE_SET, true, 2));
236+
}
179237
}

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLEarliestLatestTest.java

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,25 @@
66
package org.opensearch.sql.ppl.calcite;
77

88
import com.google.common.collect.ImmutableList;
9-
import java.sql.Date;
10-
import java.util.List;
119
import lombok.RequiredArgsConstructor;
1210
import org.apache.calcite.DataContext;
1311
import org.apache.calcite.config.CalciteConnectionConfig;
1412
import org.apache.calcite.linq4j.Enumerable;
1513
import org.apache.calcite.linq4j.Linq4j;
16-
import org.apache.calcite.plan.RelTraitDef;
1714
import org.apache.calcite.rel.RelCollations;
1815
import org.apache.calcite.rel.RelNode;
1916
import org.apache.calcite.rel.type.RelDataType;
2017
import org.apache.calcite.rel.type.RelDataTypeFactory;
2118
import org.apache.calcite.rel.type.RelProtoDataType;
2219
import org.apache.calcite.schema.ScannableTable;
2320
import org.apache.calcite.schema.Schema;
24-
import org.apache.calcite.schema.SchemaPlus;
2521
import org.apache.calcite.schema.Statistic;
2622
import org.apache.calcite.schema.Statistics;
2723
import org.apache.calcite.sql.SqlCall;
2824
import org.apache.calcite.sql.SqlNode;
29-
import org.apache.calcite.sql.parser.SqlParser;
3025
import org.apache.calcite.sql.type.SqlTypeName;
3126
import org.apache.calcite.test.CalciteAssert;
3227
import org.apache.calcite.tools.Frameworks;
33-
import org.apache.calcite.tools.Programs;
3428
import org.checkerframework.checker.nullness.qual.Nullable;
3529
import org.junit.Test;
3630

@@ -42,55 +36,7 @@ public CalcitePPLEarliestLatestTest() {
4236

4337
@Override
4438
protected Frameworks.ConfigBuilder config(CalciteAssert.SchemaSpec... schemaSpecs) {
45-
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
46-
final SchemaPlus schema = CalciteAssert.addSchema(rootSchema, schemaSpecs);
47-
48-
// Add a test table with @timestamp and created_at fields
49-
// Note: @timestamp and created_at have different orderings to test explicit field usage
50-
ImmutableList<Object[]> rows =
51-
ImmutableList.of(
52-
new Object[] {
53-
"server1",
54-
"ERROR",
55-
"Database connection failed",
56-
Date.valueOf("2023-01-01"),
57-
Date.valueOf("2023-01-05")
58-
},
59-
new Object[] {
60-
"server2",
61-
"INFO",
62-
"Service started",
63-
Date.valueOf("2023-01-02"),
64-
Date.valueOf("2023-01-04")
65-
},
66-
new Object[] {
67-
"server1",
68-
"WARN",
69-
"High memory usage",
70-
Date.valueOf("2023-01-03"),
71-
Date.valueOf("2023-01-03")
72-
},
73-
new Object[] {
74-
"server3",
75-
"ERROR",
76-
"Disk space low",
77-
Date.valueOf("2023-01-04"),
78-
Date.valueOf("2023-01-02")
79-
},
80-
new Object[] {
81-
"server2",
82-
"INFO",
83-
"Backup completed",
84-
Date.valueOf("2023-01-05"),
85-
Date.valueOf("2023-01-01")
86-
});
87-
schema.add("LOGS", new LogsTable(rows));
88-
89-
return Frameworks.newConfigBuilder()
90-
.parserConfig(SqlParser.Config.DEFAULT)
91-
.defaultSchema(schema)
92-
.traitDefs((List<RelTraitDef>) null)
93-
.programs(Programs.heuristicJoinOrder(Programs.RULE_SET, true, 2));
39+
return configureTimestampLogSchema(schemaSpecs);
9440
}
9541

9642
@Test
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.ppl.calcite;
7+
8+
import static org.junit.Assert.assertThrows;
9+
10+
import org.apache.calcite.rel.RelNode;
11+
import org.apache.calcite.test.CalciteAssert;
12+
import org.apache.calcite.tools.Frameworks;
13+
import org.junit.Test;
14+
15+
public class CalcitePPLSearchTest extends CalcitePPLAbstractTest {
16+
public CalcitePPLSearchTest() {
17+
super(CalciteAssert.SchemaSpec.SCOTT_WITH_TEMPORAL);
18+
}
19+
20+
@Override
21+
protected Frameworks.ConfigBuilder config(CalciteAssert.SchemaSpec... schemaSpecs) {
22+
return configureTimestampLogSchema(schemaSpecs);
23+
}
24+
25+
@Test
26+
public void testSearchWithFilter() {
27+
String ppl = "search source=EMP DEPTNO=20";
28+
RelNode root = getRelNode(ppl);
29+
String expectedLogical =
30+
"LogicalFilter(condition=[=($7, 20)])\n" + " LogicalTableScan(table=[[scott, EMP]])\n";
31+
verifyLogical(root, expectedLogical);
32+
33+
String expectedSparkSql = "SELECT *\n" + "FROM `scott`.`EMP`\n" + "WHERE `DEPTNO` = 20";
34+
verifyPPLToSparkSQL(root, expectedSparkSql);
35+
}
36+
37+
@Test
38+
public void testSearchWithoutTimestampShouldThrow() {
39+
String ppl = "source=EMP earliest='2020-10-11'";
40+
Throwable t = assertThrows(IllegalArgumentException.class, () -> getRelNode(ppl));
41+
verifyErrorMessageContains(t, "field [@timestamp] not found");
42+
}
43+
44+
@Test
45+
public void testSearchWithAbsoluteTimeRange() {
46+
String ppl = "source=LOGS earliest='2020-10-11' latest='2025-01-01'";
47+
RelNode root = getRelNode(ppl);
48+
// @timestamp is a field of type DATE here
49+
String expectedLogical =
50+
"LogicalFilter(condition=[AND(>=($3, DATE('2020-10-11':VARCHAR)), <=($3,"
51+
+ " DATE('2025-01-01':VARCHAR)))])\n"
52+
+ " LogicalTableScan(table=[[scott, LOGS]])\n";
53+
verifyLogical(root, expectedLogical);
54+
55+
String expectedSparkSql =
56+
"SELECT *\n"
57+
+ "FROM `scott`.`LOGS`\n"
58+
+ "WHERE `@timestamp` >= `DATE`('2020-10-11') AND `@timestamp` <= `DATE`('2025-01-01')";
59+
verifyPPLToSparkSQL(root, expectedSparkSql);
60+
}
61+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,11 @@ private String anonymizeStatement(String query, boolean isExplain) {
546546
PPLQueryDataAnonymizer anonymize = new PPLQueryDataAnonymizer(settings);
547547
return anonymize.anonymizeStatement(statement);
548548
}
549+
550+
@Test
551+
public void testSearchWithAbsoluteTimeRange() {
552+
assertEquals(
553+
"source=t | where @timestamp >= *** and @timestamp <= NOW()",
554+
anonymize("search source=t earliest='2012-12-10 15:00:00' latest=now"));
555+
}
549556
}

0 commit comments

Comments
 (0)