Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOID] Fixes #3798: apoc.cypher.runFile() - Unexpected behaviour with commented ; (#3993) #4033

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions full/src/main/java/apoc/cypher/CypherExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private void runDataStatementsInTx(
String fileName) {
while (scanner.hasNext()) {
String stmt = removeShellControlCommands(scanner.next());
if (stmt.trim().isEmpty()) continue;
if (isCommentOrEmpty(stmt)) continue;

// Periodic operations cannot be schema operations, so no need to check that here (will fail as invalid
// query)
Expand Down Expand Up @@ -303,7 +303,7 @@ private void collectError(BlockingQueue<RowResult> queue, boolean reportError, E

private Scanner createScannerFor(Reader reader) {
Scanner scanner = new Scanner(reader);
scanner.useDelimiter(";\r?\n");
scanner.useDelimiter(";\\s*\r?\n");
return scanner;
}

Expand All @@ -317,7 +317,7 @@ private void runSchemaStatementsInTx(
String fileName) {
while (scanner.hasNext()) {
String stmt = removeShellControlCommands(scanner.next());
if (stmt.trim().isEmpty()) continue;
if (isCommentOrEmpty(stmt)) continue;
boolean schemaOperation;
try {
schemaOperation = isSchemaOperation(stmt);
Expand All @@ -338,6 +338,11 @@ private void runSchemaStatementsInTx(
}
}

private static boolean isCommentOrEmpty(String stmt) {
String trimStatement = stmt.trim();
return trimStatement.isEmpty() || trimStatement.startsWith("//");
}

private static final Pattern shellControl =
Pattern.compile("^:?\\b(begin|commit|rollback)\\b", Pattern.CASE_INSENSITIVE);

Expand Down
36 changes: 36 additions & 0 deletions full/src/test/java/apoc/cypher/CypherExtendedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ public void testRunFile() throws Exception {
});
}

@Test
public void testRunFileWithCommentsAndEmptyRows() throws Exception {
testResult(db, "CALL apoc.cypher.runFile('return.cypher', {statistics: false})", r -> {
Map<String, Object> row = r.next();
assertEquals(Map.of("\"Step 1\"", "Step 1"), row.get("result"));

row = r.next();
assertEquals(Map.of("row", "Step 3"), row.get("result"));

row = r.next();
assertEquals(Map.of("row", 6L), row.get("result"));

row = r.next();
assertEquals(Map.of("row", 7L), row.get("result"));

row = r.next();
assertEquals(Map.of("'8'", "8"), row.get("result"));

row = r.next();
assertEquals(Map.of("9", 9L), row.get("result"));

row = r.next();
assertEquals(Map.of("10", 10L), row.get("result"));

assertFalse(r.hasNext());
});
}

@Test
public void testRunFileWithAutoTransaction() {
final int expectedCount = 2000;
Expand Down Expand Up @@ -640,6 +668,14 @@ public void testSchemaRunFile() {
}
}

@Test
public void testRunSchemaFileWithCommentsAndEmptyRows() {
testResult(db, "CALL apoc.cypher.runSchemaFile('schemaWithCommentsAndEmptyRows.cypher')", r -> {
assertSchemaCypherFile(r);
assertFalse(r.hasNext());
});
}

private void assertSchemaCypherFile(Result r) {
Map<String, Object> row = r.next();
Map result = (Map) row.get("result");
Expand Down
16 changes: 16 additions & 0 deletions full/src/test/resources/return.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RETURN "Step 1";
;
// RETURN "Step 2" as row;
RETURN "Step 3" AS row;
// RETURN "Step 4" as row;

// RETURN "Step 5" as row;

RETURN 6 as row;
// RETURN "Step 5" as row; // RETURN "Step 5" as row;
/*comment*/RETURN 7 /* comment*/ AS row;

/*comment*/RETURN '8'/*comment*/;
/*comment*/RETURN 9/*comment*/;
;
/*comment*/RETURN 10/*comment*/
13 changes: 13 additions & 0 deletions full/src/test/resources/schemaWithCommentsAndEmptyRows.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE FULLTEXT INDEX CustomerIndex1 FOR (n:Customer1) ON EACH [n.name1];
;
// RETURN "Step 2" as row;
CREATE FULLTEXT INDEX CustomerIndex21 FOR (n:Customer21) ON EACH [n.name12];
// RETURN "Step 4" as row;

// RETURN "Step 5" as row;

// RETURN "Step 5" as row; // RETURN "Step 5" as row;
/*comment*/ CREATE FULLTEXT INDEX CustomerIndex231 FOR (n:Customer213) /* comment*/ ON EACH [n.name123];
;
/*comment*/RETURN '8'/*comment*/;
/*comment*/CREATE INDEX node_index_name FOR (n:Person) ON (n.surname);/*comment*/
Loading