use of org.exist.xquery.value.NumericValue in project exist by eXist-db.
the class FunAbs method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result;
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final Item item = seq.itemAt(0);
NumericValue value;
if (item instanceof NumericValue) {
value = (NumericValue) item;
} else {
value = (NumericValue) item.convertTo(Type.NUMBER);
}
result = value.abs();
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.NumericValue in project exist by eXist-db.
the class FunAvg method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result;
final Sequence inner = getArgument(0).eval(contextSequence, contextItem);
if (inner.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final SequenceIterator iter = inner.iterate();
Item item = iter.nextItem();
AtomicValue value = item.atomize();
// Any values of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, Type.getTypeName(value.getType()) + "(" + value + ") " + "can not be an operand in a sum", value);
}
// Set the first value
ComputableValue sum = (ComputableValue) value;
while (iter.hasNext()) {
item = iter.nextItem();
value = item.atomize();
// Any value of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, "" + Type.getTypeName(value.getType()) + "(" + value + ") can not be an operand in a sum", value);
}
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isInfinite()) {
gotInfinity = true;
}
if (((NumericValue) value).isNaN()) {
sum = DoubleValue.NaN;
break;
}
}
try {
sum = (ComputableValue) sum.promote(value);
// Aggregate next values
sum = sum.plus((ComputableValue) value);
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0006, e.getMessage());
}
}
result = sum.div(new IntegerValue(inner.getItemCount()));
}
if (!gotInfinity) {
if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
// Throw an overflow exception here since we get an infinity
// whereas is hasn't been provided by the sequence
// TODO ? -pb
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.NumericValue in project exist by eXist-db.
the class FunDistinctValues method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
final Collator collator = getCollator(contextSequence, contextItem, 2);
final TreeSet<AtomicValue> set = new TreeSet<>(new ValueComparator(collator));
final ValueSequence result = new ValueSequence();
Item item;
AtomicValue value;
boolean hasAlreadyNaN = false;
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
value = item.atomize();
if (!set.contains(value)) {
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isNaN()) {
// contains multiple NaN values a single NaN is returned.
if (!hasAlreadyNaN) {
set.add(value);
result.add(value);
hasAlreadyNaN = true;
}
} else {
set.add(value);
result.add(value);
}
} else {
set.add(value);
result.add(value);
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.NumericValue in project exist by eXist-db.
the class RangeIndexConfigAttributeCondition method find.
@Override
public boolean find(Predicate predicate) {
final Expression inner = this.getInnerExpression(predicate);
Operator operator;
Expression lhe;
Expression rhe;
// get the type of the expression inside the predicate and determine right and left hand arguments
if (inner instanceof GeneralComparison) {
final GeneralComparison comparison = (GeneralComparison) inner;
operator = RangeQueryRewriter.getOperator(inner);
lhe = comparison.getLeft();
rhe = comparison.getRight();
} else if (inner instanceof InternalFunctionCall) {
// calls to matches() will not have been rewritten to a comparison, so check for function call
final Function func = ((InternalFunctionCall) inner).getFunction();
if (func.isCalledAs("matches")) {
operator = Operator.MATCH;
lhe = func.getArgument(0);
rhe = func.getArgument(1);
lhe = unwrapSubExpression(lhe);
rhe = unwrapSubExpression(rhe);
} else {
// predicate expression cannot be parsed as condition
return false;
}
} else {
// predicate expression cannot be parsed as condition
return false;
}
// find the attribute name and value pair from the predicate to check against
// first assume attribute is on the left and value is on the right
LocationStep testStep = findLocationStep(lhe);
AtomicValue testValue = findAtomicValue(rhe);
switch(this.operator) {
case EQ:
case NE:
// check the other way around
if (testStep == null && testValue == null) {
testStep = findLocationStep(rhe);
testValue = findAtomicValue(lhe);
}
case GT:
case LT:
case GE:
case LE:
// but the operator has to be inverted
if (testStep == null && testValue == null) {
testStep = findLocationStep(rhe);
testValue = findAtomicValue(lhe);
operator = invertOrdinalOperator(operator);
}
}
if (testStep != null && testValue != null) {
final QName qname = testStep.getTest().getName();
Comparable foundValue;
Comparable requiredValue;
boolean valueTypeMatches;
try {
if (this.numericComparison) {
valueTypeMatches = testValue instanceof NumericValue;
requiredValue = this.numericValue;
foundValue = testValue.toJavaObject(Double.class);
} else {
valueTypeMatches = testValue instanceof StringValue;
if (this.caseSensitive) {
requiredValue = this.getLowercaseValue();
foundValue = testValue.getStringValue().toLowerCase();
} else {
requiredValue = this.value;
foundValue = testValue.getStringValue();
}
}
if (qname.getNameType() == ElementValue.ATTRIBUTE && operator.equals(this.operator) && qname.equals(this.attribute) && valueTypeMatches && foundValue.equals(requiredValue)) {
return true;
}
} catch (XPathException e) {
RangeIndex.LOG.error("Value conversion error when testing predicate for condition, value: {}", testValue.toString());
RangeIndex.LOG.error(e);
}
}
return false;
}
use of org.exist.xquery.value.NumericValue in project exist by eXist-db.
the class FunSum method eval.
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result;
final Sequence inner = getArgument(0).eval(contextSequence, contextItem);
if (inner.isEmpty()) {
// If $zero is not specified, then the value returned for an empty sequence is the xs:integer value 0
Sequence zero = IntegerValue.ZERO;
if (getSignature().getArgumentCount() == 2) {
zero = getArgument(1).eval(contextSequence, contextItem);
}
result = zero;
} else {
final SequenceIterator iter = inner.iterate();
Item item = iter.nextItem();
AtomicValue value = item.atomize();
value = check(value, null);
// Set the first value
ComputableValue sum = (ComputableValue) value;
while (iter.hasNext()) {
item = iter.nextItem();
value = item.atomize();
value = check(value, sum);
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isInfinite()) {
gotInfinity = true;
}
if (((NumericValue) value).isNaN()) {
sum = DoubleValue.NaN;
break;
}
}
sum = (ComputableValue) sum.promote(value);
// Aggregate next values
sum = sum.plus((ComputableValue) value);
}
result = sum;
}
if (!gotInfinity) {
if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
// Throw an overflow eception here since we get an infinity
// whereas is hasn't been provided by the sequence
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
Aggregations