[NO ISSUE][FUN] Implement object-unwrap()
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- Implement object-unwrap function that given an object that
has exactly one name-value pair, it will return the value
of this pair.
- Add new function to docs.
- Add test case.
Change-Id: I2af84e72e2cf630afe2430f715c8d40dd075e97e
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2705
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/ObjectsQueries.xml b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/ObjectsQueries.xml
index 007a3fa..796913d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/ObjectsQueries.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/ObjectsQueries.xml
@@ -128,6 +128,11 @@
</compilation-unit>
</test-case>
<test-case FilePath="objects">
+ <compilation-unit name="object_unwrap">
+ <output-dir compare="Text">object_unwrap</output-dir>
+ </compilation-unit>
+ </test-case>
+ <test-case FilePath="objects">
<compilation-unit name="object_pairs">
<output-dir compare="Text">object_pairs</output-dir>
</compilation-unit>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.1.ddl.sqlpp
new file mode 100644
index 0000000..1aba6e0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.1.ddl.sqlpp
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+/*
+ * Description : Testing object_unwrap under different queries.
+ * Expected Res : Success
+ */
+
+drop dataverse TinySocial if exists;
+create dataverse TinySocial;
+
+use TinySocial;
+
+create type TinySocial.TwitterUserType as
+{
+ `screen-name` : string,
+ lang : string,
+ friends_count : bigint,
+ statuses_count : bigint
+};
+
+create dataset TwitterUsers(TwitterUserType) primary key `screen-name`;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.2.update.sqlpp
new file mode 100644
index 0000000..8f854e3
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.2.update.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+/*
+ * Description : Testing object_unwrap under different queries.
+ * Expected Res : Success
+ */
+
+use TinySocial;
+
+load dataset TwitterUsers using localfs ((`path`=`asterix_nc1://data/tinysocial/twu.adm`),(`format`=`adm`));
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.3.query.sqlpp
new file mode 100644
index 0000000..b79bd8b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.3.query.sqlpp
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+/*
+ * Description : Testing object_unwrap under different queries.
+ * Expected Res : Success
+ */
+
+use TinySocial;
+
+{
+ "t1": [
+ object_unwrap(missing) is missing,
+ object_unwrap(null) is null,
+ object_unwrap("non-object") is null,
+ object_unwrap({"a":1, "b":2}) is null,
+ object_unwrap({}) is null
+ ],
+
+ "t2": object_unwrap({"a":"1"}),
+ "t3": object_unwrap({"a":{"b":"1"}}),
+ "t4": object_unwrap({"a":[1, 2]}),
+
+ "t5": (
+ select object_unwrap(o) from (
+ select lang
+ from TwitterUsers u
+ order by lang
+ limit 1
+ ) o
+ )
+};
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.4.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.4.ddl.sqlpp
new file mode 100644
index 0000000..af175df
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/objects/object_unwrap/object_unwarp.4.ddl.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+/*
+ * Description : Testing object_unwrap under different queries.
+ * Expected Res : Success
+ */
+
+drop dataverse TinySocial;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/objects/object_unwrap/object_unwrap.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/objects/object_unwrap/object_unwrap.3.adm
new file mode 100644
index 0000000..87269dc
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/objects/object_unwrap/object_unwrap.3.adm
@@ -0,0 +1 @@
+{ "t1": [ true, true, true, true, true ], "t2": "1", "t3": { "b": "1" }, "t4": [ 1, 2 ], "t5": [ { "$1": "en" } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-doc/src/main/markdown/builtins/8_record.md b/asterixdb/asterix-doc/src/main/markdown/builtins/8_record.md
index 32c796d..f3c256e 100644
--- a/asterixdb/asterix-doc/src/main/markdown/builtins/8_record.md
+++ b/asterixdb/asterix-doc/src/main/markdown/builtins/8_record.md
@@ -316,7 +316,7 @@
* The expected result is:
{
- "id": "1",
+ "id": 1,
"project": "AsterixDB",
}
@@ -351,7 +351,36 @@
* The expected result is:
{
- "id": "1",
+ "id": 1,
"project": "AsterixDB",
"location": {"city": "Irvine", "state": "CA"}
- }
\ No newline at end of file
+ }
+
+### object_wrap ###
+ * Syntax:
+
+ object_wrap(input_object)
+
+ * Returns the value of the single name-value pair that appears in `input_object`.
+ * Arguments:
+ * `input_object` : an object value that consists of exactly one name-value pair.
+ * Return Value:
+ * The value of the single name-value pair that appears in `input_object`,
+ * `missing` if `input_object` is `missing`,
+ * `null` if `input_object` is null, or an empty object, or there is more than one name-value pair in `input_object`,
+ or any non-object value.
+
+ * Example:
+
+ object_wrap(
+ {
+ "id": 1
+ }
+ );
+
+ * The expected result is:
+
+ {
+ 1
+ }
+
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
index 1d33a59..a80ba28 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
@@ -224,6 +224,8 @@
new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "object-remove", 2);
public static final FunctionIdentifier RECORD_RENAME =
new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "object-rename", 3);
+ public static final FunctionIdentifier RECORD_UNWRAP =
+ new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "object-unwrap", 1);
// numeric
public static final FunctionIdentifier NUMERIC_UNARY_MINUS =
@@ -1466,6 +1468,7 @@
addFunction(GEOMETRY_CONSTRUCTOR, AGeometryTypeComputer.INSTANCE, true);
addFunction(RECORD_REMOVE, OpenARecordTypeComputer.INSTANCE, true);
addFunction(RECORD_RENAME, OpenARecordTypeComputer.INSTANCE, true);
+ addFunction(RECORD_UNWRAP, AnyTypeComputer.INSTANCE, true);
// temporal type accessors
addFunction(ACCESSOR_TEMPORAL_YEAR, AInt64TypeComputer.INSTANCE, true);
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapDescriptor.java
new file mode 100644
index 0000000..5890d7a
--- /dev/null
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapDescriptor.java
@@ -0,0 +1,73 @@
+/*
+ * 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.asterix.runtime.evaluators.functions.records;
+
+import org.apache.asterix.om.functions.BuiltinFunctions;
+import org.apache.asterix.om.functions.IFunctionDescriptor;
+import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
+import org.apache.asterix.om.functions.IFunctionTypeInferer;
+import org.apache.asterix.om.types.ARecordType;
+import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
+import org.apache.asterix.runtime.functions.FunctionTypeInferers;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+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.hyracks.api.exceptions.HyracksDataException;
+
+public class RecordUnwrapDescriptor extends AbstractScalarFunctionDynamicDescriptor {
+
+ public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() {
+ @Override
+ public IFunctionDescriptor createFunctionDescriptor() {
+ return new RecordUnwrapDescriptor();
+ }
+
+ @Override
+ public IFunctionTypeInferer createFunctionTypeInferer() {
+ return FunctionTypeInferers.RecordAccessorTypeInferer.INSTANCE_LAX;
+ }
+ };
+
+ private static final long serialVersionUID = 1L;
+ private ARecordType recordType;
+
+ @Override
+ public void setImmutableStates(Object... states) {
+ recordType = (ARecordType) states[0];
+ }
+
+ @Override
+ public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
+ return new IScalarEvaluatorFactory() {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
+ final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
+ return new RecordUnwrapEvaluator(eval0, recordType);
+ }
+ };
+ }
+
+ @Override
+ public FunctionIdentifier getIdentifier() {
+ return BuiltinFunctions.RECORD_UNWRAP;
+ }
+}
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapEvaluator.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapEvaluator.java
new file mode 100644
index 0000000..97bc01a
--- /dev/null
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/records/RecordUnwrapEvaluator.java
@@ -0,0 +1,118 @@
+/*
+ * 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.asterix.runtime.evaluators.functions.records;
+
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.asterix.om.pointables.ARecordVisitablePointable;
+import org.apache.asterix.om.pointables.base.DefaultOpenFieldType;
+import org.apache.asterix.om.pointables.base.IVisitablePointable;
+import org.apache.asterix.om.pointables.cast.ACastVisitor;
+import org.apache.asterix.om.types.ARecordType;
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
+import org.apache.hyracks.algebricks.common.utils.Triple;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.primitive.VoidPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
+
+class RecordUnwrapEvaluator implements IScalarEvaluator {
+
+ private final IPointable inputRecordPointable = new VoidPointable();
+ private final IScalarEvaluator eval0;
+ private ARecordVisitablePointable inputRecordVisitable;
+ private ARecordVisitablePointable openRecordVisitablePointable;
+ private boolean requiresCast = false;
+ private ACastVisitor castVisitor;
+ private Triple<IVisitablePointable, IAType, Boolean> castVisitorArg;
+ private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
+ private final DataOutput resultOutput = resultStorage.getDataOutput();
+
+ RecordUnwrapEvaluator(IScalarEvaluator eval0, ARecordType recordType) {
+ this.eval0 = eval0;
+ openRecordVisitablePointable = new ARecordVisitablePointable(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE);
+ if (recordType != null) {
+ inputRecordVisitable = new ARecordVisitablePointable(recordType);
+ if (hasDerivedType(recordType.getFieldTypes())) {
+ requiresCast = true;
+ castVisitor = new ACastVisitor();
+ castVisitorArg = new Triple<>(openRecordVisitablePointable,
+ openRecordVisitablePointable.getInputRecordType(), Boolean.FALSE);
+ }
+ }
+ }
+
+ @Override
+ public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
+ resultStorage.reset();
+ eval0.evaluate(tuple, inputRecordPointable);
+ final byte[] data = inputRecordPointable.getByteArray();
+ final int offset = inputRecordPointable.getStartOffset();
+ final byte typeTag = data[offset];
+ if (typeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
+ PointableHelper.setNull(result);
+ return;
+ }
+ final ARecordVisitablePointable inputRecordVisitablePointable = getInputRecordVisitablePointable();
+ final List<IVisitablePointable> recValues = inputRecordVisitablePointable.getFieldValues();
+ if (recValues.size() != 1) {
+ PointableHelper.setNull(result);
+ return;
+ }
+ writeValue(recValues.get(0));
+ result.set(resultStorage);
+ }
+
+ private boolean hasDerivedType(IAType[] types) {
+ for (IAType type : types) {
+ if (type.getTypeTag().isDerivedType()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private ARecordVisitablePointable getInputRecordVisitablePointable() throws HyracksDataException {
+ inputRecordVisitable.set(inputRecordPointable);
+ if (requiresCast) {
+ return castToOpenRecord();
+ }
+ return inputRecordVisitable;
+ }
+
+ private ARecordVisitablePointable castToOpenRecord() throws HyracksDataException {
+ inputRecordVisitable.accept(castVisitor, castVisitorArg);
+ return openRecordVisitablePointable;
+ }
+
+ private void writeValue(IVisitablePointable value) throws HyracksDataException {
+ try {
+ resultOutput.write(value.getByteArray(), value.getStartOffset(), value.getLength());
+ } catch (IOException e) {
+ throw HyracksDataException.create(e);
+ }
+ }
+}
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
index 3b989be..271a7f0 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
@@ -284,6 +284,7 @@
import org.apache.asterix.runtime.evaluators.functions.records.RecordRemoveDescriptor;
import org.apache.asterix.runtime.evaluators.functions.records.RecordRemoveFieldsDescriptor;
import org.apache.asterix.runtime.evaluators.functions.records.RecordRenameDescriptor;
+import org.apache.asterix.runtime.evaluators.functions.records.RecordUnwrapDescriptor;
import org.apache.asterix.runtime.evaluators.functions.temporal.AdjustDateTimeForTimeZoneDescriptor;
import org.apache.asterix.runtime.evaluators.functions.temporal.AdjustTimeForTimeZoneDescriptor;
import org.apache.asterix.runtime.evaluators.functions.temporal.CalendarDuartionFromDateDescriptor;
@@ -648,6 +649,7 @@
fc.addGenerated(RecordNamesDescriptor.FACTORY);
fc.addGenerated(RecordRemoveDescriptor.FACTORY);
fc.addGenerated(RecordRenameDescriptor.FACTORY);
+ fc.addGenerated(RecordUnwrapDescriptor.FACTORY);
// Spatial and temporal type accessors
fc.addGenerated(TemporalYearAccessor.FACTORY);