From 76fbf9f855525ab67997fbe90059dfa9b82c9474 Mon Sep 17 00:00:00 2001 From: dobe Date: Tue, 24 Feb 2015 16:39:59 +0100 Subject: [PATCH] fixup! fixup! fixup! fixup! fixup! handle query builder exceptions and close search context when an exception is raised --- .../expressions/ExpressionAnalyzer.java | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/sql/src/main/java/io/crate/analyze/expressions/ExpressionAnalyzer.java b/sql/src/main/java/io/crate/analyze/expressions/ExpressionAnalyzer.java index ec5f5fd7e558..e15f4c438122 100644 --- a/sql/src/main/java/io/crate/analyze/expressions/ExpressionAnalyzer.java +++ b/sql/src/main/java/io/crate/analyze/expressions/ExpressionAnalyzer.java @@ -73,11 +73,11 @@ public class ExpressionAnalyzer { private final static Map SWAP_OPERATOR_TABLE = ImmutableMap.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(); @@ -122,12 +122,12 @@ public Symbol normalize(Symbol symbol) { *

Converts a expression into a symbol.

* *

- * 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. *

* *

- * Some information (like resolved function symbols) are written onto the given expressionAnalysisContext + * Some information (like resolved function symbols) are written onto the given expressionAnalysisContext *

*/ public Symbol convert(Expression expression, ExpressionAnalysisContext expressionAnalysisContext) { @@ -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 */ @@ -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)); } @@ -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 dataType */ public Literal normalizeInputForType(Symbol inputValue, DataType dataType) { @@ -281,7 +282,7 @@ private Map normalizeObjectValue(Map value, Refe if (nestedInfo.type() == DataTypes.OBJECT && entry.getValue() instanceof Map) { value.put(entry.getKey(), normalizeObjectValue((Map) 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)); } @@ -290,14 +291,14 @@ private Map normalizeObjectValue(Map 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)arrayItem, arrayInfo, forWrite); + normalizeObjectValue((Map) arrayItem, arrayInfo, forWrite); } return value; } @@ -440,11 +441,13 @@ protected Symbol visitInPredicate(InPredicate node, ExpressionAnalysisContext co InListExpression listExpression = ((InListExpression) node.getValueList()); Set 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 { @@ -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 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; @@ -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))) {