[ASTERIXDB-2096][COMP] Fix type casting for ExternalFunction
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
1. The current IntroduceDynamicTypeCastForExternalFunctionRule
cannot handle external function calls in nested record constructor.
This patch fix this issue by visiting all nested parameters for
external functions.
2. The ResultExtractor should be able to handle multiple queries
in single statement file as AQL does.
Change-Id: I65c298def75b18fab01f513012e28fc44fdc2fd4
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2010
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: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/IntroduceDynamicTypeCastForExternalFunctionRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/IntroduceDynamicTypeCastForExternalFunctionRule.java
index c9a873b..bb39993 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/IntroduceDynamicTypeCastForExternalFunctionRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/IntroduceDynamicTypeCastForExternalFunctionRule.java
@@ -25,7 +25,6 @@
import org.apache.asterix.om.typecomputer.base.TypeCastUtils;
import org.apache.asterix.om.types.ARecordType;
import org.apache.asterix.om.types.AUnionType;
-import org.apache.asterix.om.types.BuiltinType;
import org.apache.asterix.om.types.IAType;
import org.apache.asterix.om.utils.NonTaggedFormatUtil;
import org.apache.commons.lang3.mutable.Mutable;
@@ -39,7 +38,6 @@
import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
-import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
/**
@@ -58,12 +56,21 @@
private boolean rewriteFunctionArgs(ILogicalOperator op, Mutable<ILogicalExpression> expRef,
IOptimizationContext context) throws AlgebricksException {
ILogicalExpression expr = expRef.getValue();
- if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL
- || !(expr instanceof ScalarFunctionCallExpression)) {
+ if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
- ScalarFunctionCallExpression funcCallExpr = (ScalarFunctionCallExpression) expr;
boolean changed = false;
+ // go over all arguments recursively
+ AbstractFunctionCallExpression funcCallExpr = (AbstractFunctionCallExpression) expr;
+ for (Mutable<ILogicalExpression> functionArgRef : funcCallExpr.getArguments()) {
+ if (rewriteFunctionArgs(op, functionArgRef, context)) {
+ changed = true;
+ }
+ }
+ // if the current function is builtin function, skip the type casting
+ if (BuiltinFunctions.getBuiltinFunctionIdentifier(funcCallExpr.getFunctionIdentifier()) != null) {
+ return changed;
+ }
IAType inputRecordType;
ARecordType requiredRecordType;
for (int iter1 = 0; iter1 < funcCallExpr.getArguments().size(); iter1++) {
@@ -105,19 +112,6 @@
if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
return false;
}
- AssignOperator assignOp = (AssignOperator) op;
- ILogicalExpression assignExpr = assignOp.getExpressions().get(0).getValue();
- if (assignExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
- return false;
- }
- if (BuiltinFunctions.getBuiltinFunctionIdentifier(
- ((AbstractFunctionCallExpression) assignExpr).getFunctionIdentifier()) != null) {
- return false;
- }
- if (op.acceptExpressionTransform(exprRef -> rewriteFunctionArgs(op, exprRef, context))) {
- return true;
- } else {
- return false;
- }
+ return op.acceptExpressionTransform(expr -> rewriteFunctionArgs(op, expr, context));
}
}
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/ResultExtractor.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/ResultExtractor.java
index 0bba635..8c0f8e1 100644
--- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/ResultExtractor.java
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/ResultExtractor.java
@@ -52,11 +52,13 @@
String type = "";
String status = "";
- String results = "";
+ StringBuilder resultBuilder = new StringBuilder();
String field = "";
+ String fieldPrefix = "";
for (Iterator<String> sIter = result.fieldNames(); sIter.hasNext();) {
field = sIter.next();
- switch (field) {
+ fieldPrefix = field.split("-")[0];
+ switch (fieldPrefix) {
case "requestID":
break;
case "clientContextID":
@@ -81,32 +83,30 @@
case "results":
if (result.get(field).size() <= 1) {
if (result.get(field).size() == 0) {
- results = "";
+ resultBuilder.append("");
} else if (result.get(field).isArray()) {
if (result.get(field).get(0).isTextual()) {
- results = result.get(field).get(0).asText();
+ resultBuilder.append(result.get(field).get(0).asText());
} else {
ObjectMapper omm = new ObjectMapper();
omm.setDefaultPrettyPrinter(singleLine);
omm.enable(SerializationFeature.INDENT_OUTPUT);
- results = omm.writer(singleLine).writeValueAsString(result.get(field));
+ resultBuilder.append(omm.writer(singleLine).writeValueAsString(result.get(field)));
}
} else {
- results = om.writeValueAsString(result.get(field));
+ resultBuilder.append(om.writeValueAsString(result.get(field)));
}
} else {
- StringBuilder sb = new StringBuilder();
JsonNode[] fields = Iterators.toArray(result.get(field).elements(), JsonNode.class);
if (fields.length > 1) {
for (JsonNode f : fields) {
if (f.isObject()) {
- sb.append(om.writeValueAsString(f));
+ resultBuilder.append(om.writeValueAsString(f));
} else {
- sb.append(f.asText());
+ resultBuilder.append(f.asText());
}
}
}
- results = sb.toString();
}
break;
default:
@@ -114,7 +114,7 @@
}
}
- return IOUtils.toInputStream(results);
+ return IOUtils.toInputStream(resultBuilder.toString());
}
public static String extractHandle(InputStream resultStream) throws Exception {
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/external-library/upperCase/upperCase.3.query.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/external-library/upperCase/upperCase.3.query.aql
index 61ed394..5f0b86c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries/external-library/upperCase/upperCase.3.query.aql
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/external-library/upperCase/upperCase.3.query.aql
@@ -23,3 +23,6 @@
let $i:=testlib#toUpper({"id":1, "text":"lower text"})
return $i;
+
+let $i:= {"field1" : testlib#toUpper({"id":1, "text":"lower text"}), "field2": 123}
+return $i;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.1.ddl.sqlpp
new file mode 100644
index 0000000..8130de2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.1.ddl.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+DROP DATAVERSE externallibtest if exists;
+CREATE DATAVERSE externallibtest;
+USE externallibtest;
+
+create type TextType if not exists as open {
+ id: int32,
+ text: string
+};
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.2.lib.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.2.lib.sqlpp
new file mode 100644
index 0000000..d1e0e87
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.2.lib.sqlpp
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+install externallibtest testlib target/data/externallib/asterix-external-data-testlib.zip
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.3.query.sqlpp
new file mode 100644
index 0000000..f361232
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.3.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+use externallibtest;
+
+let i={"id":1, "text":"lower text"}
+select value `testlib#toUpper`(i);
+
+let i=`testlib#toUpper`({"id":1, "text":"lower text"})
+select value i;
+
+let i= {"field1" : `testlib#toUpper`({"id":1, "text":"lower text"}), "field2": 123}
+select value i;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.4.lib.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.4.lib.sqlpp
new file mode 100644
index 0000000..86af80f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/external-library/upperCase/upperCase.4.lib.sqlpp
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+uninstall externallibtest testlib
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/string/uppercase/uppercase.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/string/uppercase/uppercase.3.query.sqlpp
index cf01d92..bd15944 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/string/uppercase/uppercase.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/string/uppercase/uppercase.3.query.sqlpp
@@ -19,5 +19,4 @@
use test;
-
{'result1':test.uppercase('Hellow'),'result2':test.uppercase(''),'result3':test.uppercase(null)};
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/external-library/upperCase/upperCase.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/external-library/upperCase/upperCase.1.adm
index f475435..e4af2be 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/external-library/upperCase/upperCase.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/external-library/upperCase/upperCase.1.adm
@@ -1,2 +1,3 @@
{ "id": -1, "text": "LOWER TEXT" }
-{ "id": -1, "text": "LOWER TEXT" }
\ No newline at end of file
+{ "id": -1, "text": "LOWER TEXT" }
+{ "field1": { "id": -1, "text": "LOWER TEXT" }, "field2": 123 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_it_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_it_sqlpp.xml
index 4502a85..83cdd82 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_it_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_it_sqlpp.xml
@@ -38,5 +38,10 @@
<output-dir compare="Text">getCapital</output-dir>
</compilation-unit>
</test-case>
+ <test-case FilePath="external-library">
+ <compilation-unit name="upperCase">
+ <output-dir compare="Text">upperCase</output-dir>
+ </compilation-unit>
+ </test-case>
</test-group>
</test-suite>