← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/markos-scratch into lp:zorba

 

Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-scratch into lp:zorba.

Commit message:
optimization: replaced use of create_value_type() in runtime with getTypeCode()

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/168595

optimization: replaced use of create_value_type() in runtime with getTypeCode()
-- 
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/168595
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'ChangeLog'
--- ChangeLog	2013-06-08 05:33:57 +0000
+++ ChangeLog	2013-06-11 05:41:36 +0000
@@ -13,6 +13,7 @@
   * Optimized implementation of fn:deep-equal
   * No need to apply document ordering on the domain expression of a FOR clause
     if the FOR clause is followed by an orderby or groupby clause.
+  * Replaced use of create_value_type() in runtime with getTypeCode().
 
 Bug Fixes/Other Changes:
   * Fixed bug #1117952 (Improve XML error output format)

=== modified file 'src/runtime/booleans/BooleanImpl.cpp'
--- src/runtime/booleans/BooleanImpl.cpp	2013-05-08 20:14:47 +0000
+++ src/runtime/booleans/BooleanImpl.cpp	2013-06-11 05:41:36 +0000
@@ -121,7 +121,6 @@
     // node => true
     result = negate ^ true;
   }
-#ifdef ZORBA_WITH_JSON
   else if (item->isJSONItem())
   {
     xqtref_t type = tm->create_value_type(item);
@@ -129,7 +128,6 @@
     RAISE_ERROR(err::FORG0006, loc,
     ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o), *type, "fn:boolean" ));
   }
