You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@vxquery.apache.org by pr...@apache.org on 2016/07/13 00:35:24 UTC
vxquery git commit: VXQUERY-212 Dynamic object construction syntax
Repository: vxquery
Updated Branches:
refs/heads/master b96caaa0f -> d19fa2630
VXQUERY-212 Dynamic object construction syntax
Project: http://git-wip-us.apache.org/repos/asf/vxquery/repo
Commit: http://git-wip-us.apache.org/repos/asf/vxquery/commit/d19fa263
Tree: http://git-wip-us.apache.org/repos/asf/vxquery/tree/d19fa263
Diff: http://git-wip-us.apache.org/repos/asf/vxquery/diff/d19fa263
Branch: refs/heads/master
Commit: d19fa26307e6d6fce3ed9f4bcb49fba9428dcc72
Parents: b96caaa
Author: riyafa <ri...@gmail.com>
Authored: Wed Jul 13 05:43:22 2016 +0530
Committer: riyafa <ri...@gmail.com>
Committed: Wed Jul 13 05:43:22 2016 +0530
----------------------------------------------------------------------
.../vxquery/functions/builtin-operators.xml | 7 ++
.../SimpleObjectUnionScalarEvaluator.java | 103 +++++++++++++++++++
...SimpleObjectUnionScalarEvaluatorFactory.java | 36 +++++++
.../xmlquery/translator/XMLQueryTranslator.java | 22 ++--
vxquery-core/src/main/javacc/xquery-grammar.jj | 30 ++++--
.../Json/Object/q13_object.txt | 1 +
.../Json/Object/q14_object.txt | 1 +
.../Queries/XQuery/Json/Object/q13_object.xq | 22 ++++
.../Queries/XQuery/Json/Object/q14_object.xq | 29 ++++++
.../test/resources/cat/JsonObjectQueries.xml | 10 ++
10 files changed, 244 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-core/src/main/java/org/apache/vxquery/functions/builtin-operators.xml
----------------------------------------------------------------------
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/functions/builtin-operators.xml b/vxquery-core/src/main/java/org/apache/vxquery/functions/builtin-operators.xml
index bb710a9..0b03c34 100644
--- a/vxquery-core/src/main/java/org/apache/vxquery/functions/builtin-operators.xml
+++ b/vxquery-core/src/main/java/org/apache/vxquery/functions/builtin-operators.xml
@@ -882,6 +882,13 @@
<runtime type="scalar" class="org.apache.vxquery.runtime.functions.jsonitem.ObjectConstructorScalarEvaluatorFactory"/>
</operator>
+ <!-- opext:simple-object-union($expression as object()*) as object() -->
+ <operator name="opext:simple-object-union">
+ <param name="expression" type="object()*"/>
+ <return type="object()"/>
+ <runtime type="scalar" class="org.apache.vxquery.runtime.functions.jsonitem.SimpleObjectUnionScalarEvaluatorFactory"/>
+ </operator>
+
<!-- opext:if-then-else($condition as xs:boolean, $then as item()*, $else as item()*) as item()* -->
<operator name="opext:if-then-else">
<param name="condition" type="xs:boolean"/>
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java
----------------------------------------------------------------------
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java
new file mode 100644
index 0000000..60347b1
--- /dev/null
+++ b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluator.java
@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+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.UTF8StringPointable;
+import org.apache.vxquery.datamodel.accessors.SequencePointable;
+import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
+import org.apache.vxquery.datamodel.accessors.jsonitem.ObjectPointable;
+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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class SimpleObjectUnionScalarEvaluator extends ObjectConstructorScalarEvaluator {
+
+ private final SequencePointable sp, sp1;
+
+ public SimpleObjectUnionScalarEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args) {
+ super(ctx, args);
+ sp = (SequencePointable) SequencePointable.FACTORY.createPointable();
+ sp1 = (SequencePointable) SequencePointable.FACTORY.createPointable();
+ }
+
+ @Override
+ protected void evaluate(TaggedValuePointable[] args, IPointable result) throws SystemException {
+ List<TaggedValuePointable> tvps = new ArrayList<>();
+
+ ObjectPointable op;
+ TaggedValuePointable key, value;
+ TaggedValuePointable arg = args[0];
+ if (arg.getTag() == ValueTag.SEQUENCE_TAG) {
+ arg.getValue(sp);
+ TaggedValuePointable tempTvp = ppool.takeOne(TaggedValuePointable.class);
+ TaggedValuePointable boolTvp = ppool.takeOne(TaggedValuePointable.class);
+ UTF8StringPointable tempKey = ppool.takeOne(UTF8StringPointable.class);
+ XDMConstants.setFalse(boolTvp);
+ try {
+ for (int i = 0; i < sp.getEntryCount(); ++i) {
+ op = (ObjectPointable) ObjectPointable.FACTORY.createPointable();
+ sp.getEntry(i, tempTvp);
+ tempTvp.getValue(op);
+ op.getKeys(tempTvp);
+ if (tempTvp.getTag() == ValueTag.XS_STRING_TAG) {
+ key = ppool.takeOne(TaggedValuePointable.class);
+ value = ppool.takeOne(TaggedValuePointable.class);
+ tempTvp.getValue(tempKey);
+ op.getValue(tempKey, value);
+ key.set(tempTvp);
+ tvps.add(key);
+ tvps.add(value);
+ tvps.add(boolTvp);
+
+ } else if (tempTvp.getTag() == ValueTag.SEQUENCE_TAG) {
+ tempTvp.getValue(sp1);
+ for (int j = 0; j < sp1.getEntryCount(); ++j) {
+ key = ppool.takeOne(TaggedValuePointable.class);
+ value = ppool.takeOne(TaggedValuePointable.class);
+ sp1.getEntry(j, tempTvp);
+ tempTvp.getValue(tempKey);
+ op.getValue(tempKey, value);
+ key.set(tempTvp);
+ tvps.add(key);
+ tvps.add(value);
+ tvps.add(boolTvp);
+ }
+
+ }
+ }
+ super.evaluate(tvps.toArray(new TaggedValuePointable[tvps.size()]), result);
+ } catch (IOException e) {
+ throw new SystemException(ErrorCode.SYSE0001, e);
+ } finally {
+ ppool.giveBack(tempKey);
+ ppool.giveBack(tempTvp);
+ ppool.giveBack(boolTvp);
+ for (TaggedValuePointable pointable : tvps) {
+ ppool.giveBack(pointable);
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluatorFactory.java
----------------------------------------------------------------------
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluatorFactory.java b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluatorFactory.java
new file mode 100644
index 0000000..9ba4798
--- /dev/null
+++ b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/jsonitem/SimpleObjectUnionScalarEvaluatorFactory.java
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.apache.vxquery.runtime.functions.jsonitem;
+
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
+import org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentScalarEvaluatorFactory;
+
+public class SimpleObjectUnionScalarEvaluatorFactory extends AbstractTaggedValueArgumentScalarEvaluatorFactory {
+
+ public SimpleObjectUnionScalarEvaluatorFactory(IScalarEvaluatorFactory[] args) {
+ super(args);
+ }
+
+ @Override
+ protected IScalarEvaluator createEvaluator(IHyracksTaskContext ctx, IScalarEvaluator[] args)
+ throws AlgebricksException {
+ return new SimpleObjectUnionScalarEvaluator(ctx, args);
+ }
+}
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/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 a635951..c8cfb2b 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
@@ -1206,14 +1206,22 @@ public class XMLQueryTranslator {
private LogicalVariable translateObjectConstructor(TranslationContext tCtx, ObjectConstructor obj)
throws SystemException {
List<ILogicalExpression> content = new ArrayList<ILogicalExpression>();
+ PairConstructor pc;
for (ASTNode aVal : obj.getContent()) {
- ILogicalExpression ke = string(data(vre(translateExpression(((PairConstructor) aVal).getKey(), tCtx))));
- 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);
+ if (aVal.getTag()==ASTTag.PAIR_CONSTRUCTOR) {
+ pc=(PairConstructor) aVal;
+ ILogicalExpression ke = string(data(vre(translateExpression(pc.getKey(), tCtx))));
+ content.add(ke);
+ ILogicalExpression ve = vre(translateExpression(pc.getValue(), tCtx));
+ content.add(ve);
+ ILogicalExpression qmce = ce(SequenceType.create(BuiltinTypeRegistry.XS_BOOLEAN, Quantifier.QUANT_ONE),
+ pc.isQuestionMarkColon());
+ content.add(qmce);
+ } else {
+ ILogicalExpression aExpr = aVal == null ? sfce(BuiltinOperators.CONCATENATE)
+ : vre(translateExpression(aVal, tCtx));
+ return createAssignment(sfce(BuiltinOperators.SIMPLE_OBJECT_UNION, aExpr), tCtx);
+ }
}
return createAssignment(
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/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 f9d25b2..9ea3943 100644
--- a/vxquery-core/src/main/javacc/xquery-grammar.jj
+++ b/vxquery-core/src/main/javacc/xquery-grammar.jj
@@ -2013,17 +2013,27 @@ ASTNode ObjectConstructor() :
ASTNode pc;
}
{
- t = <LbraceExprEnclosure>
(
- pc = PairConstructor() {
- content.add(pc);
- } (
- "," pc = PairConstructor() {
- content.add(pc);
- }
- )*
- )*
- <Rbrace>
+ (
+ t = <LbraceExprEnclosure>
+ (
+ pc = PairConstructor() {
+ content.add(pc);
+ } (
+ "," pc = PairConstructor() {
+ content.add(pc);
+ }
+ )*
+ )*
+ <Rbrace>
+ )
+ | (
+ t = "{|" pc = Expr() "|}"
+ {
+ content.add(pc);
+ }
+ )
+ )
{
ObjectConstructor obj = new ObjectConstructor(createSourceLocation(t));
obj.setContent(content);
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q13_object.txt
----------------------------------------------------------------------
diff --git a/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q13_object.txt b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q13_object.txt
new file mode 100644
index 0000000..d80c27c
--- /dev/null
+++ b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q13_object.txt
@@ -0,0 +1 @@
+{"Captain":"Kirk","First officer":"Spock"}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q14_object.txt
----------------------------------------------------------------------
diff --git a/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q14_object.txt b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q14_object.txt
new file mode 100644
index 0000000..84dd8df
--- /dev/null
+++ b/vxquery-xtest/src/test/resources/ExpectedTestResults/Json/Object/q14_object.txt
@@ -0,0 +1 @@
+{"Sunday":1,"Monday":2,"Tuesday":3,"Wednesday":4,"Thursday":5,"Friday":6,"Saturday":7}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q13_object.xq
----------------------------------------------------------------------
diff --git a/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q13_object.xq b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q13_object.xq
new file mode 100644
index 0000000..d7920e1
--- /dev/null
+++ b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q13_object.xq
@@ -0,0 +1,22 @@
+(: 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-212 :)
+let $object1 := { "Captain" : "Kirk" }
+let $object2 := { "First officer" : "Spock" }
+return {| $object1, $object2 |}
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q14_object.xq
----------------------------------------------------------------------
diff --git a/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q14_object.xq b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q14_object.xq
new file mode 100644
index 0000000..ef31848
--- /dev/null
+++ b/vxquery-xtest/src/test/resources/Queries/XQuery/Json/Object/q14_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-212 :)
+{|
+ for $d at $i in ("Sunday",
+ "Monday",
+ "Tuesday",
+ "Wednesday",
+ "Thursday",
+ "Friday",
+ "Saturday" )
+ return { $d : $i }
+|}
http://git-wip-us.apache.org/repos/asf/vxquery/blob/d19fa263/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 e8fc532..2ee68c4 100644
--- a/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
+++ b/vxquery-xtest/src/test/resources/cat/JsonObjectQueries.xml
@@ -80,4 +80,14 @@
<query name="q12_object" date="2016-07-05"/>
<output-file compare="Text">q12_object.txt</output-file>
</test-case>
+ <test-case name="json-object-q13" FilePath="Json/Object/" Creator="Riyafa Abdul Hameed">
+ <description>Object.</description>
+ <query name="q13_object" date="2016-07-09"/>
+ <output-file compare="Text">q13_object.txt</output-file>
+ </test-case>
+ <test-case name="json-object-q14" FilePath="Json/Object/" Creator="Riyafa Abdul Hameed">
+ <description>Object.</description>
+ <query name="q14_object" date="2016-07-09"/>
+ <output-file compare="Text">q14_object.txt</output-file>
+ </test-case>
</test-group>
\ No newline at end of file