Permalink
Browse files

MODE-1680: Extend query processor to allow use aliased joinings

  • Loading branch information...
okulikov authored and rhauch committed Mar 11, 2013
1 parent 7c4bee8 commit b4198c1ed174e4b71150312a0b2fb8dc301c11b7
@@ -320,6 +320,22 @@ protected ProcessingComponent createComponent( QueryCommand originalQuery,
case SORT:
// Create the component under the SORT ...
assert node.getChildCount() == 1;
+
+ List<PlanNode> project = node.findAllAtOrBelow(Type.PROJECT);
+ assert project != null;
+
+ ArrayList<Column> allColumns = new ArrayList();
+ for (PlanNode p : project) {
+ allColumns.addAll(p.getPropertyAsList(Property.PROJECT_COLUMNS, Column.class));
+ }
+
+ ArrayList<String> ctypes = new ArrayList();
+ for (PlanNode p : project) {
+ ctypes.addAll(p.getPropertyAsList(Property.PROJECT_COLUMN_TYPES, String.class));
+ }
+
+ QueryResultColumns orderColumns = new QueryResultColumns(allColumns, ctypes, context.getHints().hasFullTextSearch);
+
ProcessingComponent sortDelegate = createComponent(originalQuery,
context,
node.getFirstChild(),
@@ -343,7 +359,7 @@ protected ProcessingComponent createComponent( QueryCommand originalQuery,
if (alias != null) sourceNamesByAlias.put(alias, name);
}
// Now create the sorting component ...
- component = new SortValuesComponent(sortDelegate, orderings, sourceNamesByAlias);
+ component = new SortValuesComponent(sortDelegate, orderings, orderColumns, sourceNamesByAlias);
} else {
// Order by the location(s) because it's before a merge-join ...
component = new SortLocationsComponent(sortDelegate);
@@ -47,12 +47,19 @@
private final Comparator<Object[]> sortingComparator;
public SortValuesComponent( ProcessingComponent delegate,
- List<Ordering> orderings,
+ List<Ordering> orderings,
Map<SelectorName, SelectorName> sourceNamesByAlias ) {
super(delegate);
this.sortingComparator = createSortComparator(delegate.getContext(), delegate.getColumns(), orderings, sourceNamesByAlias);
}
+ public SortValuesComponent( ProcessingComponent delegate,
+ List<Ordering> orderings, QueryResultColumns columns,
+ Map<SelectorName, SelectorName> sourceNamesByAlias ) {
+ super(delegate);
+ this.sortingComparator = createSortComparator(delegate.getContext(), columns, orderings, sourceNamesByAlias);
+ }
+
/**
* @return sortingComparator
*/
@@ -40,6 +40,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@@ -2972,6 +2973,63 @@ public void shouldFindChildrenOfRootUsingIsChildNodeCriteria() throws Exception
assertNodesAreFound(queryString, Query.JCR_SQL2, "/jcr:system", "/Cars", "/Other", "/NodeB", "/node1", "/node2");
}
+ @Test
+ public void testOrderByWithAliases() throws Exception {
+ //fill the repository with test data
+ Node src = session.getRootNode().addNode("src", "nt:folder");
+
+ //add node f1 with child jcr:content
+ Node f1 = src.addNode("f1", "nt:file");
+ f1.addMixin("mix:simpleVersionable");
+ Node content1 = f1.addNode("jcr:content", "nt:resource");
+ content1.setProperty("jcr:data", session.getValueFactory().createBinary("Node f1".getBytes()));
+
+ //save and slip a bit to have difference in time of node creation.
+ session.save();
+ Thread.currentThread().sleep(1000);
+
+ //add node f2 with child jcr:content
+ Node f2 = src.addNode("f2", "nt:file");
+ f2.addMixin("mix:simpleVersionable");
+ Node content2 = f2.addNode("jcr:content", "nt:resource");
+ content2.setProperty("jcr:data", session.getValueFactory().createBinary("Node f2".getBytes()));
+
+ session.save();
+
+ System.out.println("-------------------- MyQueryTest---------------------");
+
+ String descOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:created] DESC";
+ String ascOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:created] ASC";
+
+ QueryManager queryManager = session.getWorkspace().getQueryManager();
+ Query query = queryManager.createQuery(descOrder, Query.JCR_SQL2);
+ QueryResult result = query.execute();
+
+ //checking first query
+ RowIterator it = result.getRows();
+ assertEquals(2, it.getSize());
+
+ Node n1 = it.nextRow().getNode();
+ Node n2 = it.nextRow().getNode();
+
+ assertEquals("f2", n1.getName());
+ assertEquals("f1", n2.getName());
+
+ //the same request with other order
+ query = queryManager.createQuery(ascOrder, Query.JCR_SQL2);
+ result = query.execute();
+
+ //checking second query
+ it = result.getRows();
+ assertEquals(2, it.getSize());
+
+ n1 = it.nextRow().getNode();
+ n2 = it.nextRow().getNode();
+
+ assertEquals("f1", n1.getName());
+ assertEquals("f2", n2.getName());
+ }
+
private void assertNodesAreFound( String queryString,
String queryType,
String... expectedNodesPaths ) throws RepositoryException {

0 comments on commit b4198c1

Please sign in to comment.