Skip to content

Commit

Permalink
Fix SQL parsing and execution of nested projections to handle any exp…
Browse files Browse the repository at this point in the history
…ression instead of single fields
  • Loading branch information
luigidellaquila committed Jul 2, 2017
1 parent c1c88d5 commit 595ed4a
Show file tree
Hide file tree
Showing 5 changed files with 707 additions and 675 deletions.
7 changes: 4 additions & 3 deletions core/src/main/grammar/OrientSQL.jjt
Expand Up @@ -1999,13 +1999,14 @@ ONestedProjectionItem NestedProjectionItem():
{ {
( (
( (
LOOKAHEAD(1)
<STAR> { jjtThis.star = true; }
|
( (
[<BANG> {jjtThis.exclude = true;} ] [<BANG> {jjtThis.exclude = true;} ]
jjtThis.field = Identifier() jjtThis.expression = Expression()
[<STAR> {jjtThis.rightWildcard = true;} ] [<STAR> {jjtThis.rightWildcard = true;} ]
) )
|
<STAR> { jjtThis.star = true; }
) )
[jjtThis.expansion = NestedProjection() ] [jjtThis.expansion = NestedProjection() ]
[<AS> jjtThis.alias = Identifier()] [<AS> jjtThis.alias = Identifier()]
Expand Down
Expand Up @@ -147,14 +147,18 @@ default String toJson(Object val) {
jsonVal = builder.toString(); jsonVal = builder.toString();
} else if (val instanceof byte[]) { } else if (val instanceof byte[]) {
jsonVal = "\"" + Base64.getEncoder().encodeToString((byte[]) val) + "\""; jsonVal = "\"" + Base64.getEncoder().encodeToString((byte[]) val) + "\"";
} else if (val instanceof Date) {
//TODO use "2012-04-23T18:25:43.511Z" instead...?
jsonVal = String.valueOf(((Date) val).getTime());
} else { } else {

throw new UnsupportedOperationException("Cannot convert " + val + " - " + val.getClass() + " to JSON"); throw new UnsupportedOperationException("Cannot convert " + val + " - " + val.getClass() + " to JSON");
} }
return jsonVal; return jsonVal;
} }


default String encode(String s) { default String encode(String s) {
String result = s.replaceAll("\"", "\\\""); String result = s.replaceAll("\"", "\\\\\"");
result = result.replaceAll("\n", "\\\\n"); result = result.replaceAll("\n", "\\\\n");
result = result.replaceAll("\t", "\\\\t"); result = result.replaceAll("\t", "\\\\t");
return result; return result;
Expand Down
Expand Up @@ -62,21 +62,25 @@ public Object apply(OExpression expression, Object input, OCommandContext ctx) {
return input; return input;
} }


private Object apply(OExpression expression, OResult input, OCommandContext ctx, int recursion) { private Object apply(OExpression expression, OResult elem, OCommandContext ctx, int recursion) {
OResultInternal result = new OResultInternal(); OResultInternal result = new OResultInternal();
if (starItem != null || includeItems.size() == 0) { if (starItem != null || includeItems.size() == 0) {
for (String property : input.getPropertyNames()) { for (String property : elem.getPropertyNames()) {
if (isExclude(property)) { if (isExclude(property)) {
continue; continue;
} }
result.setProperty(property, convert(tryExpand(expression, property, input.getProperty(property), ctx, recursion))); result.setProperty(property, convert(tryExpand(expression, property, elem.getProperty(property), ctx, recursion)));
} }
} }
if (includeItems.size() > 0) { if (includeItems.size() > 0) {
//TODO manage wildcards!
for (ONestedProjectionItem item : includeItems) { for (ONestedProjectionItem item : includeItems) {
String name = item.field.getStringValue(); String alias = item.alias != null ? item.alias.getStringValue() : item.expression.getDefaultAlias().getStringValue();
Object value = input.getProperty(name); Object value = item.expression.execute(elem, ctx);
result.setProperty(name, convert(tryExpand(expression, name, value, ctx, recursion))); if (item.expansion != null) {
value = item.expand(expression, alias, value, ctx, recursion - 1);
}
result.setProperty(alias, convert(value));
} }
} }
return result; return result;
Expand Down Expand Up @@ -123,21 +127,21 @@ private Object apply(OExpression expression, OIdentifiable input, OCommandContex
} }
if (includeItems.size() > 0) { if (includeItems.size() > 0) {
//TODO manage wildcards! //TODO manage wildcards!
//TODO aliases
for (ONestedProjectionItem item : includeItems) { for (ONestedProjectionItem item : includeItems) {
String name = item.field.getStringValue(); String alias = item.alias != null ? item.alias.getStringValue() : item.expression.getDefaultAlias().getStringValue();
Object value = elem.getProperty(name); Object value = item.expression.execute(elem, ctx);
if (item.expansion != null) { if (item.expansion != null) {
value = item.expand(expression, name, value, ctx, recursion - 1); value = item.expand(expression, alias, value, ctx, recursion - 1);
} }
result.setProperty(name, convert(value)); result.setProperty(alias, convert(value));
} }
} }
return result; return result;
} }


private Object apply(OExpression expression, Map<String, Object> input, OCommandContext ctx, int recursion) { private Object apply(OExpression expression, Map<String, Object> input, OCommandContext ctx, int recursion) {
OResultInternal result = new OResultInternal(); OResultInternal result = new OResultInternal();

if (starItem != null || includeItems.size() == 0) { if (starItem != null || includeItems.size() == 0) {
for (String property : input.keySet()) { for (String property : input.keySet()) {
if (isExclude(property)) { if (isExclude(property)) {
Expand All @@ -147,13 +151,18 @@ private Object apply(OExpression expression, Map<String, Object> input, OCommand
} }
} }
if (includeItems.size() > 0) { if (includeItems.size() > 0) {
//TODO manage wildcards!
for (ONestedProjectionItem item : includeItems) { for (ONestedProjectionItem item : includeItems) {
String name = item.field.getStringValue(); String alias = item.alias != null ? item.alias.getStringValue() : item.expression.getDefaultAlias().getStringValue();
Object value = input.get(name); OResultInternal elem = new OResultInternal();
result.setProperty(name, convert(tryExpand(expression, name, value, ctx, recursion))); input.entrySet().forEach(x -> elem.setProperty(x.getKey(), x.getValue()));
Object value = item.expression.execute(elem, ctx);
if (item.expansion != null) {
value = item.expand(expression, alias, value, ctx, recursion - 1);
}
result.setProperty(alias, convert(value));
} }
} }

return result; return result;
} }


Expand Down
Expand Up @@ -14,7 +14,7 @@ public class ONestedProjectionItem extends SimpleNode {


protected boolean star = false; protected boolean star = false;


protected OIdentifier field; protected OExpression expression;
protected boolean rightWildcard = false; protected boolean rightWildcard = false;


protected ONestedProjection expansion; protected ONestedProjection expansion;
Expand All @@ -33,7 +33,7 @@ public ONestedProjectionItem copy() {
ONestedProjectionItem result = new ONestedProjectionItem(-1); ONestedProjectionItem result = new ONestedProjectionItem(-1);
result.exclude = exclude; result.exclude = exclude;
result.star = star; result.star = star;
result.field = field == null ? null : field.copy(); result.expression = expression == null ? null : expression.copy();
result.rightWildcard = rightWildcard; result.rightWildcard = rightWildcard;
result.expansion = expansion == null ? null : expansion.copy(); result.expansion = expansion == null ? null : expansion.copy();
result.alias = alias == null ? null : alias.copy(); result.alias = alias == null ? null : alias.copy();
Expand All @@ -56,8 +56,8 @@ public boolean matches(String propertyName) {
if (star) { if (star) {
return true; return true;
} }
if (field != null) { if (expression != null) {
String fieldString = field.getStringValue(); String fieldString = expression.getDefaultAlias().getStringValue();
if (fieldString.equals(propertyName)) { if (fieldString.equals(propertyName)) {
return true; return true;
} }
Expand All @@ -76,8 +76,8 @@ public void toString(Map<Object, Object> params, StringBuilder builder) {
if (star) { if (star) {
builder.append("*"); builder.append("*");
} }
if (field != null) { if (expression != null) {
field.toString(params, builder); expression.toString(params, builder);
if (rightWildcard) { if (rightWildcard) {
builder.append("*"); builder.append("*");
} }
Expand Down Expand Up @@ -106,7 +106,7 @@ public boolean equals(Object o) {
return false; return false;
if (rightWildcard != that.rightWildcard) if (rightWildcard != that.rightWildcard)
return false; return false;
if (field != null ? !field.equals(that.field) : that.field != null) if (expression != null ? !expression.equals(that.expression) : that.expression != null)
return false; return false;
if (expansion != null ? !expansion.equals(that.expansion) : that.expansion != null) if (expansion != null ? !expansion.equals(that.expansion) : that.expansion != null)
return false; return false;
Expand All @@ -117,7 +117,7 @@ public boolean equals(Object o) {
public int hashCode() { public int hashCode() {
int result = (exclude ? 1 : 0); int result = (exclude ? 1 : 0);
result = 31 * result + (star ? 1 : 0); result = 31 * result + (star ? 1 : 0);
result = 31 * result + (field != null ? field.hashCode() : 0); result = 31 * result + (expression != null ? expression.hashCode() : 0);
result = 31 * result + (rightWildcard ? 1 : 0); result = 31 * result + (rightWildcard ? 1 : 0);
result = 31 * result + (expansion != null ? expansion.hashCode() : 0); result = 31 * result + (expansion != null ? expansion.hashCode() : 0);
result = 31 * result + (alias != null ? alias.hashCode() : 0); result = 31 * result + (alias != null ? alias.hashCode() : 0);
Expand All @@ -132,8 +132,8 @@ public OResult serialize() {
OResultInternal result = new OResultInternal(); OResultInternal result = new OResultInternal();
result.setProperty("exclude", exclude); result.setProperty("exclude", exclude);
result.setProperty("star", star); result.setProperty("star", star);
if (field != null) { if (expression != null) {
result.setProperty("field", field.serialize()); result.setProperty("expression", expression.serialize());
} }
result.setProperty("rightWildcard", rightWildcard); result.setProperty("rightWildcard", rightWildcard);
if (expansion != null) { if (expansion != null) {
Expand All @@ -149,8 +149,8 @@ public void deserialize(OResult fromResult) {
exclude = fromResult.getProperty("exclude"); exclude = fromResult.getProperty("exclude");
star = fromResult.getProperty("star"); star = fromResult.getProperty("star");
if (fromResult.getProperty("field") != null) { if (fromResult.getProperty("field") != null) {
field = new OIdentifier(-1); expression = new OExpression(-1);
field.deserialize(fromResult.getProperty("field")); expression.deserialize(fromResult.getProperty("expression"));
} }
rightWildcard = fromResult.getProperty("rightWildcard"); rightWildcard = fromResult.getProperty("rightWildcard");
if (fromResult.getProperty("expansion") != null) { if (fromResult.getProperty("expansion") != null) {
Expand Down

0 comments on commit 595ed4a

Please sign in to comment.