Skip to content

Commit

Permalink
Implement exclude projections in SELECT
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed Aug 6, 2021
1 parent f258781 commit 8c86633
Show file tree
Hide file tree
Showing 5 changed files with 2,389 additions and 2,321 deletions.
1 change: 1 addition & 0 deletions core/src/main/grammar/OrientSQL.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,7 @@ OProjectionItem ProjectionItem():
{}
{
(
[<BANG> {jjtThis.exclude = true;}]
jjtThis.expression = Expression()
[ jjtThis.nestedProjection = NestedProjection() ]
[ <AS> jjtThis.alias = Alias() ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import com.orientechnologies.orient.core.sql.query.OLegacyResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public class OProjection extends SimpleNode {
Expand All @@ -20,6 +17,9 @@ public class OProjection extends SimpleNode {

List<OProjectionItem> items;

// runtime
private Set<String> excludes;

public OProjection(List<OProjectionItem> items, boolean distinct) {
super(-1);
this.items = items;
Expand Down Expand Up @@ -79,6 +79,7 @@ public void toString(Map<Object, Object> params, StringBuilder builder) {
}

public OResult calculateSingle(OCommandContext iContext, OResult iRecord) {
initExcludes(iContext);
if (isExpand()) {
throw new IllegalStateException(
"This is an expand projection, it cannot be calculated as a single result" + toString());
Expand All @@ -91,8 +92,14 @@ public OResult calculateSingle(OCommandContext iContext, OResult iRecord) {

OResultInternal result = new OResultInternal();
for (OProjectionItem item : items) {
if (item.exclude) {
continue;
}
if (item.isAll()) {
for (String alias : iRecord.getPropertyNames()) {
if (this.excludes.contains(alias)) {
continue;
}
Object val = item.convert(iRecord.getProperty(alias));
if (item.nestedProjection != null) {
val = item.nestedProjection.apply(item.expression, val, iContext);
Expand All @@ -101,10 +108,16 @@ public OResult calculateSingle(OCommandContext iContext, OResult iRecord) {
}
if (iRecord.getElement().isPresent()) {
OElement x = iRecord.getElement().get();
result.setProperty("@rid", x.getIdentity());
result.setProperty("@version", x.getVersion());
result.setProperty(
"@class", x.getSchemaType().map(clazz -> clazz.getName()).orElse(null));
if (!this.excludes.contains("@rid")) {
result.setProperty("@rid", x.getIdentity());
}
if (!this.excludes.contains("@version")) {
result.setProperty("@version", x.getVersion());
}
if (!this.excludes.contains("@class")) {
result.setProperty(
"@class", x.getSchemaType().map(clazz -> clazz.getName()).orElse(null));
}
}
} else {
result.setProperty(item.getProjectionAliasAsString(), item.execute(iRecord, iContext));
Expand All @@ -119,6 +132,17 @@ public OResult calculateSingle(OCommandContext iContext, OResult iRecord) {
return result;
}

private void initExcludes(OCommandContext iContext) {
if (excludes == null) {
this.excludes = new HashSet<String>();
for (OProjectionItem item : items) {
if (item.exclude) {
this.excludes.add(item.getProjectionAliasAsString());
}
}
}
}

public OLegacyResultSet calculateExpand(OCommandContext iContext, OResult iRecord) {
if (!isExpand()) {
throw new IllegalStateException("This is not an expand projection:" + toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
import com.orientechnologies.orient.core.sql.executor.OInternalResultSet;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

public class OProjectionItem extends SimpleNode {

protected boolean exclude = false;

protected boolean all = false;

protected OIdentifier alias;
Expand Down Expand Up @@ -82,6 +81,9 @@ public void toString(Map<Object, Object> params, StringBuilder builder) {
if (all) {
builder.append("*");
} else {
if (exclude) {
builder.append("!");
}
if (expression != null) {
expression.toString(params, builder);
}
Expand Down Expand Up @@ -233,6 +235,7 @@ public AggregationContext getAggregationContext(OCommandContext ctx) {

public OProjectionItem copy() {
OProjectionItem result = new OProjectionItem(-1);
result.exclude = exclude;
result.all = all;
result.alias = alias == null ? null : alias.copy();
result.expression = expression == null ? null : expression.copy();
Expand All @@ -245,30 +248,18 @@ public OProjectionItem copy() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

OProjectionItem that = (OProjectionItem) o;

if (all != that.all) return false;
if (alias != null ? !alias.equals(that.alias) : that.alias != null) return false;
if (expression != null ? !expression.equals(that.expression) : that.expression != null)
return false;
if (nestedProjection != null
? !nestedProjection.equals(that.nestedProjection)
: that.nestedProjection != null) return false;
if (aggregate != null ? !aggregate.equals(that.aggregate) : that.aggregate != null)
return false;

return true;
return exclude == that.exclude
&& all == that.all
&& Objects.equals(alias, that.alias)
&& Objects.equals(expression, that.expression)
&& Objects.equals(aggregate, that.aggregate)
&& Objects.equals(nestedProjection, that.nestedProjection);
}

@Override
public int hashCode() {
int result = (all ? 1 : 0);
result = 31 * result + (alias != null ? alias.hashCode() : 0);
result = 31 * result + (expression != null ? expression.hashCode() : 0);
result = 31 * result + (nestedProjection != null ? nestedProjection.hashCode() : 0);
result = 31 * result + (aggregate != null ? aggregate.hashCode() : 0);
return result;
return Objects.hash(exclude, all, alias, expression, aggregate, nestedProjection);
}

public void extractSubQueries(SubQueryCollector collector) {
Expand Down Expand Up @@ -297,6 +288,7 @@ public OResult serialize() {
if (nestedProjection != null) {
result.setProperty("nestedProjection", nestedProjection.serialize());
}
result.setProperty("exclude", exclude);
return result;
}

Expand All @@ -314,6 +306,9 @@ public void deserialize(OResult fromResult) {
nestedProjection = new ONestedProjection(-1);
nestedProjection.deserialize(fromResult.getProperty("nestedProjection"));
}
if (Boolean.TRUE.equals(fromResult.getProperty("exclude"))) {
exclude = true;
}
}

public void setNestedProjection(ONestedProjection nestedProjection) {
Expand Down
Loading

0 comments on commit 8c86633

Please sign in to comment.