Skip to content

Commit

Permalink
Implement equals() and hashCode() for SQL query tree
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed Jul 28, 2016
1 parent 632b8d8 commit b5dc089
Show file tree
Hide file tree
Showing 145 changed files with 3,541 additions and 53 deletions.
Expand Up @@ -244,21 +244,19 @@ private void handleIndexAsTarget(OSelectExecutionPlan result, OIndexIdentifier i
String indexName = indexIdentifier.getIndexName();
OIndex<?> index = ctx.getDatabase().getMetadata().getIndexManager().getIndex(indexName);
if (index == null) {
throw new OCommandExecutionException(
"Index not found: " + indexName);//TODO check if you can iterate over the index without a key
throw new OCommandExecutionException("Index not found: " + indexName);
}
if (flattenedWhereClause == null) {
throw new OCommandExecutionException(
"Index queries must have a WHERE conditioni with like 'key = [val1, val2, valN]");//TODO check if you can iterate over the index without a key
}
if (flattenedWhereClause.size() != 1) {
throw new OCommandExecutionException(
"Index queries with this kind of condition are not supported yet: " + flattenedWhereClause);//TODO
throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);//TODO
}

OAndBlock andBlock = flattenedWhereClause.get(0);
if (andBlock.getSubBlocks().size() != 1) {
throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + andBlock);//TODO
throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);//TODO
}
this.whereClause = null;//The WHERE clause won't be used anymore, the index does all the filtering

Expand Down Expand Up @@ -286,7 +284,7 @@ private void handleMetadataAsTarget(OSelectExecutionPlan plan, OMetadataIdentifi
ORecordId schemaRid = new ORecordId(schemaRecordIdAsString);
plan.chain(new FetchFromRidsStep(Collections.singleton(schemaRid), ctx));
} else {
throw new UnsupportedOperationException();//TODO
throw new UnsupportedOperationException();//TODO other metadata types
}

}
Expand Down Expand Up @@ -330,8 +328,6 @@ private void handleOrderBy(OSelectExecutionPlan plan, OOrderBy orderBy, OCommand
}