-#endif
   else
   {
     store::SchemaTypeCode type = item->getTypeCode();

=== modified file 'src/runtime/core/arithmetic_impl.cpp'
--- src/runtime/core/arithmetic_impl.cpp	2013-05-08 20:14:47 +0000
+++ src/runtime/core/arithmetic_impl.cpp	2013-06-11 05:41:36 +0000
@@ -167,16 +167,16 @@
   assert(n0->isAtomic());
   assert(n1->isAtomic());
 
-  xqtref_t type0 = tm->create_value_type(n0);
-  xqtref_t type1 = tm->create_value_type(n1);
+  store::SchemaTypeCode type0 = n0->getTypeCode();
+  store::SchemaTypeCode type1 = n1->getTypeCode();
   
-  if (TypeOps::is_numeric(tm, *type0) &&
-      (TypeOps::is_subtype(tm, *type1, *rtm.YM_DURATION_TYPE_ONE) ||
-       TypeOps::is_subtype(tm, *type1, *rtm.DT_DURATION_TYPE_ONE)))
+  if (TypeOps::is_numeric(type0) &&
+      (TypeOps::is_subtype(type1, store::XS_YM_DURATION) ||
+       TypeOps::is_subtype(type1, store::XS_DT_DURATION)))
   {
     GenericCast::castToAtomic(n0, n0, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, aLoc);
 
-    if (TypeOps::is_subtype(tm, *type1, *rtm.YM_DURATION_TYPE_ONE))
+    if (TypeOps::is_subtype(type1, store::XS_YM_DURATION))
     {
       return Operation::template
              compute<store::XS_DOUBLE, store::XS_YM_DURATION>
@@ -189,44 +189,44 @@
              (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if (TypeOps::is_subtype(tm, *type0, *rtm.DT_DURATION_TYPE_ONE) &&
-           TypeOps::is_subtype(tm, *type1, *rtm.TIME_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_DT_DURATION) &&
+           TypeOps::is_subtype(type1, store::XS_TIME))
   {
     return Operation::template
            compute<store::XS_DURATION,store::XS_TIME>
            (result, dctx, tm, &aLoc, n0, n1);
   }
-  else if (TypeOps::is_subtype(tm, *type0, *rtm.YM_DURATION_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_YM_DURATION))
   {
-    if(TypeOps::is_numeric(tm, *type1))
+    if (TypeOps::is_numeric(type1))
     {
       GenericCast::castToAtomic(n1, n1, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, aLoc);
       return Operation::template
              compute<store::XS_YM_DURATION,store::XS_DOUBLE>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DATETIME_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DATETIME))
     {
       return Operation::template
              compute<store::XS_DURATION,store::XS_DATETIME>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DATE_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DATE))
     {
       return Operation::template
              compute<store::XS_DURATION,store::XS_DATE>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_equal(tm, *type0, *type1))
+    else if (type0 == type1)
     {
       return Operation::template
       computeSingleType<store::XS_YM_DURATION>
       (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if (TypeOps::is_subtype(tm, *type0, *rtm.DT_DURATION_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_DT_DURATION))
   {
-    if(TypeOps::is_numeric(tm, *type1))
+    if (TypeOps::is_numeric(type1))
     {
       GenericCast::castToAtomic(n1, n1, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, aLoc);
 
@@ -234,94 +234,96 @@
              compute<store::XS_DT_DURATION,store::XS_DOUBLE>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DATETIME_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DATETIME))
     {
       return Operation::template 
              compute<store::XS_DURATION,store::XS_DATETIME>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DATE_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DATE))
     {
       return Operation::template
              compute<store::XS_DURATION,store::XS_DATE>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_equal(tm, *type0, *type1))
+    else if (type0 == type1)
     {
       return Operation::template
              computeSingleType<store::XS_DT_DURATION>
              (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if(TypeOps::is_subtype(tm, *type0, *rtm.DATETIME_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_DATETIME))
   {
-    if(TypeOps::is_subtype(tm, *type1, *rtm.DATETIME_TYPE_ONE))
+    if (TypeOps::is_subtype(type1, store::XS_DATETIME))
     {
       return Operation::template
              compute<store::XS_DATETIME,store::XS_DATETIME>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.YM_DURATION_TYPE_ONE ))
+    else if (TypeOps::is_subtype(type1, store::XS_YM_DURATION))
     {
       return Operation::template
              compute<store::XS_DATETIME,store::XS_DURATION>
             (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DT_DURATION_TYPE_ONE ))
+    else if (TypeOps::is_subtype(type1, store::XS_DT_DURATION))
     {
       return Operation::template
              compute<store::XS_DATETIME,store::XS_DURATION>
             (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if(TypeOps::is_subtype(tm, *type0, *rtm.DATE_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_DATE))
   {
-    if (TypeOps::is_subtype(tm, *type1, *rtm.DATE_TYPE_ONE))
+    if (TypeOps::is_subtype(type1, store::XS_DATE))
     {
       return Operation::template
              compute<store::XS_DATE,store::XS_DATE>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.YM_DURATION_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_YM_DURATION))
     {
       return Operation::template
              compute<store::XS_DATE,store::XS_DURATION>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DT_DURATION_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DT_DURATION))
     {
       return Operation::template
              compute<store::XS_DATE,store::XS_DURATION>
              (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if(TypeOps::is_subtype(tm, *type0, *rtm.TIME_TYPE_ONE))
+  else if (TypeOps::is_subtype(type0, store::XS_TIME))
   {
-    if(TypeOps::is_subtype(tm, *type1, *rtm.TIME_TYPE_ONE))
+    if (TypeOps::is_subtype(type1, store::XS_TIME))
     {
       return Operation::template
              compute<store::XS_TIME,store::XS_TIME>
              (result, dctx, tm, &aLoc, n0, n1);
     }
-    else if (TypeOps::is_subtype(tm, *type1, *rtm.DT_DURATION_TYPE_ONE))
+    else if (TypeOps::is_subtype(type1, store::XS_DT_DURATION))
     {
       return Operation::template 
              compute<store::XS_TIME,store::XS_DURATION>
              (result, dctx, tm, &aLoc, n0, n1);
     }
   }
-  else if ((TypeOps::is_numeric(tm, *type0) ||
-            TypeOps::is_subtype(tm, *type0, *rtm.UNTYPED_ATOMIC_TYPE_ONE)) &&
-           ( TypeOps::is_numeric(tm, *type1) ||
-             TypeOps::is_subtype(tm, *type1, *rtm.UNTYPED_ATOMIC_TYPE_ONE)))
+  else if ((TypeOps::is_numeric(type0) || type0 == store::XS_UNTYPED_ATOMIC) &&
+           (TypeOps::is_numeric(type1) || type1 == store::XS_UNTYPED_ATOMIC))
   {
     return NumArithIterator<Operation>::
            computeAtomic(result, dctx, tm, aLoc, n0, type0, n1, type1);
   }
-  
-  
-  RAISE_ERROR(err::XPTY0004, aLoc,
-  ERROR_PARAMS(ZED(ArithOpNotDefinedBetween_23), *type0, *type1));
+
+  {  
+    xqtref_t type0 = tm->create_value_type(n0);
+    xqtref_t type1 = tm->create_value_type(n1);
+
+    RAISE_ERROR(err::XPTY0004, aLoc,
+    ERROR_PARAMS(ZED(ArithOpNotDefinedBetween_23), *type0, *type1));
+  }
 
   return false; // suppresses wanring
 }

=== modified file 'src/runtime/indexing/index_ddl.cpp'
--- src/runtime/indexing/index_ddl.cpp	2013-05-02 18:34:27 +0000
+++ src/runtime/indexing/index_ddl.cpp	2013-06-11 05:41:36 +0000
@@ -70,7 +70,8 @@
 
 
 /*******************************************************************************
-
+  This function is called from the probe function to chaeck that the type of
+  the probing key is consistent with the index declaration.
 ********************************************************************************/
 static void checkKeyType(
     const QueryLoc& loc,
@@ -138,6 +139,7 @@
     {
       GENV_STORE.getItemFactory()->
       createDouble(searchKey, xs_float(searchKey->getDecimalValue()));
+
       return;
     }
 
@@ -1217,7 +1219,6 @@
     PlanState& planState) const
 {
   TypeManager* tm = theSctx->get_typemanager();
-  RootTypeManager& rtm = GENV_TYPESYSTEM;
   store::IndexCondition_t cond;
   csize numChildren = theChildren.size();
   csize numNonKeyParams = (theSkip ? 2 : 1);
@@ -1262,37 +1263,37 @@
     if (state->theIndexDecl->isGeneral() &&
         (state->theIndexDecl->getKeyTypes())[keyNo] == NULL)
     {
-      xqtref_t leftType;
-      xqtref_t rightType;
+      store::SchemaTypeCode leftType;
+      store::SchemaTypeCode rightType;
 
       if (tempLeft != NULL)
       {
-        leftType = tm->create_value_type(tempLeft);
+        leftType = tempLeft->getTypeCode();
           
-        if (TypeOps::is_equal(tm, *leftType, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+        if (leftType == store::XS_UNTYPED_ATOMIC)
         {
           zstring str = tempLeft->getStringValue();
           GENV_ITEMFACTORY->createString(tempLeft, str);
-          leftType = rtm.STRING_TYPE_ONE;
+          leftType = store::XS_STRING;
         }
       }
         
       if (tempRight != NULL)
       {
-        rightType = tm->create_value_type(tempRight);
+        rightType = tempRight->getTypeCode();
           
-        if (TypeOps::is_equal(tm, *rightType, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+        if (rightType == store::XS_UNTYPED_ATOMIC)
         {
           zstring str = tempRight->getStringValue();
           GENV_ITEMFACTORY->createString(tempRight, str);
-          rightType = rtm.STRING_TYPE_ONE;
+          rightType = store::XS_STRING;
         }
       }
         
-      if (leftType != NULL && rightType != NULL)
+      if (tempLeft != NULL && tempRight != NULL)
       {
-        if (!TypeOps::is_subtype(tm, *leftType, *rightType) &&
-            !TypeOps::is_subtype(tm, *rightType, *leftType))
+        if (!TypeOps::is_subtype(leftType, rightType) &&
+            !TypeOps::is_subtype(rightType, leftType))
         {
           RAISE_ERROR(zerr::ZDDY0034_INDEX_RANGE_VALUE_PROBE_BAD_KEY_TYPES, loc,
           ERROR_PARAMS(state->theIndexDecl->getName()->getStringValue()));

=== modified file 'src/runtime/maths/maths_impl.cpp'
--- src/runtime/maths/maths_impl.cpp	2013-02-07 17:24:36 +0000
+++ src/runtime/maths/maths_impl.cpp	2013-06-11 05:41:36 +0000
@@ -30,6 +30,8 @@
 #include "store/api/item.h"
 
 #include "diagnostics/xquery_diagnostics.h"
+#include "diagnostics/util_macros.h"
+
 #include "zorbatypes/numconversions.h"
 
 namespace zorba {
@@ -345,44 +347,27 @@
   {
     if (consumeNext(n1, this->theChild1.getp(), planState))
     {
+      assert(n1->isAtomic());
+
       {
-        xqtref_t type;
-
         const TypeManager* tm = theSctx->get_typemanager();
-
         const RootTypeManager& rtm = GENV_TYPESYSTEM;
+
         xs_double doub1 = n0->getDoubleValue();
-        //xs_double  doub2 = n1->getDoubleValue();
-
-        //GENV_ITEMFACTORY->createDouble(result, doub1.pow(doub2));
-
-        assert(n1->isAtomic());
-
-        type = tm->create_value_type(n1);
-
-        if (TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+
+        store::SchemaTypeCode type1 = n1->getTypeCode();
+
+        if (type1 == store::XS_UNTYPED_ATOMIC)
         {
           GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-          type = tm->create_value_type(result);
-        }
-
-        if (TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
-        {
-          GENV_ITEMFACTORY->createDouble(result, doub1.pow(n1->getDoubleValue()));
-        }
-        else if (TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE))
-        {
-          store::Item_t n1_double;
-          GenericCast::castToAtomic(n1_double,
-                                    n1,
-                                    rtm.DOUBLE_TYPE_ONE.getp(),
-                                    tm,
-                                    NULL,
-                                    loc);
-
-          GENV_ITEMFACTORY->createDouble(result, doub1.pow(n1_double->getDoubleValue()));
-        }
-        else if (TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE))
+
+          GENV_ITEMFACTORY->createDouble(result, doub1.pow(n1->getDoubleValue()));
+        }
+        else if (type1 == store::XS_DOUBLE || type1 == store::XS_FLOAT)
+        {
+          GENV_ITEMFACTORY->createDouble(result, doub1.pow(n1->getDoubleValue()));
+        }
+        else if (type1 == store::XS_INTEGER)
         {
           xs_integer n1_integer = n1->getIntegerValue();
           try
@@ -390,16 +375,13 @@
             xs_int const n1_int = to_xs_int(n1_integer);
             GENV_ITEMFACTORY->createDouble(result, doub1.pow(n1_int));
           }
-          catch ( std::range_error const& )
+          catch (std::range_error const&)
           {
-            throw XQUERY_EXCEPTION(
-              err::XPTY0004,
-              ERROR_PARAMS( ZED( NoCastToCInt_2 ), n1_integer ),
-              ERROR_LOC( loc )
-            );
+            RAISE_ERROR(err::XPTY0004, loc,
+            ERROR_PARAMS(ZED(NoCastToCInt_2), n1_integer));
           }
         }
-        else if (TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE))
+        else if (type1 == store::XS_DECIMAL)
         {
           store::Item_t n1_double;
           GenericCast::castToAtomic(n1_double,
@@ -413,19 +395,13 @@
         }
         else
         {
-          throw XQUERY_EXCEPTION(
-            err::XPTY0004,
-            ERROR_PARAMS( ZED( BadTypeFor_23 ), type, "math:pow" ),
-            ERROR_LOC( loc )
-          );
+          xqtref_t type = tm->create_value_type(n1);
+
+          RAISE_ERROR(err::XPTY0004, loc,
+          ERROR_PARAMS(ZED(BadTypeFor_23), type, "math:pow"));
         }
       }
 
-      if (consumeNext(n0, this->theChild0.getp(), planState) ||
-          consumeNext(n1, this->theChild1.getp(), planState))
-        throw XQUERY_EXCEPTION(
-          err::XPTY0004, ERROR_PARAMS( ZED( NoSeqForFnOp_2 ), "math:pow" )
-        );
       STACK_PUSH(true, state);
     }
   }

=== modified file 'src/runtime/numerics/NumericsImpl.cpp'
--- src/runtime/numerics/NumericsImpl.cpp	2013-05-05 03:16:33 +0000
+++ src/runtime/numerics/NumericsImpl.cpp	2013-06-11 05:41:36 +0000
@@ -687,8 +687,8 @@
   assert(n0->isAtomic());
   assert(n1->isAtomic());
 
-  xqtref_t type0 = tm->create_value_type(n0);
-  xqtref_t type1 = tm->create_value_type(n1);
+  store::SchemaTypeCode type0 = n0->getTypeCode();
+  store::SchemaTypeCode type1 = n1->getTypeCode();
 
   return computeAtomic(result, dctx, tm, aLoc, n0, type0, n1, type1);
 }
@@ -701,9 +701,9 @@
     const TypeManager* tm,
     const QueryLoc& aLoc,
     store::Item_t& item0,
-    xqtref_t type0,
+    store::SchemaTypeCode type0,
     store::Item_t& item1,
-    xqtref_t type1)
+    store::SchemaTypeCode type1)
 {
   bool res;
   store::Item_t n0;
@@ -711,14 +711,14 @@
 
   bool isDivision = Operation::getOperationKind() == ArithmeticConsts::DIVISION;
 
-  xqtref_t resultType = TypeOps::arithmetic_type(tm, *type0, *type1, isDivision);
+  store::SchemaTypeCode resultType = TypeOps::arithmetic_type(type0, type1, isDivision);
 
-  switch (TypeOps::get_atomic_type_code(*resultType))
+  switch (resultType)
   {
   case store::XS_DOUBLE:
   {
-    GenericCast::castToAtomic(n0, item0, &*resultType, tm, NULL, aLoc);
-    GenericCast::castToAtomic(n1, item1, &*resultType, tm, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n0, item0, resultType, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n1, item1, resultType, NULL, aLoc);
     
     res = Operation::template
           computeSingleType<store::XS_DOUBLE>
@@ -727,8 +727,8 @@
   }
   case store::XS_FLOAT:
   {
-    GenericCast::castToAtomic(n0, item0, &*resultType, tm, NULL, aLoc);
-    GenericCast::castToAtomic(n1, item1, &*resultType, tm, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n0, item0, resultType, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n1, item1, resultType, NULL, aLoc);
 
     res = Operation::template 
           computeSingleType<store::XS_FLOAT>
@@ -737,8 +737,8 @@
   }
   case store::XS_DECIMAL:
   {
-    GenericCast::castToAtomic(n0, item0, &*resultType, tm, NULL, aLoc);
-    GenericCast::castToAtomic(n1, item1, &*resultType, tm, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n0, item0, resultType, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n1, item1, resultType, NULL, aLoc);
 
     res = Operation::template
           computeSingleType<store::XS_DECIMAL>
@@ -747,8 +747,8 @@
   }
   case store::XS_INTEGER:
   {
-    GenericCast::castToAtomic(n0, item0, &*resultType, tm, NULL, aLoc);
-    GenericCast::castToAtomic(n1, item1, &*resultType, tm, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n0, item0, resultType, NULL, aLoc);
+    GenericCast::castToBuiltinAtomic(n1, item1, resultType, NULL, aLoc);
 
     res = Operation::template 
           computeSingleType<store::XS_INTEGER>
@@ -757,11 +757,13 @@
   }
   default:
   {
-    throw XQUERY_EXCEPTION(
-        err::XPTY0004,
-        ERROR_PARAMS( ZED( ArithOpNotDefinedBetween_23 ), *type0, *type1 ),
-        ERROR_LOC( aLoc )
-      );
+    xqtref_t type0 = tm->create_value_type(item0);
+    xqtref_t type1 = tm->create_value_type(item1);
+
+    RAISE_ERROR(err::XPTY0004, aLoc,
+    ERROR_PARAMS(ZED(ArithOpNotDefinedBetween_23),
+                 type0->toSchemaString(),
+                 type1->toSchemaString()));
   }
   }
 
@@ -896,9 +898,8 @@
 bool OpNumericUnaryIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t item;
-  xqtref_t type;
+  store::SchemaTypeCode type;
 
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
   const TypeManager* tm = theSctx->get_typemanager();
 
   PlanIteratorState* state;
@@ -908,35 +909,35 @@
   {
     assert(item->isAtomic());
 
-    type = tm->create_value_type(item);
+    type = item->getTypeCode();
 
-    if (TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE ) )
+    if (type == store::XS_UNTYPED_ATOMIC)
     {
-      GenericCast::castToAtomic(item, item, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      type = rtm.DOUBLE_TYPE_ONE;
+      GenericCast::castToBuiltinAtomic(item, item, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
     }
     
     // TODO Optimizations (e.g. if item has already the correct type and value,
     // it does not have to be created newly)
-    if (TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
     {
       GENV_ITEMFACTORY->
       createDouble(result,
                    (thePlus ? item->getDoubleValue() : -item->getDoubleValue()));
     }
-    else if ( TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE ) )
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
     {
       GENV_ITEMFACTORY->
       createFloat(result,
                   (thePlus ? item->getFloatValue() : -item->getFloatValue()));
     }
-    else if ( TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE ) )
+    else if (TypeOps::is_subtype(type, store::XS_INTEGER))
     {
       GENV_ITEMFACTORY->
       createInteger(result,
                     (thePlus ? item->getIntegerValue() : -item->getIntegerValue()));
     }
-    else if ( TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE ) )
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
     {
       GENV_ITEMFACTORY->
       createDecimal(result,
@@ -944,8 +945,9 @@
     }
     else
     {
+      xqtref_t type = tm->create_value_type(item);
       RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(BadTypeFor_23), type, ZED(UnaryArithOp)));
+      ERROR_PARAMS(ZED(BadTypeFor_23), type->toSchemaString(), ZED(UnaryArithOp)));
     }
     
     STACK_PUSH(true, state);
@@ -985,7 +987,7 @@
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
 
-  if (consumeNext(item, theChild.getp(), planState ))
+  if (consumeNext(item, theChild.getp(), planState))
   {
     assert(item->isAtomic());
 

=== modified file 'src/runtime/numerics/NumericsImpl.h'
--- src/runtime/numerics/NumericsImpl.h	2013-02-07 17:24:36 +0000
+++ src/runtime/numerics/NumericsImpl.h	2013-06-11 05:41:36 +0000
@@ -61,9 +61,9 @@
         const TypeManager* tm,
         const QueryLoc&,
         store::Item_t& item0,
-        xqtref_t type0,
+        store::SchemaTypeCode type0,
         store::Item_t& item1,
-        xqtref_t type1);
+        store::SchemaTypeCode type1);
 
 public:
   NumArithIterator(static_context* sctx, const QueryLoc&, PlanIter_t&, PlanIter_t&);

=== modified file 'src/runtime/numerics/numerics_impl.cpp'
--- src/runtime/numerics/numerics_impl.cpp	2013-05-02 18:34:27 +0000
+++ src/runtime/numerics/numerics_impl.cpp	2013-06-11 05:41:36 +0000
@@ -52,12 +52,9 @@
 AbsIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t item;
-  xqtref_t type;
-
+  store::SchemaTypeCode type;
   const TypeManager* tm = theSctx->get_typemanager();
 
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
-
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
 
@@ -65,31 +62,31 @@
   {
     assert(result->isAtomic());
 
-    type = tm->create_value_type(result);
+    type = result->getTypeCode();
 
-    if (TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+    if (type == store::XS_UNTYPED_ATOMIC)
     {
-      GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      type = tm->create_value_type(result);
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
     }
 
-    if (TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
     {
       if (result->getDoubleValue().isPos() || result->getDoubleValue().isPosZero())
       {
-        if ( !TypeOps::is_equal(tm, *type, *rtm.DOUBLE_TYPE_ONE))
-          GENV_ITEMFACTORY->createDouble (result, result->getDoubleValue());
+        if (type != store::XS_DOUBLE)
+          GENV_ITEMFACTORY->createDouble(result, result->getDoubleValue());
       }
       else
       {
         GENV_ITEMFACTORY->createDouble(result, -result->getDoubleValue());
       }
     }
-    else if (TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE))
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
     {
       if ( result->getFloatValue().isPos() || result->getFloatValue().isPosZero())
       {
-        if ( !TypeOps::is_equal(tm, *type, *rtm.FLOAT_TYPE_ONE))
+        if (type != store::XS_FLOAT)
           GENV_ITEMFACTORY->createFloat (result, result->getFloatValue());
       }
       else
@@ -97,11 +94,11 @@
         GENV_ITEMFACTORY->createFloat(result, -result->getFloatValue());
       }
     }
-    else if (TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE))
+    else if (TypeOps::is_subtype(type, store::XS_INTEGER))
     {
       if (result->getIntegerValue().sign() >= 0)
       {
-        if ( !TypeOps::is_equal(tm, *type, *rtm.INTEGER_TYPE_ONE))
+        if (type != store::XS_INTEGER)
           GENV_ITEMFACTORY->createInteger(result, result->getIntegerValue());
       }
       else
@@ -109,28 +106,23 @@
         GENV_ITEMFACTORY->createInteger(result, -result->getIntegerValue());
       }
     }
-    else if (TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE))
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
     {
       if ( result->getDecimalValue().sign() >= 0)
       {
-        if ( !TypeOps::is_equal(tm, *type, *rtm.DECIMAL_TYPE_ONE))
+        if (type != store::XS_DECIMAL)
           GENV_ITEMFACTORY->createDecimal(result, result->getDecimalValue());
       }
       else
       {
-        GENV_ITEMFACTORY->createDecimal (result, -result->getDecimalValue());
+        GENV_ITEMFACTORY->createDecimal(result, -result->getDecimalValue());
       }
     }
     else
     {
-      RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(BadTypeFor_23), type, "fn:abs" ));
-    }
-
-    if ( consumeNext(item, theChildren[0].getp(), planState ))
-    {
-      RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(NoSeqForFnOp_2), "fn:abs"));
+      xqtref_t type = tm->create_value_type(result);
+      RAISE_ERROR(err::XPTY0004, loc,
+      ERROR_PARAMS(ZED(BadTypeFor_23), type->toSchemaString(), "fn:abs" ));
     }
 
     STACK_PUSH(true, state);
@@ -138,15 +130,15 @@
   STACK_END(state);
 }
 
+
 //6.4.2 fn:ceiling
 bool
 CeilingIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t item;
-  xqtref_t type;
+  store::SchemaTypeCode type;
 
   const TypeManager* tm = theSctx->get_typemanager();
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
 
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
@@ -156,116 +148,109 @@
     //get the value and the type of the item
     assert(result->isAtomic());
 
-    type = tm->create_value_type(result);
+    type = result->getTypeCode();
 
     //Parameters of type xs:untypedAtomic are always promoted to xs:double
-    if ( TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_UNTYPED_ATOMIC))
     {
-      GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      type = tm->create_value_type(result);
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
     }
 
-    //item type is subtype of DOUBLE
-    if ( TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
+    {
       GENV_ITEMFACTORY->createDouble(result, result->getDoubleValue().ceil());
-
-    //item type is subtype of FLOAT
-    else if ( TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE))
+    }
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
+    {
       GENV_ITEMFACTORY->createFloat(result, result->getFloatValue().ceil());
-
-    //item type is subtype of INTEGER
-    else if(TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE))
-    { /* do nothing */ }
-
-    //item type is subtype of DECIMAL
-    else if (TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE))
+    }
+    else if (TypeOps::is_subtype(type, store::XS_INTEGER))
+    {
+      /* do nothing */
+    }
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
+    {
       GENV_ITEMFACTORY->createDecimal(result, result->getDecimalValue().ceil());
-
+    }
     else
     {
+      xqtref_t type = tm->create_value_type(result);
       RAISE_ERROR(err::XPTY0004, loc,
       ERROR_PARAMS(ZED(BadTypeFor_23), type, "fn:ceiling" ));
     }
 
