Skip to content

Commit 3712c1d

Browse files
committed
add UT
1 parent adc66fe commit 3712c1d

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.db.pipe.pattern;
21+
22+
import org.apache.iotdb.commons.pipe.config.constant.PipeSourceConstant;
23+
import org.apache.iotdb.commons.pipe.datastructure.pattern.IoTDBTreePattern;
24+
import org.apache.iotdb.commons.pipe.datastructure.pattern.TreePattern;
25+
import org.apache.iotdb.commons.pipe.datastructure.pattern.UnionIoTDBTreePattern;
26+
import org.apache.iotdb.commons.pipe.datastructure.pattern.WithExclusionIoTDBTreePattern;
27+
import org.apache.iotdb.commons.pipe.datastructure.pattern.WithExclusionTreePattern;
28+
import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameters;
29+
import org.apache.iotdb.pipe.api.exception.PipeException;
30+
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
34+
import java.util.HashMap;
35+
36+
public class TreePatternPruningTest {
37+
38+
@Test
39+
public void testUnionInternalPruning_Cover() {
40+
final PipeParameters params =
41+
new PipeParameters(
42+
new HashMap<String, String>() {
43+
{
44+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.db.d1.*,root.db.d1.s1");
45+
}
46+
});
47+
48+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
49+
50+
Assert.assertTrue("Should be IoTDBTreePattern", result instanceof IoTDBTreePattern);
51+
Assert.assertEquals("root.db.d1.*", result.getPattern());
52+
}
53+
54+
@Test
55+
public void testUnionInternalPruning_Duplicates() {
56+
final PipeParameters params =
57+
new PipeParameters(
58+
new HashMap<String, String>() {
59+
{
60+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.db.d1,root.db.d1");
61+
}
62+
});
63+
64+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
65+
66+
Assert.assertTrue(result instanceof IoTDBTreePattern);
67+
Assert.assertEquals("root.db.d1", result.getPattern());
68+
}
69+
70+
@Test
71+
public void testInclusionPrunedByExclusion_Partial() {
72+
final PipeParameters params =
73+
new PipeParameters(
74+
new HashMap<String, String>() {
75+
{
76+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1,root.sg.d2");
77+
put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.d1");
78+
}
79+
});
80+
81+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
82+
83+
Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern);
84+
final WithExclusionIoTDBTreePattern exclusionPattern = (WithExclusionIoTDBTreePattern) result;
85+
86+
Assert.assertEquals("root.sg.d2", exclusionPattern.getInclusionPattern().getPattern());
87+
Assert.assertEquals("root.sg.d1", exclusionPattern.getExclusionPattern().getPattern());
88+
}
89+
90+
@Test
91+
public void testInclusionPrunedByExclusion_FullCoverage_Exception() {
92+
final PipeParameters params =
93+
new PipeParameters(
94+
new HashMap<String, String>() {
95+
{
96+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1");
97+
put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.**");
98+
}
99+
});
100+
101+
try {
102+
TreePattern.parsePipePatternFromSourceParameters(params);
103+
Assert.fail("Should throw PipeException because Exclusion fully covers Inclusion");
104+
} catch (final PipeException ignored) {
105+
// Expected exception
106+
}
107+
}
108+
109+
@Test
110+
public void testComplexPruning() {
111+
final PipeParameters params =
112+
new PipeParameters(
113+
new HashMap<String, String>() {
114+
{
115+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.A,root.sg.B,root.sg.A.sub");
116+
put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.A,root.sg.A.**");
117+
}
118+
});
119+
120+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
121+
122+
Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern);
123+
final WithExclusionIoTDBTreePattern excPattern = (WithExclusionIoTDBTreePattern) result;
124+
125+
Assert.assertEquals("root.sg.B", excPattern.getInclusionPattern().getPattern());
126+
}
127+
128+
@Test
129+
public void testComplexPruning_Prefix() {
130+
final PipeParameters params =
131+
new PipeParameters(
132+
new HashMap<String, String>() {
133+
{
134+
put(PipeSourceConstant.SOURCE_PATTERN_KEY, "root.sg.A,root.sg.B,root.sg.A.sub");
135+
put(PipeSourceConstant.SOURCE_PATTERN_EXCLUSION_KEY, "root.sg.A");
136+
put(PipeSourceConstant.SOURCE_PATTERN_FORMAT_KEY, "prefix");
137+
}
138+
});
139+
140+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
141+
142+
Assert.assertTrue(result instanceof WithExclusionTreePattern);
143+
final WithExclusionTreePattern excPattern = (WithExclusionTreePattern) result;
144+
145+
Assert.assertEquals("root.sg.B", excPattern.getInclusionPattern().getPattern());
146+
}
147+
148+
@Test
149+
public void testUnionPreservedWhenNotCovered() {
150+
final PipeParameters params =
151+
new PipeParameters(
152+
new HashMap<String, String>() {
153+
{
154+
put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1,root.sg.d2");
155+
put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.other");
156+
}
157+
});
158+
159+
final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params);
160+
161+
Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern);
162+
final WithExclusionIoTDBTreePattern excResult = (WithExclusionIoTDBTreePattern) result;
163+
164+
Assert.assertTrue(excResult.getInclusionPattern() instanceof UnionIoTDBTreePattern);
165+
final UnionIoTDBTreePattern unionInc = (UnionIoTDBTreePattern) excResult.getInclusionPattern();
166+
Assert.assertEquals(2, unionInc.getPatterns().size());
167+
}
168+
}

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/pattern/UnionIoTDBTreePattern.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.iotdb.commons.path.PartialPath;
2323
import org.apache.iotdb.commons.path.PathPatternTree;
24+
import org.apache.iotdb.commons.utils.TestOnly;
2425

