in v1/src/main/java/com/google/cloud/teleport/spanner/ddl/InformationSchemaScanner.java [1122:1258]
private void listPropertyGraphTables(Ddl.Builder builder, String tableType) {
ResultSet resultSet =
context.executeQuery(
Statement.of(
"SELECT t.property_graph_schema, t.property_graph_name, "
+ "t.property_graph_metadata_json."
+ tableType
+ " FROM information_schema.property_graphs AS t "
+ "WHERE t.property_graph_schema NOT IN ('INFORMATION_SCHEMA', 'SPANNER_SYS')"));
while (resultSet.next()) {
String propertyGraphSchema = resultSet.getString(0);
String propertyGraphName = resultSet.getString(1);
String propertyGraphNameQualified = getQualifiedName(propertyGraphSchema, propertyGraphName);
String tablesJson;
try {
tablesJson = resultSet.getJson(2);
} catch (Exception edgeTableException) {
LOG.debug(propertyGraphNameQualified + " does not contain any edge tables");
return;
}
LOG.debug("Schema PropertyGraph {}", propertyGraphNameQualified);
try {
JSONArray tablesArray = new JSONArray(tablesJson);
PropertyGraph propertyGraph =
getPropertyGraphByName(builder.build().propertyGraphs(), propertyGraphNameQualified);
PropertyGraph.Builder propertyGraphBuilder = propertyGraph.toBuilder();
if (propertyGraph == null) {
throw new RuntimeException("Property graph not found: " + propertyGraphNameQualified);
}
for (int i = 0; i < tablesArray.length(); i++) {
JSONObject table = tablesArray.getJSONObject(i);
String baseTableName = table.getString("baseTableName");
JSONArray keyColumnsArray = table.getJSONArray("keyColumns");
String kind = table.getString("kind");
JSONArray labelNamesArray = table.getJSONArray("labelNames");
String name = table.getString("name");
JSONArray propertyDefinitionsArray = table.getJSONArray("propertyDefinitions");
ImmutableList.Builder<String> keyColumnsBuilder = ImmutableList.builder();
for (int j = 0; j < keyColumnsArray.length(); j++) {
keyColumnsBuilder.add(keyColumnsArray.getString(j));
}
ImmutableList<String> keyColumns = keyColumnsBuilder.build();
GraphElementTable.Builder graphElementTableBuilder =
GraphElementTable.builder()
.propertyGraphBuilder(propertyGraphBuilder)
.name(name)
.baseTableName(baseTableName)
.kind(GraphElementTable.Kind.valueOf(kind))
.keyColumns(keyColumns);
// If it's an edge table, extract source and destination node table references
if (tableType.equals("edgeTables")) {
JSONObject sourceNodeTable = table.getJSONObject("sourceNodeTable");
JSONObject destinationNodeTable = table.getJSONObject("destinationNodeTable");
GraphElementTable.GraphNodeTableReference sourceNodeTableReference =
new GraphElementTable.GraphNodeTableReference(
sourceNodeTable.getString("nodeTableName"),
ImmutableList.copyOf(
toStringList(sourceNodeTable.getJSONArray("nodeTableColumns"))),
ImmutableList.copyOf(
toStringList(sourceNodeTable.getJSONArray("edgeTableColumns"))));
GraphElementTable.GraphNodeTableReference destinationNodeTableReference =
new GraphElementTable.GraphNodeTableReference(
destinationNodeTable.getString("nodeTableName"),
ImmutableList.copyOf(
toStringList(destinationNodeTable.getJSONArray("nodeTableColumns"))),
ImmutableList.copyOf(
toStringList(destinationNodeTable.getJSONArray("edgeTableColumns"))));
graphElementTableBuilder
.sourceNodeTable(sourceNodeTableReference)
.targetNodeTable(destinationNodeTableReference);
}
List<GraphElementTable.LabelToPropertyDefinitions> labelsToPropertyDefinitions =
new ArrayList<>();
for (int j = 0; j < labelNamesArray.length(); j++) {
String labelName = labelNamesArray.getString(j);
PropertyGraph.GraphElementLabel propertyGraphLabel = propertyGraph.getLabel(labelName);
if (propertyGraphLabel != null) {
ImmutableList.Builder<GraphElementTable.PropertyDefinition>
propertyDefinitionsBuilder = ImmutableList.builder();
for (String propertyName : propertyGraphLabel.properties) {
for (int k = 0; k < propertyDefinitionsArray.length(); k++) {
JSONObject propertyDefinition = propertyDefinitionsArray.getJSONObject(k);
String propertyDeclarationName =
propertyDefinition.getString("propertyDeclarationName");
if (propertyName.equals(propertyDeclarationName)) {
PropertyGraph.PropertyDeclaration propertyDeclaration =
propertyGraph.getPropertyDeclaration(propertyDeclarationName);
propertyDefinitionsBuilder.add(
new GraphElementTable.PropertyDefinition(
propertyDeclaration.name,
propertyDefinition.getString("valueExpressionSql")));
break;
}
}
}
ImmutableList<GraphElementTable.PropertyDefinition> propertyDefinitions =
propertyDefinitionsBuilder.build();
labelsToPropertyDefinitions.add(
new GraphElementTable.LabelToPropertyDefinitions(labelName, propertyDefinitions));
}
}
graphElementTableBuilder.labelToPropertyDefinitions(
ImmutableList.copyOf(labelsToPropertyDefinitions));
// Add the GraphElementTable to the PropertyGraph builder
if (tableType.equals("nodeTables")) {
propertyGraphBuilder.addNodeTable(graphElementTableBuilder.autoBuild());
} else { // tableType.equals("edgeTables")
propertyGraphBuilder.addEdgeTable(graphElementTableBuilder.autoBuild());
}
}
propertyGraph = propertyGraphBuilder.build();
builder.addPropertyGraph(propertyGraph);
} catch (Exception e) {
LOG.error("Error parsing {} JSON: {}", tableType, e.getMessage());
}
}
}