-    if ( consumeNext(item, theChildren[0].getp(), planState ))
-    {
-      RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(NoSeqForFnOp_2), "fn:ceiling"));
-    }
     STACK_PUSH(true, state);
   }
+
   STACK_END(state);
 }
 
+
 //6.4.3 fn:floor
 bool
 FloorIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t item;
-  xqtref_t type;
+  store::SchemaTypeCode type;
 
   const TypeManager* tm = theSctx->get_typemanager();
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
 
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
 
   if (consumeNext(result, theChildren[0].getp(), planState))
   {
-    //get the value and the type of the item
     assert(result->isAtomic());
 
-    type = tm->create_value_type(result);
+    type = result->getTypeCode();
 
     //Parameters of type xs:untypedAtomic are always promoted to xs:double
-    if (TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_UNTYPED_ATOMIC))
     {
-      GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      type = tm->create_value_type(result);
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
     }
 
-    //item type is subtype of DOUBLE
-    if (TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
+    {
       GENV_ITEMFACTORY->createDouble(result, result->getDoubleValue().floor());
-
-    //item type is subtype of FLOAT
-    else if (TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE))
+    }
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
+    {
       GENV_ITEMFACTORY->createFloat(result, result->getFloatValue().floor());
-
-    //item type is subtype of INTEGER
-    else if(TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE))
-    { /* do nothing */ }
-
-    //item type is subtype of DECIMAL
-    else if (TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE))
+    }
+    else if(TypeOps::is_subtype(type, store::XS_INTEGER))
+    {
+      /* do nothing */
+    }
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
+    {
       GENV_ITEMFACTORY->createDecimal(result, result->getDecimalValue().floor());
-
+    }
     else
     {
+      xqtref_t type = tm->create_value_type(result);
       RAISE_ERROR(err::XPTY0004, loc,
       ERROR_PARAMS(ZED(BadTypeFor_23), type, "fn:floor"));
     }
 
