From 4c8b4486bc313abe641be1b95f688b7cb9b99a68 Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Thu, 31 Jan 2019 04:11:57 -0800
Subject: [PATCH] Fix overwritten columns in selclause resultSchema

the `SelectClause.computeSchema` method aggregates a `resultSchema`
member by appending all of the ColumnInfos for each of the selectValues
in the statement.

However, when efforts were made to make Schema less "mutable", I think
a change was overlooked that causes all but the last selectValue's
ColumnInfos to remain in the resultSchema.

I believe this was left undetected because the resultSchema of the
SelectClause isn't really used in many places, except for nested
subqueries. This problem was found by a student, that the first out of
two columns in a nested subquery would "disappear" from the schema.
---
 .../java/edu/caltech/nanodb/queryast/SelectClause.java    | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/main/java/edu/caltech/nanodb/queryast/SelectClause.java b/src/main/java/edu/caltech/nanodb/queryast/SelectClause.java
index 25be891..20b774c 100755
--- a/src/main/java/edu/caltech/nanodb/queryast/SelectClause.java
+++ b/src/main/java/edu/caltech/nanodb/queryast/SelectClause.java
@@ -368,8 +368,9 @@ public class SelectClause {
         // known and non-ambiguous names from the FROM clause.
 
         // SELECT values:  SELECT a, b + c, tbl.* ...
-        resultSchema = new Schema();
+        List<ColumnInfo> resultColumnInfos = new ArrayList<>();
         Set<String> fromTables = fromSchema.getTableNames();
+
         for (SelectValue selVal : selectValues) {
             if (selVal.isWildcard()) {
                 // Make sure that if a table name is specified, that the table
@@ -400,9 +401,12 @@ public class SelectClause {
             }
 
             // Update the result-schema with this select-value's column-info(s).
-            resultSchema = new Schema(selVal.getColumnInfos(fromSchema, resultSchema));
+            resultColumnInfos.addAll(selVal.getColumnInfos(fromSchema, resultSchema));
         }
 
+        // Construct a resultSchema which is the "summation" of all SelectValues' columnInfos.
+        resultSchema = new Schema(resultColumnInfos);
+
         logger.debug("Query schema:  " + resultSchema);
         logger.debug("FROM-clause schema:  " + fromSchema);
 
-- 
GitLab