1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.IntegerVector; 28 29 35 36 public class TableFunctions { 37 38 39 52 static Table anyAllNonCorrelated(Table left_table, Variable[] left_vars, 53 Operator op, Table right_table) { 54 if (right_table.getColumnCount() != left_vars.length) { 56 throw new RuntimeException ( 57 "Input table <> " + left_vars.length + " columns."); 58 } 59 60 if (left_table.getRowCount() == 0) { 62 return left_table; 63 } 64 65 int sz = left_vars.length; 68 int[] left_col_map = new int[sz]; 69 int[] right_col_map = new int[sz]; 70 for (int i = 0; i < sz; ++i) { 71 left_col_map[i] = left_table.findFieldName(left_vars[i]); 72 right_col_map[i] = i; 73 74 78 if (left_col_map[i] == -1) { 79 throw new RuntimeException ("Invalid reference: " + left_vars[i]); 80 } 81 DataTableColumnDef left_type = 82 left_table.getColumnDefAt(left_col_map[i]); 83 DataTableColumnDef right_type = right_table.getColumnDefAt(i); 84 if (!left_type.getTType().comparableTypes(right_type.getTType())) { 85 throw new Error ( 86 "The type of the sub-query expression " + left_vars[i] + "(" + 87 left_type.getSQLTypeString() + ") is incompatible with " + 88 "the sub-query type " + right_type.getSQLTypeString() + "."); 89 } 90 } 91 92 94 IntegerVector select_vec; 95 96 if (op.isSubQueryForm(Operator.ALL)) { 97 110 if (op.is(">") || op.is(">=")) { 111 TObject[] highest_cells = 113 right_table.getLastCellContent(right_col_map); 114 select_vec = left_table.selectRows(left_col_map, op, highest_cells); 117 } 118 else if (op.is("<") || op.is("<=")) { 119 TObject[] lowest_cells = 121 right_table.getFirstCellContent(right_col_map); 122 select_vec = left_table.selectRows(left_col_map, op, lowest_cells); 125 } 126 else if (op.is("=")) { 127 TObject[] single_cell = 129 right_table.getSingleCellContent(right_col_map); 130 if (single_cell != null) { 131 select_vec = left_table.selectRows(left_col_map, op, single_cell); 133 } 134 else { 135 return left_table.emptySelect(); 138 } 139 } 140 else if (op.is("<>")) { 141 select_vec = INHelper.notIn(left_table, right_table, 143 left_col_map, right_col_map); 144 } 145 else { 146 throw new RuntimeException ( 147 "Don't understand operator '" + op + "' in ALL."); 148 } 149 } 150 else if (op.isSubQueryForm(Operator.ANY)) { 151 152 164 if (op.is(">") || op.is(">=")) { 165 TObject[] lowest_cells = 167 right_table.getFirstCellContent(right_col_map); 168 select_vec = left_table.selectRows(left_col_map, op, lowest_cells); 171 } 172 else if (op.is("<") || op.is("<=")) { 173 TObject[] highest_cells = 175 right_table.getLastCellContent(right_col_map); 176 select_vec = left_table.selectRows(left_col_map, op, highest_cells); 179 } 180 else if (op.is("=")) { 181 select_vec = INHelper.in(left_table, right_table, 183 left_col_map, right_col_map); 184 } 185 else if (op.is("<>")) { 186 TObject[] cells = right_table.getSingleCellContent(right_col_map); 188 if (cells != null) { 189 select_vec = left_table.selectRows(left_col_map, op, cells); 191 } 192 else { 193 return left_table; 196 } 197 } 198 else { 199 throw new RuntimeException ( 200 "Don't understand operator '" + op + "' in ANY."); 201 } 202 } 203 else { 204 throw new RuntimeException ("Unrecognised sub-query operator."); 205 } 206 207 VirtualTable rtable = new VirtualTable(left_table); 209 rtable.set(left_table, select_vec); 210 211 return rtable; 212 } 213 214 215 216 217 218 219 220 221 222 } 223 | Popular Tags |