-    if ( consumeNext(item, theChildren[0].getp(), planState ) )
-    {
-      RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(NoSeqForFnOp_2), "fn:floor" ));
-    }
-    STACK_PUSH (true, state );
+    STACK_PUSH(true, state);
   }
-  STACK_END (state);
+
+  STACK_END(state);
 }
 
+
 bool
 RoundIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t item;
-  xqtref_t type;
+  store::SchemaTypeCode type;
   store::Item_t precision;
   Integer precision_integer(0);
 
   const TypeManager* tm = theSctx->get_typemanager();
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
 
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
@@ -280,125 +265,120 @@
       precision_integer = precision->getIntegerValue();
     }
 
-    //get the value and the type of the item
     assert(result->isAtomic());
 
-    type = tm->create_value_type(result);
-
-    //Parameters of type xs:untypedAtomic are always promoted to xs:double
-    if ( TypeOps::is_subtype(tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
-    {
-      GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      type = tm->create_value_type(result);
-    }
-
-    //item type is subtype of DOUBLE
-    if ( TypeOps::is_subtype(tm, *type, *rtm.DOUBLE_TYPE_ONE))
-      GENV_ITEMFACTORY->createDouble(result, result->getDoubleValue().round(precision_integer));
-
-    //item type is subtype of FLOAT
-    else if ( TypeOps::is_subtype(tm, *type, *rtm.FLOAT_TYPE_ONE))
-      GENV_ITEMFACTORY->createFloat(result, result->getFloatValue().round(precision_integer));
-
-    //item type is subtype of INTEGER
-    else if(TypeOps::is_subtype(tm, *type, *rtm.INTEGER_TYPE_ONE))
-    {
-      if(precision_integer.sign() < 0)
-        GENV_ITEMFACTORY->createInteger(result, result->getIntegerValue().round(precision_integer));
-    }
-
-    //item type is subtype of DECIMAL
-    else if (TypeOps::is_subtype(tm, *type, *rtm.DECIMAL_TYPE_ONE))
-      GENV_ITEMFACTORY->createDecimal(result, result->getDecimalValue().round(precision_integer));
-
+    type = result->getTypeCode();
+
+    if (TypeOps::is_subtype(type, store::XS_UNTYPED_ATOMIC))
+    {
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
+    }
+
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
+    {
+      GENV_ITEMFACTORY->
+      createDouble(result, result->getDoubleValue().round(precision_integer));
+    }
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
+    {
+      GENV_ITEMFACTORY->
+      createFloat(result, result->getFloatValue().round(precision_integer));
+    }
+    else if (TypeOps::is_subtype(type, store::XS_INTEGER))
+    {
+      if (precision_integer.sign() < 0)
+        GENV_ITEMFACTORY->
+        createInteger(result, result->getIntegerValue().round(precision_integer));
+    }
+
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
+    {
+      GENV_ITEMFACTORY->
+      createDecimal(result, result->getDecimalValue().round(precision_integer));
+    }
     else
     {
+      xqtref_t type = tm->create_value_type(result);
       RAISE_ERROR(err::XPTY0004, loc,
       ERROR_PARAMS(ZED(BadTypeFor_23), type, "fn:round"));
     }
 
-    if ( consumeNext(item, theChildren[0].getp(), planState ))
-    {
-      RAISE_ERROR(err::XPTY0004, loc,
-      ERROR_PARAMS(ZED(NoSeqForFnOp_2), "fn:round" ));
-    }
-    STACK_PUSH (true, state );
+    STACK_PUSH(true, state);
   }
-  STACK_END (state);
+
+  STACK_END(state);
 }
 