private void handleClassAsTarget(OSelectExecutionPlan plan, OIdentifier identifier, OCommandContext ctx) {
//TODO optimize fetch from class, eg. when you can use and index or when you can do early sort.

if (handleClassAsTargetWithIndex(plan, identifier, ctx)) {
return;
}
Expand All @@ -353,7 +349,7 @@ private void handleClassAsTarget(OSelectExecutionPlan plan, OIdentifier identifi
}

private boolean handleClassAsTargetWithIndex(OSelectExecutionPlan plan, OIdentifier identifier, OCommandContext ctx) {
if (flattenedWhereClause == null || flattenedWhereClause.size()==0) {
if (flattenedWhereClause == null || flattenedWhereClause.size() == 0) {
return false;
}
OClass clazz = ctx.getDatabase().getMetadata().getSchema().getClass(identifier.getStringValue());
Expand Down Expand Up @@ -399,11 +395,28 @@ private OWhereClause createWhereFrom(OBooleanExpression remainingCondition) {
return result;
}

/**
* given a flat AND block and a set of indexes, returns the best index to be used to process it, with the complete descripton on how to use it
*
* @param ctx
* @param indexes
* @param block
* @return
*/
private IndexSearchDescriptor findBestIndexFor(OCommandContext ctx, Set<OIndex<?>> indexes, OAndBlock block) {
return indexes.stream().map(index -> buildIndexSearchDescriptor(ctx, index, block)).filter(Objects::nonNull)
.min(Comparator.comparing(x -> x.cost(ctx))).orElse(null);
}

/**
* given an index and a flat AND block, returns a descriptor on how to process it with an index (index, index key and additional filters
* to apply after index fetch
*
* @param ctx
* @param index
* @param block
* @return
*/
private IndexSearchDescriptor buildIndexSearchDescriptor(OCommandContext ctx, OIndex<?> index, OAndBlock block) {
List<String> indexFields = index.getDefinition().getFields();
OBinaryCondition keyCondition = new OBinaryCondition(-1);
Expand Down
Expand Up @@ -134,5 +134,57 @@ public OStatement copy() {
result.unsafe = unsafe;
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAlterClassStatement that = (OAlterClassStatement) o;

if (unsafe != that.unsafe)
return false;
if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (property != that.property)
return false;
if (identifierValue != null ? !identifierValue.equals(that.identifierValue) : that.identifierValue != null)
return false;
if (identifierListValue != null ? !identifierListValue.equals(that.identifierListValue) : that.identifierListValue != null)
return false;
if (add != null ? !add.equals(that.add) : that.add != null)
return false;
if (remove != null ? !remove.equals(that.remove) : that.remove != null)
return false;
if (numberValue != null ? !numberValue.equals(that.numberValue) : that.numberValue != null)
return false;
if (booleanValue != null ? !booleanValue.equals(that.booleanValue) : that.booleanValue != null)
return false;
if (customKey != null ? !customKey.equals(that.customKey) : that.customKey != null)
return false;
if (customValue != null ? !customValue.equals(that.customValue) : that.customValue != null)
return false;
if (customString != null ? !customString.equals(that.customString) : that.customString != null)
return false;

return true;
}

@Override public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (property != null ? property.hashCode() : 0);
result = 31 * result + (identifierValue != null ? identifierValue.hashCode() : 0);
result = 31 * result + (identifierListValue != null ? identifierListValue.hashCode() : 0);
result = 31 * result + (add != null ? add.hashCode() : 0);
result = 31 * result + (remove != null ? remove.hashCode() : 0);
result = 31 * result + (numberValue != null ? numberValue.hashCode() : 0);
result = 31 * result + (booleanValue != null ? booleanValue.hashCode() : 0);
result = 31 * result + (customKey != null ? customKey.hashCode() : 0);
result = 31 * result + (customValue != null ? customValue.hashCode() : 0);
result = 31 * result + (customString != null ? customString.hashCode() : 0);
result = 31 * result + (unsafe ? 1 : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=4668bb1cd336844052df941f39bdb634 (do not edit this line) */
Expand Up @@ -39,5 +39,33 @@ public OAlterClusterStatement(OrientSql p, int id) {
result.attributeValue = attributeValue == null ? null : attributeValue.copy();
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAlterClusterStatement that = (OAlterClusterStatement) o;

if (starred != that.starred)
return false;
if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (attributeName != null ? !attributeName.equals(that.attributeName) : that.attributeName != null)
return false;
if (attributeValue != null ? !attributeValue.equals(that.attributeValue) : that.attributeValue != null)
return false;

return true;
}

@Override public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (attributeName != null ? attributeName.hashCode() : 0);
result = 31 * result + (starred ? 1 : 0);
result = 31 * result + (attributeValue != null ? attributeValue.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=ed78ea0f1a05b0963db625ed1f338bd6 (do not edit this line) */
Expand Up @@ -43,5 +43,33 @@ public OAlterDatabaseStatement(OrientSql p, int id) {
result.settingValue = settingValue == null ? null : settingValue.copy();
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAlterDatabaseStatement that = (OAlterDatabaseStatement) o;

if (customPropertyName != null ? !customPropertyName.equals(that.customPropertyName) : that.customPropertyName != null)
return false;
if (customPropertyValue != null ? !customPropertyValue.equals(that.customPropertyValue) : that.customPropertyValue != null)
return false;
if (settingName != null ? !settingName.equals(that.settingName) : that.settingName != null)
return false;
if (settingValue != null ? !settingValue.equals(that.settingValue) : that.settingValue != null)
return false;

return true;
}

@Override public int hashCode() {
int result = customPropertyName != null ? customPropertyName.hashCode() : 0;
result = 31 * result + (customPropertyValue != null ? customPropertyValue.hashCode() : 0);
result = 31 * result + (settingName != null ? settingName.hashCode() : 0);
result = 31 * result + (settingValue != null ? settingValue.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=8fec57db8dd2a3b52aaa52dec7367cd4 (do not edit this line) */
Expand Up @@ -57,5 +57,39 @@ public OAlterPropertyStatement(OrientSql p, int id) {
result.settingValue = settingValue == null ? null : settingValue.copy();
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAlterPropertyStatement that = (OAlterPropertyStatement) o;

if (className != null ? !className.equals(that.className) : that.className != null)
return false;
if (propertyName != null ? !propertyName.equals(that.propertyName) : that.propertyName != null)
return false;
if (customPropertyName != null ? !customPropertyName.equals(that.customPropertyName) : that.customPropertyName != null)
return false;
if (customPropertyValue != null ? !customPropertyValue.equals(that.customPropertyValue) : that.customPropertyValue != null)
return false;
if (settingName != null ? !settingName.equals(that.settingName) : that.settingName != null)
return false;
if (settingValue != null ? !settingValue.equals(that.settingValue) : that.settingValue != null)
return false;

return true;
}

@Override public int hashCode() {
int result = className != null ? className.hashCode() : 0;
result = 31 * result + (propertyName != null ? propertyName.hashCode() : 0);
result = 31 * result + (customPropertyName != null ? customPropertyName.hashCode() : 0);
result = 31 * result + (customPropertyValue != null ? customPropertyValue.hashCode() : 0);
result = 31 * result + (settingName != null ? settingName.hashCode() : 0);
result = 31 * result + (settingValue != null ? settingValue.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=2421f6ad3b5f1f8e18149650ff80f1e7 (do not edit this line) */
Expand Up @@ -44,5 +44,33 @@ public OAlterSequenceStatement(OrientSql p, int id) {
result.cache = cache == null ? null : cache.copy();
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAlterSequenceStatement that = (OAlterSequenceStatement) o;

if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (start != null ? !start.equals(that.start) : that.start != null)
return false;
if (increment != null ? !increment.equals(that.increment) : that.increment != null)
return false;
if (cache != null ? !cache.equals(that.cache) : that.cache != null)
return false;

return true;
}

@Override public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (start != null ? start.hashCode() : 0);
result = 31 * result + (increment != null ? increment.hashCode() : 0);
result = 31 * result + (cache != null ? cache.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=def62b1d04db5223307fe51873a9edd0 (do not edit this line) */
Expand Up @@ -171,5 +171,23 @@ public OAndBlock copy() {
}
return null;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OAndBlock andBlock = (OAndBlock) o;

if (subBlocks != null ? !subBlocks.equals(andBlock.subBlocks) : andBlock.subBlocks != null)
return false;

return true;
}

@Override public int hashCode() {
return subBlocks != null ? subBlocks.hashCode() : 0;
}
}
/* JavaCC - OriginalChecksum=cf1f66cc86cfc93d357f9fcdfa4a4604 (do not edit this line) */
Expand Up @@ -96,5 +96,33 @@ public OArrayNumberSelector copy() {
result.integer = integer;
return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OArrayNumberSelector that = (OArrayNumberSelector) o;

if (inputFinalValue != null ? !inputFinalValue.equals(that.inputFinalValue) : that.inputFinalValue != null)
return false;
if (inputValue != null ? !inputValue.equals(that.inputValue) : that.inputValue != null)
return false;
if (expressionValue != null ? !expressionValue.equals(that.expressionValue) : that.expressionValue != null)
return false;
if (integer != null ? !integer.equals(that.integer) : that.integer != null)
return false;

return true;
}

@Override public int hashCode() {
int result = inputFinalValue != null ? inputFinalValue.hashCode() : 0;
result = 31 * result + (inputValue != null ? inputValue.hashCode() : 0);
result = 31 * result + (expressionValue != null ? expressionValue.hashCode() : 0);
result = 31 * result + (integer != null ? integer.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=5b2e495391ede3ccdc6c25aa63c8e591 (do not edit this line) */
Expand Up @@ -148,5 +148,36 @@ public OArrayRangeSelector copy() {

return result;
}

@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

OArrayRangeSelector that = (OArrayRangeSelector) o;

if (newRange != that.newRange)
return false;
if (from != null ? !from.equals(that.from) : that.from != null)
return false;
if (to != null ? !to.equals(that.to) : that.to != null)
return false;
if (fromSelector != null ? !fromSelector.equals(that.fromSelector) : that.fromSelector != null)
return false;
if (toSelector != null ? !toSelector.equals(that.toSelector) : that.toSelector != null)
return false;

return true;
}

@Override public int hashCode() {
int result = from != null ? from.hashCode() : 0;
result = 31 * result + (to != null ? to.hashCode() : 0);
result = 31 * result + (newRange ? 1 : 0);
result = 31 * result + (fromSelector != null ? fromSelector.hashCode() : 0);
result = 31 * result + (toSelector != null ? toSelector.hashCode() : 0);
return result;
}
}
/* JavaCC - OriginalChecksum=594a372e31fcbcd3ed962c2260e76468 (do not edit this line) */

0 comments on commit b5dc089

Please sign in to comment.