[ASTERIXDB-2391][SQL] Insure that var for return clause gets optimized
Places the variable created for the return clause below the insert
so future optimizations can work on this variable
Added test
Change-Id: Id285435c4dc8a603c60b177dacd9f09722faac21
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2689
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
index 2dbcb36..0c46383 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
@@ -610,18 +610,26 @@
}
SourceLocation sourceLoc = compiledInsert.getSourceLocation();
+ //Create an assign operator that makes the variable used by the return expression
+ LogicalVariable insertedVar = context.newVar();
+ AssignOperator insertedVarAssignOperator =
+ new AssignOperator(insertedVar, new MutableObject<>(insertOp.getPayloadExpression().getValue()));
+ insertedVarAssignOperator.getInputs().add(insertOp.getInputs().get(0));
+ insertedVarAssignOperator.setSourceLocation(sourceLoc);
+ insertOp.getInputs().set(0, new MutableObject<>(insertedVarAssignOperator));
+
// Makes the id of the insert var point to the record variable.
context.newVarFromExpression(compiledInsert.getVar());
- context.setVar(compiledInsert.getVar(),
- ((VariableReferenceExpression) insertOp.getPayloadExpression().getValue()).getVariableReference());
+ context.setVar(compiledInsert.getVar(), insertedVar);
+
Pair<ILogicalExpression, Mutable<ILogicalOperator>> p =
langExprToAlgExpression(returnExpression, new MutableObject<>(inputOperator));
- // Adds an assign operator for the returning expression.
+ // Adds an assign operator for the result of the returning expression.
LogicalVariable resultVar = context.newVar();
- AssignOperator assignOperator = new AssignOperator(resultVar, new MutableObject<>(p.first));
- assignOperator.getInputs().add(p.second);
- assignOperator.setSourceLocation(sourceLoc);
+ AssignOperator createResultAssignOperator = new AssignOperator(resultVar, new MutableObject<>(p.first));
+ createResultAssignOperator.getInputs().add(p.second);
+ createResultAssignOperator.setSourceLocation(sourceLoc);
// Adds a distribute result operator.
List<Mutable<ILogicalExpression>> expressions = new ArrayList<>();
@@ -629,7 +637,7 @@
ResultSetSinkId rssId = new ResultSetSinkId(metadataProvider.getResultSetId());
ResultSetDataSink sink = new ResultSetDataSink(rssId, null);
DistributeResultOperator distResultOperator = new DistributeResultOperator(expressions, sink);
- distResultOperator.getInputs().add(new MutableObject<>(assignOperator));
+ distResultOperator.getInputs().add(new MutableObject<>(createResultAssignOperator));
distResultOperator.setSourceLocation(sourceLoc);
return distResultOperator;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.1.ddl.sqlpp
new file mode 100644
index 0000000..a41976b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.1.ddl.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+/*
+ * Test case Name : insert-returning-fieldname
+ * Description : Check fields returned on insert
+ * Expected Result : Success
+ * Date : Mar 2015
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use test;
+
+create type sub as
+{ subscriptionId: uuid }
+;
+
+create dataset subscriptions(sub) primary key subscriptionId;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.3.query.sqlpp
new file mode 100644
index 0000000..dad3a82
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dml/insert-returning-fieldname-qualified/insert-returning-fieldname-qualified.3.query.sqlpp
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+/*
+ * Test case Name : insert-returning-fieldname
+ * Description : Check fields returned on insert
+ * Expected Result : Success
+ * Date : Mar 2015
+ */
+
+use test;
+
+upsert into subscriptions as record(
+[{"subscriptionId":create_uuid(), "word": "hello"}]
+) returning record.word;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 50c2dcc..acb544f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1953,6 +1953,11 @@
</compilation-unit>
</test-case>
<test-case FilePath="dml">
+ <compilation-unit name="insert-returning-fieldname-qualified">
+ <output-dir compare="Text">insert-returning-fieldname</output-dir>
+ </compilation-unit>
+ </test-case>
+ <test-case FilePath="dml">
<compilation-unit name="insert-returning-fieldname-implicit">
<output-dir compare="Text">insert-returning-fieldname</output-dir>
<expected-error>Need an alias for the enclosed expression</expected-error>