Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #137 from pontusmelke/explain-rule-planner
Browse files Browse the repository at this point in the history
EXPLAIN and rule planner
  • Loading branch information
pontusmelke authored Mar 20, 2019
2 parents 93ad4f8 + ceb010b commit 15abf87
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.neo4j.shell;

import static java.lang.String.format;

@SuppressWarnings("WeakerAccess")
public class Version implements Comparable<Version> {
private final int major;
private final int minor;
private final int patch;

Version(int major, int minor, int patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}

public int major() {
return major;
}

public int minor() {
return minor;
}

public int patch() {
return patch;
}

@Override
public int compareTo(Version o) {
int comp = Integer.compare(major, o.major);
if (comp == 0) {
comp = Integer.compare(minor, o.minor);
if (comp == 0) {
comp = Integer.compare(patch, o.patch);
}
}
return comp;
}

@Override
public String toString() {
return format("%d.%d.%d", major, minor, patch);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.neo4j.shell;

import static java.lang.Integer.parseInt;
import static java.lang.String.format;

@SuppressWarnings({"WeakerAccess", "unused"})
public final class Versions {

private Versions() {
throw new UnsupportedOperationException("Don't instantiate");
}

public static int majorVersion(String version) {
return version(version).major();
}

public static int minorVersion(String version) {
return version(version).minor();
}

public static int patch(String version) {
return version(version).patch();
}

public static Version version(String version) {
if (version == null) {
throw new AssertionError("null is not a valid version string");
}
//remove -alpha, and -beta etc
int offset = version.indexOf("-");
if (offset > 0) {
version = version.substring(0, offset);
}
String[] split = version.split("\\.");
switch (split.length) {
case 1:
return new Version(parseInt(split[0]), 0, 0);
case 2:
return new Version(parseInt(split[0]), parseInt(split[1]), 0);
case 3:
return new Version(parseInt(split[0]), parseInt(split[1]), parseInt(split[2]));
default:
throw new AssertionError(
format("%s is not a proper version string, it should be of the form X.Y.Z ", version));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.neo4j.shell.commands;


import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -20,10 +19,9 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;
import static org.neo4j.shell.Versions.majorVersion;

public class CypherShellVerboseIntegrationTest {
@Rule
Expand Down Expand Up @@ -228,4 +226,24 @@ public void cypherWithOrder() throws CommandException {
assertThat( actual, containsString( "n.age ASC" ) );
}
}

@Test
public void cypherWithExplainAndRulePlanner() throws CommandException {
//given (there is no rule planner in neo4j 4.0)
assumeTrue( majorVersion( shell.getServerVersion() ) < 4 );

//when
shell.execute("CYPHER planner=rule EXPLAIN MATCH (e:E) WHERE e.bucket='Live' and e.id = 23253473 RETURN count(e)");

//then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(logger, times(1)).printOut(captor.capture());

List<String> result = captor.getAllValues();
String actual = result.get(0);
assertThat(actual, containsString("\"EXPLAIN\""));
assertThat(actual, containsString("\"READ_ONLY\""));
assertThat(actual, containsString("\"RULE\""));
assertThat(actual, containsString("\"INTERPRETED\""));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package org.neo4j.shell.prettyprint;

import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.Values;
import org.neo4j.driver.v1.summary.Plan;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.Values;
import org.neo4j.driver.v1.summary.Plan;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -105,7 +112,7 @@ String formatPlan(@Nonnull Plan plan) {
private String serialize(@Nonnull String key, @Nonnull Value v) {
switch (key) {
case "ColumnsLeft":
return "keep columns" + String.join(SEPARATOR, v.asList(Value::asString));
return removeGeneratedNames(v.asString());
case "LegacyExpression":
return removeGeneratedNames(v.asString());
case "Expression":
Expand Down

0 comments on commit 15abf87

Please sign in to comment.