Repository: vxquery
Updated Branches:
  refs/heads/master 479ed7193 -> 2db20490c


VXQUERY-211 Support "?:" in object constructor


Project: http://git-wip-us.apache.org/repos/asf/vxquery/repo
Commit: http://git-wip-us.apache.org/repos/asf/vxquery/commit/f6d6a86f
Tree: http://git-wip-us.apache.org/repos/asf/vxquery/tree/f6d6a86f
Diff: http://git-wip-us.apache.org/repos/asf/vxquery/diff/f6d6a86f

Branch: refs/heads/master
Commit: f6d6a86f73a90ace398118a4808e6ff52ef8ee44
Parents: 3d14785
Author: riyafa <[email protected]>
Authored: Wed Jul 6 10:34:25 2016 +0530
Committer: riyafa <[email protected]>
Committed: Fri Jul 8 05:14:51 2016 +0530

----------------------------------------------------------------------
 .../ObjectConstructorScalarEvaluator.java       | 66 ++++++++++++++------
 .../vxquery/xmlquery/ast/PairConstructor.java   | 12 +++-
 .../xmlquery/translator/XMLQueryTranslator.java | 10 ++-
 vxquery-core/src/main/javacc/xquery-grammar.jj  |  4 +-
 .../Json/Object/q12_object.txt                  |  1 +
 .../Queries/XQuery/Json/Object/q12_object.xq    | 29 +++++++++
 .../test/resources/cat/JsonObjectQueries.xml    |  5 ++
 7 files changed, 102 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java
index edfa883..c002ad7 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/ObjectConstructorScalarEvaluator.java
@@ -19,13 +19,16 @@ package org.apache.vxquery.runtime.functions.jsonitem;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
 import org.apache.hyracks.api.context.IHyracksTaskContext;
 import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.primitive.BooleanPointable;
 import org.apache.hyracks.data.std.primitive.UTF8StringPointable;
 import org.apache.hyracks.data.std.primitive.VoidPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
 import org.apache.vxquery.datamodel.accessors.SequencePointable;
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
+import org.apache.vxquery.datamodel.builders.jsonitem.ArrayBuilder;
 import org.apache.vxquery.datamodel.builders.jsonitem.ObjectBuilder;
 import org.apache.vxquery.datamodel.values.ValueTag;
+import org.apache.vxquery.datamodel.values.XDMConstants;
 import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import 
org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentScalarEvaluator;
@@ -41,15 +44,21 @@ public class ObjectConstructorScalarEvaluator extends 
AbstractTaggedValueArgumen
     private SequencePointable seqp;
     protected final IHyracksTaskContext ctx;
     private final ArrayBackedValueStorage abvs;
