Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! handle query builder exceptions an…
Browse files Browse the repository at this point in the history
…d close search context when an exception is raised
  • Loading branch information
dobe committed Feb 24, 2015
1 parent e1dd927 commit 76fbf9f
Showing 1 changed file with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public class ExpressionAnalyzer {

private final static Map<ComparisonExpression.Type, ComparisonExpression.Type> SWAP_OPERATOR_TABLE =
ImmutableMap.<ComparisonExpression.Type, ComparisonExpression.Type>builder()
.put(ComparisonExpression.Type.GREATER_THAN, ComparisonExpression.Type.LESS_THAN)
.put(ComparisonExpression.Type.LESS_THAN, ComparisonExpression.Type.GREATER_THAN)
.put(ComparisonExpression.Type.GREATER_THAN_OR_EQUAL, ComparisonExpression.Type.LESS_THAN_OR_EQUAL)
.put(ComparisonExpression.Type.LESS_THAN_OR_EQUAL, ComparisonExpression.Type.GREATER_THAN_OR_EQUAL)
.build();
.put(ComparisonExpression.Type.GREATER_THAN, ComparisonExpression.Type.LESS_THAN)
.put(ComparisonExpression.Type.LESS_THAN, ComparisonExpression.Type.GREATER_THAN)
.put(ComparisonExpression.Type.GREATER_THAN_OR_EQUAL, ComparisonExpression.Type.LESS_THAN_OR_EQUAL)
.put(ComparisonExpression.Type.LESS_THAN_OR_EQUAL, ComparisonExpression.Type.GREATER_THAN_OR_EQUAL)
.build();

private final static DataTypeAnalyzer DATA_TYPE_ANALYZER = new DataTypeAnalyzer();
private final static NegativeLiteralVisitor NEGATIVE_LITERAL_VISITOR = new NegativeLiteralVisitor();
Expand Down Expand Up @@ -122,12 +122,12 @@ public Symbol normalize(Symbol symbol) {
* <h2>Converts a expression into a symbol.</h2>
*
* <p>
* Expressions like QualifiedName that reference a column are resolved using the fieldResolver that were passed
* to the constructor.
* Expressions like QualifiedName that reference a column are resolved using the fieldResolver that were passed
* to the constructor.
* </p>
*
* <p>
* Some information (like resolved function symbols) are written onto the given expressionAnalysisContext
* Some information (like resolved function symbols) are written onto the given expressionAnalysisContext
* </p>
*/
public Symbol convert(Expression expression, ExpressionAnalysisContext expressionAnalysisContext) {
Expand All @@ -153,7 +153,7 @@ private FunctionInfo getFunctionInfo(FunctionIdent ident) {
* normalize and validate given value according to the corresponding {@link io.crate.planner.symbol.Reference}
*
* @param valueSymbol the value to normalize, might be anything from {@link io.crate.metadata.Scalar} to {@link io.crate.planner.symbol.Literal}
* @param reference the reference to which the value has to comply in terms of type-compatibility
* @param reference the reference to which the value has to comply in terms of type-compatibility
* @return the normalized Symbol, should be a literal
* @throws io.crate.exceptions.ColumnValidationException
*/
Expand Down Expand Up @@ -225,12 +225,13 @@ public Symbol normalizeInputForReference(

/**
* validate input types to not be nested arrays/collection types
*
* @throws ColumnValidationException if input type is a nested array type
*/
private void validateInputType(DataType dataType, ColumnIdent columnIdent) throws ColumnValidationException {
if (dataType != null
&& DataTypes.isCollectionType(dataType)
&& DataTypes.isCollectionType(((CollectionType)dataType).innerType())) {
&& DataTypes.isCollectionType(((CollectionType) dataType).innerType())) {
throw new ColumnValidationException(columnIdent.sqlFqn(),
String.format(Locale.ENGLISH, "Invalid datatype '%s'", dataType));
}
Expand All @@ -241,7 +242,7 @@ private void validateInputType(DataType dataType, ColumnIdent columnIdent) throw
* normalize and validate the given value according to the given {@link io.crate.types.DataType}
*
* @param inputValue any {@link io.crate.planner.symbol.Symbol} that evaluates to a Literal or Parameter
* @param dataType the type to convert this input to
* @param dataType the type to convert this input to
* @return a {@link io.crate.planner.symbol.Literal} of type <code>dataType</code>
*/
public Literal normalizeInputForType(Symbol inputValue, DataType dataType) {
Expand Down Expand Up @@ -281,7 +282,7 @@ private Map<String, Object> normalizeObjectValue(Map<String, Object> value, Refe
if (nestedInfo.type() == DataTypes.OBJECT && entry.getValue() instanceof Map) {
value.put(entry.getKey(), normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo, forWrite));
} else if (isObjectArray(nestedInfo.type()) && entry.getValue() instanceof Object[]) {
value.put(entry.getKey(), normalizeObjectArrayValue((Object[])entry.getValue(), nestedInfo, forWrite));
value.put(entry.getKey(), normalizeObjectArrayValue((Object[]) entry.getValue(), nestedInfo, forWrite));
} else {
value.put(entry.getKey(), normalizePrimitiveValue(entry.getValue(), nestedInfo));
}
Expand All @@ -290,14 +291,14 @@ private Map<String, Object> normalizeObjectValue(Map<String, Object> value, Refe
}

private boolean isObjectArray(DataType type) {
return type.id() == ArrayType.ID && ((ArrayType)type).innerType().id() == ObjectType.ID;
return type.id() == ArrayType.ID && ((ArrayType) type).innerType().id() == ObjectType.ID;
}

private Object[] normalizeObjectArrayValue(Object[] value, ReferenceInfo arrayInfo, boolean forWrite) {
for (Object arrayItem : value) {
Preconditions.checkArgument(arrayItem instanceof Map, "invalid value for object array type");
// return value not used and replaced in value as arrayItem is a map that is mutated
normalizeObjectValue((Map<String, Object>)arrayItem, arrayInfo, forWrite);
normalizeObjectValue((Map<String, Object>) arrayItem, arrayInfo, forWrite);
}
return value;
}
Expand Down Expand Up @@ -440,11 +441,13 @@ protected Symbol visitInPredicate(InPredicate node, ExpressionAnalysisContext co
InListExpression listExpression = ((InListExpression) node.getValueList());

Set<Function> comparisons = new HashSet<>(listExpression.getValues().size());
FunctionIdent ident = new FunctionIdent(EqOperator.NAME, Arrays.asList(leftType, leftType));
FunctionInfo eqInfo = new FunctionInfo(
new FunctionIdent(EqOperator.NAME, Arrays.asList(leftType, leftType)),
DataTypes.BOOLEAN);
for (Expression expression : ((InListExpression) node.getValueList()).getValues()) {
Symbol right = expression.accept(this, context);
if (!right.valueType().equals(leftType)){
if (right.valueType().isConvertableTo(leftType)){
if (!right.valueType().equals(leftType)) {
if (right.valueType().isConvertableTo(leftType)) {
right = context.allocateFunction(CastFunctionResolver.functionInfo(right.valueType(), leftType),
Arrays.asList(right));
} else {
Expand All @@ -454,16 +457,14 @@ protected Symbol visitInPredicate(InPredicate node, ExpressionAnalysisContext co
leftType.getName()));
}
}
comparisons.add(context.allocateFunction(
new FunctionInfo(ident, DataTypes.BOOLEAN),
Arrays.asList(left, right)));
comparisons.add(context.allocateFunction(eqInfo, Arrays.asList(left, right)));
}
if (comparisons.size()==1) {
if (comparisons.size() == 1) {
return comparisons.iterator().next();
} else {
Iterator<Function> iter = comparisons.iterator();
Symbol result = iter.next();
while(iter.hasNext()) {
while (iter.hasNext()) {
result = context.allocateFunction(OrOperator.INFO, Arrays.asList(result, iter.next()));
}
return result;
Expand Down Expand Up @@ -843,7 +844,7 @@ void normalize(ExpressionAnalysisContext context) {
/**
* swaps the comparison so that references are on the left side.
* e.g.:
* eq(2, name) becomes eq(name, 2)
* eq(2, name) becomes eq(name, 2)
*/
private void swapIfNecessary() {
if (!(left.symbolType().isValueSymbol() && (right instanceof Reference || right instanceof Field))) {
Expand Down

0 comments on commit 76fbf9f

Please sign in to comment.