Example #1
0
/*
This method evaluates the Field using the first and second value
and returns the result value. If the second operand type is a
missing return a missing value. If it is a string, and the
field is case insensitive, then convert the second operand to
lower case, range through the fields of the first and compare,
each field with the second. When equal, return the value. If
the field is case sensitive, use the Field method to directly
access the field and return it. For all other types, if the
first operand expression is missing, return missing, else return
null.
*/
func (this *Field) Apply(context Context, first, second value.Value) (value.Value, error) {
	switch second.Type() {
	case value.STRING:
		s := second.Actual().(string)
		v, ok := first.Field(s)

		if !ok && this.caseInsensitive {
			s = strings.ToLower(s)
			fields := first.Fields()
			for f, val := range fields {
				if s == strings.ToLower(f) {
					return value.NewValue(val), nil
				}
			}
		}

		return v, nil
	case value.MISSING:
		return value.MISSING_VALUE, nil
	default:
		if first.Type() == value.MISSING {
			return value.MISSING_VALUE, nil
		} else {
			return value.NULL_VALUE, nil
		}
	}
}
Example #2
0
/*
Aggregate distinct intermediate results and return them.
If no partial result exists(its value is a null) return the
cumulative value. If the cumulative input value is null,
return the partial value. Get the input partial and cumulative
sets and add the smaller set to the bigger. Return this set.
*/
func cumulateSets(part, cumulative value.Value) (value.Value, error) {
	if part.Type() == value.NULL {
		return cumulative, nil
	} else if cumulative.Type() == value.NULL {
		return part, nil
	}

	pset, e := getSet(part)
	if e != nil {
		return nil, e
	}

	cset, e := getSet(cumulative)
	if e != nil {
		return nil, e
	}

	// Add smaller set to bigger
	var smaller, bigger *value.Set
	if pset.Len() <= cset.Len() {
		smaller, bigger = pset, cset
	} else {
		smaller, bigger = cset, pset
	}

	for _, v := range smaller.Values() {
		bigger.Add(v)
	}

	cumulative.(value.AnnotatedValue).SetAttachment("set", bigger)
	return cumulative, nil
}
Example #3
0
/*
If the input argument type is greater than NULL, we return the complement
of its Truth() method's return type. If Null or missing return the argument
itself.
*/
func (this *Not) Apply(context Context, arg value.Value) (value.Value, error) {
	if arg.Type() > value.NULL {
		return value.NewValue(!arg.Truth()), nil
	} else {
		return arg, nil
	}
}
Example #4
0
/*
This method returns an object value. The input of types
missing, null and object return themselves. For all other
values, return an _EMPTY_OBJECT value.
*/
func (this *ToObject) Apply(context Context, arg value.Value) (value.Value, error) {
	switch arg.Type() {
	case value.MISSING, value.NULL, value.OBJECT:
		return arg, nil
	}

	return _EMPTY_OBJECT, nil
}
Example #5
0
/*
This method evaluates the less than equal to condition and
returns a value representing if the two operands satisfy the
condition or not. If either of the input operands are
missing, return missing value, and if they are null, then
return null value. For all other types call the Collate
method and check if it is less than equal to 0 for the
two values. If it is, then return true.
*/
func (this *LE) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING || second.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() == value.NULL || second.Type() == value.NULL {
		return value.NULL_VALUE, nil
	}

	return value.NewValue(first.Collate(second) <= 0), nil
}
Example #6
0
/*
This method takes in an operand value and context and returns a value.
If the type of operand is missing then return it. Call MarshalJSON
to get the bytes, and then use Go's encoding/base64 package to
encode the bytes to string. Create a newValue using the string and
return it.
*/
func (this *Base64) Apply(context Context, operand value.Value) (value.Value, error) {
	if operand.Type() == value.MISSING {
		return operand, nil
	}

	bytes, _ := operand.MarshalJSON() // Ignore errors from BINARY values
	str := base64.StdEncoding.EncodeToString(bytes)
	return value.NewValue(str), nil
}
Example #7
0
/*
It returns a string based on the input expr value. Values
missing, null and strings return themselves. False, true
(boolean) and numbers return their string representation.
This is done using the Sprint method defined in fmt for Go.
All other values map to null.
*/
func (this *ToString) Apply(context Context, arg value.Value) (value.Value, error) {
	switch arg.Type() {
	case value.MISSING, value.NULL, value.STRING:
		return arg, nil
	case value.BOOLEAN, value.NUMBER:
		return value.NewValue(fmt.Sprint(arg.Actual())), nil
	default:
		return value.NULL_VALUE, nil
	}
}
Example #8
0
/*
Evaluates the Is Null comparison operation for expressions.
If the type of input argument is a null value, return true,
if missing return a missing value and by for all other types
return a false value.
*/
func (this *IsNull) Apply(context Context, arg value.Value) (value.Value, error) {
	switch arg.Type() {
	case value.NULL:
		return value.TRUE_VALUE, nil
	case value.MISSING:
		return value.MISSING_VALUE, nil
	default:
		return value.FALSE_VALUE, nil
	}
}
Example #9
0
func (this *ParentScan) RunOnce(context *Context, parent value.Value) {
	this.once.Do(func() {
		defer context.Recover()       // Recover from any panic
		defer close(this.itemChannel) // Broadcast that I have stopped
		defer this.notify()           // Notify that I have stopped

		// Shallow copy of the parent includes
		// correlated and annotated aspects
		this.sendItem(parent.Copy().(value.AnnotatedValue))
	})
}
Example #10
0
/*
Aggregate input partial values into cumulative result value.
If partial result is null return the current cumulative value,
and if the cumulative result is null, return the partial value.
For non null partial and cumulative values, call Collate and
return the smaller value depending on the N1QL collation order.
*/
func (this *Min) cumulatePart(part, cumulative value.Value, context Context) (value.Value, error) {
	if part == value.NULL_VALUE {
		return cumulative, nil
	} else if cumulative == value.NULL_VALUE {
		return part, nil
	} else if part.Collate(cumulative) < 0 {
		return part, nil
	} else {
		return cumulative, nil
	}
}
Example #11
0
/*
Aggregates input data by evaluating operands. For missing
item values, return the input value itself. Call
cumulatePart to compute the intermediate aggregate value
and return it.
*/
func (this *ArrayAgg) CumulateInitial(item, cumulative value.Value, context Context) (value.Value, error) {
	item, e := this.Operand().Evaluate(item, context)
	if e != nil {
		return nil, e
	}

	if item.Type() <= value.MISSING {
		return cumulative, nil
	}

	return this.cumulatePart(value.NewValue([]interface{}{item}), cumulative, context)
}
Example #12
0
func (this *Sum) CumulateInitial(item, cumulative value.Value, context Context) (value.Value, error) {
	item, e := this.Operand().Evaluate(item, context)
	if e != nil {
		return nil, e
	}

	if item.Type() != value.NUMBER {
		return cumulative, nil
	}

	return this.cumulatePart(item, cumulative, context)
}
Example #13
0
/*
Aggregates input data by evaluating operands.For all
values other than Number, return the input value itself.
Call setAdd to compute the intermediate aggregate value
and return it.
*/
func (this *AvgDistinct) CumulateInitial(item, cumulative value.Value, context Context) (value.Value, error) {
	item, e := this.Operand().Evaluate(item, context)
	if e != nil {
		return nil, e
	}

	if item.Type() != value.NUMBER {
		return cumulative, nil
	}

	return setAdd(item, cumulative)
}
Example #14
0
/*
This method takes in an operand value and context and returns a value.
If the type of operand is missing then return it. If the operand
type is AnnotatedValue then we call NewValue using the GetAttachment
method on the operand with input string meta. In the event the there
is no attachment present, the default case is to return a NULL value.
*/
func (this *Meta) Apply(context Context, operand value.Value) (value.Value, error) {
	if operand.Type() == value.MISSING {
		return operand, nil
	}

	switch operand := operand.(type) {
	case value.AnnotatedValue:
		return value.NewValue(operand.GetAttachment("meta")), nil
	default:
		return value.NULL_VALUE, nil
	}
}
Example #15
0
func (this *Order) Less(i, j int) bool {
	v1 := this.values[i]
	v2 := this.values[j]

	var ev1, ev2 value.Value
	var c int
	var e error

	for i, term := range this.plan.Terms() {
		s := strconv.Itoa(i)

		sv1 := v1.GetAttachment(s)
		switch sv1 := sv1.(type) {
		case value.Value:
			ev1 = sv1
		default:
			ev1, e = term.Expression().Evaluate(v1, this.context)
			if e != nil {
				this.context.Error(errors.NewError(e, "Error evaluating ORDER BY."))
				return false
			}

			v1.SetAttachment(s, ev1)
		}

		sv2 := v2.GetAttachment(s)
		switch sv2 := sv2.(type) {
		case value.Value:
			ev2 = sv2
		default:
			ev2, e = term.Expression().Evaluate(v2, this.context)
			if e != nil {
				this.context.Error(errors.NewError(e, "Error evaluating ORDER BY."))
				return false
			}

			v2.SetAttachment(s, ev2)
		}

		c = ev1.Collate(ev2)

		if c == 0 {
			continue
		} else if term.Descending() {
			return c > 0
		} else {
			return c < 0
		}
	}

	return false
}
Example #16
0
/*
This method checks to see if the values of the two input
expressions are equal, and if true then returns a null
value. If not it returns the first input value. Use the
Equals method for the two values to determine equality.
*/
func (this *NullIf) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING || second.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() == value.NULL || second.Type() == value.NULL {
		return value.NULL_VALUE, nil
	}

	if first.Equals(second) {
		return value.NULL_VALUE, nil
	} else {
		return first, nil
	}
}
Example #17
0
/*
Aggregates input data by evaluating operands. For all
values other than Number, return the input value itself. Call
cumulatePart to compute the intermediate aggregate value
and return it.
*/
func (this *Avg) CumulateInitial(item, cumulative value.Value, context Context) (value.Value, error) {
	item, e := this.Operand().Evaluate(item, context)
	if e != nil {
		return nil, e
	}

	if item.Type() != value.NUMBER {
		return cumulative, nil
	}

	part := value.NewValue(map[string]interface{}{"sum": item.Actual(), "count": 1})
	return this.cumulatePart(part, cumulative, context)
}
Example #18
0
/*
Retrieve the set for annotated values. If the attachment type
is not a set, then throw an invalid distinct set error and
return.
*/
func getSet(item value.Value) (*value.Set, error) {
	switch item := item.(type) {
	case value.AnnotatedValue:
		ps := item.GetAttachment("set")
		switch ps := ps.(type) {
		case *value.Set:
			return ps, nil
		default:
			return nil, fmt.Errorf("Invalid DISTINCT set %v of type %T.", ps, ps)
		}
	default:
		return nil, fmt.Errorf("Invalid DISTINCT %v of type %T.", item, item)
	}
}
Example #19
0
/*
Aggregates input data by evaluating operands. For missing and
null values return the input value itself. Call cumulatePart
to compute the intermediate aggregate value and return it.
*/
func (this *Count) CumulateInitial(item, cumulative value.Value, context Context) (value.Value, error) {
	if this.Operand() != nil {
		item, e := this.Operand().Evaluate(item, context)
		if e != nil {
			return nil, e
		}

		if item.Type() <= value.NULL {
			return cumulative, nil
		}
	}

	return this.cumulatePart(value.ONE_VALUE, cumulative, context)

}
Example #20
0
/*
Aggregate input partial values into cumulative result number value.
If the partial and current cumulative result are both float64
numbers, add them and return.
*/
func (this *Count) cumulatePart(part, cumulative value.Value, context Context) (value.Value, error) {
	actual := part.Actual()
	switch actual := actual.(type) {
	case float64:
		count := cumulative.Actual()
		switch count := count.(type) {
		case float64:
			return value.NewValue(count + actual), nil
		default:
			return nil, fmt.Errorf("Invalid COUNT %v of type %T.", count, count)
		}
	default:
		return nil, fmt.Errorf("Invalid partial COUNT %v of type %T.", actual, actual)
	}
}
Example #21
0
/*
Directly call the evaluate method for aggregate functions and
passe in the receiver, current item and current context, for
count with an input expression operand. For a count with no
operands (count (*)), get the count from the attachment and
then evaluate.
*/
func (this *Count) Evaluate(item value.Value, context expression.Context) (result value.Value, e error) {
	if this.Operand() != nil {
		return this.evaluate(this, item, context)
	}

	// Full keyspace count is short-circuited
	switch item := item.(type) {
	case value.AnnotatedValue:
		count := item.GetAttachment("count")
		if count != nil {
			return value.NewValue(count), nil
		}
	}

	return this.evaluate(this, item, context)
}
Example #22
0
/*
IN evaluates to TRUE if the right-hand-side first value is an array
and directly contains the left-hand-side second value. If either
of the input operands are missing, return missing value, and
if the second is not an array return null. Range over the elements of the
array and check if any element is equal to the first value, return true.
For all other cases, return false.
*/
func (this *In) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING || second.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if second.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	}

	sa := second.Actual().([]interface{})
	for _, s := range sa {
		if first.Equals(value.NewValue(s)) {
			return value.TRUE_VALUE, nil
		}
	}

	return value.FALSE_VALUE, nil
}
Example #23
0
/*
This method ranges through the array and returns the position
of the second value in the array (first value). If the input
values are of type missing return a missing value, and for all
non array values return null. If not found then return -1.
*/
func (this *ArrayPosition) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING || second.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	}

	fa := first.Actual().([]interface{})
	for i, f := range fa {
		if second.Equals(value.NewValue(f)) {
			return value.NewValue(float64(i)), nil
		}
	}

	return value.NewValue(float64(-1)), nil
}
Example #24
0
/*
This method evaluates the input value and returns the length
based on its type. If the input argument is a missing then
return a missing value. Convert it to a valid Go type. If
it is a string slice of interfaces or object then return
its length cast as a number float64. By default return a
null value.
*/
func (this *PolyLength) Apply(context Context, arg value.Value) (value.Value, error) {
	if arg.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	}

	switch oa := arg.Actual().(type) {
	case string:
		return value.NewValue(float64(len(oa))), nil
	case []interface{}:
		return value.NewValue(float64(len(oa))), nil
	case map[string]interface{}:
		return value.NewValue(float64(len(oa))), nil
	default:
		return value.NULL_VALUE, nil
	}
}
Example #25
0
/*
This method Evaluates the slive using the input args depending on the
number of args. The form source-expr [ start : end ] is called array
slicing. It returns a new array containing a subset of the source,
containing the elements from position start to end-1. The element at
start is included, while the element at end is not. If end is omitted,
all elements from start to the end of the source array are included.
The source is the first argument. If it is missing return a missing
value. The first argument represents start. If missing return missing.
If there are more than 2 arguments, then an end is specified. Check
its type, and if missing return missing value. Since start and end
represent indices, make sure they are integer values and if not return
null value. Call Slice or Slice tail on the source (depending on whether
end is specified) to return the specified slice.
*/
func (this *Slice) Apply(context Context, args ...value.Value) (rv value.Value, re error) {
	source := args[0]
	if source.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	}

	start := args[1]
	if start.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	}

	ev := -1
	var end value.Value
	if len(args) >= 3 {
		end = args[2]
		if end.Type() == value.MISSING {
			return value.MISSING_VALUE, nil
		}

		ea, ok := end.Actual().(float64)
		if !ok || ea != math.Trunc(ea) {
			return value.NULL_VALUE, nil
		}

		ev = int(ea)
	}

	sa, ok := start.Actual().(float64)
	if !ok || sa != math.Trunc(sa) {
		return value.NULL_VALUE, nil
	}

	if source.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	}

	if end != nil {
		rv, _ = source.Slice(int(sa), ev)
	} else {
		rv, _ = source.SliceTail(int(sa))
	}

	return
}
Example #26
0
/*
This method evaluates the array append function. If either
of the input argument types are missing, or not an array return
a missing and null value respectively. Use the append method
to append the second expression to the first expression. Return
the new array.
*/
func (this *ArrayAppend) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING || second.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	}

	f := first.Actual().([]interface{})
	ra := append(f, second)
	return value.NewValue(ra), nil
}
Example #27
0
/*
If the input argument type is a missing, null or boolean value, it returns
itself. Check to see the Go type of the input. If it is float64, then
use the isNaN(returns if input is not a number) method defined in the math
package and make sure that it returns false and the number is not 0, return
true. If type is string, slice of interface or map which are are not empty,
return true. All other input types return NULLs.
*/
func (this *ToBoolean) Apply(context Context, arg value.Value) (value.Value, error) {
	switch arg.Type() {
	case value.MISSING, value.NULL, value.BOOLEAN:
		return arg, nil
	default:
		switch a := arg.Actual().(type) {
		case float64:
			return value.NewValue(!math.IsNaN(a) && a != 0), nil
		case string:
			return value.NewValue(len(a) > 0), nil
		case []interface{}:
			return value.NewValue(len(a) > 0), nil
		case map[string]interface{}:
			return value.NewValue(len(a) > 0), nil
		default:
			return value.NULL_VALUE, nil
		}
	}
}
Example #28
0
/*
This method removes all the occurences of the second value from the
first array value. If the first value is MISSING or not an array,
then return missing or a null value. If the second value is missing
then return the first array value itself. Range through the array
and and check for the value, append all unequal values. Return the
final array.
*/
func (this *ArrayRemove) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	} else if second.Type() == value.MISSING {
		return first, nil
	}

	fa := first.Actual().([]interface{})
	ra := make([]interface{}, 0, len(fa))
	for _, f := range fa {
		if !second.Equals(value.NewValue(f)) {
			ra = append(ra, f)
		}
	}

	return value.NewValue(ra), nil
}
Example #29
0
/*
Compute the Final. Compute the sum and the count. If these
arent numbers throw an error. Compute the avg as sum/count.
Check for divide by zero, and return a NULL value if true.
*/
func (this *Avg) ComputeFinal(cumulative value.Value, context Context) (value.Value, error) {
	if cumulative == value.NULL_VALUE {
		return cumulative, nil
	}

	sum, _ := cumulative.Field("sum")
	count, _ := cumulative.Field("count")

	if sum.Type() != value.NUMBER || count.Type() != value.NUMBER {
		return nil, fmt.Errorf("Missing or invalid sum or count in AVG: %v, %v.",
			sum.Actual(), count.Actual())
	}

	if count.Actual().(float64) > 0.0 {
		return value.NewValue(sum.Actual().(float64) / count.Actual().(float64)), nil
	} else {
		return value.NULL_VALUE, nil
	}
}
Example #30
0
/*
This method appends the value into the array if it isnt
present. Range over the array and check if the value exists.
If it does return the array as is. If either of the input
argument types are missing, or not an array return a missing
and null value respectively.
*/
func (this *ArrayPut) Apply(context Context, first, second value.Value) (value.Value, error) {
	if first.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.ARRAY {
		return value.NULL_VALUE, nil
	} else if second.Type() == value.MISSING {
		return first, nil
	}

	f := first.Actual().([]interface{})
	for _, a := range f {
		v := value.NewValue(a)
		if second.Equals(v) {
			return first, nil
		}
	}

	ra := append(f, second)
	return value.NewValue(ra), nil
}