+
 //6.4.5 fn:round-half-to-even
-bool RoundHalfToEvenIterator::nextImpl( store::Item_t &result,
-                                        PlanState& planState ) const {
+bool RoundHalfToEvenIterator::nextImpl(
+    store::Item_t& result,
+    PlanState& planState ) const
+{
   store::Item_t item;
   xs_integer precision;
-  RootTypeManager const &rtm = GENV_TYPESYSTEM;
   TypeManager const *const tm = theSctx->get_typemanager();
-  xqtref_t type;
-
-  PlanIteratorState *state;
-  DEFAULT_STACK_INIT( PlanIteratorState, state, planState );
-
-  if ( consumeNext( result, theChildren [0].getp(), planState ) ) {
-    assert( result->isAtomic() );
-    type = tm->create_value_type( result );
-
-    if ( theChildren.size() == 2 ) {
-      consumeNext( item, theChildren[1].getp(), planState );
-      assert( item->isAtomic() );
+  store::SchemaTypeCode type;
+
+  PlanIteratorState* state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
+
+  if (consumeNext(result, theChildren [0].getp(), planState))
+  {
+    assert(result->isAtomic());
+    type = result->getTypeCode();
+
+    if (theChildren.size() == 2)
+    {
+      consumeNext(item, theChildren[1].getp(), planState);
+      assert(item->isAtomic());
       precision = item->getIntegerValue();
     }
 
-    if ( TypeOps::is_subtype( tm, *type, *rtm.UNTYPED_ATOMIC_TYPE_ONE ) ) {
+    if (TypeOps::is_subtype(type, store::XS_UNTYPED_ATOMIC))
+    {
       //
       // XQuery 3.0 3.1.5.2: Each item in the atomic sequence that is of type
       // xs:untypedAtomic is cast to the expected generalized atomic type. For
       // built-in functions where the expected type is specified as numeric,
       // arguments of type xs:untypedAtomic are cast to xs:double.
       //
-      GenericCast::castToAtomic(
-        result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc
-      );
-      type = tm->create_value_type( result );
-    }
-
-    if ( TypeOps::is_subtype( tm, *type, *rtm.DOUBLE_TYPE_ONE ) )
-      GENV_ITEMFACTORY->createDouble(
-        result, result->getDoubleValue().roundHalfToEven( precision )
-      );
-
-    else if ( TypeOps::is_subtype( tm, *type, *rtm.FLOAT_TYPE_ONE ) )
-      GENV_ITEMFACTORY->createFloat(
-        result, result->getFloatValue().roundHalfToEven( precision )
-      );
-
-    else if ( TypeOps::is_subtype( tm, *type, *rtm.INTEGER_TYPE_ONE ) )
-      GENV_ITEMFACTORY->createInteger(
-        result, result->getIntegerValue().roundHalfToEven( precision )
-      );
-
-    else if ( TypeOps::is_subtype( tm, *type, *rtm.DECIMAL_TYPE_ONE ) )
-      GENV_ITEMFACTORY->createDecimal(
-        result, result->getDecimalValue().roundHalfToEven( precision )
-      );
-
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      type = store::XS_DOUBLE;
+    }
+
+    if (TypeOps::is_subtype(type, store::XS_DOUBLE))
+    {
+      GENV_ITEMFACTORY->
+      createDouble(result, result->getDoubleValue().roundHalfToEven(precision));
+    }
+    else if (TypeOps::is_subtype(type, store::XS_FLOAT))
+    {
+      GENV_ITEMFACTORY->
+      createFloat(result, result->getFloatValue().roundHalfToEven(precision));
+    }
+    else if (TypeOps::is_subtype(type, store::XS_INTEGER))
+    {
+      GENV_ITEMFACTORY->
+      createInteger(result, result->getIntegerValue().roundHalfToEven(precision));
+    }
+    else if (TypeOps::is_subtype(type, store::XS_DECIMAL))
+    {
+      GENV_ITEMFACTORY->
+      createDecimal(result, result->getDecimalValue().roundHalfToEven(precision));
+    }
     else
-      throw XQUERY_EXCEPTION(
-        err::XPTY0004,
-        ERROR_PARAMS( ZED( BadTypeFor_23 ), type, "fn:round-half-to-even" ),
-        ERROR_LOC( loc )
-      );
+    {
+      xqtref_t type = tm->create_value_type(result);
+      RAISE_ERROR(err::XPTY0004, loc,
+      ERROR_PARAMS(ZED(BadTypeFor_23), type, "fn:round-half-to-even"));
+    }
 
-    if ( consumeNext( item, theChildren [0].getp(), planState ) ) {
-      throw XQUERY_EXCEPTION(
-        err::XPTY0004,
-        ERROR_PARAMS( ZED( NoSeqForFnOp_2 ), "fn:round-half-to-even" ),
-        ERROR_LOC( loc )
-      );
-    }
-    STACK_PUSH( true, state );
+    STACK_PUSH(true, state);
   }
-  STACK_END( state );
+
+  STACK_END(state);
 }
 
 } // namespace zorba

=== modified file 'src/runtime/sequences/SequencesImpl.cpp'
--- src/runtime/sequences/SequencesImpl.cpp	2013-05-15 23:22:01 +0000
+++ src/runtime/sequences/SequencesImpl.cpp	2013-06-11 05:41:36 +0000
@@ -62,35 +62,6 @@
 
 NARY_ACCEPT(FnMinMaxIterator);
 
-namespace sequences_impl_ns{
-static XQPCollator* getCollator(
-    static_context* sctx,
-    const QueryLoc& loc,
-    PlanState& planState,
-    const PlanIterator* iter)
-{
-  store::Item_t lCollationItem;
-  store::Item_t temp;
-
-  if (!PlanIterator::consumeNext(lCollationItem, iter, planState))
-    throw XQUERY_EXCEPTION(
-      err::XPTY0004,
-      ERROR_PARAMS( ZED( NoEmptySeqAsCollationParam ) ),
-      ERROR_LOC( loc )
-    );
-
-  if (PlanIterator::consumeNext(temp, iter, planState))
-    throw XQUERY_EXCEPTION(
-      err::XPTY0004,
-      ERROR_PARAMS( ZED( NoSeqAsCollationParam ) ),
-      ERROR_LOC( loc )
-    );
-
-  xqtref_t lCollationItemType = sctx->get_typemanager()->create_value_type(lCollationItem);
-
-  return sctx->get_collator(lCollationItem->getStringValue().str(), loc);
-}
-}
 
 /////////////////////////////////////////////////////////////////////////////////
 //                                                                             //
@@ -121,104 +92,108 @@
 bool
 FnMinMaxIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
-  store::Item_t lRunningItem = NULL;
-  xqtref_t lMaxType;
+  store::Item_t runningItem = NULL;
+  store::SchemaTypeCode maxType = store::XS_LAST;
 
   const TypeManager* tm = theSctx->get_typemanager();
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
-
   long timezone = planState.theLocalDynCtx->get_implicit_timezone();
-  XQPCollator*  lCollator = 0;
+  XQPCollator* collator = 0;
   unsigned elems_in_seq = 0;
   result = NULL;
 
   try
   {
-  PlanIteratorState* state;
-  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
-
-  if (theChildren.size() == 2)
-    lCollator = sequences_impl_ns::getCollator(theSctx, loc, planState, theChildren[1].getp());
-  else
-    lCollator = theSctx->get_default_collator(loc);
-
-  if (consumeNext(lRunningItem, theChildren[0].getp(), planState))
-  {
-    do
-    {
-      // casting of untyped atomic
-      xqtref_t lRunningType = tm->create_value_type(lRunningItem);
-
-      if (TypeOps::is_subtype(tm, *lRunningType, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
-      {
-        GenericCast::castToAtomic(lRunningItem,
-                                  lRunningItem,
-                                  &*rtm.DOUBLE_TYPE_ONE,
-                                  tm,
-                                  NULL,
-                                  loc);
-        lRunningType = rtm.DOUBLE_TYPE_ONE;
-      }
-
-      // implementation dependent: return the first occurence)
-      if (lRunningItem->isNaN())
-      {
-        /** It must be checked if the sequence contains any
-         * xs:double("NaN") [xs:double("NaN") is returned] or
-         * only xs:float("NaN")'s [xs:float("NaN") is returned]'.
-         */
-        result = lRunningItem;
-        if (TypeOps::is_subtype(tm, *lRunningType, *rtm.DOUBLE_TYPE_ONE))
-          break;
-
-        lMaxType = tm->create_value_type(result);
-      }
-
-      if (result != 0)
-      {
-        // Type Promotion
-        store::Item_t lItemCur;
-        if (!GenericCast::promote(lItemCur, lRunningItem, &*lMaxType, NULL, tm, loc))
-        {
-          if (GenericCast::promote(lItemCur, result, &*lRunningType, NULL, tm, loc))
+    PlanIteratorState* state;
+    DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
+
+    if (theChildren.size() == 2)
+    {
+      store::Item_t collationItem;
+      consumeNext(collationItem, theChildren[1], planState);
+      collator = theSctx->get_collator(collationItem->getStringValue().str(), loc);
+    }
+    else
+    {
+      collator = theSctx->get_default_collator(loc);
+    }
+
+    if (consumeNext(runningItem, theChildren[0], planState))
+    {
+      do
+      {
+        // casting of untyped atomic
+        store::SchemaTypeCode runningType = runningItem->getTypeCode();
+
+        if (runningType == store::XS_UNTYPED_ATOMIC)
+        {
+          GenericCast::castToBuiltinAtomic(runningItem,
+                                           runningItem,
+                                           store::XS_DOUBLE,
+                                           NULL,
+                                           loc);
+          runningType = store::XS_DOUBLE;
+        }
+
+        // implementation dependent: return the first occurence)
+        if (runningItem->isNaN())
+        {
+          // It must be checked if the sequence contains any
+          // xs:double("NaN") [xs:double("NaN") is returned] or
+          // only xs:float("NaN")'s [xs:float("NaN") is returned]'.
+
+          result = runningItem;
+          if (TypeOps::is_subtype(runningType, store::XS_DOUBLE))
+            break;
+
+          maxType = runningType;
+        }
+
+        if (result != 0)
+        {
+          // Type Promotion
+          store::Item_t lItemCur;
+          if (!GenericCast::promote(lItemCur, runningItem, maxType, NULL, tm, loc))
           {
-            result.transfer(lItemCur);
-            lMaxType = tm->create_value_type(result);
+            if (GenericCast::promote(lItemCur, result, runningType, NULL, tm, loc))
+            {
+              result.transfer(lItemCur);
+              maxType = result->getTypeCode();
+            }
+            else
+            {
+              RAISE_ERROR(err::FORG0006, loc,
+						  ERROR_PARAMS(ZED(PromotionImpossible)));
+            }
           }
           else
           {
-						RAISE_ERROR(err::FORG0006, loc,
-						ERROR_PARAMS(ZED(PromotionImpossible)));
+            runningItem.transfer(lItemCur);
+            runningType = runningItem->getTypeCode();
+          }
+ 
+          store::Item_t current_copy(runningItem);
+          store::Item_t max_copy(result);
+          if (CompareIterator::valueComparison(loc,
+                                               current_copy,
+                                               max_copy,
+                                               theCompareType,
+                                               tm,
+                                               timezone,
+                                               collator))
+          {
+            maxType = runningType;
+            result.transfer(runningItem);
           }
         }
         else
         {
-          lRunningItem.transfer(lItemCur);
-          lRunningType = tm->create_value_type(lRunningItem);
-        }
-
-        store::Item_t current_copy(lRunningItem);
-        store::Item_t max_copy(result);
-        if (CompareIterator::valueComparison(loc,
-                                             current_copy,
-                                             max_copy,
-                                             theCompareType,
-                                             tm,
-                                             timezone,
-                                             lCollator) )
-        {
-          lMaxType = lRunningType;
-          result.transfer(lRunningItem);
-        }
-      }
-      else
-      {
-        lMaxType = lRunningType;
-        result.transfer(lRunningItem);
-      }
-
-      elems_in_seq++;
-    } while (consumeNext(lRunningItem, theChildren[0].getp(), planState));
+          maxType = runningType;
+          result.transfer(runningItem);
+        }
+
+        elems_in_seq++;
+      }
+      while (consumeNext(runningItem, theChildren[0], planState));
 
     if (elems_in_seq == 1)
     {
@@ -231,7 +206,7 @@
                                        theCompareType,
                                        tm,
                                        timezone,
-                                       lCollator);
+                                       collator);
     }
 
     STACK_PUSH(result != NULL, state);

=== modified file 'src/runtime/sequences/sequences_impl.cpp'
--- src/runtime/sequences/sequences_impl.cpp	2013-06-05 00:37:35 +0000
+++ src/runtime/sequences/sequences_impl.cpp	2013-06-11 05:41:36 +0000
@@ -1438,7 +1438,7 @@
 {
   store::Item_t lSumItem;
   store::Item_t lRunningItem;
-  xqtref_t      lRunningType;
+  store::SchemaTypeCode lRunningType;
   store::Item_t countItem;
   int lCount = 0;
   bool lHitNumeric = false, lHitYearMonth = false, lHitDayTime = false;
@@ -1446,86 +1446,101 @@
   const TypeManager* tm = theSctx->get_typemanager();
   const RootTypeManager& rtm = GENV_TYPESYSTEM;
 
-  xqtref_t lUntypedAtomic     = rtm.UNTYPED_ATOMIC_TYPE_ONE;
-  xqtref_t lYearMonthDuration = rtm.YM_DURATION_TYPE_ONE;
-  xqtref_t lDayTimeDuration   = rtm.DT_DURATION_TYPE_ONE;
-
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
 
   while (consumeNext(lRunningItem, theChildren[0].getp(), planState))
   {
-    lRunningType = tm->create_value_type(lRunningItem);
+    lRunningType = lRunningItem->getTypeCode();
 
-    if (TypeOps::is_numeric(tm, *lRunningType) ||
-        TypeOps::is_equal(tm, *lRunningType, *lUntypedAtomic))
+    if (TypeOps::is_numeric(lRunningType) ||
+        lRunningType == store::XS_UNTYPED_ATOMIC)
     {
       lHitNumeric = true;
 
-      if ( lHitYearMonth )
+      if (lHitYearMonth)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
 				ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED(ExpectedType_5),
-                     *lYearMonthDuration));
+                     *rtm.YM_DURATION_TYPE_ONE));
+      }
 
-      if ( lHitDayTime )
+      if (lHitDayTime)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
         ERROR_PARAMS(ZED( BadArgTypeForFn_2o34o ),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED( ExpectedType_5 ),
-                     *lDayTimeDuration));
+                     *rtm.DT_DURATION_TYPE_ONE));
+      }
     }
