Skip to content

Commit

Permalink
Merge pull request #5485 from evolvedbinary/hotfix/number-comparisons-3
Browse files Browse the repository at this point in the history
Fix further issues with number types
  • Loading branch information
reinhapa authored Oct 11, 2024
2 parents 3e9f799 + 52fc06d commit 11dc839
Show file tree
Hide file tree
Showing 10 changed files with 1,997 additions and 135 deletions.
6 changes: 6 additions & 0 deletions exist-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderParser.java</exclude>
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderSerializer.java</exclude>
<exclude>src/main/java/org/exist/xquery/Cardinality.java</exclude>
<exclude>src/test/java/org/exist/xquery/CastExpressionTest.java</exclude>
<exclude>src/test/java/org/exist/xquery/ImportModuleTest.java</exclude>
<exclude>src/main/java/org/exist/xquery/Materializable.java</exclude>
<exclude>src/test/java/org/exist/xquery/XQueryContextAttributesTest.java</exclude>
Expand All @@ -792,6 +793,8 @@
<exclude>src/test/java/org/exist/xquery/value/SubSequenceRangeTest.java</exclude>
<exclude>src/test/java/org/exist/xquery/value/SubSequenceTest.java</exclude>
<exclude>src/test/xquery/binary-value.xqm</exclude>
<exclude>src/test/xquery/instance-of.xqm</exclude>
<exclude>src/test/xquery/operator-mapping.xqm</exclude>
<exclude>src/test/xquery/order.xqm</exclude>
<exclude>src/test/xquery/type-promotion.xqm</exclude>
<exclude>src/test/xquery/xqsuite/xqsuite-assertions-dynamic.xqm</exclude>
Expand Down Expand Up @@ -936,6 +939,7 @@ The original license statement is also included below.]]></preamble>
<include>src/main/java/org/exist/xmlrpc/ACEAiderParser.java</include>
<include>src/main/java/org/exist/xmlrpc/ACEAiderSerializer.java</include>
<include>src/main/java/org/exist/xquery/Cardinality.java</include>
<exclude>src/test/java/org/exist/xquery/CastExpressionTest.java</exclude>
<include>src/test/java/org/exist/xquery/ImportModuleTest.java</include>
<include>src/main/java/org/exist/xquery/Materializable.java</include>
<include>src/test/java/org/exist/xquery/XQueryContextAttributesTest.java</include>
Expand All @@ -956,6 +960,8 @@ The original license statement is also included below.]]></preamble>
<include>src/test/java/org/exist/xquery/value/SubSequenceRangeTest.java</include>
<include>src/test/java/org/exist/xquery/value/SubSequenceTest.java</include>
<include>src/test/xquery/binary-value.xqm</include>
<include>src/test/xquery/instance-of.xqm</include>
<include>src/test/xquery/operator-mapping.xqm</include>
<include>src/test/xquery/order.xqm</include>
<include>src/test/xquery/type-promotion.xqm</include>
<include>src/test/xquery/xqsuite/xqsuite-assertions-dynamic.xqm</include>
Expand Down
223 changes: 105 additions & 118 deletions exist-core/src/main/java/org/exist/xquery/CastExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,173 +21,160 @@
*/
package org.exist.xquery;

import org.exist.dom.persistent.DocumentSet;
import org.exist.dom.QName;
import org.exist.dom.persistent.DocumentSet;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.*;

import java.util.ArrayList;
import java.util.List;

/**
* CastExpression represents cast expressions as well as all type
* constructors.
*
* CastExpression represents cast expressions AND all type constructors.
*
* @author wolf
*/
public class CastExpression extends AbstractExpression {


private final int requiredType;
private final Cardinality cardinality;
private Expression expression;
private Cardinality cardinality;
private final int requiredType;

/**
* Constructor. When calling {@link #eval(Sequence, Item)}
* the passed expression will be cast into the required type and cardinality.
*
* @param context current context
* @param expr expression to cast
* Constructor. When calling {@link #eval(Sequence, Item)}
* the passed expression will be cast into the required type and cardinality.
*
* @param context current context
* @param expr expression to cast
* @param requiredType the {@link Type} expected
* @param cardinality the {@link Cardinality} expected
*/
public CastExpression(final XQueryContext context, final Expression expr, final int requiredType, final Cardinality cardinality) {
super(context);
this.requiredType = requiredType;
this.cardinality = cardinality;
* @param cardinality the {@link Cardinality} expected
*/
public CastExpression(final XQueryContext context, final Expression expr, final int requiredType, final Cardinality cardinality) {
super(context);
this.requiredType = requiredType;
this.cardinality = cardinality;
setExpression(expr);
}
}

protected Expression getInnerExpression() {
return expression;
}
protected Expression getInnerExpression() {
return expression;
}

public void setExpression(Expression expr) {
public void setExpression(final Expression expr) {
this.expression = expr;
}

@Override
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
contextInfo.setParent(this);
@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
contextInfo.setParent(this);
expression.analyze(contextInfo);
contextInfo.setStaticReturnType(requiredType);
}

@Override
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {

public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
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());}
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());
}
}
//Should be handled by the parser

// Should be handled by the parser
if (requiredType == Type.ANY_ATOMIC_TYPE || (requiredType == Type.NOTATION && expression.returnsType() != Type.NOTATION)) {
throw new XPathException(this, ErrorCodes.XPST0080, "cannot cast to " +
Type.getTypeName(requiredType));
throw new XPathException(this, ErrorCodes.XPST0080, "cannot cast to " + Type.getTypeName(requiredType));
}

