You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by bu...@apache.org on 2016/04/23 01:56:29 UTC

incubator-asterixdb git commit: ASTERIXDB-1409: fix the type check for functions with two string arguments.

Repository: incubator-asterixdb
Updated Branches:
  refs/heads/master 68914a985 -> eb2170ea1


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 <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <bu...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/commit/eb2170ea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/tree/eb2170ea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/diff/eb2170ea

Branch: refs/heads/master
Commit: eb2170ea1e766446a0253f00317330977035d1b0
Parents: 68914a9
Author: Yingyi Bu <yi...@couchbase.com>
Authored: Fri Apr 22 15:55:41 2016 -0700
Committer: Yingyi Bu <bu...@gmail.com>
Committed: Fri Apr 22 16:56:42 2016 -0700

----------------------------------------------------------------------
 .../substring-after-5.1.ddl.aql                 | 29 +++++++++++
 .../substring-after-5.2.update.aql              | 29 +++++++++++
 .../substring-after-5.3.query.aql               | 27 ++++++++++
 .../substring-after-6.1.ddl.aql                 | 28 ++++++++++
 .../substring-after-6.2.update.aql              | 29 +++++++++++
 .../substring-after-6.3.query.aql               | 27 ++++++++++
 .../substring-after-5/substring-after-5.1.adm   |  1 +
 .../src/test/resources/runtimets/testsuite.xml  | 10 ++++
 .../impl/AbstractBinaryStringTypeComputer.java  | 55 +++++++++++++++-----
 .../BinaryStringBoolOrNullTypeComputer.java     |  5 +-
 .../BinaryStringStringOrNullTypeComputer.java   |  5 +-
 11 files changed, 227 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.1.ddl.aql
----------------------------------------------------------------------
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;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.2.update.aql
----------------------------------------------------------------------
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

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-5/substring-after-5.3.query.aql
----------------------------------------------------------------------
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, "@");

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.1.ddl.aql
----------------------------------------------------------------------
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;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.2.update.aql
----------------------------------------------------------------------
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

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/queries/string/substring-after-6/substring-after-6.3.query.aql
----------------------------------------------------------------------
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, "@");

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/results/string/substring-after-5/substring-after-5.1.adm
----------------------------------------------------------------------
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"

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
----------------------------------------------------------------------
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>

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AbstractBinaryStringTypeComputer.java
----------------------------------------------------------------------
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 @@ package org.apache.asterix.om.typecomputer.impl;
 
 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 @@ abstract public class AbstractBinaryStringTypeComputer implements IResultTypeCom
     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);
+        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();
+        }
+
+        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 ((t0.getTypeTag() != ATypeTag.NULL && t0.getTypeTag() != ATypeTag.STRING)
-                || (t1.getTypeTag() != ATypeTag.NULL && t1.getTypeTag() != ATypeTag.STRING)) {
-            throw new AlgebricksException("Expects String Type.");
+
+        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;
         }
 
-        return getResultType(t0, t1);
+        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);
 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringBoolOrNullTypeComputer.java
----------------------------------------------------------------------
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 @@ package org.apache.asterix.om.typecomputer.impl;
 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 @@ public class BinaryStringBoolOrNullTypeComputer extends AbstractBinaryStringType
     }
 
     @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;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/eb2170ea/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/BinaryStringStringOrNullTypeComputer.java
----------------------------------------------------------------------
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 @@ package org.apache.asterix.om.typecomputer.impl;
 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 @@ public class BinaryStringStringOrNullTypeComputer extends AbstractBinaryStringTy
     }
 
     @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;