This is an automated email from the ASF dual-hosted git repository. mblow pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/asterixdb.git
commit cda30085fa50f25f812436599701cfcdfdc1c54b Author: Ali Alsuliman <[email protected]> AuthorDate: Mon Aug 19 20:01:13 2024 -0700 [ASTERIXDB-3487][COMP] Fix inlining UDFs in UnaryExpr/IndexAccessor - user model changes: no - storage format changes: no - interface changes: no Details: Fix inlining UDFs in UnaryExpr/IndexAccessor. Ext-ref: MB-63229 Change-Id: I88c1bd5b67633a609117d80e1e427fd108e323d7 Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18688 Reviewed-by: Ali Alsuliman <[email protected]> Reviewed-by: Michael Blow <[email protected]> Tested-by: Michael Blow <[email protected]> Integration-Tests: Jenkins <[email protected]> --- .../translator/LangExpressionToPlanTranslator.java | 3 +- .../inline-in-expr/inline-in-expr.01.ddl.sqlpp | 37 ++++++++++++++++++++++ .../inline-in-expr/inline-in-expr.02.update.sqlpp | 22 +++++++++++++ .../inline-in-expr/inline-in-expr.03.query.sqlpp | 25 +++++++++++++++ .../inline-in-expr/inline-in-expr.03.adm | 1 + .../src/test/resources/runtimets/sqlpp_queries.xml | 5 +++ .../common/visitor/AbstractInlineUdfsVisitor.java | 19 ++++++++--- 7 files changed, 106 insertions(+), 6 deletions(-) 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 077b39bfe1..d5c916f293 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 @@ -1120,7 +1120,8 @@ abstract class LangExpressionToPlanTranslator } if (!function.isExternal()) { // all non-external UDFs should've been inlined by now - throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, sourceLoc, signature); + throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, sourceLoc, + "UDF not inlined: " + signature); } IFunctionInfo finfo = ExternalFunctionCompilerUtil.getExternalFunctionInfo(metadataProvider, function); AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(finfo, args); diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.01.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.01.ddl.sqlpp new file mode 100644 index 0000000000..d930fc9efb --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.01.ddl.sqlpp @@ -0,0 +1,37 @@ +/* + * 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 : Test inlining of UDFs + */ + +drop dataverse experiments if exists; +create dataverse experiments; +use experiments; + +create function fun1(...) { + args[0] + args[1] +}; + +create function fun2(...) { + args[0] - args[1] +}; + +CREATE TYPE openType AS {id: int}; +CREATE DATASET ds(openType) primary key id; \ No newline at end of file diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.02.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.02.update.sqlpp new file mode 100644 index 0000000000..22cfcc1ffc --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.02.update.sqlpp @@ -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. + */ + +use experiments; + +UPSERT INTO ds {"id": 1, "a": 1, "b": 2, "c": [10,20,30,40], "d": [100,200,300,400]}; \ No newline at end of file diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.03.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.03.query.sqlpp new file mode 100644 index 0000000000..5ae85ea176 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/user-defined-functions/inline-in-expr/inline-in-expr.03.query.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. + */ + +USE experiments; + +SELECT -fun1(ds.a, ds.b) AS x1, +ds.c[fun1(ds.a, ds.b)] AS x2, +ds.d[-fun2(ds.a, ds.b)] AS x3 +FROM ds; \ No newline at end of file diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/user-defined-functions/inline-in-expr/inline-in-expr.03.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/user-defined-functions/inline-in-expr/inline-in-expr.03.adm new file mode 100644 index 0000000000..145a6e3271 --- /dev/null +++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/user-defined-functions/inline-in-expr/inline-in-expr.03.adm @@ -0,0 +1 @@ +{ "x1": -3, "x2": 40, "x3": 200 } \ No newline at end of file diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml index 509d90b2d9..6f614ef13e 100644 --- a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml +++ b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml @@ -13191,6 +13191,11 @@ </test-case> </test-group> <test-group name="user-defined-functions"> + <test-case FilePath="user-defined-functions"> + <compilation-unit name="inline-in-expr"> + <output-dir compare="Text">inline-in-expr</output-dir> + </compilation-unit> + </test-case> <test-case FilePath="user-defined-functions"> <compilation-unit name="bad-function-ddl-1"> <output-dir compare="Text">bad-function-ddl-1</output-dir> diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/AbstractInlineUdfsVisitor.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/AbstractInlineUdfsVisitor.java index 39754251de..629be84e94 100644 --- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/AbstractInlineUdfsVisitor.java +++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/AbstractInlineUdfsVisitor.java @@ -153,7 +153,14 @@ public abstract class AbstractInlineUdfsVisitor extends AbstractQueryExpressionV public Boolean visit(IndexAccessor fa, Void arg) throws CompilationException { Pair<Boolean, Expression> p = inlineUdfsAndViewsInExpr(fa.getExpr()); fa.setExpr(p.second); - return p.first; + boolean inlined = p.first; + Expression indexExpr = fa.getIndexExpr(); + if (indexExpr != null) { + Pair<Boolean, Expression> p2 = inlineUdfsAndViewsInExpr(indexExpr); + fa.setIndexExpr(p2.second); + inlined |= p2.first; + } + return inlined; } @Override @@ -250,7 +257,9 @@ public abstract class AbstractInlineUdfsVisitor extends AbstractQueryExpressionV @Override public Boolean visit(UnaryExpr u, Void arg) throws CompilationException { - return u.getExpr().accept(this, arg); + Pair<Boolean, Expression> p = inlineUdfsAndViewsInExpr(u.getExpr()); + u.setExpr(p.second); + return p.first; } @Override @@ -275,7 +284,7 @@ public abstract class AbstractInlineUdfsVisitor extends AbstractQueryExpressionV if (returnExpression != null) { Pair<Boolean, Expression> rewrittenReturnExpr = inlineUdfsAndViewsInExpr(returnExpression); insert.setReturnExpression(rewrittenReturnExpr.second); - changed |= rewrittenReturnExpr.first; + changed = rewrittenReturnExpr.first; } Pair<Boolean, Expression> rewrittenBodyExpression = inlineUdfsAndViewsInExpr(insert.getBody()); insert.setBody(rewrittenBodyExpression.second); @@ -284,10 +293,10 @@ public abstract class AbstractInlineUdfsVisitor extends AbstractQueryExpressionV @Override public Boolean visit(CopyToStatement stmtCopy, Void arg) throws CompilationException { - boolean changed = false; + boolean changed; Pair<Boolean, Expression> queryBody = inlineUdfsAndViewsInExpr(stmtCopy.getBody()); - changed |= queryBody.first; + changed = queryBody.first; stmtCopy.setBody(queryBody.second); Pair<Boolean, List<Expression>> path = inlineUdfsInExprList(stmtCopy.getPathExpressions());