if (requiredType == Type.ANY_SIMPLE_TYPE || expression.returnsType() == Type.ANY_SIMPLE_TYPE || requiredType == Type.UNTYPED || expression.returnsType() == Type.UNTYPED) {
throw new XPathException(this, ErrorCodes.XPST0051, "cannot cast to " +
Type.getTypeName(requiredType));
throw new XPathException(this, ErrorCodes.XPST0051, "cannot cast to " + Type.getTypeName(requiredType));
}

Sequence result;
final Sequence seq = Atomize.atomize(expression.eval(contextSequence, contextItem));
if (seq.isEmpty()) {
if (cardinality.atLeastOne())
{throw new XPathException(this, "Type error: empty sequence is not allowed here");}
else
{result = Sequence.EMPTY_SEQUENCE;}
} else {
final Sequence result;
final Sequence seq = Atomize.atomize(expression.eval(contextSequence, contextItem));
if (seq.isEmpty()) {
if (cardinality.atLeastOne()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: empty sequence is not allowed here");
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else if (seq.hasMany() && Type.subTypeOf(requiredType, Type.ANY_ATOMIC_TYPE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "cardinality error: sequence with more than one item is not allowed here");
} else {
final Item item = seq.itemAt(0);

if (seq.hasMany() && Type.subTypeOf(requiredType, Type.ANY_ATOMIC_TYPE))
{throw new XPathException(this,
ErrorCodes.XPTY0004,
"cardinality error: sequence with more than one item is not allowed here");}
try {
// casting to QName needs special treatment
if(requiredType == Type.QNAME) {
if (item.getType() == Type.QNAME)
{result = item.toSequence();}

else if(item.getType() == Type.ANY_ATOMIC_TYPE || Type.subTypeOf(item.getType(), Type.STRING)) {
result = new QNameValue(this, context, item.getStringValue());

} else {
throw new XPathException(this,
ErrorCodes.XPTY0004,
"Cannot cast " + Type.getTypeName(item.getType()) + " to xs:QName");
}
} else
{result = item.convertTo(requiredType);}

} catch(final XPathException e) {
e.setLocation(e.getLine(), e.getColumn());
throw e;
// Casting to QName needs special treatment
if (requiredType == Type.QNAME) {
if (item.getType() == Type.QNAME) {
result = item.toSequence();
} else if (item.getType() == Type.ANY_ATOMIC_TYPE || Type.subTypeOf(item.getType(), Type.STRING)) {
result = new QNameValue(this, context, item.getStringValue());

} else {
throw new XPathException(this, ErrorCodes.XPTY0004, "Cannot cast " + Type.getTypeName(item.getType()) + " to xs:QName");
}
} else {
result = item.convertTo(requiredType);
}
}

if (context.getProfiler().isEnabled())
{context.getProfiler().end(this, "", result);}

return result;
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}

return result;
}

/* (non-Javadoc)
* @see org.exist.xquery.Expression#dump(org.exist.xquery.util.ExpressionDumper)
*/
public void dump(ExpressionDumper dumper) {
@Override
public void dump(final ExpressionDumper dumper) {
expression.dump(dumper);
dumper.display(" cast as ");
dumper.display(Type.getTypeName(requiredType));
}


@Override
public String toString() {
return expression.toString() + " cast as " + Type.getTypeName(requiredType);
}

/* (non-Javadoc)
* @see org.exist.xquery.Expression#returnsType()
*/
public int returnsType() {
return requiredType;
}

/* (non-Javadoc)
* @see org.exist.xquery.AbstractExpression#getDependencies()
*/
public int getDependencies() {

@Override
public int returnsType() {
return requiredType;
}

@Override
public int getDependencies() {
return expression.getDependencies() | Dependency.CONTEXT_ITEM;
}
@Override
public Cardinality getCardinality() {
return Cardinality.ZERO_OR_ONE;
}
public void setContextDocSet(DocumentSet contextSet) {
super.setContextDocSet(contextSet);
expression.setContextDocSet(contextSet);
}
/* (non-Javadoc)
* @see org.exist.xquery.AbstractExpression#resetState()
*/
public void resetState(boolean postOptimization) {
super.resetState(postOptimization);
expression.resetState(postOptimization);
}

public void accept(ExpressionVisitor visitor) {
visitor.visitCastExpr(this);
}
}

@Override
public Cardinality getCardinality() {
return Cardinality.ZERO_OR_ONE;
}

@Override
public void setContextDocSet(final DocumentSet contextSet) {
super.setContextDocSet(contextSet);
expression.setContextDocSet(contextSet);
}

@Override
public void resetState(final boolean postOptimization) {
super.resetState(postOptimization);
expression.resetState(postOptimization);
}

@Override
public void accept(final ExpressionVisitor visitor) {
visitor.visitCastExpr(this);
}

public Function toFunction() throws XPathException {
final String typeName = Type.getTypeName(CastExpression.this.requiredType);
try {
try {
final QName qname = QName.parse(context, typeName);
final FunctionSignature signature = new FunctionSignature(qname);
final SequenceType argType = new SequenceType(Type.ANY_ATOMIC_TYPE, Cardinality.ZERO_OR_ONE);
Expand All @@ -211,12 +198,12 @@ protected FunctionWrapper(final CastExpression castExpression, final FunctionSig
}

@Override
public void setArguments(final List<Expression> arguments) throws XPathException {
public void setArguments(final List<Expression> arguments) {
castExpression.setExpression(arguments.get(0));
}

@Override
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
return castExpression.eval(contextSequence, null);
}
}
Expand Down
Loading

0 comments on commit 11dc839

Please sign in to comment.