Ejemplo n.º 1
0
/*

Constrain the WHERE condition to reflect the aggregate query. For
example:

SELECT AVG(v) FROM widget w;

is rewritten as:

SELECT AVG(v) FROM widget w WHERE v IS NOT NULL;

This enables the query to use an index on v.

*/
func constrainAggregate(cond expression.Expression, aggs map[string]algebra.Aggregate) expression.Expression {
	var first expression.Expression
	for _, agg := range aggs {
		if first == nil {
			first = agg.Operand()
			if first == nil {
				return cond
			}

			continue
		}

		op := agg.Operand()
		if op == nil || !first.EquivalentTo(op) {
			return cond
		}
	}

	if first == nil {
		return cond
	}

	var constraint expression.Expression = expression.NewIsNotNull(first)
	if cond != nil {
		constraint = expression.NewAnd(cond, constraint)
	}

	return constraint
}
Ejemplo n.º 2
0
func newSubsetDefault(expr expression.Expression) *subsetDefault {
	rv := &subsetDefault{}
	rv.test = func(expr2 expression.Expression) (bool, error) {
		value2 := expr2.Value()
		if value2 != nil {
			return value2.Truth(), nil
		}

		if expr.EquivalentTo(expr2) {
			return true, nil
		}

		switch expr2 := expr2.(type) {
		case *expression.And:
			for _, op := range expr2.Operands() {
				if !SubsetOf(expr, op) {
					return false, nil
				}
			}

			return true, nil
		case *expression.Or:
			for _, op := range expr2.Operands() {
				if SubsetOf(expr, op) {
					return true, nil
				}
			}

			return false, nil
		case *expression.IsNotMissing:
			return expr.PropagatesMissing() &&
				expr.DependsOn(expr2.Operand()), nil
		case *expression.IsNotNull:
			return expr.PropagatesNull() &&
				expr.DependsOn(expr2.Operand()), nil
		case *expression.IsValued:
			return expr.PropagatesNull() &&
				expr.DependsOn(expr2.Operand()), nil
		}

		return false, nil
	}

	return rv
}
Ejemplo n.º 3
0
func LessThanOrEquals(expr1, expr2 expression.Expression) bool {
	return LessThan(expr1, expr2) || expr1.EquivalentTo(expr2)
}