Skip to content

Commit 22b2350

Browse files
authored
feat: adding basic support for tables (#19)
Start by adding support for table flattening and max depth, based on [table_tests.go](https://github.com/cloudquery/plugin-sdk/blob/main/schema/table_test.go)
1 parent 5e32126 commit 22b2350

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Diff for: lib/src/main/java/io/cloudquery/schema/Table.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.cloudquery.schema;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@Builder(toBuilder = true)
12+
@Getter
13+
public class Table {
14+
public static List<Table> flattenTables(List<Table> tables) {
15+
Map<String, Table> flattenMap = new HashMap<>();
16+
for (Table table : tables) {
17+
Table newTable = table.toBuilder().relations(Collections.emptyList()).build();
18+
flattenMap.put(newTable.name, newTable);
19+
for (Table child : flattenTables(table.getRelations())) {
20+
flattenMap.put(child.name, child);
21+
}
22+
}
23+
return flattenMap.values().stream().toList();
24+
}
25+
26+
public static int maxDepth(List<Table> tables) {
27+
int depth = 0;
28+
if (tables.isEmpty()) {
29+
return 0;
30+
}
31+
for (Table table : tables) {
32+
int newDepth = 1 + maxDepth(table.getRelations());
33+
if (newDepth > depth) {
34+
depth = newDepth;
35+
}
36+
}
37+
return depth;
38+
}
39+
40+
private String name;
41+
42+
@Builder.Default
43+
private List<Table> relations = Collections.emptyList();
44+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.cloudquery.schema;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class TableTest {
12+
13+
public Table testTable;
14+
15+
@Before
16+
public void setUp() {
17+
testTable = Table.builder().
18+
name("test").
19+
relations(List.of(
20+
Table.builder().name("test2").build(),
21+
Table.builder().name("test3").build(),
22+
Table.builder().name("test4").build()
23+
)).build();
24+
}
25+
26+
@Test
27+
public void shouldFlattenTables() {
28+
List<Table> srcTables = List.of(testTable);
29+
List<Table> flattenedTables = Table.flattenTables(srcTables);
30+
31+
assertEquals(1, srcTables.size());
32+
assertEquals(3, testTable.getRelations().size());
33+
assertEquals(4, flattenedTables.size());
34+
}
35+
36+
@Test
37+
public void shouldFlattenTablesWithDuplicates() {
38+
List<Table> srcTables = List.of(testTable, testTable, testTable);
39+
List<Table> flattenedTables = Table.flattenTables(srcTables);
40+
41+
assertEquals(3, srcTables.size());
42+
assertEquals(3, testTable.getRelations().size());
43+
assertEquals(4, flattenedTables.size());
44+
}
45+
46+
@Test
47+
public void shouldReturnMaxDepth() {
48+
assertEquals(0, Table.maxDepth(Collections.emptyList()));
49+
assertEquals(2, Table.maxDepth(List.of(testTable)));
50+
assertEquals(3, Table.maxDepth(List.of(testTable.toBuilder().relations(List.of(testTable)).build())));
51+
}
52+
}

0 commit comments

Comments
 (0)