[NO ISSUE][FUN] Fix type inference for random()
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- random(arg) return type should be nullable
- fix documentation for random(arg) to match implementation
Change-Id: Ifc46ddeaad5bb8999c4a869e7fbc0a5b3c5cde7c
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3240
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: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.1.query.sqlpp
index bf35f87..7d6bb85 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.1.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.1.query.sqlpp
@@ -38,5 +38,7 @@
select distinct value random(t)
)),
- "t5": [ random(missing) is missing, random(null) is null ]
+ "t5": [ random(missing) is missing, random(null) is null ],
+
+ "t6": [ random("ILLEGAL_TYPE") ]
}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.1.adm
index 4be3e9a..9366d4e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.1.adm
@@ -1 +1 @@
-{ "t1": 6, "t2": 6, "t3": 6, "t4": 6, "t5": [ true, true ] }
\ No newline at end of file
+{ "t1": 6, "t2": 6, "t3": 6, "t4": 6, "t5": [ true, true ], "t6": [ null ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-doc/src/main/markdown/builtins/12_misc.md b/asterixdb/asterix-doc/src/main/markdown/builtins/12_misc.md
index dfb1b0b..df7cf8c 100644
--- a/asterixdb/asterix-doc/src/main/markdown/builtins/12_misc.md
+++ b/asterixdb/asterix-doc/src/main/markdown/builtins/12_misc.md
@@ -87,8 +87,7 @@
* Return Value:
* A random number of type `double` between 0 and 1,
* `missing` if the argument is a `missing` value,
- * `null` if the argument is a `null` value,
- * any other non-numeric input value will cause a type error.
+ * `null` if the argument is a `null` value or a non-numeric value.
* Example:
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
index 623cf08..43896d0 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
@@ -1483,7 +1483,7 @@
addFunction(CREATE_QUERY_UID, ABinaryTypeComputer.INSTANCE, false);
addFunction(UUID_CONSTRUCTOR, AUUIDTypeComputer.INSTANCE, true);
addFunction(RANDOM, ADoubleTypeComputer.INSTANCE, false);
- addFunction(RANDOM_WITH_SEED, ADoubleTypeComputer.INSTANCE, false);
+ addFunction(RANDOM_WITH_SEED, ADoubleTypeComputer.INSTANCE_NULLABLE, false);
addFunction(DATE_CONSTRUCTOR, ADateTypeComputer.INSTANCE, true);
addFunction(DATETIME_CONSTRUCTOR, ADateTimeTypeComputer.INSTANCE, true);
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ADoubleTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ADoubleTypeComputer.java
index ec415e8..00301b6 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ADoubleTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ADoubleTypeComputer.java
@@ -19,6 +19,7 @@
package org.apache.asterix.om.typecomputer.impl;
import org.apache.asterix.om.typecomputer.base.AbstractResultTypeComputer;
+import org.apache.asterix.om.types.AUnionType;
import org.apache.asterix.om.types.BuiltinType;
import org.apache.asterix.om.types.IAType;
import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -26,14 +27,19 @@
public class ADoubleTypeComputer extends AbstractResultTypeComputer {
- public static final ADoubleTypeComputer INSTANCE = new ADoubleTypeComputer();
+ public static final ADoubleTypeComputer INSTANCE = new ADoubleTypeComputer(false);
- private ADoubleTypeComputer() {
+ public static final ADoubleTypeComputer INSTANCE_NULLABLE = new ADoubleTypeComputer(true);
+
+ private final IAType type;
+
+ private ADoubleTypeComputer(boolean nullable) {
+ IAType t = BuiltinType.ADOUBLE;
+ type = nullable ? AUnionType.createNullableType(t) : t;
}
@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
- return BuiltinType.ADOUBLE;
+ return type;
}
-
}