Merge branch 'zheilbron/asterix_msr_demo' of https://code.google.com/p/asterixdb into zheilbron/asterix_msr_demo
diff --git a/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index-open.aql b/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index-open.aql
index b4a5796..14d31f0 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index-open.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index-open.aql
@@ -29,5 +29,5 @@
 write output to nc1:"rttest/index_rtree-secondary-index-open.adm";
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(4.0,1.0), create-point(4.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([4.0,1.0,4.0,4.0,12.0,4.0,12.0,1.0]))
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index.aql b/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index.aql
index 41a5a91..be95f18 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/rtree-secondary-index.aql
@@ -29,5 +29,5 @@
 write output to nc1:"rttest/index_rtree-secondary-index.adm";
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(4.0,1.0), create-point(4.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([4.0,1.0,4.0,4.0,12.0,4.0,12.0,1.0]))
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index-open.plan b/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index-open.plan
index 1845ed7..993dd23 100644
--- a/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index-open.plan
+++ b/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index-open.plan
@@ -8,11 +8,11 @@
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                 -- BTREE_SEARCH  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    -- STABLE_SORT [$$22(ASC)]  |PARTITIONED|
+                    -- STABLE_SORT [$$19(ASC)]  |PARTITIONED|
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                         -- STREAM_PROJECT  |PARTITIONED|
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                             -- RTREE_SEARCH  |PARTITIONED|
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                 -- ASSIGN  |PARTITIONED|
-                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index.plan b/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index.plan
index 1845ed7..993dd23 100644
--- a/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index.plan
+++ b/asterix-app/src/test/resources/optimizerts/results/rtree-secondary-index.plan
@@ -8,11 +8,11 @@
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                 -- BTREE_SEARCH  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    -- STABLE_SORT [$$22(ASC)]  |PARTITIONED|
+                    -- STABLE_SORT [$$19(ASC)]  |PARTITIONED|
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                         -- STREAM_PROJECT  |PARTITIONED|
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                             -- RTREE_SEARCH  |PARTITIONED|
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                 -- ASSIGN  |PARTITIONED|
-                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable/scan-delete-rtree-secondary-index-nullable.5.query.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable/scan-delete-rtree-secondary-index-nullable.5.query.aql
index e453b2a..87fc60d 100644
--- a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable/scan-delete-rtree-secondary-index-nullable.5.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index-nullable/scan-delete-rtree-secondary-index-nullable.5.query.aql
@@ -8,6 +8,6 @@
 use dataverse test;
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([0.0,1.0,0.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index/scan-delete-rtree-secondary-index.5.query.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index/scan-delete-rtree-secondary-index.5.query.aql
index 5b8dbf7..a8d43ff 100644
--- a/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index/scan-delete-rtree-secondary-index.5.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-delete-rtree-secondary-index/scan-delete-rtree-secondary-index.5.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([0.0,1.0,0.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable/scan-insert-rtree-secondary-index-nullable.5.query.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable/scan-insert-rtree-secondary-index-nullable.5.query.aql
index d3e9cbb..5f828b3 100644
--- a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable/scan-insert-rtree-secondary-index-nullable.5.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index-nullable/scan-insert-rtree-secondary-index-nullable.5.query.aql
@@ -8,6 +8,6 @@
 use dataverse test;
 
 for $o in dataset('MyMiniData')
-where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([0.0,1.0,0.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index/scan-insert-rtree-secondary-index.5.query.aql b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index/scan-insert-rtree-secondary-index.5.query.aql
index 412f7da..fce3d9b 100644
--- a/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index/scan-insert-rtree-secondary-index.5.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/scan-insert-rtree-secondary-index/scan-insert-rtree-secondary-index.5.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
 for $o in dataset('MyMiniData')
-where spatial-intersect($o.point, create-polygon(create-point(0.0,1.0), create-point(0.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([0.0,1.0,0.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-nullable/rtree-secondary-index-nullable.4.query.aql b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-nullable/rtree-secondary-index-nullable.4.query.aql
index 9d067bc..0589145 100644
--- a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-nullable/rtree-secondary-index-nullable.4.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-nullable/rtree-secondary-index-nullable.4.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(4.0,1.0), create-point(4.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([4.0,1.0,4.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-open/rtree-secondary-index-open.4.query.aql b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-open/rtree-secondary-index-open.4.query.aql
index 9d067bc..0589145 100644
--- a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-open/rtree-secondary-index-open.4.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index-open/rtree-secondary-index-open.4.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(4.0,1.0), create-point(4.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([4.0,1.0,4.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index/rtree-secondary-index.4.query.aql b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index/rtree-secondary-index.4.query.aql
index 9d067bc..0589145 100644
--- a/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index/rtree-secondary-index.4.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/index-selection/rtree-secondary-index/rtree-secondary-index.4.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
 for $o in dataset('MyData')
-where spatial-intersect($o.point, create-polygon(create-point(4.0,1.0), create-point(4.0,4.0), create-point(12.0,4.0), create-point(12.0,1.0)))
+where spatial-intersect($o.point, create-polygon([4.0,1.0,4.0,4.0,12.0,4.0,12.0,1.0]))
 order by $o.id
 return {"id":$o.id}
diff --git a/asterix-app/src/test/resources/runtimets/queries/spatial/polygon_accessor/polygon_accessor.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/spatial/polygon_accessor/polygon_accessor.3.query.aql
index 54ccf92..c00ff16 100644
--- a/asterix-app/src/test/resources/runtimets/queries/spatial/polygon_accessor/polygon_accessor.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/spatial/polygon_accessor/polygon_accessor.3.query.aql
@@ -6,7 +6,7 @@
 
 use dataverse test;
 
-let $polygon := create-polygon(create-point(1.0,1.0), create-point(2.0,2.0), create-point(3.0,3.0), create-point(4.0,4.0))
+let $polygon := create-polygon([1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0])
 let $polygon_list := get-points($polygon)
 for $p in $polygon_list
 return $p
diff --git a/asterix-app/src/test/resources/runtimets/queries/spatial/spatial-area/spatial-area.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/spatial/spatial-area/spatial-area.3.query.aql
index 940dfe8..72a02a8 100644
--- a/asterix-app/src/test/resources/runtimets/queries/spatial/spatial-area/spatial-area.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/spatial/spatial-area/spatial-area.3.query.aql
@@ -1,6 +1,6 @@
 use dataverse test;
 
-let $polygonArea := spatial-area(create-polygon(create-point(1.0,1.0),create-point(1.0,4.0),create-point(3.0,4.0),create-point(3.0,1.0)))
+let $polygonArea := spatial-area(create-polygon([1.0,1.0,1.0,4.0,3.0,4.0,3.0,1.0]))
 let $circleArea := spatial-area(create-circle(create-point(0.0,0.0), 1.0))
 let $rectangleArea := spatial-area(create-rectangle(create-point(0.0,5.0), create-point(8.0,8.0)))
 return {"polygonArea":$polygonArea, "circleArea":$circleArea, "rectangleArea":$rectangleArea}
diff --git a/asterix-doc/src/site/markdown/aql/functions.md b/asterix-doc/src/site/markdown/aql/functions.md
index b140c2c..0c183a9 100644
--- a/asterix-doc/src/site/markdown/aql/functions.md
+++ b/asterix-doc/src/site/markdown/aql/functions.md
@@ -553,19 +553,19 @@
 ### create-polygon ###
  * Syntax:
 
-        create-polygon(point_expression1, point_expression2, point_expression3, […, point_expressionn])
+        create-polygon(list_expression)
 
- * Creates the primitive type `polygon` using unlimited number of arguments `point_expression1`, `point_expression2`, ..., `point_expressionn`. Note that at least three points should be specified.
+ * Creates the primitive type `polygon` using the double values provided in the argument `list_expression`. Each two consecutive double values represent a point starting from the first double value in the list. Note that at least six double values should be specified, meaning a total of three points.
  * Arguments:
-   * `point_expression1`/.../`point_expressionn` : A `point` that represents a vertex of the polygon.
+   * `list_expression` : An OrderedList of doubles representing the points of the polygon. 
  * Return Value:
-   * A `polygon`, represents a spatial simple polygon created using the points provided in `point_expression1`, `point_expression2`, ..., `point_expressionn`.
+   * A `polygon`, represents a spatial simple polygon created using the points provided in `list_expression`.
 
  * Example:
 
         use dataverse TinySocial;
         
-        let $c :=  create-polygon(create-point(1.0,1.0), create-point(2.0,2.0), create-point(3.0,3.0), create-point(4.0,4.0))
+        let $c :=  create-polygon([1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0])
         return {"polygon": $c}
 
 
@@ -740,7 +740,7 @@
         
         let $line := create-line(create-point(100.6,99.4), create-point(-72.0,-76.9))
         let $rectangle := create-rectangle(create-point(9.2,49.0), create-point(77.8,111.1))
-        let $polygon := create-polygon(create-point(1.0,1.0), create-point(2.0,2.0), create-point(3.0,3.0), create-point(4.0,4.0))
+        let $polygon := create-polygon([1.0,1.0,2.0,2.0,3.0,3.0,4.0,4.0])
         let $line_list := get-points($line)
         let $rectangle_list := get-points($rectangle)
         let $polygon_list := get-points($polygon)
@@ -1927,4 +1927,4 @@
 
  * The expected result is:
 
-        { "start": date("1984-01-01"), "end": date("1985-01-01") }
\ No newline at end of file
+        { "start": date("1984-01-01"), "end": date("1985-01-01") }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/functions/AsterixBuiltinFunctions.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/functions/AsterixBuiltinFunctions.java
index aa70ebf..299bbfb 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/functions/AsterixBuiltinFunctions.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/functions/AsterixBuiltinFunctions.java
@@ -478,7 +478,7 @@
     public final static FunctionIdentifier CREATE_LINE = new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
             "create-line", 2);
     public final static FunctionIdentifier CREATE_POLYGON = new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
-            "create-polygon", FunctionIdentifier.VARARGS);
+            "create-polygon", 1);
     public final static FunctionIdentifier CREATE_CIRCLE = new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
             "create-circle", 2);
     public final static FunctionIdentifier CREATE_RECTANGLE = new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CreatePolygonDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CreatePolygonDescriptor.java
index 8a35f87..438f856 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CreatePolygonDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CreatePolygonDescriptor.java
@@ -17,15 +17,24 @@
 import java.io.DataOutput;
 import java.io.IOException;
 
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.dataflow.data.nontagged.serde.ADoubleSerializerDeserializer;
+import edu.uci.ics.asterix.dataflow.data.nontagged.serde.APointSerializerDeserializer;
+import edu.uci.ics.asterix.formats.nontagged.AqlSerializerDeserializerProvider;
+import edu.uci.ics.asterix.om.base.ANull;
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptor;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptorFactory;
 import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.BuiltinType;
+import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
+import edu.uci.ics.asterix.runtime.evaluators.common.AsterixListAccessor;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 import edu.uci.ics.hyracks.algebricks.runtime.base.ICopyEvaluator;
 import edu.uci.ics.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory;
+import edu.uci.ics.hyracks.api.dataflow.value.ISerializerDeserializer;
 import edu.uci.ics.hyracks.data.std.api.IDataOutputProvider;
 import edu.uci.ics.hyracks.data.std.util.ArrayBackedValueStorage;
 import edu.uci.ics.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
@@ -33,6 +42,10 @@
 public class CreatePolygonDescriptor extends AbstractScalarFunctionDynamicDescriptor {
 
     private static final long serialVersionUID = 1L;
+
+    private static final byte SER_ORDEREDLIST_TYPE_TAG = ATypeTag.ORDEREDLIST.serialize();
+    private final static byte SER_POLYGON_TYPE_TAG = ATypeTag.POLYGON.serialize();
+
     public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() {
         public IFunctionDescriptor createFunctionDescriptor() {
             return new CreatePolygonDescriptor();
@@ -40,45 +53,79 @@
     };
 
     @Override
-    public ICopyEvaluatorFactory createEvaluatorFactory(final ICopyEvaluatorFactory[] args) throws AlgebricksException {
+    public ICopyEvaluatorFactory createEvaluatorFactory(final ICopyEvaluatorFactory[] args) {
         return new ICopyEvaluatorFactory() {
-            private static final long serialVersionUID = 1L;
 
-            private DataOutput out;
-            private ArrayBackedValueStorage outInput;
+            private static final long serialVersionUID = 1L;
 
             @Override
             public ICopyEvaluator createEvaluator(final IDataOutputProvider output) throws AlgebricksException {
-
-                final ICopyEvaluator[] argEvals = new ICopyEvaluator[args.length];
-                out = output.getDataOutput();
-
-                outInput = new ArrayBackedValueStorage();
-
-                for (int i = 0; i < args.length; i++) {
-                    argEvals[i] = args[i].createEvaluator(outInput);
-                }
-
                 return new ICopyEvaluator() {
 
+                    private final AsterixListAccessor listAccessor = new AsterixListAccessor();
+                    private final DataOutput out = output.getDataOutput();
+                    private final ICopyEvaluatorFactory listEvalFactory = args[0];
+                    private final ArrayBackedValueStorage outInputList = new ArrayBackedValueStorage();
+                    private final ICopyEvaluator evalList = listEvalFactory.createEvaluator(outInputList);
+                    @SuppressWarnings("unchecked")
+                    private final ISerializerDeserializer<ANull> nullSerde = AqlSerializerDeserializerProvider.INSTANCE
+                            .getSerializerDeserializer(BuiltinType.ANULL);
+
                     @Override
                     public void evaluate(IFrameTupleReference tuple) throws AlgebricksException {
                         try {
-                            out.writeByte(ATypeTag.POLYGON.serialize());
-                            out.writeShort(args.length);
-                        } catch (IOException e) {
-                            throw new AlgebricksException(e);
-                        }
-
-                        for (int i = 0; i < argEvals.length; i++) {
-                            outInput.reset();
-                            argEvals[i].evaluate(tuple);
+                            outInputList.reset();
+                            evalList.evaluate(tuple);
+                            byte[] listBytes = outInputList.getByteArray();
+                            if (listBytes[0] != SER_ORDEREDLIST_TYPE_TAG) {
+                                throw new AlgebricksException(AsterixBuiltinFunctions.CREATE_POLYGON.getName()
+                                        + ": expects input type ORDEREDLIST, but got "
+                                        + EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(listBytes[0]));
+                            }
                             try {
-                                out.write(outInput.getByteArray(), outInput.getStartOffset() + 1,
-                                        outInput.getLength() - 1);
-                            } catch (IOException e) {
+                                listAccessor.reset(listBytes, 0);
+                            } catch (AsterixException e) {
                                 throw new AlgebricksException(e);
                             }
+                            try {
+                                // First check the list consists of a valid items
+                                for (int i = 0; i < listAccessor.size(); i++) {
+                                    int itemOffset = listAccessor.getItemOffset(i);
+                                    ATypeTag itemType = listAccessor.getItemType(itemOffset);
+                                    if (itemType != ATypeTag.DOUBLE) {
+                                        if (itemType == ATypeTag.NULL) {
+                                            nullSerde.serialize(ANull.NULL, out);
+                                            return;
+                                        }
+                                        throw new AlgebricksException(AsterixBuiltinFunctions.CREATE_POLYGON.getName()
+                                                + ": expects type DOUBLE/NULL for the list item but got " + itemType);
+                                    }
+
+                                }
+                                if (listAccessor.size() < 6) {
+                                    throw new AlgebricksException(
+                                            "A polygon instance must consists of at least 3 points");
+                                } else if (listAccessor.size() % 2 != 0) {
+                                    throw new AlgebricksException(
+                                            "There must be an even number of double values in the list to form a polygon");
+                                }
+                                out.writeByte(SER_POLYGON_TYPE_TAG);
+                                out.writeShort(listAccessor.size() / 2);
+
+                                for (int i = 0; i < listAccessor.size() / 2; i++) {
+                                    int firstDoubleOffset = listAccessor.getItemOffset(i * 2);
+                                    int secondDobuleOffset = listAccessor.getItemOffset((i * 2) + 1);
+
+                                    APointSerializerDeserializer
+                                            .serialize(ADoubleSerializerDeserializer.getDouble(listBytes,
+                                                    firstDoubleOffset), ADoubleSerializerDeserializer.getDouble(
+                                                    listBytes, secondDobuleOffset), out);
+                                }
+                            } catch (AsterixException ex) {
+                                throw new AlgebricksException(ex);
+                            }
+                        } catch (IOException e1) {
+                            throw new AlgebricksException(e1.getMessage());
                         }
                     }
                 };
@@ -90,5 +137,4 @@
     public FunctionIdentifier getIdentifier() {
         return AsterixBuiltinFunctions.CREATE_POLYGON;
     }
-
-}
\ No newline at end of file
+}