+    private final ArrayBackedValueStorage abvs1;
+    private final BooleanPointable bp;
+    private final ArrayBuilder ab;
 
     public ObjectConstructorScalarEvaluator(IHyracksTaskContext ctx, 
IScalarEvaluator[] args) {
         super(args);
         this.ctx = ctx;
         abvs = new ArrayBackedValueStorage();
+        abvs1 = new ArrayBackedValueStorage();
         ob = new ObjectBuilder();
         vp = VoidPointable.FACTORY.createPointable();
         sp = (UTF8StringPointable) 
UTF8StringPointable.FACTORY.createPointable();
         seqp = (SequencePointable) SequencePointable.FACTORY.createPointable();
+        bp = (BooleanPointable) BooleanPointable.FACTORY.createPointable();
+        ab = new ArrayBuilder();
     }
 
     private boolean isDuplicate(TaggedValuePointable tempKey) {
@@ -64,37 +73,54 @@ public class ObjectConstructorScalarEvaluator extends 
AbstractTaggedValueArgumen
 
     @Override
     protected void evaluate(TaggedValuePointable[] args, IPointable result) 
throws SystemException {
-        TaggedValuePointable tvp;
-        TaggedValuePointable tempKey = 
ppool.takeOne(TaggedValuePointable.class);
-        TaggedValuePointable tempValue = 
ppool.takeOne(TaggedValuePointable.class);
+        TaggedValuePointable key, value, qmc;
         try {
+            abvs.reset();
             ob.reset(abvs);
 
-            tvp = args[0];
-            if (tvp.getTag() == ValueTag.SEQUENCE_TAG) {
-                tvp.getValue(seqp);
-                int len = seqp.getEntryCount();
-                pointables = new TaggedValuePointable[len / 2];
-                for (int i = 0; i < len; i += 2) {
-                    seqp.getEntry(i, tempKey);
-                    seqp.getEntry(i + 1, tempValue);
-                    if (!isDuplicate(tempKey)) {
-                        pointables[i / 2] = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
-                        tempKey.getValue(pointables[i / 2]);
-                        sp.set(vp);
-                        ob.addItem(sp, tempValue);
+            int len = args.length;
+            pointables = new TaggedValuePointable[len / 3];
+            for (int i = 0; i < len; i += 3) {
+                key = args[i];
+                value = args[i + 1];
+                qmc = args[i + 2];
+                if (!isDuplicate(key)) {
+                    pointables[i / 3] = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
+                    key.getValue(pointables[i / 3]);
+                    sp.set(vp);
+                    if (value.getTag() == ValueTag.SEQUENCE_TAG) {
+                        qmc.getValue(bp);
+                        value.getValue(seqp);
+                        if (seqp.getEntryCount() == 0) {
+                            if (bp.getBoolean()) {
+                                continue;
+                            }
+                            XDMConstants.setJsNull(value);
+                            ob.addItem(sp, value);
+                        } else {
+                            abvs1.reset();
+                            ab.reset(abvs1);
+                            int l = seqp.getEntryCount();
+                            for (int j = 0; j < l; j++) {
+                                seqp.getEntry(j, value);
+                                ab.addItem(value);
+                            }
+                            ab.finish();
+                            vp.set(abvs1);
+                            ob.addItem(sp, vp);
+                        }
                     } else {
-                        throw new SystemException(ErrorCode.JNDY0003);
+                        ob.addItem(sp, value);
                     }
+                } else {
+                    throw new SystemException(ErrorCode.JNDY0003);
                 }
             }
+
             ob.finish();
             result.set(abvs);
         } catch (IOException e) {
             throw new SystemException(ErrorCode.SYSE0001, e);
-        } finally {
-            ppool.giveBack(tempKey);
-            ppool.giveBack(tempValue);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/ast/PairConstructor.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/ast/PairConstructor.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/ast/PairConstructor.java
index add12ad..626c0e8 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/ast/PairConstructor.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/ast/PairConstructor.java
@@ -18,9 +18,11 @@ package org.apache.vxquery.xmlquery.ast;
 
 import org.apache.vxquery.util.SourceLocation;
 
-public class PairConstructor extends ASTNode{
+public class PairConstructor extends ASTNode {
     private ASTNode key;
     private ASTNode value;
+    private boolean qmc;
+
     public PairConstructor(SourceLocation loc) {
         super(loc);
     }
@@ -45,4 +47,12 @@ public class PairConstructor extends ASTNode{
     public void setValue(ASTNode value) {
         this.value = value;
     }
+
+    public void setQuestionMarkColon(boolean qmc) {
+        this.qmc = qmc;
+    }
+
+    public boolean isQuestionMarkColon() {
+        return qmc;
+    }
 }

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/translator/XMLQueryTranslator.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/translator/XMLQueryTranslator.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/translator/XMLQueryTranslator.java
index dde2cec..8e18651 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/translator/XMLQueryTranslator.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/xmlquery/translator/XMLQueryTranslator.java
@@ -1211,10 +1211,14 @@ public class XMLQueryTranslator {
             content.add(ke);
             ILogicalExpression ve = vre(translateExpression(((PairConstructor) 
aVal).getValue(), tCtx));
             content.add(ve);
+            ILogicalExpression qmce = 
ce(SequenceType.create(BuiltinTypeRegistry.XS_BOOLEAN, Quantifier.QUANT_ONE),
+                    ((PairConstructor) aVal).isQuestionMarkColon());
+            content.add(qmce);
         }
-        ILogicalExpression contentExpr = sfce(BuiltinOperators.CONCATENATE,
-                content.toArray(new ILogicalExpression[content.size()]));
-        return createAssignment(sfce(BuiltinOperators.OBJECT_CONSTRUCTOR, 
contentExpr), tCtx);
+
+        return createAssignment(
+                sfce(BuiltinOperators.OBJECT_CONSTRUCTOR, content.toArray(new 
ILogicalExpression[content.size()])),
+                tCtx);
     }
 
     private LogicalVariable 
translateDirectElementConstructorNode(TranslationContext tCtx,

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-core/src/main/javacc/xquery-grammar.jj
----------------------------------------------------------------------
diff --git a/vxquery-core/src/main/javacc/xquery-grammar.jj 
b/vxquery-core/src/main/javacc/xquery-grammar.jj
index fe72ba7..f9d25b2 100644
--- a/vxquery-core/src/main/javacc/xquery-grammar.jj
+++ b/vxquery-core/src/main/javacc/xquery-grammar.jj
@@ -2035,12 +2035,13 @@ ASTNode PairConstructor() :
 {
     ASTNode key;
     ASTNode value;
+    Token qmc = null;
 }
 {
     key = ExprSingle()
     (
         ":"
-        | ":?"
+        | qmc = "?:"
     )
     (
         value = ExprSingle()
@@ -2048,6 +2049,7 @@ ASTNode PairConstructor() :
         PairConstructor pc = new PairConstructor(key.getSourceLocation());
         pc.setKey(key);
         pc.setValue(value);
+        pc.setQuestionMarkColon(qmc != null);
         return pc;
     }
 }

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q12_object.txt
----------------------------------------------------------------------
diff --git 
a/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q12_object.txt
 
b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q12_object.txt
new file mode 100644
index 0000000..8da91af
--- /dev/null
+++ 
b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q12_object.txt
@@ -0,0 +1 @@
+{"Sunday":1,"Monday":2,"Tuesday":[1,2,3],"Wednesday":4,"Thursday":null,"Friday":6,"Saturday":[1,2,3]}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q12_object.xq
----------------------------------------------------------------------
diff --git 
a/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q12_object.xq 
b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q12_object.xq
new file mode 100644
index 0000000..5f54fb6
--- /dev/null
+++ b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q12_object.xq
@@ -0,0 +1,29 @@
+(: Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+   
+     http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License. :)
+
+(: Json Object Query :)
+(: Issue VXQUERY-211 :)
+{
+    "Sunday" : 1,
+    "Monday" ?: 1 + 1,
+    "Tuesday" ?: (1,2,3),
+    "Wednesday" : 8 div 2,
+    "Thursday" : (),
+    "Friday" : count(for $i in 1 to 6 return $i),
+    "Saturday" : (1,2,3),
+    "NotADay" ?: ()
+}

http://git-wip-us.apache.org/repos/asf/vxquery/blob/f6d6a86f/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
----------------------------------------------------------------------
diff --git a/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml 
b/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
index 9bba114..e8fc532 100644
--- a/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
+++ b/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
@@ -75,4 +75,9 @@
         <query name="q11_object" date="2016-06-30"/>
         <expected-error>JNDY0003</expected-error>
     </test-case>
+    <test-case name="json-object-q12" FilePath="Json/Object/" Creator="Riyafa 
Abdul Hameed">
+        <description>Object.</description>
+        <query name="q12_object" date="2016-07-05"/>
+        <output-file compare="Text">q12_object.txt</output-file>
+    </test-case>
 </test-group>
\ No newline at end of file

Reply via email to