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

Add support for simple IN expression. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ nb-configuration.xml
.externalToolBuilders
maven-eclipse.xml
dependency-reduced-pom.xml
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ public enum BinaryOperatorKind {
/**
* Or operator
*/
OR("or");
OR("or"),

/**
* In operator
*/
IN("in");

private String syntax;

/**
* Constructor for enumeration value
* @param Syntax used in the URI
* @param syntax used in the URI
*/
private BinaryOperatorKind(final String syntax) {
this.syntax = syntax;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,13 @@ T visitLambdaExpression(String lambdaFunction, String lambdaVariable, Expression
*/
T visitEnum(EdmEnumType type, List<String> enumValues) throws ExpressionVisitException, ODataApplicationException;

/**
* Called for each traversed {@link Literal} expression 被每个遍历的字面量集合调用
* @param literalList literalList
* @return Application return value of type T
* @throws ExpressionVisitException Thrown if an exception while traversing occured
* @throws ODataApplicationException Thrown by the application
*/
T visitLiteralList(LiteralList literalList) throws ExpressionVisitException, ODataApplicationException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.olingo.server.api.uri.queryoption.expression;

import org.apache.olingo.commons.api.edm.EdmType;

import java.util.List;

/**
* Represents a literal expression node in the expression tree
* Literal is not validated by default
*
* E.g. for
* $filter=style_code in ('AB','CD')
* $filter=style_value in (123,345)
*/
public interface LiteralList extends Expression {

/**
* @return Literal
*/
public List<String> getText();

/**
* Numeric literals without an dot and without an e return the smallest possible Edm Integer type.
* 没有点且没有e的数字文字返回尽可能小的Edm Integer类型。
* Numeric literals without an dot, without an e and larger than 2^63 - 1 are considered as Edm.Decimal
* 没有点,没有e且大于2 ^ 63-1的数字文字被认为是Edm.Decimal
* Numeric literals with an e, are considered to be Edm.Double
* 带有e的数字文字被认为是Edm.Double
* Numeric literals with an dot and without an e, are supposed to be Edm.Decimal
* 带有点而没有e的数字文字应该是 Edm.Decimal
*
* @return Type of the literal if detected. The type of the literal is guessed by the parser.
*/
public EdmType getType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@
import org.apache.olingo.server.api.uri.UriResourceNavigation;
import org.apache.olingo.server.api.uri.UriResourcePartTyped;
import org.apache.olingo.server.api.uri.UriResourceSingleton;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitException;
import org.apache.olingo.server.api.uri.queryoption.expression.ExpressionVisitor;
import org.apache.olingo.server.api.uri.queryoption.expression.Literal;
import org.apache.olingo.server.api.uri.queryoption.expression.Member;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -70,6 +63,7 @@ public class ExpressionJsonVisitor implements ExpressionVisitor<JsonNode> {
private static final String MEMBER_NAME = "member";
private static final String VALUE_NAME = "value";
private static final String LITERAL_NAME = "literal";
private static final String LITERAL_LIST_NAME = "literals";
private static final String EXPRESSION_NAME = "expression";
private static final String LAMBDA_VARIABLE_NAME = "lambdaVariable";
private static final String LAMBDA_FUNCTION_NAME = "lambdaFunction";
Expand Down Expand Up @@ -251,6 +245,14 @@ public JsonNode visitEnum(final EdmEnumType type, final List<String> enumValues)
return result;
}

@Override
public JsonNode visitLiteralList(LiteralList literalList) throws ExpressionVisitException, ODataApplicationException {
return nodeFactory.objectNode()
.put(NODE_TYPE_NAME, LITERAL_LIST_NAME)
.put(TYPE_NAME, getTypeString(literalList.getType()))
.put(VALUE_NAME, literalList.getText().toString());
}

private String getType(final UnaryOperatorKind operator) {
switch (operator) {
case MINUS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,83 +18,32 @@
*/
package org.apache.olingo.server.core.uri.parser;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmElement;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
import org.apache.olingo.commons.api.edm.EdmEnumType;
import org.apache.olingo.commons.api.edm.EdmFunction;
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.api.edm.EdmReturnType;
import org.apache.olingo.commons.api.edm.EdmSingleton;
import org.apache.olingo.commons.api.edm.EdmStructuredType;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.*;
import org.apache.olingo.commons.api.edm.constants.EdmTypeKind;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.uri.UriParameter;
import org.apache.olingo.server.api.uri.UriResourceFunction;
import org.apache.olingo.server.api.uri.UriResourceLambdaVariable;
import org.apache.olingo.server.api.uri.UriResourceNavigation;
import org.apache.olingo.server.api.uri.UriResourcePartTyped;
import org.apache.olingo.server.api.uri.*;
import org.apache.olingo.server.api.uri.queryoption.AliasQueryOption;
import org.apache.olingo.server.api.uri.queryoption.expression.Alias;
import org.apache.olingo.server.api.uri.queryoption.expression.Binary;
import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind;
import org.apache.olingo.server.api.uri.queryoption.expression.Enumeration;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.apache.olingo.server.api.uri.queryoption.expression.LambdaRef;
import org.apache.olingo.server.api.uri.queryoption.expression.Literal;
import org.apache.olingo.server.api.uri.queryoption.expression.Member;
import org.apache.olingo.server.api.uri.queryoption.expression.Method;
import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind;
import org.apache.olingo.server.api.uri.queryoption.expression.TypeLiteral;
import org.apache.olingo.server.api.uri.queryoption.expression.Unary;
import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind;
import org.apache.olingo.server.core.uri.UriInfoImpl;
import org.apache.olingo.server.core.uri.UriResourceComplexPropertyImpl;
import org.apache.olingo.server.core.uri.UriResourceCountImpl;
import org.apache.olingo.server.core.uri.UriResourceEntitySetImpl;
import org.apache.olingo.server.core.uri.UriResourceFunctionImpl;
import org.apache.olingo.server.core.uri.UriResourceItImpl;
import org.apache.olingo.server.core.uri.UriResourceLambdaAllImpl;
import org.apache.olingo.server.core.uri.UriResourceLambdaAnyImpl;
import org.apache.olingo.server.core.uri.UriResourceLambdaVarImpl;
import org.apache.olingo.server.core.uri.UriResourceNavigationPropertyImpl;
import org.apache.olingo.server.core.uri.UriResourcePrimitivePropertyImpl;
import org.apache.olingo.server.core.uri.UriResourceRootImpl;
import org.apache.olingo.server.core.uri.UriResourceSingletonImpl;
import org.apache.olingo.server.core.uri.UriResourceStartingTypeFilterImpl;
import org.apache.olingo.server.core.uri.UriResourceTypedImpl;
import org.apache.olingo.server.core.uri.UriResourceWithKeysImpl;
import org.apache.olingo.server.api.uri.queryoption.expression.*;
import org.apache.olingo.server.core.uri.*;
import org.apache.olingo.server.core.uri.parser.UriTokenizer.TokenKind;
import org.apache.olingo.server.core.uri.queryoption.expression.AliasImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.BinaryImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.EnumerationImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.LiteralImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.MemberImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.MethodImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.TypeLiteralImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.UnaryImpl;
import org.apache.olingo.server.core.uri.queryoption.expression.*;
import org.apache.olingo.server.core.uri.validator.UriValidationException;

import java.util.*;

public class ExpressionParser {

public static final List<FullQualifiedName> NUMBERSEDMTYPESQFN =
Arrays.asList(EdmPrimitiveTypeKind.SByte.getFullQualifiedName(),
EdmPrimitiveTypeKind.Int16.getFullQualifiedName(),
EdmPrimitiveTypeKind.Int32.getFullQualifiedName(),
EdmPrimitiveTypeKind.Int64.getFullQualifiedName(),
EdmPrimitiveTypeKind.Decimal.getFullQualifiedName(),
EdmPrimitiveTypeKind.Single.getFullQualifiedName(),
EdmPrimitiveTypeKind.Double.getFullQualifiedName()
);

private static final Map<TokenKind, BinaryOperatorKind> tokenToBinaryOperator;
static {
Map<TokenKind, BinaryOperatorKind> temp = new EnumMap<TokenKind, BinaryOperatorKind>(TokenKind.class);
Expand All @@ -115,6 +64,7 @@ public class ExpressionParser {
temp.put(TokenKind.MulOperator, BinaryOperatorKind.MUL);
temp.put(TokenKind.DivOperator, BinaryOperatorKind.DIV);
temp.put(TokenKind.ModOperator, BinaryOperatorKind.MOD);
temp.put(TokenKind.InOperator, BinaryOperatorKind.IN);

tokenToBinaryOperator = Collections.unmodifiableMap(temp);
}
Expand Down Expand Up @@ -224,6 +174,16 @@ private Expression parseExprEquality() throws UriParserException, UriValidationE
odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
operatorTokenKind = ParserHelper.next(tokenizer, TokenKind.EqualsOperator, TokenKind.NotEqualsOperator);
}

TokenKind operatorTokenKindIn = ParserHelper.next(tokenizer, TokenKind.InOperator);
// Null for everything other than EQ or NE
while (operatorTokenKindIn != null) {
final Expression right = parseExprMultiValues();
//checkInTypes(left, right);
left = new BinaryImpl(left, tokenToBinaryOperator.get(operatorTokenKindIn), right,
odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
operatorTokenKindIn = ParserHelper.next(tokenizer, TokenKind.InOperator);
}
return left;
}

Expand Down Expand Up @@ -313,7 +273,7 @@ private Expression parseExprUnary() throws UriParserException, UriValidationExce
}
return new UnaryImpl(UnaryOperatorKind.MINUS, expression, getType(expression));
} else if (tokenizer.next(TokenKind.NotOperator)) {
final Expression expression = parseExprValue();
final Expression expression = parseExprPrimary();
checkType(expression, EdmPrimitiveTypeKind.Boolean);
checkNoCollection(expression);
return new UnaryImpl(UnaryOperatorKind.NOT, expression, getType(expression));
Expand Down Expand Up @@ -390,6 +350,63 @@ private Expression parseExprValue() throws UriParserException, UriValidationExce
throw new UriParserSyntaxException("Unexpected token.", UriParserSyntaxException.MessageKeys.SYNTAX);
}

private Expression parseExprMultiValues() throws UriParserException, UriValidationException {
final LiteralListImpl literalList = new LiteralListImpl();
if (tokenizer.next(TokenKind.OPEN)) {
while (!tokenizer.next(TokenKind.CLOSE)) {
ParserHelper.bws(tokenizer);
if (tokenizer.next(TokenKind.COMMA)) {
ParserHelper.bws(tokenizer);
}
final Literal literal = (Literal) parseExprValue();
if (literalList.getType() == null) {
literalList.setType(literal.getType());
}

EdmType compatibleNumberEdmType = null;

if (literal.getType() == null || literalList.getType() == null) {
throw new UriParserSemanticException("incompatible type filter",
UriParserSemanticException.MessageKeys.INCOMPATIBLE_TYPE_FILTER);
} else if (NUMBERSEDMTYPESQFN.contains(literalList.getType().getFullQualifiedName())
|| NUMBERSEDMTYPESQFN.contains(literal.getType().getFullQualifiedName())) {
compatibleNumberEdmType = compatibleEdmTypeForNumbers(literal.getType(), literalList.getType());
}

if (literal.getType().equals(literalList.getType())) {
literalList.getText().add(literal.getText());
} else if (compatibleNumberEdmType != null) {
literalList.getText().add(literal.getText());
literalList.setType(compatibleNumberEdmType);
} else {
literal.getType().getFullQualifiedName();
throw new UriParserSemanticException("Inconsistent Type -> " + literal.getType().toString() + " != "
+ literalList.getType().toString(),
UriParserSemanticException.MessageKeys.INCONSISTENT_VALUE_TYPE_AFTER_IN_SENTENCE,
literal.getType().toString(), literalList.getType().toString());
}
//ParserHelper.bws(tokenizer);
}
//ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
return literalList;
}

throw new UriParserSyntaxException("Unexpected token.", UriParserSyntaxException.MessageKeys.SYNTAX);
}

private EdmType compatibleEdmTypeForNumbers(EdmType... edmTypes) {
int max = -1;
EdmType retEdmType = null;
for (EdmType edmType : edmTypes) {
int of = NUMBERSEDMTYPESQFN.indexOf(edmType.getFullQualifiedName());
if (of > max) {
max = of;
retEdmType = edmType;
}
}
return retEdmType;
}

private Expression parseMethod(final TokenKind nextMethod) throws UriParserException, UriValidationException {
// The method token text includes the opening parenthesis so that method calls can be recognized unambiguously.
// OData identifiers have to be considered after that.
Expand Down Expand Up @@ -1104,6 +1121,8 @@ protected static EdmType getType(final Expression expression) throws UriParserEx
type = ((Literal) expression).getType();
} else if (expression instanceof TypeLiteral) {
type = ((TypeLiteral) expression).getType();
} else if (expression instanceof LiteralList) {
type = ((LiteralList) expression).getType();
} else if (expression instanceof Enumeration) {
type = ((Enumeration) expression).getType();
} else if (expression instanceof Member) {
Expand Down Expand Up @@ -1201,6 +1220,15 @@ && isType(rightType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)) {
}
}

private void checkInTypes(final Expression left, final Expression right) throws UriParserException {

final EdmType leftType = getType(left);
final EdmType rightType = getType(right);
if (leftType == null || rightType == null || leftType.equals(rightType)) {
return;
}
}

private EdmEnumType getEnumType(final String primitiveValueLiteral) throws UriParserException {
final String enumTypeName = primitiveValueLiteral.substring(0, primitiveValueLiteral.indexOf('\''));
final EdmEnumType type = edm.getEnumType(new FullQualifiedName(enumTypeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public static enum MessageKeys implements MessageKey {
FUNCTION_MUST_USE_COLLECTIONS,
COLLECTION_NOT_ALLOWED,
/** parameter: not implemented part for system query option $id */
NOT_IMPLEMENTED_SYSTEM_QUERY_OPTION;
NOT_IMPLEMENTED_SYSTEM_QUERY_OPTION,
/** IN 字句的值类型不匹配 */
INCONSISTENT_VALUE_TYPE_AFTER_IN_SENTENCE;

@Override
public String getKey() {
Expand Down
Loading