[ASTERIXDB-3259][MTD] Add 'database' to DatasetFullyQualifiedName

- user model changes: no
- storage format changes: no
- interface changes: no

Change-Id: I87e5a4877a195b917667c7d59e19d4d734124615
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17801
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
index 15f4e98..9bc0f85 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
@@ -2475,7 +2475,8 @@
                 }
             }
 
-            DatasetFullyQualifiedName viewQualifiedName = new DatasetFullyQualifiedName(dataverseName, viewName);
+            DatasetFullyQualifiedName viewQualifiedName =
+                    new DatasetFullyQualifiedName(database, dataverseName, viewName);
 
             Datatype itemTypeEntity = null;
             boolean itemTypeIsInline = false;
@@ -2530,7 +2531,8 @@
                             }
                             ViewDetails refViewDetails = (ViewDetails) refDataset.getDatasetDetails();
                             refDatasetType = refDataset.getDatasetType();
-                            refQualifiedName = new DatasetFullyQualifiedName(refDataverseName, refDatasetName);
+                            refQualifiedName =
+                                    new DatasetFullyQualifiedName(refDatabase, refDataverseName, refDatasetName);
                             refPrimaryKeyFields = refViewDetails.getPrimaryKeyFields();
                         }
 
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/metadata/DatasetFullyQualifiedName.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/metadata/DatasetFullyQualifiedName.java
index 4a1a118..f53a3a3 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/metadata/DatasetFullyQualifiedName.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/metadata/DatasetFullyQualifiedName.java
@@ -23,15 +23,21 @@
 
 public class DatasetFullyQualifiedName implements Serializable {
 
-    private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = 2L;
+    private final String databaseName;
     private final DataverseName dataverseName;
     private final String datasetName;
 
-    public DatasetFullyQualifiedName(DataverseName dataverseName, String datasetName) {
+    public DatasetFullyQualifiedName(String databaseName, DataverseName dataverseName, String datasetName) {
+        this.databaseName = databaseName;
         this.dataverseName = dataverseName;
         this.datasetName = datasetName;
     }
 
+    public String getDatabaseName() {
+        return databaseName;
+    }
+
     public DataverseName getDataverseName() {
         return dataverseName;
     }
@@ -52,13 +58,14 @@
         }
         if (o instanceof DatasetFullyQualifiedName) {
             DatasetFullyQualifiedName that = (DatasetFullyQualifiedName) o;
-            return dataverseName.equals(that.dataverseName) && datasetName.equals(that.datasetName);
+            return Objects.equals(databaseName, that.databaseName) && dataverseName.equals(that.dataverseName)
+                    && datasetName.equals(that.datasetName);
         }
         return false;
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(dataverseName, datasetName);
+        return Objects.hash(databaseName, dataverseName, datasetName);
     }
 }
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/FunctionUtil.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/FunctionUtil.java
index 56432ba..baa5443 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/FunctionUtil.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/FunctionUtil.java
@@ -278,7 +278,8 @@
         if (datasetName == null) {
             throw new CompilationException(ErrorCode.COMPILATION_ERROR, sourceLoc, "Invalid argument to dataset()");
         }
-        return new DatasetFullyQualifiedName(dataverseName, datasetName);
+        String databaseName = MetadataUtil.resolveDatabase(null, dataverseName);
+        return new DatasetFullyQualifiedName(databaseName, dataverseName, datasetName);
     }
 
     private static String getStringConstant(Mutable<ILogicalExpression> arg) {
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/VariableCheckAndRewriteVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/VariableCheckAndRewriteVisitor.java
index bdf1115..b36aff5 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/VariableCheckAndRewriteVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/VariableCheckAndRewriteVisitor.java
@@ -284,9 +284,10 @@
     }
 
     private ViewDecl findDeclaredView(DataverseName dataverseName, String viewName) {
+        String databaseName = MetadataUtil.resolveDatabase(null, dataverseName);
         Map<DatasetFullyQualifiedName, ViewDecl> declaredViews = context.getDeclaredViews();
         return declaredViews.isEmpty() ? null
-                : declaredViews.get(new DatasetFullyQualifiedName(dataverseName, viewName));
+                : declaredViews.get(new DatasetFullyQualifiedName(databaseName, dataverseName, viewName));
     }
 
     @Override
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/Dataset.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/Dataset.java
index 5e46b6c..0ecc988 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/Dataset.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/Dataset.java
@@ -213,7 +213,7 @@
         this.hints = hints;
         this.rebalanceCount = rebalanceCount;
         this.compressionScheme = compressionScheme;
-        datasetFullyQualifiedName = new DatasetFullyQualifiedName(dataverseName, datasetName);
+        this.datasetFullyQualifiedName = new DatasetFullyQualifiedName(databaseName, dataverseName, datasetName);
         this.datasetFormatInfo = datasetFormatInfo;
     }
 
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entitytupletranslators/DatasetTupleTranslator.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entitytupletranslators/DatasetTupleTranslator.java
index c74724d..aa95cc3 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entitytupletranslators/DatasetTupleTranslator.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entitytupletranslators/DatasetTupleTranslator.java
@@ -352,8 +352,10 @@
                         String refDatasetName =
                                 ((AString) foreignKeyRecord.getValueByPos(refDatasetNameFieldPos)).getStringValue();
 
+                        //TODO(DB):
+                        String refDatabase = MetadataUtil.resolveDatabase(null, refDataverseName);
                         foreignKeys.add(new ViewDetails.ForeignKey(foreignKeyFields,
-                                new DatasetFullyQualifiedName(refDataverseName, refDatasetName)));
+                                new DatasetFullyQualifiedName(refDatabase, refDataverseName, refDatasetName)));
                     }
                 }