@Override protected String visitComparisonExpression(ComparisonExpression node, Boolean negate) { String left = process(node.getLeft(), negate); String right = process(node.getRight(), negate); String predicate = node.getType().getValue() + " " + right; if (negate) { predicate = String.format("not(%s)", predicate); } return "SELECT date, dimension, _user_set FROM " + left + " WHERE dimension " + predicate; }
@Override protected String visitComparisonExpression(ComparisonExpression node, Boolean context) { return String.format(getComparisonFormat(node.getType()), process(node.getRight(), context), process(node.getLeft(), context)); }
@Override protected String visitComparisonExpression(ComparisonExpression node, Void context) { return formatBinaryExpression(node.getType().getValue(), node.getLeft(), node.getRight()); }
@Override protected String visitComparisonExpression(ComparisonExpression node, Boolean unmangleNames) { return '(' + process(node.getLeft(), unmangleNames) + ' ' + node.getType().getValue() + ' ' + process(node.getRight(), unmangleNames) + ')'; }
private boolean isEqualComparisonExpression(Expression conjunct) { return conjunct instanceof ComparisonExpression && ((ComparisonExpression) conjunct).getType() == ComparisonExpression.Type.EQUAL; }
@Override protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) { print(indentLevel, node.getType().toString()); super.visitComparisonExpression(node, indentLevel + 1); return null; }
@Override protected Type visitComparisonExpression(ComparisonExpression node, StackableAstVisitorContext<AnalysisContext> context) { OperatorType operatorType; if (node.getType() == ComparisonExpression.Type.IS_DISTINCT_FROM) { operatorType = OperatorType.EQUAL; } else { operatorType = OperatorType.valueOf(node.getType().name()); } return getOperator(context, node, operatorType, node.getLeft(), node.getRight()); }
/** * Applies to a single clause in a WHERE. Process the left hand side, * the comparator, then the right hand side. */ @Override public Void visitComparisonExpression( ComparisonExpression node, Boolean capture) { process(node.getLeft(), true); translation.append(" is") .append(SlotUtil.comparisonTypeToEnglish(node.getType())); process(node.getRight(), true); return null; }
private static Predicate<Expression> joinEqualityExpression(final Collection<Symbol> leftSymbols) { return expression -> { // At this point in time, our join predicates need to be deterministic if (isDeterministic(expression) && expression instanceof ComparisonExpression) { ComparisonExpression comparison = (ComparisonExpression) expression; if (comparison.getType() == ComparisonExpression.Type.EQUAL) { Set<Symbol> symbols1 = DependencyExtractor.extractUnique(comparison.getLeft()); Set<Symbol> symbols2 = DependencyExtractor.extractUnique(comparison.getRight()); return (Iterables.all(symbols1, in(leftSymbols)) && Iterables.all(symbols2, not(in(leftSymbols)))) || (Iterables.all(symbols2, in(leftSymbols)) && Iterables.all(symbols1, not(in(leftSymbols)))); } } return false; }; }
private static Set<Expression> equalityAsSet(Expression expression) { Preconditions.checkArgument(expression instanceof ComparisonExpression); ComparisonExpression comparisonExpression = (ComparisonExpression) expression; Preconditions.checkArgument(comparisonExpression.getType() == EQUAL); return ImmutableSet.of(comparisonExpression.getLeft(), comparisonExpression.getRight()); }
@Override protected String visitComparisonExpression(ComparisonExpression node, Boolean unmangleNames) { return formatBinaryExpression(node.getType().getValue(), node.getLeft(), node.getRight(), unmangleNames); }
@Override protected String visitComparisonExpression(ComparisonExpression node, Boolean unmangleNames) { return formatBinaryExpression(node.getType().getValue(), node.getLeft(), node.getRight(), unmangleNames); }
/** * Extract a normalized simple comparison between a QualifiedNameReference and a native value if possible. */ private static Optional<NormalizedSimpleComparison> toNormalizedSimpleComparison(Session session, Metadata metadata, Map<Symbol, Type> types, ComparisonExpression comparison) { IdentityHashMap<Expression, Type> expressionTypes = ExpressionAnalyzer.getExpressionTypes(session, metadata, new SqlParser(), types, comparison); Object left = ExpressionInterpreter.expressionOptimizer(comparison.getLeft(), metadata, session, expressionTypes).optimize(NoOpSymbolResolver.INSTANCE); Object right = ExpressionInterpreter.expressionOptimizer(comparison.getRight(), metadata, session, expressionTypes).optimize(NoOpSymbolResolver.INSTANCE); if (left instanceof QualifiedNameReference && !(right instanceof Expression)) { return Optional.of(new NormalizedSimpleComparison((QualifiedNameReference) left, comparison.getType(), new NullableValue(expressionTypes.get(comparison.getRight()), right))); } if (right instanceof QualifiedNameReference && !(left instanceof Expression)) { return Optional.of(new NormalizedSimpleComparison((QualifiedNameReference) right, comparison.getType().flip(), new NullableValue(expressionTypes.get(comparison.getLeft()), left))); } return Optional.empty(); }
/** * Determines whether an Expression may be successfully applied to the equality inference */ public static Predicate<Expression> isInferenceCandidate() { return expression -> { expression = normalizeInPredicateToEquality(expression); if (DeterminismEvaluator.isDeterministic(expression) && expression instanceof ComparisonExpression) { ComparisonExpression comparison = (ComparisonExpression) expression; if (comparison.getType() == ComparisonExpression.Type.EQUAL) { // We should only consider equalities that have distinct left and right components return !comparison.getLeft().equals(comparison.getRight()); } } return false; }; }
public static Expression normalize(Expression expression) { if (expression instanceof NotExpression) { NotExpression not = (NotExpression) expression; if (not.getValue() instanceof ComparisonExpression && ((ComparisonExpression) not.getValue()).getType() != IS_DISTINCT_FROM) { ComparisonExpression comparison = (ComparisonExpression) not.getValue(); return new ComparisonExpression(comparison.getType().negate(), comparison.getLeft(), comparison.getRight()); } if (not.getValue() instanceof NotExpression) { return normalize(((NotExpression) not.getValue()).getValue()); } } return expression; } }
String[] types = new String[state.getSources().size()]; for(int i=0; i<types.length; i++) types[i] = state.getSources().get(i).getSource(); if(compareExp.getType() == ComparisonExpressionType.EQUAL){ if(field.equals(Heading.ID)) comparison = QueryBuilders.idsQuery(types).addIds((String)value); else if(field.equals(Heading.SEARCH)) comparison = QueryBuilders.queryStringQuery((String)value); else if(value instanceof String) comparison = queryForString(field, (String)value); else comparison = QueryBuilders.termQuery(field, value); }else if(compareExp.getType() == ComparisonExpressionType.GREATER_THAN_OR_EQUAL){ comparison = QueryBuilders.rangeQuery(field).from(value); }else if(compareExp.getType() == ComparisonExpressionType.LESS_THAN_OR_EQUAL){ comparison = QueryBuilders.rangeQuery(field).to(value); }else if(compareExp.getType() == ComparisonExpressionType.GREATER_THAN){ comparison = QueryBuilders.rangeQuery(field).gt(value); }else if(compareExp.getType() == ComparisonExpressionType.LESS_THAN){ comparison = QueryBuilders.rangeQuery(field).lt(value); }else if(compareExp.getType() == ComparisonExpressionType.NOT_EQUAL){ if(field.equals(Heading.ID)){ state.addException("Matching document _id using '<>' is not supported");
@Override protected RowExpression visitComparisonExpression(ComparisonExpression node, Void context) { RowExpression left = process(node.getLeft(), context); RowExpression right = process(node.getRight(), context); return call( comparisonExpressionSignature(node.getType(), left.getType(), right.getType()), BOOLEAN, left, right); }
@Override protected Object visitComparisonExpression(ComparisonExpression node, Object context) { ComparisonExpression.Type type = node.getType(); Object left = process(node.getLeft(), context); if (left == null && type != ComparisonExpression.Type.IS_DISTINCT_FROM) { return null; } Object right = process(node.getRight(), context); if (type == ComparisonExpression.Type.IS_DISTINCT_FROM) { if (left == null && right == null) { return false; } else if (left == null || right == null) { return true; } } else if (right == null) { return null; } if (hasUnresolvedValue(left, right)) { return new ComparisonExpression(type, toExpression(left, expressionTypes.get(node.getLeft())), toExpression(right, expressionTypes.get(node.getRight()))); } if (type == ComparisonExpression.Type.IS_DISTINCT_FROM) { type = ComparisonExpression.Type.NOT_EQUAL; } return invokeOperator(OperatorType.valueOf(type.name()), types(node.getLeft(), node.getRight()), ImmutableList.of(left, right)); }
@Override public Expression rewriteNotExpression(NotExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { if (node.getValue() instanceof LogicalBinaryExpression) { LogicalBinaryExpression child = (LogicalBinaryExpression) node.getValue(); List<Expression> predicates = extractPredicates(child); List<Expression> negatedPredicates = predicates.stream() .map(predicate -> treeRewriter.rewrite((Expression) new NotExpression(predicate), context)) .collect(toImmutableList()); return combinePredicates(child.getType().flip(), negatedPredicates); } else if (node.getValue() instanceof ComparisonExpression && ((ComparisonExpression) node.getValue()).getType() != IS_DISTINCT_FROM) { ComparisonExpression child = (ComparisonExpression) node.getValue(); return new ComparisonExpression( child.getType().negate(), treeRewriter.rewrite(child.getLeft(), context), treeRewriter.rewrite(child.getRight(), context)); } else if (node.getValue() instanceof NotExpression) { NotExpression child = (NotExpression) node.getValue(); return treeRewriter.rewrite(child.getValue(), context); } return new NotExpression(treeRewriter.rewrite(node.getValue(), context)); } }