-    else if (TypeOps::is_equal(tm, *lRunningType, *lYearMonthDuration))
+    else if (lRunningType == store::XS_YM_DURATION)
     {
       lHitYearMonth = true;
 
       if (lHitNumeric)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
         ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED(ExpectedNumericType)));
+      }
 
       if (lHitDayTime)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
         ERROR_PARAMS(ZED( BadArgTypeForFn_2o34o ),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED( ExpectedType_5 ),
-                     *lDayTimeDuration));
+                     *rtm.DT_DURATION_TYPE_ONE));
+      }
     }
-    else if (TypeOps::is_equal(tm, *lRunningType, *lDayTimeDuration))
+    else if (lRunningType == store::XS_DT_DURATION)
     {
       lHitDayTime = true;
 
-      if ( lHitNumeric )
+      if (lHitNumeric)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
         ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED(ExpectedNumericType)));
+      }
 
-      if ( lHitYearMonth )
+      if (lHitYearMonth)
+      {
+        xqtref_t type = tm->create_value_type(lRunningItem);
         RAISE_ERROR(err::FORG0006, loc,
         ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o),
-                     *lRunningType,
+                     *type,
                      "fn:avg",
                      ZED(ExpectedType_5),
-                     *lYearMonthDuration));
+                     *rtm.YM_DURATION_TYPE_ONE));
+      }
     }
     else
     {
+      xqtref_t type = tm->create_value_type(lRunningItem);
 			RAISE_ERROR(err::FORG0006, loc,
 			ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o),
-                   *lRunningType,
+                   *type,
                    "fn:avg",
                    ZED(ExpectedNumericOrDurationType)));
     }
 
