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

Revert "init" #1345

Merged
merged 1 commit into from
Aug 16, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* {"username" : {"$eq" : "aaron"}} In here we expand the shortcut into a canonical long form, so it
* is all the same.
*/
public class ComparisonExpression implements Invertible {
public class ComparisonExpression {

private final String path;

Expand All @@ -33,12 +33,8 @@ public List<FilterOperation<?>> getFilterOperations() {
return filterOperations;
}

/**
* implements Invertible, method to invert a ComparisonExpression this method will be called when
* $not operator is pushed down
*/
@Override
public Object invert() {
/** This method will be called when not operation is pushed down */
public void flip() {
List<FilterOperation<?>> filterOperations = new ArrayList<>(this.filterOperations.size());
for (FilterOperation<?> filterOperation : this.filterOperations) {
final FilterOperator flippedOperator = filterOperation.operator().flip();
Expand All @@ -47,7 +43,6 @@ public Object invert() {
filterOperations.add(new ValueComparisonOperation<>(flippedOperator, operand));
}
this.filterOperations = filterOperations;
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.InvertibleCommandClause;
import io.stargate.sgv2.jsonapi.api.model.command.ValidatableCommandClause;
import io.stargate.sgv2.jsonapi.api.model.command.deserializers.FilterClauseDeserializer;
import io.stargate.sgv2.jsonapi.config.constants.DocumentConstants;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.CollectionSchemaObject;
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.TableSchemaObject;
import io.stargate.sgv2.jsonapi.service.projection.IndexingProjector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
Expand All @@ -26,7 +23,7 @@
{"name": "Aaron", "country": "US"}
""")
public record FilterClause(LogicalExpression logicalExpression)
implements ValidatableCommandClause, InvertibleCommandClause {
implements ValidatableCommandClause {

@Override
public void validateTableCommand(CommandContext<TableSchemaObject> commandContext) {
Expand Down Expand Up @@ -130,103 +127,4 @@ private void validateCollectionList(
}
}
}

public void invertForCollectionCommand(CommandContext<CollectionSchemaObject> commandContext) {
invertLogicalExpression(this.logicalExpression(), null);
}

/**
* Recursive method Invert the currentLogicalExpression, add its inverted children to
* parentLogicalExpression examples: 1. {age=10, not{name=tim}} -> one comparisonExpression within
* not, just revert it and add to ancestorLogicalExpression -> {age=10, name!=tim}
*
* <p>2. {age=10, not{or[address=Shanghai ,gender=male]}} -> one logicalExpression within not,
* just revert it and add to ancestorLogicalExpression -> {age=10, and[address!=Shanghai
* ,gender!=male]}
*
* <p>3. {age=10, not{name=tim, school=cmu}} -> two comparisonExpressions within not, revert them
* and construct explicit or relation -> {age=10, or[name!=tim, school!=cmu]}
*
* <p>4. {age=10, not{or[address=Shanghai ,gender=male], name=tim}} -> one comparisonExpression
* and one logicalExpression within not -> invert them and construct explicit or relation ->
* {age=10, or[and[address!=Shanghai ,gender!=male], name!=tim]}
*
* <p>5. {age=10, not{or[address=Shanghai ,gender=male], and[color=yellow,height=175]}} ->
* multiple logicalExpressions within not -> invert them and construct explicit or relation ->
* {age=10, or[and[address!=Shanghai ,gender!=male], or[color!=yellow,height!=175]]}
*
* @param logicalExpression current logicalExpression
* @param parentLogicalExpression parent logicalExpression
*/
private void invertLogicalExpression(
LogicalExpression logicalExpression, LogicalExpression parentLogicalExpression) {

// create this temp list to avoid concurrentModification
List<LogicalExpression> tempLogicalExpressions =
new ArrayList<>(logicalExpression.logicalExpressions);

for (LogicalExpression childLogicalExpression : tempLogicalExpressions) {
invertLogicalExpression(childLogicalExpression, logicalExpression);
}

Iterator<LogicalExpression> iterator = logicalExpression.logicalExpressions.iterator();
while (iterator.hasNext()) {
LogicalExpression childLogicalExpression = iterator.next();
if (childLogicalExpression.getLogicalRelation() == LogicalExpression.LogicalOperator.NOT) {
iterator.remove();
}
}

// Handle all the comparisonExpressions and logicalExpressions inside this $ånot
if (logicalExpression.getLogicalRelation() == LogicalExpression.LogicalOperator.NOT) {
// 1. recursively flip all the ComparisonExpression and LogicalExpression
flip(logicalExpression);

// 2. Different of situations here
if (logicalExpression.comparisonExpressions.size() == 1
&& logicalExpression.logicalExpressions.isEmpty()) {
// 2.1 only one comparisonExpression
parentLogicalExpression.addComparisonExpressionsFlipped(
List.of(logicalExpression.comparisonExpressions.get(0)));
} else if (!logicalExpression.logicalExpressions.isEmpty()
&& logicalExpression.comparisonExpressions.isEmpty()) {
// 2.2 only one logicalExpression
logicalExpression.logicalExpressions.forEach(
parentLogicalExpression::addLogicalExpressionFlipped);
} else {
// 2.3 multiple comparisonExpression
// 2.4 multiple comparisonExpression and multiple logicalExpression
// 2.5 multiple logicalExpression
final LogicalExpression or = LogicalExpression.or();
logicalExpression.comparisonExpressions.forEach(
comparisonExpression ->
or.addComparisonExpressionsFlipped(List.of(comparisonExpression)));
logicalExpression.logicalExpressions.forEach(or::addLogicalExpressionFlipped);

parentLogicalExpression.addLogicalExpressionFlipped(or);
}

// TODO: Is this needed? since we will remove the $not node no matter what
// 3. clear the all the comparisonExpressions inside this $NOT
logicalExpression.comparisonExpressions.clear();
}
}

/**
* Recursive method Invert the currentLogicalExpression recursively 1. AND -> OR 2. OR -> AND
* 3.comparisonExpression -> opposite comparisonExpression
*
* @param logicalExpression logicalExpression
*/
private void flip(LogicalExpression logicalExpression) {
logicalExpression.setLogicalRelation(logicalExpression.getLogicalRelation().invert());
// flip child LogicalExpressions
for (LogicalExpression childLogicalExpression : logicalExpression.logicalExpressions) {
flip(childLogicalExpression);
}
// flip child ComparisonExpression
for (ComparisonExpression childComparisonExpression : logicalExpression.comparisonExpressions) {
childComparisonExpression.invert();
}
}
}

This file was deleted.

Loading