-
-
Notifications
You must be signed in to change notification settings - Fork 188
Fix XQuery 3.1 casting and numeric comparison compliance #6165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4434be9
97f7bde
22b515d
1f69c48
f92f3e6
bd7ccdc
a28275b
8b7a95a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,21 +195,27 @@ public AtomicValue convertTo(final int requiredType) throws XPathException { | |
|
|
||
| public DecimalValue toDecimalValue() throws XPathException { | ||
| if (isNaN() || isInfinite()) { | ||
| throw conversionError(Type.DECIMAL); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why was the method call |
||
| throw new XPathException(getExpression(), ErrorCodes.FOCA0002, | ||
| "Cannot convert " + Type.getTypeName(getType()) + "('" + getStringValue() | ||
| + "') to " + Type.getTypeName(Type.DECIMAL)); | ||
| } | ||
| return new DecimalValue(getExpression(), BigDecimal.valueOf(value)); | ||
| } | ||
|
|
||
| public IntegerValue toIntegerValue() throws XPathException { | ||
| if (isNaN() || isInfinite()) { | ||
| throw conversionError(Type.INTEGER); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no longer using the existing
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as in |
||
| throw new XPathException(getExpression(), ErrorCodes.FOCA0002, | ||
| "Cannot convert " + Type.getTypeName(getType()) + "('" + getStringValue() | ||
| + "') to " + Type.getTypeName(Type.INTEGER)); | ||
| } | ||
| return new IntegerValue(getExpression(), (long) value); | ||
| } | ||
|
|
||
| public IntegerValue toIntegerSubType(final int subType) throws XPathException { | ||
| if (isNaN() || isInfinite()) { | ||
| throw conversionError(subType); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no longer using the existing
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as in |
||
| throw new XPathException(getExpression(), ErrorCodes.FOCA0002, | ||
| "Cannot convert " + Type.getTypeName(getType()) + "('" + getStringValue() | ||
| + "') to " + Type.getTypeName(subType)); | ||
| } | ||
| if (subType != Type.INTEGER && value > Integer.MAX_VALUE) { | ||
| throw new XPathException(getExpression(), ErrorCodes.FOCA0003, "Value is out of range for type " | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -716,6 +716,117 @@ | |
| return primitiveType; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a cast from sourceType to targetType is allowed per XPath F&O 3.1 | ||
| * Section 19 (Casting), Table 6. This implements the casting table that determines | ||
| * whether a cast between two primitive types is possible (may succeed for some values) | ||
| * or impossible (will never succeed for any value). | ||
| * | ||
| * <p>If the cast is impossible, the caller should raise XPTY0004 rather than | ||
| * attempting the cast (which would incorrectly raise FORG0001).</p> | ||
| * | ||
| * @param sourceType the type constant of the source type | ||
| * @param targetType the type constant of the target type | ||
| * | ||
| * @return true if the cast may succeed (for some values), false if the cast can never succeed | ||
| */ | ||
| public static boolean isCastable(final int sourceType, final int targetType) { | ||
|
Check warning on line 733 in exist-core/src/main/java/org/exist/xquery/value/Type.java
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please address the the NPath complexity of 8960, current threshold is 200. |
||
| // Casting to/from ITEM, ANY_ATOMIC_TYPE, or same type is always allowed | ||
| if (sourceType == targetType || targetType == ITEM || targetType == ANY_ATOMIC_TYPE) { | ||
| return true; | ||
| } | ||
|
|
||
| // xs:untypedAtomic and xs:string can be cast to any atomic type | ||
| if (sourceType == UNTYPED_ATOMIC || sourceType == STRING || subTypeOf(sourceType, STRING)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Any atomic type can be cast to xs:untypedAtomic or xs:string | ||
| if (targetType == UNTYPED_ATOMIC || targetType == STRING || subTypeOf(targetType, STRING)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Get primitive types for the casting table lookup | ||
| final int srcPrimitive; | ||
| final int tgtPrimitive; | ||
| try { | ||
| srcPrimitive = primitiveTypeOf(sourceType); | ||
| tgtPrimitive = primitiveTypeOf(targetType); | ||
| } catch (final IllegalArgumentException e) { | ||
| // Unknown type — allow the cast to proceed and let convertTo() handle errors | ||
| return true; | ||
| } | ||
|
|
||
| // Same primitive type is always castable | ||
| if (srcPrimitive == tgtPrimitive) { | ||
| return true; | ||
| } | ||
|
|
||
| // XPath F&O 3.1, Section 19, Table 6 — Casting table by primitive type pairs. | ||
| // 'M' (may) or 'Y' (yes) entries return true; 'N' (no) entries return false. | ||
| return switch (srcPrimitive) { | ||
| case FLOAT, DOUBLE, DECIMAL -> | ||
| // Numerics can cast to: other numerics, boolean, duration subtypes | ||
| tgtPrimitive == FLOAT || tgtPrimitive == DOUBLE || tgtPrimitive == DECIMAL | ||
| || tgtPrimitive == BOOLEAN; | ||
|
|
||
| case BOOLEAN -> | ||
| // Boolean can cast to: numerics | ||
| tgtPrimitive == FLOAT || tgtPrimitive == DOUBLE || tgtPrimitive == DECIMAL; | ||
|
|
||
| case DURATION -> | ||
| // Duration can cast to: duration subtypes (yearMonthDuration, dayTimeDuration) | ||
| // duration subtypes share the same primitive: DURATION | ||
| // So this is already handled by srcPrimitive == tgtPrimitive above | ||
| false; | ||
|
|
||
| case DATE_TIME -> | ||
| // dateTime can cast to: date, time, gYearMonth, gYear, gMonthDay, gDay, gMonth | ||
| tgtPrimitive == DATE || tgtPrimitive == TIME | ||
| || tgtPrimitive == G_YEAR_MONTH || tgtPrimitive == G_YEAR | ||
| || tgtPrimitive == G_MONTH_DAY || tgtPrimitive == G_DAY | ||
| || tgtPrimitive == G_MONTH; | ||
|
|
||
| case DATE -> | ||
| // date can cast to: dateTime, gYearMonth, gYear, gMonthDay, gDay, gMonth | ||
| tgtPrimitive == DATE_TIME | ||
| || tgtPrimitive == G_YEAR_MONTH || tgtPrimitive == G_YEAR | ||
| || tgtPrimitive == G_MONTH_DAY || tgtPrimitive == G_DAY | ||
| || tgtPrimitive == G_MONTH; | ||
|
|
||
| case TIME -> | ||
| // time CANNOT cast to anything except string/untypedAtomic (handled above) | ||
| false; | ||
|
|
||
| case G_YEAR_MONTH, G_YEAR, G_MONTH_DAY, G_DAY, G_MONTH -> | ||
| // Gregorian types can only cast to: dateTime (if enough info), but spec says N | ||
| // except string/untypedAtomic (handled above) | ||
| false; | ||
|
|
||
| case HEX_BINARY -> | ||
| // hexBinary can cast to: base64Binary | ||
| tgtPrimitive == BASE64_BINARY; | ||
|
|
||
| case BASE64_BINARY -> | ||
| // base64Binary can cast to: hexBinary | ||
| tgtPrimitive == HEX_BINARY; | ||
|
|
||
| case ANY_URI -> | ||
| // anyURI cannot cast to anything except string/untypedAtomic (handled above) | ||
| false; | ||
|
|
||
| case QNAME -> | ||
| // QName cannot cast to anything except string/untypedAtomic (handled above) | ||
| // (and NOTATION, but that's rarely used) | ||
| tgtPrimitive == NOTATION; | ||
|
|
||
| case NOTATION -> | ||
| tgtPrimitive == QNAME; | ||
|
|
||
| default -> true; // Unknown — allow and let convertTo() decide | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the XDM equivalent type of a DOM Node type (i.e. {@link Node#getNodeType()}). | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why no longer using the existing
conversionError()method?