Skip to content

Commit

Permalink
fix: supported creation of unidirectional edges from SQL CREATE EDG…
Browse files Browse the repository at this point in the history
…E command

Fixed issue #1217
  • Loading branch information
lvca committed Aug 23, 2023
1 parent 5843dc6 commit 99dd0a2
Show file tree
Hide file tree
Showing 8 changed files with 4,219 additions and 4,058 deletions.
5 changes: 5 additions & 0 deletions engine/src/main/grammar/SQLGrammar.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ TOKEN:
|
<SYSTEM: ("s"|"S")("y"|"Y")("s"|"S")("t"|"T")("e"|"E")("m"|"M") >
|
<UNIDIRECTIONAL: ("u"|"U")("n"|"N")("i"|"I")("d"|"D")("i"|"I")("r"|"R")("e"|"E")("c"|"C")("t"|"T")("i"|"I")("o"|"O")("n"|"N")("a"|"A")("l"|"L") >
|
<THIS: "@" ( ("t"|"T")("h"|"H")("i"|"I")("s"|"S") ) >
|
<RECORD_ATTRIBUTE: <RID_ATTR> | <TYPE_ATTR> | <RID_ID_ATTR> | <RID_POS_ATTR> | <FIELDS_ATTR> | <OUT_ATTR> | <IN_ATTR> >
Expand Down Expand Up @@ -897,6 +899,8 @@ Identifier Identifier():
|
token = <SYSTEM>
|
token = <UNIDIRECTIONAL>
|
quotedToken = <QUOTED_IDENTIFIER>
) {
if(token!=null){
Expand Down Expand Up @@ -1710,6 +1714,7 @@ CreateEdgeStatement CreateEdgeStatement():
(
jjtThis.rightExpression = Expression()
)
[ <UNIDIRECTIONAL> { jjtThis.unidirectional = true; }]
[ <IF> <NOT> <EXISTS> { jjtThis.ifNotExists = true; }]
[ jjtThis.body = InsertBody() ]
[ jjtThis.retry = Retry() ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class CreateEdgeExecutionPlanner {
protected final Identifier targetClusterName;
protected final Expression leftExpression;
protected final Expression rightExpression;
protected final boolean unidirectional;
protected final boolean ifNotExists;
protected final InsertBody body;
protected final Number retry;
Expand All @@ -49,6 +50,7 @@ public CreateEdgeExecutionPlanner(final CreateEdgeStatement statement) {
this.targetClusterName = statement.getTargetBucketName() == null ? null : statement.getTargetBucketName().copy();
this.leftExpression = statement.getLeftExpression() == null ? null : statement.getLeftExpression().copy();
this.rightExpression = statement.getRightExpression() == null ? null : statement.getRightExpression().copy();
this.unidirectional = statement.isUnidirectional();
this.ifNotExists = statement.ifNotExists();
this.body = statement.getBody() == null ? null : statement.getBody().copy();
this.retry = statement.getRetry();
Expand Down Expand Up @@ -88,7 +90,7 @@ public InsertExecutionPlan createExecutionPlan(final CommandContext context, fin
uniqueIndexName = null;

result.chain(new CreateEdgesStep(targetClass, targetClusterName, uniqueIndexName, new Identifier("$__ARCADEDB_CREATE_EDGE_fromV"),
new Identifier("$__ARCADEDB_CREATE_EDGE_toV"), ifNotExists, wait, retry, context, enableProfiling));
new Identifier("$__ARCADEDB_CREATE_EDGE_toV"), unidirectional, ifNotExists, wait, retry, context, enableProfiling));

handleSetFields(result, body, context, enableProfiling);
handleSave(result, targetClusterName, context, enableProfiling);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
*/
public class CreateEdgesStep extends AbstractExecutionStep {

private final Identifier targetClass;
private final Identifier targetCluster;
private final String uniqueIndexName;
private final Identifier fromAlias;
private final Identifier toAlias;
private final boolean ifNotExists;
private final Number wait;
private final Number retry;

private final Identifier targetClass;
private final Identifier targetCluster;
private final String uniqueIndexName;
private final Identifier fromAlias;
private final Identifier toAlias;
private final boolean ifNotExists;
private final Number wait;
private final Number retry;
private final boolean unidirectional;
// operation stuff
private Iterator fromIter;
private Iterator toIterator;
Expand All @@ -61,14 +61,15 @@ public class CreateEdgesStep extends AbstractExecutionStep {
private boolean inited = false;

public CreateEdgesStep(final Identifier targetClass, final Identifier targetClusterName, final String uniqueIndex, final Identifier fromAlias,
final Identifier toAlias, final boolean ifNotExists, final Number wait, final Number retry, final CommandContext context,
final Identifier toAlias, final boolean unidirectional, final boolean ifNotExists, final Number wait, final Number retry, final CommandContext context,
final boolean profilingEnabled) {
super(context, profilingEnabled);
this.targetClass = targetClass;
this.targetCluster = targetClusterName;
this.uniqueIndexName = uniqueIndex;
this.fromAlias = fromAlias;
this.toAlias = toAlias;
this.unidirectional = unidirectional;
this.ifNotExists = ifNotExists;
this.wait = wait;
this.retry = retry;
Expand Down Expand Up @@ -109,7 +110,7 @@ public Result next() {
return null;
}

final MutableEdge edge = edgeToUpdate != null ? edgeToUpdate : currentFrom.newEdge(targetClass.getStringValue(), currentTo, true);
final MutableEdge edge = edgeToUpdate != null ? edgeToUpdate : currentFrom.newEdge(targetClass.getStringValue(), currentTo, !unidirectional);

final UpdatableResult result = new UpdatableResult(edge);
currentTo = null;
Expand Down Expand Up @@ -265,7 +266,7 @@ public String prettyPrint(final int depth, final int indent) {
final String spaces = ExecutionStepInternal.getIndent(depth, indent);
String result = spaces + "+ FOR EACH x in " + fromAlias + "\n";
result += spaces + " FOR EACH y in " + toAlias + "\n";
result += spaces + " CREATE EDGE " + targetClass + " FROM x TO y";
result += spaces + " CREATE EDGE " + targetClass + " FROM x TO y " + (unidirectional ? "UNIDIRECTIONAL" : "BIDIRECTIONAL");
if (profilingEnabled)
result += " (" + getCostFormatted() + ")";

Expand All @@ -283,6 +284,7 @@ public boolean canBeCached() {
@Override
public ExecutionStep copy(final CommandContext context) {
return new CreateEdgesStep(targetClass == null ? null : targetClass.copy(), targetCluster == null ? null : targetCluster.copy(), uniqueIndexName,
fromAlias == null ? null : fromAlias.copy(), toAlias == null ? null : toAlias.copy(), ifNotExists, wait, retry, context, profilingEnabled);
fromAlias == null ? null : fromAlias.copy(), toAlias == null ? null : toAlias.copy(), unidirectional, ifNotExists, wait, retry, context,
profilingEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class CreateEdgeStatement extends Statement {
protected Number retry;
protected Number wait;
protected boolean ifNotExists;
protected boolean unidirectional = false;

public CreateEdgeStatement(final int id) {
super(id);
Expand Down Expand Up @@ -90,6 +91,9 @@ public void toString(final Map<String, Object> params, final StringBuilder build
builder.append(" TO ");
rightExpression.toString(params, builder);

if (unidirectional)
builder.append(" UNIDIRECTIONAL");

if (ifNotExists)
builder.append(" IF NOT EXISTS");

Expand All @@ -115,6 +119,7 @@ public CreateEdgeStatement copy() {
result.leftExpression = leftExpression == null ? null : leftExpression.copy();
result.rightExpression = rightExpression == null ? null : rightExpression.copy();
result.ifNotExists = ifNotExists;
result.unidirectional = unidirectional;
result.body = body == null ? null : body.copy();
result.retry = retry;
result.wait = wait;
Expand All @@ -123,7 +128,7 @@ public CreateEdgeStatement copy() {

@Override
protected Object[] getIdentityElements() {
return new Object[] { targetType, targetBucketName, leftExpression, rightExpression, ifNotExists, body, retry, wait };
return new Object[] { targetType, targetBucketName, leftExpression, rightExpression, unidirectional, ifNotExists, body, retry, wait };
}

public Identifier getTargetType() {
Expand All @@ -146,6 +151,10 @@ public boolean ifNotExists() {
return ifNotExists;
}

public boolean isUnidirectional() {
return unidirectional;
}

public InsertBody getBody() {
return body;
}
Expand Down
Loading

0 comments on commit 99dd0a2

Please sign in to comment.