bamaer commented on code in PR #8849:
URL: https://github.com/apache/hop/pull/8849#discussion_r4245112115


##########
plugins/tech/age/src/main/java/org/apache/hop/age/AgeGraphDialect.java:
##########
@@ -45,6 +50,82 @@ public String toString() {
     return getId();
   }
 
+  @Override
+  public boolean isSupportingNodeIndexes() {
+    return true;
+  }
+
+  /**
+   * Create the label if it doesn't exist yet, then the index on its table: 
the index can be created
+   * before the nodes are loaded.
+   */
+  @Override
+  public String getCreateIndexStatement(GraphIndexDefinition index) throws 
HopException {
+    validateIndex(index);
+    if (index.properties().isEmpty()) {
+      throw new HopException("Please specify the properties to index on " + 
index.objectName());
+    }
+    return getCreateLabelStatement(index.objectName())
+        + ";\n"
+        + getCreateNodeIndexStatement(index.name(), index.objectName(), 
index.properties());
+  }
+
+  @Override
+  public String getDropIndexStatement(GraphIndexDefinition index) throws 
HopException {
+    validateIndex(index);
+    return "DROP INDEX IF EXISTS "
+        + quoteIdentifier(graphName)
+        + '.'
+        + quoteIdentifier(index.name());
+  }
+
+  /** Only named indexes on nodes: PostgreSQL needs the name to create the 
index if not exists. */
+  private void validateIndex(GraphIndexDefinition index) throws HopException {
+    if (index.isRelationship()) {
+      throw IGraphDialect.indexesNotSupported(this, index.objectType(), 
index.objectName());
+    }
+    if (StringUtils.isEmpty(index.objectName())) {
+      throw new HopException("Please specify the label of the index " + 
index.name());
+    }
+    if (StringUtils.isEmpty(index.name())) {
+      throw new HopException(
+          "Apache AGE indexes need a name. Label: "
+              + index.objectName()
+              + ", properties: "
+              + String.join(",", index.properties()));
+    }
+  }
+
+  /**
+   * A block creating the node label in the graph unless it exists: AGE 
creates the table of a label
+   * with the first node.
+   */
+  String getCreateLabelStatement(String label) {
+    String graphLiteral = quoteLiteral(graphName);
+    String labelLiteral = quoteLiteral(label);
+    String body =
+        "BEGIN IF NOT EXISTS (SELECT 1 FROM ag_catalog.ag_label l"
+            + " JOIN ag_catalog.ag_graph g ON g.graphid = l.graph WHERE g.name 
= "
+            + graphLiteral
+            + " AND l.kind = 'v' AND l.name = "
+            + labelLiteral
+            + ") THEN PERFORM ag_catalog.create_vlabel("

Review Comment:
   Thanks @mattcasters, good catch. Confirmed against 
`apache/age:release_PG18_1.8.0`: with one session holding the new label in an 
open transaction, the second one failed with `relation "…" already exists` and 
skipped the index.
   
   Fixed by wrapping `create_vlabel` in its own `EXCEPTION` block that 
re-checks `ag_label`. If the label exists by then, it carries on with `CREATE 
INDEX IF NOT EXISTS`; otherwise it re-raises. That's the same approach as 
`createGraphIfMissing`. The same two-session test now creates the index, and a 
real error (e.g. a missing graph) still fails.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to