public void setUp() { Constant c1 = new Constant("3"); Constant c2 = new Constant("f"); Table t = new Table("mytable"); Field f = new Field("myfield", t); v1 = new SelectValue(c1, "alias1"); v2 = new SelectValue(c1, "alias1"); v3 = new SelectValue(c1, "alias2"); v4 = new SelectValue(c2, "alias1"); v5 = new SelectValue(c2, "alias3"); v6 = new SelectValue(f, null); v7 = new SelectValue(f, "myfield"); v8 = new SelectValue(f, "alias4"); }
/** * Populates the SELECT list of a new Query object, given an old Query's SELECT list to use as a * reference, a valueMap (to pass to reconstructAbstractValue), and a Table to represent the * PrecomputedTable. * * @param oldSelect the old Query's SELECT list to use a pattern * @param precomputedSqlTable the Table object that remapped AbstractValues should refer to * @param valueMap a mapping from AbstractValue in the PrecomputedTable onto the SelectValue * that contains it * @param tableSet a Set of all the tables that are being replaced - ie the Set of tables in the * PrecomputedTable. We use this to work out which unrepresented AbstractValues are problems. * @param groupBy true if the PrecomputedTable contains a GROUP BY clause * @param newQuery the new Query object to populate * @throws QueryOptimiserException if reconstructAbstractValue finds an AbstractValue that * cannot be constructed, given the PrecomputedTable */ protected static void reconstructSelectValues(List<SelectValue> oldSelect, Table precomputedSqlTable, Map<AbstractValue, SelectValue> valueMap, Set<AbstractTable> tableSet, boolean groupBy, Query newQuery) throws QueryOptimiserException { for (SelectValue selectValue : oldSelect) { AbstractValue value = selectValue.getValue(); AbstractValue newValue = reconstructAbstractValue(value, precomputedSqlTable, valueMap, tableSet, groupBy); SelectValue newSelectValue = new SelectValue(newValue, selectValue.getAlias()); newQuery.addSelect(newSelectValue); } }
public void testEquals() throws Exception { assertEquals(v1, v2); assertTrue("Expected v1 to not equal v3", !v1.equals(v3)); assertTrue("Expected v1 to not equal v4", !v1.equals(v4)); assertTrue("Expected v1 to not equal v5", !v1.equals(v5)); assertTrue("Expected v1 to not equal v6", !v1.equals(v6)); assertTrue("Expected v1 to not equal v7", !v1.equals(v7)); assertTrue("Expected v1 to not equal v8", !v1.equals(v8)); assertEquals(v6, v7); assertTrue("Expected v6 to not equal v8", !v6.equals(v8)); }
/** * Compares two Lists of SelectValues, and returns true if all the items in the first List are * present in the second List, ignoring the SelectValue alias. * * @param list1 the list of items that must be present in list2 * @param list2 the list of items to look in * @return true if every item in list1 is present in list2, ignoring SelectValue aliases * TODO: take this function out - we don't need it. */ protected static boolean compareSelectLists(List<SelectValue> list1, List<SelectValue> list2) { Set<AbstractValue> allValues = new LinkedHashSet<AbstractValue>(); for (SelectValue selectValue : list2) { allValues.add(selectValue.getValue()); } for (SelectValue selectValue : list1) { if (!allValues.contains(selectValue.getValue())) { return false; } } return true; }
sb.append(obv.getAlias());
public void testHashCode() throws Exception { assertEquals(v1.hashCode(), v1.hashCode()); assertEquals(v1.hashCode(), v2.hashCode()); assertTrue("Expected v1.hashCode() to not equal v3.hashCode()", !(v1.hashCode() == v3.hashCode())); assertTrue("Expected v1.hashCode() to not equal v4.hashCode()", !(v1.hashCode() == v4.hashCode())); assertTrue("Expected v1.hashCode() to not equal v5.hashCode()", !(v1.hashCode() == v5.hashCode())); assertTrue("Expected v1.hashCode() to not equal v6.hashCode()", !(v1.hashCode() == v6.hashCode())); assertTrue("Expected v1.hashCode() to not equal v7.hashCode()", !(v1.hashCode() == v7.hashCode())); assertTrue("Expected v1.hashCode() to not equal v8.hashCode()", !(v1.hashCode() == v8.hashCode())); assertEquals(v6.hashCode(), v7.hashCode()); assertTrue("Expected v6.hashCode() to not equal v8.hashCode()", !(v6.hashCode() == v8.hashCode())); } }
public void testGetSQLString() throws Exception { assertEquals("3 AS alias1", v1.getSQLString()); assertEquals("f AS alias1", v4.getSQLString()); assertEquals("mytable.myfield", v6.getSQLString()); assertEquals("mytable.myfield", v7.getSQLString()); assertEquals("mytable.myfield AS alias4", v8.getSQLString()); }
/** * Compares two Lists of SelectValues, and returns true if all the items in the first List are * present in the second List, ignoring the SelectValue alias. * * @param list1 the list of items that must be present in list2 * @param list2 the list of items to look in * @return true if every item in list1 is present in list2, ignoring SelectValue aliases * TODO: take this function out - we don't need it. */ protected static boolean compareSelectLists(List<SelectValue> list1, List<SelectValue> list2) { Set<AbstractValue> allValues = new LinkedHashSet<AbstractValue>(); for (SelectValue selectValue : list2) { allValues.add(selectValue.getValue()); } for (SelectValue selectValue : list1) { if (!allValues.contains(selectValue.getValue())) { return false; } } return true; }
sb.append(obv.getAlias());
AbstractValue v3 = new Constant("c3"); AbstractValue v4 = new Constant("c4"); SelectValue sq1 = new SelectValue(v1, "queryalias1"); SelectValue sq2 = new SelectValue(v2, "queryalias2"); SelectValue sq3 = new SelectValue(v3, "queryalias3"); SelectValue sq4 = new SelectValue(v4, "queryalias4"); SelectValue sp1 = new SelectValue(v1, "precompalias1"); SelectValue sp2 = new SelectValue(v2, "precompalias2"); Map valueMap = new HashMap(); valueMap.put(v1, sp1); QueryOptimiser.reconstructSelectValues(oldSelect, precomputedSqlTable, valueMap, tableSet, true, newQuery); SelectValue se1 = new SelectValue(new Field("precompalias1", precomputedSqlTable), "queryalias1"); SelectValue se2 = new SelectValue(new Field("precompalias2", precomputedSqlTable), "queryalias2"); SelectValue se3 = new SelectValue(v3, "queryalias3"); SelectValue se4 = new SelectValue(v4, "queryalias4"); List expectedSelect = new ArrayList(); expectedSelect.add(se1);
/** * Populates the SELECT list of a new Query object, given an old Query's SELECT list to use as a * reference, a valueMap (to pass to reconstructAbstractValue), and a Table to represent the * PrecomputedTable. * * @param oldSelect the old Query's SELECT list to use a pattern * @param precomputedSqlTable the Table object that remapped AbstractValues should refer to * @param valueMap a mapping from AbstractValue in the PrecomputedTable onto the SelectValue * that contains it * @param tableSet a Set of all the tables that are being replaced - ie the Set of tables in the * PrecomputedTable. We use this to work out which unrepresented AbstractValues are problems. * @param groupBy true if the PrecomputedTable contains a GROUP BY clause * @param newQuery the new Query object to populate * @throws QueryOptimiserException if reconstructAbstractValue finds an AbstractValue that * cannot be constructed, given the PrecomputedTable */ protected static void reconstructSelectValues(List<SelectValue> oldSelect, Table precomputedSqlTable, Map<AbstractValue, SelectValue> valueMap, Set<AbstractTable> tableSet, boolean groupBy, Query newQuery) throws QueryOptimiserException { for (SelectValue selectValue : oldSelect) { AbstractValue value = selectValue.getValue(); AbstractValue newValue = reconstructAbstractValue(value, precomputedSqlTable, valueMap, tableSet, groupBy); SelectValue newSelectValue = new SelectValue(newValue, selectValue.getAlias()); newQuery.addSelect(newSelectValue); } }
valueMap.put(value.getValue(), value);
private static void populateWhereClause(PrecomputedTable precomputedTable, Query query, Query precompQuery, Map<AbstractValue, SelectValue> valueMap, Query currentQuery, Set<AbstractConstraint> whereConstraintEqualsSet, Table precomputedSqlTable, Query newQuery, Field orderByField, List<AbstractValue> precompOrderBy) throws QueryOptimiserException { if ((orderByField == null) || (!query.getGroupBy().isEmpty())) { reconstructAbstractConstraints(currentQuery.getWhere(), precomputedSqlTable, valueMap, precompQuery.getFrom(), false, newQuery.getWhere(), whereConstraintEqualsSet, null, 0, null, false, false); } else { AbstractValue maybeDesc = precompOrderBy.get(0); boolean reverse = false; if (maybeDesc instanceof OrderDescending) { maybeDesc = ((OrderDescending) maybeDesc).getValue(); reverse = true; } Field firstPrecompOrderBy = new Field(valueMap.get( maybeDesc).getAlias(), precomputedSqlTable); reconstructAbstractConstraints(currentQuery.getWhere(), precomputedSqlTable, valueMap, precompQuery.getFrom(), false, newQuery.getWhere(), whereConstraintEqualsSet, firstPrecompOrderBy, precompOrderBy.size(), orderByField, precomputedTable.getFirstOrderByHasNoNulls(), reverse); } }
public void testValueMap() throws Exception { Set values = new HashSet(); AbstractValue v1 = new Constant("'c1'"); AbstractValue v2 = new Constant("'c2'"); AbstractValue v3 = new Constant("'c3'"); SelectValue s1 = new SelectValue(v1, "alias1"); SelectValue s2 = new SelectValue(v2, "alias2"); SelectValue s3 = new SelectValue(v3, "alias3"); values.add(s1); values.add(s2); values.add(s3); Map result = new HashMap(); result.put(v1, s1); result.put(v2, s2); result.put(v3, s3); Query q1 = new Query("SELECT 'c1' AS alias1, 'c2' AS alias2, 'c3' AS alias3 FROM table"); PrecomputedTable pt = new PrecomputedTable(q1, q1.getSQLString(), "name", null, con); assertEquals(result, pt.getValueMap()); }
nextPrecompOrderBy = new OrderDescending(new Field( valueMap.get(((OrderDescending) precompOrderByField).getValue()) .getAlias(), precomputedSqlTable)); } else { nextPrecompOrderBy = new Field(valueMap .get(precompOrderByField).getAlias(), precomputedSqlTable); Set<AbstractValue> newS = new LinkedHashSet<AbstractValue>(); for (SelectValue newSelectSV : newQuery.getSelect()) { newS.add(newSelectSV.getValue()); nextPrecompOrderBy = new Field(valueMap.get( ((OrderDescending) precompOrderByField) .getValue()).getAlias(), precomputedSqlTable); } else { nextPrecompOrderBy = new Field(valueMap .get(precompOrderByField).getAlias(), precomputedSqlTable); newQuery.addSelect(new SelectValue(orderByField, "orderby_field_from_pt"));
valueMap.put(value.getValue(), value);
private static void populateWhereClause(PrecomputedTable precomputedTable, Query query, Query precompQuery, Map<AbstractValue, SelectValue> valueMap, Query currentQuery, Set<AbstractConstraint> whereConstraintEqualsSet, Table precomputedSqlTable, Query newQuery, Field orderByField, List<AbstractValue> precompOrderBy) throws QueryOptimiserException { if ((orderByField == null) || (!query.getGroupBy().isEmpty())) { reconstructAbstractConstraints(currentQuery.getWhere(), precomputedSqlTable, valueMap, precompQuery.getFrom(), false, newQuery.getWhere(), whereConstraintEqualsSet, null, 0, null, false, false); } else { AbstractValue maybeDesc = precompOrderBy.get(0); boolean reverse = false; if (maybeDesc instanceof OrderDescending) { maybeDesc = ((OrderDescending) maybeDesc).getValue(); reverse = true; } Field firstPrecompOrderBy = new Field(valueMap.get( maybeDesc).getAlias(), precomputedSqlTable); reconstructAbstractConstraints(currentQuery.getWhere(), precomputedSqlTable, valueMap, precompQuery.getFrom(), false, newQuery.getWhere(), whereConstraintEqualsSet, firstPrecompOrderBy, precompOrderBy.size(), orderByField, precomputedTable.getFirstOrderByHasNoNulls(), reverse); } }
SelectValue sv = new SelectValue(v, alias);
nextPrecompOrderBy = new OrderDescending(new Field( valueMap.get(((OrderDescending) precompOrderByField).getValue()) .getAlias(), precomputedSqlTable)); } else { nextPrecompOrderBy = new Field(valueMap .get(precompOrderByField).getAlias(), precomputedSqlTable); Set<AbstractValue> newS = new LinkedHashSet<AbstractValue>(); for (SelectValue newSelectSV : newQuery.getSelect()) { newS.add(newSelectSV.getValue()); nextPrecompOrderBy = new Field(valueMap.get( ((OrderDescending) precompOrderByField) .getValue()).getAlias(), precomputedSqlTable); } else { nextPrecompOrderBy = new Field(valueMap .get(precompOrderByField).getAlias(), precomputedSqlTable); newQuery.addSelect(new SelectValue(orderByField, "orderby_field_from_pt"));
while (iter.hasNext() && (!hasAggregate)) { SelectValue sv = iter.next(); AbstractValue av = sv.getValue(); hasAggregate = hasAggregate || av.isAggregate();