-    if ( lCount++ == 0 )
+    if (lCount++ == 0)
     {
       lSumItem = lRunningItem;
     }
@@ -1559,17 +1574,17 @@
   STACK_END (state);
 }
 
+
 /*******************************************************************************
   15.4.5 fn:sum - Generic
 ********************************************************************************/
 bool FnSumIterator::nextImpl(store::Item_t& result, PlanState& planState) const
 {
   store::Item_t lRunningItem;
-  xqtref_t      lResultType;
-  xqtref_t      lRunningType;
+  store::SchemaTypeCode lResultType;
+  store::SchemaTypeCode lRunningType;
 
   const TypeManager* tm = theSctx->get_typemanager();
-  const RootTypeManager& rtm = GENV_TYPESYSTEM;
 
   PlanIteratorState* state;
   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
@@ -1577,37 +1592,37 @@
   if (consumeNext(result, theChildren[0].getp(), planState))
   {
     // casting of untyped atomic
-    lResultType = tm->create_value_type(result);
+    lResultType = result->getTypeCode();
 
-    if (TypeOps::is_subtype(tm, *lResultType, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+    if (lResultType == store::XS_UNTYPED_ATOMIC)
     {
-      GenericCast::castToAtomic(result, result, &*rtm.DOUBLE_TYPE_ONE, tm, NULL, loc);
-      lResultType = rtm.DOUBLE_TYPE_ONE;
+      GenericCast::castToBuiltinAtomic(result, result, store::XS_DOUBLE, NULL, loc);
+      lResultType = store::XS_DOUBLE;
     }
 
-    if (!TypeOps::is_numeric(tm, *lResultType) &&
-        (!TypeOps::is_subtype(tm, *lResultType, *rtm.DURATION_TYPE_ONE) ||
-         TypeOps::is_equal(tm, *lResultType, *rtm.DURATION_TYPE_ONE)))
+    if (!TypeOps::is_numeric(lResultType) &&
+        (!TypeOps::is_subtype(lResultType, store::XS_DURATION) ||
+         lResultType == store::XS_DURATION))
     {
+      xqtref_t type = tm->create_value_type(result);
       RAISE_ERROR(err::FORG0006, loc,
-			ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o), *lResultType, "fn:sum"));
+			ERROR_PARAMS(ZED(BadArgTypeForFn_2o34o), *type, "fn:sum"));
     }
 
     while (consumeNext(lRunningItem, theChildren[0].getp(), planState))
     {
       // casting of untyped atomic
-      lRunningType = tm->create_value_type(lRunningItem);
+      lRunningType = lRunningItem->getTypeCode();
 
-      if (TypeOps::is_subtype(tm, *lRunningType, *rtm.UNTYPED_ATOMIC_TYPE_ONE))
+      if (lRunningType == store::XS_UNTYPED_ATOMIC)
       {
-        GenericCast::castToAtomic(lRunningItem,
-                                  lRunningItem,
-                                  &*rtm.DOUBLE_TYPE_ONE,
-                                  tm,
-                                  NULL,
-                                  loc);
+        GenericCast::castToBuiltinAtomic(lRunningItem,
+                                         lRunningItem,
+                                         store::XS_DOUBLE,
+                                         NULL,
+                                         loc);
 
-        lRunningType = rtm.DOUBLE_TYPE_ONE;
+        lRunningType = store::XS_DOUBLE;
       }
 
       // handling of NaN
@@ -1617,12 +1632,12 @@
         break;
       }
 
-      if((TypeOps::is_numeric(tm, *lResultType) &&
-          TypeOps::is_numeric(tm, *lRunningType)) ||
-         (TypeOps::is_subtype(tm, *lResultType, *rtm.YM_DURATION_TYPE_ONE) &&
-          TypeOps::is_subtype(tm, *lRunningType, *rtm.YM_DURATION_TYPE_ONE)) ||
-         (TypeOps::is_subtype(tm, *lResultType, *rtm.DT_DURATION_TYPE_ONE) &&
-          TypeOps::is_subtype(tm, *lRunningType, *rtm.DT_DURATION_TYPE_ONE)))
+      if ((TypeOps::is_numeric(lResultType) &&
+           TypeOps::is_numeric(lRunningType)) ||
+          (TypeOps::is_subtype(lResultType, store::XS_YM_DURATION) &&
+           TypeOps::is_subtype(lRunningType, store::XS_YM_DURATION)) ||
+          (TypeOps::is_subtype(lResultType, store::XS_DT_DURATION) &&
+           TypeOps::is_subtype(lRunningType, store::XS_DT_DURATION)))
       {
         GenericArithIterator<AddOperation>::compute(result,
                                                     planState.theLocalDynCtx,
@@ -1633,13 +1648,10 @@
       }
       else
       {
-				throw XQUERY_EXCEPTION(
-					err::FORG0006,
-					ERROR_PARAMS(
-						ZED( SumImpossibleWithTypes_23 ), *lResultType, *lRunningType
-					),
-					ERROR_LOC( loc )
-				);
+        xqtref_t type1 = tm->create_value_type(result);
+        xqtref_t type2 = tm->create_value_type(lRunningItem);
+				RAISE_ERROR(err::FORG0006, loc,
+				ERROR_PARAMS(ZED( SumImpossibleWithTypes_23 ), *type1, *type2));
       }
     }
 
@@ -1657,18 +1669,16 @@
     }
     else
     {
-      STACK_PUSH(
-				GENV_ITEMFACTORY->createInteger(
-          result, numeric_consts<xs_integer>::zero()
-        ),
-        state
-			);
+      STACK_PUSH(GENV_ITEMFACTORY->
+                 createInteger(result, numeric_consts<xs_integer>::zero()),
+                 state);
     }
   }
 
-  STACK_END (state);
+  STACK_END(state);
 }
 
+
 /*******************************************************************************
   15.4.5 fn:sum - Double
 ********************************************************************************/
@@ -1766,6 +1776,7 @@
   STACK_END (state);
 }
 
+
 /*******************************************************************************
   15.4.5 fn:sum - Decimal
 ********************************************************************************/
@@ -1814,6 +1825,7 @@
   STACK_END (state);
 }
 
+
 /*******************************************************************************
   15.4.5 fn:sum - Integer
 ********************************************************************************/
@@ -1823,8 +1835,8 @@
 {
   xs_integer    sum;
   store::Item_t item;
-  xqtref_t      lResultType;
-  xqtref_t      lTmpType;
+  store::SchemaTypeCode lResultType;
+  store::SchemaTypeCode lTmpType;
 
   const TypeManager* tm = theSctx->get_typemanager();
 
@@ -1833,13 +1845,15 @@
 
   if (consumeNext(item, theChildren[0].getp(), planState))
   {
-    lResultType = tm->create_value_type(item);
+    lResultType = item->getTypeCode();
+
     sum = item->getIntegerValue();
 
     while (consumeNext(item, theChildren[0].getp(), planState))
     {
-      lTmpType = tm->create_value_type(item);
-      if(TypeOps::is_subtype(tm, *lResultType, *lTmpType))
+      lTmpType = item->getTypeCode();
+
+      if (TypeOps::is_subtype(lResultType, lTmpType))
         lResultType = lTmpType;
 
       if (item->isNaN())
@@ -1852,7 +1866,8 @@
     }
 
     GENV_ITEMFACTORY->createInteger(result, sum);
-    GenericCast::castToAtomic(result, result, &*lResultType, tm, NULL, loc);
+
+    GenericCast::castToBuiltinAtomic(result, result, lResultType, NULL, loc);
 
     STACK_PUSH(true, state);
   }
@@ -1872,6 +1887,7 @@
   STACK_END (state);
 }
 
