Skip to content

Commit

Permalink
Merge pull request eXist-db#3409 from adamretter/hotfix/number-compar…
Browse files Browse the repository at this point in the history
…isons-2

Fix the Node/Item XDM Relationship
  • Loading branch information
dizzzz authored Oct 10, 2024
2 parents 0e45a3d + 439307e commit 4fcb864
Show file tree
Hide file tree
Showing 192 changed files with 1,121 additions and 916 deletions.
12 changes: 6 additions & 6 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQueryTree.g
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ throws XPathException
try {
QName qn= QName.parse(staticContext, t.getText());
int code= Type.getType(qn);
if (!Type.subTypeOf(code, Type.ATOMIC))
if (!Type.subTypeOf(code, Type.ANY_ATOMIC_TYPE))
throw new XPathException(t.getLine(), t.getColumn(), ErrorCodes.XPST0051, qn.toString() + " is not atomic");
type.setPrimaryType(code);
} catch (final XPathException e) {
Expand All @@ -1049,21 +1049,21 @@ throws XPathException
#(
"empty"
{
type.setPrimaryType(Type.EMPTY);
type.setPrimaryType(Type.EMPTY_SEQUENCE);
type.setCardinality(Cardinality.EMPTY_SEQUENCE);
}
)
|
#(
"empty-sequence"
{
type.setPrimaryType(Type.EMPTY);
type.setPrimaryType(Type.EMPTY_SEQUENCE);
type.setCardinality(Cardinality.EMPTY_SEQUENCE);
}
)
|
#(
FUNCTION_TEST { type.setPrimaryType(Type.FUNCTION_REFERENCE); }
FUNCTION_TEST { type.setPrimaryType(Type.FUNCTION); }
(
STAR
|
Expand All @@ -1083,7 +1083,7 @@ throws XPathException
)
|
#(
MAP_TEST { type.setPrimaryType(Type.MAP); }
MAP_TEST { type.setPrimaryType(Type.MAP_ITEM); }
(
STAR
|
Expand All @@ -1101,7 +1101,7 @@ throws XPathException
)
|
#(
ARRAY_TEST { type.setPrimaryType(Type.ARRAY); }
ARRAY_TEST { type.setPrimaryType(Type.ARRAY_ITEM); }
(
STAR
|
Expand Down
25 changes: 5 additions & 20 deletions exist-core/src/main/java/org/exist/dom/persistent/NodeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public int compareTo(final NodeProxy other) {
*/
@Override
public boolean equals(final Object other) {
if(!(other instanceof NodeProxy otherNode)) {
if(!(other instanceof final NodeProxy otherNode)) {
return false;
}

Expand Down Expand Up @@ -671,25 +671,10 @@ public String debugContext() {
// methods of interface Item
@Override
public int getType() {
return nodeType2XQuery(nodeType);
}

public static int nodeType2XQuery(final short nodeType) {
return switch (nodeType) {
case Node.ELEMENT_NODE ->
//TODO : return Type.DOCUMENT for some in-memory nodes :
//http://sourceforge.net/tracker/index.php?func=detail&aid=1730690&group_id=17691&atid=117691
//Ideally compute this when proxy is constructed
Type.ELEMENT;
case Node.ATTRIBUTE_NODE -> Type.ATTRIBUTE;
case Node.TEXT_NODE -> Type.TEXT;
case Node.CDATA_SECTION_NODE -> Type.CDATA_SECTION;
case Node.PROCESSING_INSTRUCTION_NODE -> Type.PROCESSING_INSTRUCTION;
case Node.COMMENT_NODE -> Type.COMMENT;
case Node.DOCUMENT_NODE -> Type.DOCUMENT;
//(yet) unknown type : return generic
default -> Type.NODE;
};
if (nodeType == UNKNOWN_NODE_TYPE) {
return Type.NODE;
}
return Type.fromDomNodeType(nodeType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class NativeValueIndex implements ContentLoadingObserver {
private final static Logger LOG = LogManager.getLogger(NativeValueIndex.class);

public static final String FILE_NAME = "values.dbx";
public static final short FILE_FORMAT_VERSION_ID = 14;
public static final short FILE_FORMAT_VERSION_ID = 15;
public static final String FILE_KEY_IN_CONFIG = "db-connection.values";

private static final double DEFAULT_VALUE_CACHE_GROWTH = 1.25;
Expand Down Expand Up @@ -1421,7 +1421,7 @@ private static class SimplePrefixValue extends Value {
data = new byte[len];
data[SimpleValue.OFFSET_IDX_TYPE] = IndexType.GENERIC.val;
ByteConversion.intToByte(collectionId, data, SimpleValue.OFFSET_COLLECTION_ID);
data[SimpleValue.OFFSET_VALUE] = (byte) type;
data[SimpleValue.OFFSET_VALUE] = (byte) type; // NOTE(AR) the XDM type from org.exist.xquery.value.Type will always fit within a single byte
pos = SimpleValue.OFFSET_IDX_TYPE;
}
}
Expand Down Expand Up @@ -1480,7 +1480,7 @@ private static class QNamePrefixValue extends Value {
data[QNameValue.OFFSET_QNAME_TYPE] = qname.getNameType();
ByteConversion.shortToByte(namespaceId, data, QNameValue.OFFSET_NS_URI);
ByteConversion.shortToByte(localNameId, data, QNameValue.OFFSET_LOCAL_NAME);
data[QNameValue.OFFSET_VALUE] = (byte) type;
data[QNameValue.OFFSET_VALUE] = (byte) type; // NOTE(AR) the XDM type from org.exist.xquery.value.Type will always fit within a single byte
pos = QNameValue.OFFSET_IDX_TYPE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ValueIndexFactory {
private static final int LENGTH_VALUE_TYPE = 1; // sizeof byte

public final static Indexable deserialize(final byte[] data, final int start, final int len) throws EXistException {
final int type = data[start];
final int type = data[start]; // NOTE(AR) the XDM type from org.exist.xquery.value.Type will always fit within a single byte
// TODO : improve deserialization (use static methods in the org.exist.xquery.Value package
/* xs:string */
if (Type.subTypeOf(type, Type.STRING)) {
Expand Down Expand Up @@ -134,7 +134,7 @@ public final static byte[] serialize(final Indexable value, final int offset, fi
((StringValue) value).getStringValue() :
((StringValue) value).getStringValue().toLowerCase();
final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + UTF8.encoded(val)];
data[offset] = (byte) value.getType(); // TODO: cast to byte is not safe
data[offset] = (byte) value.getType(); // NOTE(AR) the XDM type from org.exist.xquery.value.Type will always fit within a single byte
UTF8.encode(val, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
return data;
}
Expand Down Expand Up @@ -199,7 +199,7 @@ else if (value.getType() == Type.BOOLEAN) {
final BigDecimal dec = ((DecimalValue) value).getValue();
final String val = dec.toString();
final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + UTF8.encoded(val)];
data[offset] = (byte) value.getType(); // TODO: cast to byte is not safe
data[offset] = (byte) value.getType(); // NOTE(AR) the XDM type from org.exist.xquery.value.Type will always fit within a single byte
UTF8.encode(val, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ExtTestAssumptionFailedFunction(final XQueryContext context, final String
super("ext-test-assumption-failed-function",
params(
param("name", Type.STRING, "name of the test"),
optParam("error", Type.MAP, "error detail of the test")
optParam("error", Type.MAP_ITEM, "error detail of the test")
), context, parentName, notifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ExtTestErrorFunction(final XQueryContext context, final String parentName
super("ext-test-error-function",
params(
param("name", Type.STRING, "name of the test"),
optParam("error", Type.MAP, "error detail of the test. e.g. map { \"code\": $err:code, \"description\": $err:description, \"value\": $err:value, \"module\": $err:module, \"line-number\": $err:line-number, \"column-number\": $err:column-number, \"additional\": $err:additional, \"xquery-stack-trace\": $exerr:xquery-stack-trace, \"java-stack-trace\": $exerr:java-stack-trace}")
optParam("error", Type.MAP_ITEM, "error detail of the test. e.g. map { \"code\": $err:code, \"description\": $err:description, \"value\": $err:value, \"module\": $err:module, \"line-number\": $err:line-number, \"column-number\": $err:column-number, \"additional\": $err:additional, \"xquery-stack-trace\": $exerr:xquery-stack-trace, \"java-stack-trace\": $exerr:java-stack-trace}")
), context, parentName, notifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public ExtTestFailureFunction(final XQueryContext context, final String parentNa
super("ext-test-failure-function",
params(
param("name", Type.STRING, "name of the test"),
param("expected", Type.MAP, "expected result of the test"),
param("actual", Type.MAP, "actual result of the test")
param("expected", Type.MAP_ITEM, "expected result of the test"),
param("actual", Type.MAP_ITEM, "actual result of the test")
), context, parentName, notifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ public void write(final Sequence sequence, final String itemSep, final boolean e
final QName qn = ((QNameValue)item).getQName();
writeText("Q{" + qn.getNamespaceURI() + '}' + qn.getLocalPart());
break;
case Type.ARRAY:
case Type.ARRAY_ITEM:
writeArray((ArrayType)item);
break;
case Type.MAP:
case Type.MAP_ITEM:
writeMap((AbstractMapType)item);
break;
case Type.FUNCTION_REFERENCE:
case Type.FUNCTION:
writeFunctionItem((FunctionReference) item);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ private void serializeSequence(Sequence sequence, JsonGenerator generator) throw
}

private void serializeItem(Item item, JsonGenerator generator) throws IOException, XPathException, SAXException {
if (item.getType() == Type.ARRAY) {
if (item.getType() == Type.ARRAY_ITEM) {
serializeArray((ArrayType) item, generator);
} else if (item.getType() == Type.MAP) {
} else if (item.getType() == Type.MAP_ITEM) {
serializeMap((MapType) item, generator);
} else if (Type.subTypeOf(item.getType(), Type.ATOMIC)) {
if (Type.subTypeOfUnion(item.getType(), Type.NUMBER)) {
} else if (Type.subTypeOf(item.getType(), Type.ANY_ATOMIC_TYPE)) {
if (Type.subTypeOfUnion(item.getType(), Type.NUMERIC)) {
generator.writeNumber(item.getStringValue());
} else {
switch (item.getType()) {
Expand Down
6 changes: 4 additions & 2 deletions exist-core/src/main/java/org/exist/xqj/Marshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public static void marshallItem(final DBBroker broker, final Item item, final Co
throws SAXException, XPathException {
final AttributesImpl attrs = new AttributesImpl();
int type = item.getType();
if (type == Type.NODE)
{type = ((NodeValue)item).getNode().getNodeType();}
if (type == Type.NODE) {
final short nodeType = ((NodeValue)item).getNode().getNodeType();
type = Type.fromDomNodeType(nodeType);
}
attrs.addAttribute("", ATTR_TYPE, ATTR_TYPE, "CDATA", Type.getTypeName(type));
if (Type.subTypeOf(item.getType(), Type.NODE)) {
handler.startElement(NAMESPACE, VALUE_ELEMENT, VALUE_ELEMENT_QNAME, attrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Sequence eval(Sequence contextSequence, final Item contextItem) throws XP
"Expected exactly one item for the function to be called, got " + funcSeq.getItemCount() +
". Expression: " + ExpressionDumper.dump(funcSpec));}
final Item item0 = funcSeq.itemAt(0);
if (!Type.subTypeOf(item0.getType(), Type.FUNCTION_REFERENCE)) {
if (!Type.subTypeOf(item0.getType(), Type.FUNCTION)) {
throw new XPathException(this, ErrorCodes.XPTY0004,
"Type error: expected function, got " + Type.getTypeName(item0.getType()));
}
Expand Down
2 changes: 1 addition & 1 deletion exist-core/src/main/java/org/exist/xquery/Atomize.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public String toString() {
* @see org.exist.xquery.Expression#returnsType()
*/
public int returnsType() {
return Type.ATOMIC;
return Type.ANY_ATOMIC_TYPE;
}

/* (non-Javadoc)
Expand Down
8 changes: 4 additions & 4 deletions exist-core/src/main/java/org/exist/xquery/CastExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
{context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());}
}
//Should be handled by the parser
if (requiredType == Type.ATOMIC || (requiredType == Type.NOTATION && expression.returnsType() != Type.NOTATION)) {
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));
}
Expand All @@ -103,7 +103,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
} else {
final Item item = seq.itemAt(0);

if (seq.hasMany() && Type.subTypeOf(requiredType, Type.ATOMIC))
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");}
Expand All @@ -113,7 +113,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
if (item.getType() == Type.QNAME)
{result = item.toSequence();}

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

} else {
Expand Down Expand Up @@ -190,7 +190,7 @@ public Function toFunction() throws XPathException {
try {
final QName qname = QName.parse(context, typeName);
final FunctionSignature signature = new FunctionSignature(qname);
final SequenceType argType = new SequenceType(Type.ATOMIC, Cardinality.ZERO_OR_ONE);
final SequenceType argType = new SequenceType(Type.ANY_ATOMIC_TYPE, Cardinality.ZERO_OR_ONE);
signature.setArgumentTypes(new SequenceType[]{argType});
signature.setReturnType(new SequenceType(CastExpression.this.requiredType, CastExpression.this.cardinality));
return new FunctionWrapper(this, signature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
{context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());}
}

if (requiredType == Type.ATOMIC || (requiredType == Type.NOTATION && expression.returnsType() != Type.NOTATION))
if (requiredType == Type.ANY_ATOMIC_TYPE || (requiredType == Type.NOTATION && expression.returnsType() != Type.NOTATION))
{throw new XPathException(this, ErrorCodes.XPST0080, "cannot convert to " + Type.getTypeName(requiredType));}

if (requiredType == Type.ANY_SIMPLE_TYPE || expression.returnsType() == Type.ANY_SIMPLE_TYPE || requiredType == Type.UNTYPED || expression.returnsType() == Type.UNTYPED)
Expand Down
4 changes: 2 additions & 2 deletions exist-core/src/main/java/org/exist/xquery/ConcatExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
public void add(PathExpr pathExpr) {
Expression expr = new DynamicCardinalityCheck(context, Cardinality.ZERO_OR_ONE, pathExpr,
new Error(Error.FUNC_PARAM_CARDINALITY));
if (!Type.subTypeOf(expr.returnsType(), Type.ATOMIC))
if (!Type.subTypeOf(expr.returnsType(), Type.ANY_ATOMIC_TYPE))
{expr = new Atomize(context, expr);}
super.add(expr);
}
Expand All @@ -65,7 +65,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem)
final Sequence seq = step.eval(contextSequence, contextItem);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item item = i.nextItem();
if (Type.subTypeOf(item.getType(), Type.FUNCTION_REFERENCE))
if (Type.subTypeOf(item.getType(), Type.FUNCTION))
{throw new XPathException(this, ErrorCodes.FOTY0013, "Got a function item as operand in string concatenation");}
concat.append(item.getStringValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr

// function types in context item are returned unchanged
if (contextItem != null &&
Type.subTypeOf(contextItem.getType(), Type.FUNCTION_REFERENCE)) {
Type.subTypeOf(contextItem.getType(), Type.FUNCTION)) {
return contextItem.toSequence();
}

// function types in context sequence are returned unchanged
if (contextSequence != null &&
Type.subTypeOf(contextSequence.getItemType(), Type.FUNCTION_REFERENCE)) {
Type.subTypeOf(contextSequence.getItemType(), Type.FUNCTION)) {
return contextSequence;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
Type.getTypeName(next.getType()) + " inside a document constructor");}
// if item is an atomic value, collect the string values of all
// following atomic values and seperate them by a space.
if (Type.subTypeOf(next.getType(), Type.ATOMIC)) {
if (Type.subTypeOf(next.getType(), Type.ANY_ATOMIC_TYPE)) {
if(buf == null)
{buf = new StringBuilder();}
else if (buf.length() > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem)
"Expected exactly one item for the function to be called, got " + funcSeq.getItemCount() +
". Expression: " + ExpressionDumper.dump(functionExpr));}
final Item item0 = funcSeq.itemAt(0);
if (!Type.subTypeOf(item0.getType(), Type.FUNCTION_REFERENCE))
if (!Type.subTypeOf(item0.getType(), Type.FUNCTION))
{throw new XPathException(this, ErrorCodes.XPTY0004,
"Type error: expected function, got " + Type.getTypeName(item0.getType()));}
final FunctionReference ref = (FunctionReference)item0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
itemType = item.getType();
if (itemType == NodeProxy.UNKNOWN_NODE_TYPE) {
//Retrieve the actual node
itemType = NodeProxy.nodeType2XQuery(((NodeProxy) item).getNode().getNodeType());
itemType = Type.fromDomNodeType(((NodeProxy) item).getNode().getNodeType());
}
}
if (!Type.subTypeOf(itemType, test.getType())) {
Expand Down
Loading

0 comments on commit 4fcb864

Please sign in to comment.