BaseXdb / basex

BaseX Main Repository.
http://basex.org
BSD 3-Clause "New" or "Revised" License
694 stars 265 forks source link

Add support for choice item types #2298

Closed GuntherRademacher closed 6 months ago

GuntherRademacher commented 6 months ago

These changes add support for choice item types per XQuery 4.

A choice item type is repesented by an instance of class ChoiceItemType, which holds the alternatives in a list of SeqTypes. The parser ensures that there are no nested ChoiceItemTypes by flattening out.

ChoiceItemType has specific implementations of some methods, e.g. instanceOf, union, and intersect. The implementations of those three are also reused by all other derivations of Type. Most of the properties of ChoiceItemType are based on the single type calculated by the union of all alternatives.

A choice item type may be an atomic type, but cannot derive from enum AtomType. Thus there now is an atomic type that is not covered by that and some occurrences of AtomType needed to be changed, most notably MapType.keyType. As a consequence, SeqType.mapTypes had to change from an EnumMap to a HashMap. This also required the implementation of SeqType.hashCode.

In SeqType.coerce, alternatives of a choice item type are tried sequentially until a matching one is found. Here a possible optimization might be to calculate the atomization just once, and reuse it in subsequent attempts. Not sure whether it is worth implementing it this way.

With respect to QT4 tests, these changes make all but three tests cases with prefixes union-type and choice-type succeed, the remaining three fail for unrelated reasons. Also there are more test cases that now succeed, e.g. with prefixes DynamicFunctionCall, enum, and Keywords-fn-in-scope-namespaces.

If you see anything that I might have missed, please let me know. I will gladly continue to work on this.

ChristianGruen commented 6 months ago

…merged, with (really) minor tweaks: c755726fdbc2cd702d95b8aa4ef4b665aa258119