2526
import org.apache.tsfile.file.metadata.IDeviceID;
2627

@@ -55,6 +56,11 @@ public UnionIoTDBTreePattern(final IoTDBTreePattern pattern) {
5556
this.patterns = Collections.singletonList(pattern);
5657
}
5758

59+
@TestOnly
60+
public List<IoTDBTreePattern> getPatterns() {
61+
return patterns;
62+
}
63+
5864
//////////////////////////// Tree Pattern Operations ////////////////////////////
5965

6066
@Override

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/pattern/WithExclusionIoTDBTreePattern.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.iotdb.commons.path.PartialPath;
2323
import org.apache.iotdb.commons.path.PathPatternTree;
24+
import org.apache.iotdb.commons.utils.TestOnly;
2425

2526
import org.apache.tsfile.file.metadata.IDeviceID;
2627

@@ -56,6 +57,16 @@ public WithExclusionIoTDBTreePattern(
5657
this(true, inclusionPattern, exclusionPattern);
5758
}
5859

60+
@TestOnly
61+
public IoTDBTreePatternOperations getInclusionPattern() {
62+
return inclusionPattern;
63+
}
64+
65+
@TestOnly
66+
public IoTDBTreePatternOperations getExclusionPattern() {
67+
return exclusionPattern;
68+
}
69+
5970
//////////////////////////// Tree Pattern Operations ////////////////////////////
6071

6172
@Override

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/pattern/WithExclusionTreePattern.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.iotdb.commons.pipe.datastructure.pattern;
2121

2222
import org.apache.iotdb.commons.path.PartialPath;
23+
import org.apache.iotdb.commons.utils.TestOnly;
2324

2425
import org.apache.tsfile.file.metadata.IDeviceID;
2526

@@ -48,6 +49,16 @@ public WithExclusionTreePattern(
4849
TreePattern.checkAndLogPatternCoverage(inclusionPattern, exclusionPattern);
4950
}
5051

52+
@TestOnly
53+
public TreePattern getInclusionPattern() {
54+
return inclusionPattern;
55+
}
56+
57+
@TestOnly
58+
public TreePattern getExclusionPattern() {
59+
return exclusionPattern;
60+
}
61+
5162
@Override
5263
public String getPattern() {
5364
return "INCLUSION("

0 commit comments

Comments
 (0)