ASTERIXDB-1409: fix the type check for functions with two string arguments.
Change-Id: Idd36d9f5ee0f7d6d20afdff2e0669d6be6c6769b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/829
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.1.ddl.aql
new file mode 100644
index 0000000..a455abc
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.1.ddl.aql
@@ -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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type LogType as {
+ id: int64,
+ RemoteHost: string?
+}
+
+create dataset log(LogType) primary key id;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.2.update.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.2.update.aql
new file mode 100644
index 0000000..595405f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.2.update.aql
@@ -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.
+ */
+
+use dataverse test;
+
+insert into dataset log {
+ "id": 1,
+ "RemoteHost": "10.0.0.2@123"
+}
+
+insert into dataset log {
+ "id": 2
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.3.query.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.3.query.aql
new file mode 100644
index 0000000..356b97b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.3.query.aql
@@ -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.
+ */
+
+/* This query is to verify the fix for ASTERIXDB-1409. */
+
+use dataverse test;
+
+for $e in dataset log
+where not(is-null($e.RemoteHost))
+limit 1
+return substring-after($e.RemoteHost, "@");
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.1.ddl.aql
new file mode 100644
index 0000000..ecb80a8
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.1.ddl.aql
@@ -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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type LogType as {
+ id: int64
+}
+
+create dataset log(LogType) primary key id;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.2.update.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.2.update.aql
new file mode 100644
index 0000000..595405f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.2.update.aql
@@ -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.
+ */
+
+use dataverse test;
+
+insert into dataset log {
+ "id": 1,
+ "RemoteHost": "10.0.0.2@123"
+}
+
+insert into dataset log {
+ "id": 2
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.3.query.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.3.query.aql
new file mode 100644
index 0000000..23e6898
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.3.query.aql
@@ -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.
+ */
+
+/* This query is to verify substring-after can work with open type. */
+
+use dataverse test;
+
+for $e in dataset log
+where not(is-null($e.RemoteHost))
+limit 1
+return substring-after($e.RemoteHost, "@");
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring-after-5/substring-after-5.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring-after-5/substring-after-5.1.adm
new file mode 100644
index 0000000..9560a45
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring-after-5/substring-after-5.1.adm
@@ -0,0 +1 @@
+"123"
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
index 43c754b..9101339 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -5374,6 +5374,16 @@
</compilation-unit>
</test-case>
<test-case FilePath="string">
+ <compilation-unit name="substring-after-5">
+ <output-dir compare="Text">substring-after-5</output-dir>
+ </compilation-unit>
+ </test-case>
+ <test-case FilePath="string">
+ <compilation-unit name="substring-after-6">
+ <output-dir compare="Text">substring-after-5</output-dir>
+ </compilation-unit>
+ </test-case>
+ <test-case FilePath="string">
<compilation-unit name="substring-before-1">
<output-dir compare="Text">substring-before-1</output-dir>
</compilation-unit>
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AbstractBinaryStringTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AbstractBinaryStringTypeComputer.java
index 007747d..522218e 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AbstractBinaryStringTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AbstractBinaryStringTypeComputer.java
@@ -20,7 +20,10 @@
import org.apache.asterix.om.typecomputer.base.IResultTypeComputer;
import org.apache.asterix.om.types.ATypeTag;
+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.util.NonTaggedFormatUtil;
import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
@@ -36,24 +39,52 @@
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env,
IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expression;
- if (fce.getArguments().size() < 2)
+ if (fce.getArguments().size() < 2) {
throw new AlgebricksException("Wrong Argument Number.");
+ }
ILogicalExpression arg0 = fce.getArguments().get(0).getValue();
ILogicalExpression arg1 = fce.getArguments().get(1).getValue();
- IAType t0, t1;
- try {
- t0 = (IAType) env.getType(arg0);
- t1 = (IAType) env.getType(arg1);
- } catch (AlgebricksException e) {
- throw new AlgebricksException(e);
- }
- if ((t0.getTypeTag() != ATypeTag.NULL && t0.getTypeTag() != ATypeTag.STRING)
- || (t1.getTypeTag() != ATypeTag.NULL && t1.getTypeTag() != ATypeTag.STRING)) {
- throw new AlgebricksException("Expects String Type.");
+ IAType t0 = (IAType) env.getType(arg0);
+ IAType t1 = (IAType) env.getType(arg1);
+
+ boolean nullable = false;
+ ATypeTag tag0, tag1;
+ if (NonTaggedFormatUtil.isOptional(t0)) {
+ tag0 = ((AUnionType) t0).getNullableType().getTypeTag();
+ nullable = true;
+ } else {
+ tag0 = t0.getTypeTag();
}
- return getResultType(t0, t1);
+ if (NonTaggedFormatUtil.isOptional(t1)) {
+ tag1 = ((AUnionType) t1).getNullableType().getTypeTag();
+ nullable = true;
+ } else {
+ tag1 = t1.getTypeTag();
+ }
+
+ if (tag0 == ATypeTag.NULL && tag1 == ATypeTag.NULL) {
+ return BuiltinType.ANULL;
+ }
+
+ if (tag0 == ATypeTag.NULL || tag1 == ATypeTag.NULL) {
+ nullable = true;
+ }
+
+ // Allow substring to work with ANY types, i.e., types that are unknown at compile time.
+ if (tag0 == ATypeTag.ANY || tag1 == ATypeTag.ANY) {
+ return BuiltinType.ANY;
+ }
+
+ if (tag0 != ATypeTag.NULL && tag0 != ATypeTag.STRING) {
+ throw new AlgebricksException("First argument should be String Type, but it is " + tag0);
+ }
+
+ if (tag1 != ATypeTag.NULL && tag1 != ATypeTag.STRING) {
+ throw new AlgebricksException("Second argument should be String Type, but it is " + tag1);
+ }
+ return getResultType(t0, t1, nullable);
}
- public abstract IAType getResultType(IAType t0, IAType t1);
+ public abstract IAType getResultType(IAType t0, IAType t1, boolean nullable);
}
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringBoolOrNullTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringBoolOrNullTypeComputer.java
index c223b4a..6d46533 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringBoolOrNullTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringBoolOrNullTypeComputer.java
@@ -21,7 +21,6 @@
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.types.TypeHelper;
/**
* @author Xiaoyu Ma
@@ -33,8 +32,8 @@
}
@Override
- public IAType getResultType(IAType t0, IAType t1) {
- if (TypeHelper.canBeNull(t0) || TypeHelper.canBeNull(t1)) {
+ public IAType getResultType(IAType t0, IAType t1, boolean nullable) {
+ if (nullable) {
return AUnionType.createNullableType(BuiltinType.ABOOLEAN);
}
return BuiltinType.ABOOLEAN;
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringStringOrNullTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringStringOrNullTypeComputer.java
index 562d965..55df1e3 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringStringOrNullTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringStringOrNullTypeComputer.java
@@ -21,7 +21,6 @@
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.types.TypeHelper;
/**
* @author Xiaoyu Ma
@@ -34,8 +33,8 @@
}
@Override
- public IAType getResultType(IAType t0, IAType t1) {
- if (TypeHelper.canBeNull(t0) || TypeHelper.canBeNull(t1)) {
+ public IAType getResultType(IAType t0, IAType t1, boolean nullable) {
+ if (nullable) {
return AUnionType.createNullableType(BuiltinType.ASTRING);
}
return BuiltinType.ASTRING;