+
 /*******************************************************************************
   15.5.1 op:to
 ********************************************************************************/
@@ -1906,6 +1922,7 @@
   STACK_END (state);
 }
 
+
 /*******************************************************************************
   15.5.4 fn:doc
 ********************************************************************************/

=== modified file 'src/runtime/store/maps_impl.cpp'
--- src/runtime/store/maps_impl.cpp	2013-05-08 20:14:47 +0000
+++ src/runtime/store/maps_impl.cpp	2013-06-11 05:41:36 +0000
@@ -77,10 +77,7 @@
               loc ) )
         {
           RAISE_ERROR(zerr::ZXQD0005_INVALID_KEY_FOR_MAP, loc,
-            ERROR_PARAMS(*searchKeyType,
-                         *indexKeyType,
-                         aQName->getStringValue())
-          );
+          ERROR_PARAMS(*searchKeyType, *indexKeyType, aQName->getStringValue()));
         }
         else
         {
@@ -91,10 +88,7 @@
         if ( e.diagnostic() == err::FORG0001 )
         {
           RAISE_ERROR(zerr::ZXQD0005_INVALID_KEY_FOR_MAP, loc,
-            ERROR_PARAMS(*searchKeyType,
-                         *indexKeyType,
-                         aQName->getStringValue())
-          );
+          ERROR_PARAMS(*searchKeyType, *indexKeyType, aQName->getStringValue()));
         }
         throw;
       }

=== modified file 'src/types/typeops.cpp'
--- src/types/typeops.cpp	2013-06-03 23:03:58 +0000
+++ src/types/typeops.cpp	2013-06-11 05:41:36 +0000
@@ -1151,7 +1151,6 @@
 
   RootTypeManager& rtm = GENV_TYPESYSTEM;
 
-  xqtref_t resultType;
   TypeConstants::quantifier_t resultQuant = TypeConstants::QUANT_ONE;
 
   TypeConstants::quantifier_t quant1 = type1.get_quantifier();
@@ -1166,43 +1165,43 @@
   }
 
   if (division &&
-      TypeOps::is_subtype(tm, type1, *rtm.INTEGER_TYPE_STAR) &&
-      TypeOps::is_subtype(tm, type2, *rtm.INTEGER_TYPE_STAR))
+      is_subtype(tm, type1, *rtm.INTEGER_TYPE_STAR) &&
+      is_subtype(tm, type2, *rtm.INTEGER_TYPE_STAR))
   {
     return (resultQuant == TypeConstants::QUANT_ONE ?
             rtm.DECIMAL_TYPE_ONE : rtm.DECIMAL_TYPE_QUESTION); 
   }
 
-  if (TypeOps::is_subtype(tm, type1, *rtm.UNTYPED_ATOMIC_TYPE_STAR) ||
-      TypeOps::is_subtype(tm, type2, *rtm.UNTYPED_ATOMIC_TYPE_STAR)) 
-  {
-    return (resultQuant == TypeConstants::QUANT_ONE ?
-            rtm.DOUBLE_TYPE_ONE : rtm.DOUBLE_TYPE_QUESTION);
-  }
- 
-  if (TypeOps::is_subtype(tm, type1, *rtm.DOUBLE_TYPE_STAR) ||
-      TypeOps::is_subtype(tm, type2, *rtm.DOUBLE_TYPE_STAR)) 
-  {
-    return (resultQuant == TypeConstants::QUANT_ONE ?
-            rtm.DOUBLE_TYPE_ONE : rtm.DOUBLE_TYPE_QUESTION);
-  }
- 
-  if (TypeOps::is_subtype(tm, type1, *rtm.FLOAT_TYPE_STAR) ||
-      TypeOps::is_subtype(tm, type2, *rtm.FLOAT_TYPE_STAR)) 
+  if (is_subtype(tm, type1, *rtm.UNTYPED_ATOMIC_TYPE_STAR) ||
+      is_subtype(tm, type2, *rtm.UNTYPED_ATOMIC_TYPE_STAR)) 
+  {
+    return (resultQuant == TypeConstants::QUANT_ONE ?
+            rtm.DOUBLE_TYPE_ONE : rtm.DOUBLE_TYPE_QUESTION);
+  }
+ 
+  if (is_subtype(tm, type1, *rtm.DOUBLE_TYPE_STAR) ||
+      is_subtype(tm, type2, *rtm.DOUBLE_TYPE_STAR)) 
+  {
+    return (resultQuant == TypeConstants::QUANT_ONE ?
+            rtm.DOUBLE_TYPE_ONE : rtm.DOUBLE_TYPE_QUESTION);
+  }
+ 
+  if (is_subtype(tm, type1, *rtm.FLOAT_TYPE_STAR) ||
+      is_subtype(tm, type2, *rtm.FLOAT_TYPE_STAR)) 
   {
     return (resultQuant == TypeConstants::QUANT_ONE ?
             rtm.FLOAT_TYPE_ONE : rtm.FLOAT_TYPE_QUESTION);
   }
 
-  if (TypeOps::is_subtype(tm, type1, *rtm.INTEGER_TYPE_STAR) &&
-      TypeOps::is_subtype(tm, type2, *rtm.INTEGER_TYPE_STAR)) 
+  if (is_subtype(tm, type1, *rtm.INTEGER_TYPE_STAR) &&
+      is_subtype(tm, type2, *rtm.INTEGER_TYPE_STAR)) 
   {
     return (resultQuant == TypeConstants::QUANT_ONE ?
             rtm.INTEGER_TYPE_ONE : rtm.INTEGER_TYPE_QUESTION);
   }
 
-  if (TypeOps::is_subtype(tm, type1, *rtm.DECIMAL_TYPE_STAR) &&
-      TypeOps::is_subtype(tm, type2, *rtm.DECIMAL_TYPE_STAR)) 
+  if (is_subtype(tm, type1, *rtm.DECIMAL_TYPE_STAR) &&
+      is_subtype(tm, type2, *rtm.DECIMAL_TYPE_STAR)) 
   {
     return (resultQuant == TypeConstants::QUANT_ONE ?
             rtm.DECIMAL_TYPE_ONE : rtm.DECIMAL_TYPE_QUESTION); 
@@ -1215,6 +1214,50 @@
 /*******************************************************************************
 
 ********************************************************************************/
+store::SchemaTypeCode TypeOps::arithmetic_type(
+    store::SchemaTypeCode type1,
+    store::SchemaTypeCode type2,
+    bool division)
+{
+  if (division &&
+      is_subtype(type1, store::XS_INTEGER) &&
+      is_subtype(type2, store::XS_INTEGER))
+  {
+    return store::XS_DECIMAL;
+  }
+
+  if (type1 == store::XS_UNTYPED_ATOMIC || type2 == store::XS_UNTYPED_ATOMIC) 
+  {
+    return store::XS_DOUBLE;
+  }
+ 
+  if (is_subtype(type1, store::XS_DOUBLE) || is_subtype(type2, store::XS_DOUBLE))
+  {
+    return store::XS_DOUBLE;
+  }
+ 
+  if (is_subtype(type1, store::XS_FLOAT) || is_subtype(type2, store::XS_FLOAT)) 
+  {
+    return store::XS_FLOAT;
+  }
+
+  if (is_subtype(type1, store::XS_INTEGER) && is_subtype(type2, store::XS_INTEGER)) 
+  {
+    return store::XS_INTEGER;
+  }
+
+  if (is_subtype(type1, store::XS_DECIMAL) && is_subtype(type2, store::XS_DECIMAL)) 
+  {
+    return store::XS_DECIMAL;
+  }
+
+  return store::XS_ANY_ATOMIC;
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
 static inline IdentTypes::quantifier_t get_typeident_quant(
     TypeConstants::quantifier_t quant)
 {

=== modified file 'src/types/typeops.h'
--- src/types/typeops.h	2013-02-07 17:24:36 +0000
+++ src/types/typeops.h	2013-06-11 05:41:36 +0000
@@ -181,6 +181,11 @@
         const XQType& type2,
         bool division);
 
+  static store::SchemaTypeCode arithmetic_type(
+    store::SchemaTypeCode type1,
+    store::SchemaTypeCode type2,
+    bool division);
+
   /*
    * Returns a type identifier that represents the given type.
    * The invariant that is guaranteed is:


Follow ups