@Test public void testUnknownFunction() throws Throwable { // will be wrapped somewhere above expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Cannot find implementation for function unknown()"); Symbol unknownFunction = new Function( new FunctionInfo( new FunctionIdent("unknown", ImmutableList.<DataType>of()), DataTypes.BOOLEAN), ImmutableList.<Symbol>of()); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "unknownFunction", testRouting, Collections.singletonList(unknownFunction), EMPTY_PROJECTIONS); try { getBucket(collectNode); } catch (ExecutionException e) { throw e.getCause(); } }
@Test public void testCollectDocId() throws Exception { setUpCharacters(); Planner.Context plannerContext = new Planner.Context(clusterService(), UUID.randomUUID()); CollectPhase collectNode = createCollectNode(plannerContext, false); List<Bucket> results = getBuckets(collectNode); assertThat(results.size(), is(2)); int seenJobSearchContextId = -1; for (Bucket rows : results) { assertThat(rows.size(), is(1)); Object docIdCol = rows.iterator().next().get(0); assertNotNull(docIdCol); assertThat(docIdCol, instanceOf(Long.class)); long docId = (long) docIdCol; // unpack jobSearchContextId and reader doc id from docId int jobSearchContextId = (int) (docId >> 32); int doc = (int) docId; assertThat(doc, is(0)); assertThat(jobSearchContextId, greaterThan(-1)); if (seenJobSearchContextId == -1) { assertThat(jobSearchContextId, anyOf(is(0), is(1))); seenJobSearchContextId = jobSearchContextId; } else { assertThat(jobSearchContextId, is(seenJobSearchContextId == 0 ? 1 : 0)); } } }
@Test public void testCollectUnknownReference() throws Throwable { expectedException.expect(UnhandledServerException.class); expectedException.expectMessage("Unknown Reference some.table.some_column"); Reference unknownReference = new Reference( new ReferenceInfo( new ReferenceIdent(new TableIdent("some", "table"), "some_column"), RowGranularity.NODE, DataTypes.BOOLEAN)); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "unknown", testRouting, Collections.<Symbol>singletonList(unknownReference), EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.NODE); try { getBucket(collectNode); } catch (ExecutionException e) { throw e.getCause(); } }
@Test public void testWrongRouting() throws Exception { expectedException.expect(UnhandledServerException.class); expectedException.expectMessage("unsupported routing"); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "wrong", new Routing( TreeMapBuilder.<String, Map<String, List<Integer>>>newMapBuilder() .put( "bla", TreeMapBuilder.<String, List<Integer>>newMapBuilder() .put("my_index", Arrays.asList(1)) .put("my_index", Arrays.asList(1)) .map()) .map()), ImmutableList.<Symbol>of(), EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.DOC); operation.collect(collectNode, new CollectingProjector(), null); }
@Test public void testCollectShardExpressions() throws Exception { List<Symbol> toCollect = ImmutableList.<Symbol>of(testShardIdReference); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), toCollect, EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.SHARD); Bucket result = getBucket(collectNode); assertThat(result.size(), is(2)); assertThat(result, containsInAnyOrder(isRow(0), isRow(1))); }
@Test public void testCollectLiterals() throws Exception { List<Symbol> toCollect = Arrays.<Symbol>asList( Literal.newLiteral("foobar"), Literal.newLiteral(true), Literal.newLiteral(1), Literal.newLiteral(4.2)); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "literals", testRouting, toCollect, EMPTY_PROJECTIONS); Bucket result = getBucket(collectNode); assertThat(result, contains(isRow(new BytesRef("foobar"), true, 1, 4.2))); }
@Test public void testCollectShardExpressionsLiteralsAndNodeExpressions() throws Exception { CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), Arrays.asList(testShardIdReference, Literal.newLiteral(true), testNodeReference), EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.SHARD); Bucket result = getBucket(collectNode); assertThat(result.size(), is(2)); assertThat(result, containsInAnyOrder(isRow(0, true, (short) 1), isRow(1, true, (short) 1))); }
@Test public void testCollectFunction() throws Exception { Function twoTimesTruthFunction = new Function(TestFunction.info, TO_COLLECT_TEST_REF); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "unknown", testRouting, Arrays.asList(twoTimesTruthFunction, testNodeReference), EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.NODE); Bucket result = getBucket(collectNode); assertThat(result.size(), equalTo(1)); assertThat(result, contains(isRow(2, (short) 1))); }
@Test public void testCollectExpressions() throws Exception { CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "collect", testRouting, Collections.<Symbol>singletonList(testNodeReference), EMPTY_PROJECTIONS); collectNode.maxRowGranularity(RowGranularity.NODE); Bucket result = getBucket(collectNode); assertThat(result.size(), equalTo(1)); assertThat(result, contains(isRow((short) 1))); }
@Test public void testCollectWithFalseWhereClause() throws Exception { CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "whereClause", testRouting, TO_COLLECT_TEST_REF, EMPTY_PROJECTIONS); collectNode.whereClause( new WhereClause( new Function( AndOperator.INFO, Arrays.<Symbol>asList(Literal.newLiteral(false), Literal.newLiteral(false))))); Bucket result = getBucket(collectNode); assertThat(result.size(), is(0)); }
@Test public void testCollectWithTrueWhereClause() throws Exception { CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "whereClause", testRouting, TO_COLLECT_TEST_REF, EMPTY_PROJECTIONS); collectNode.whereClause( new WhereClause( new Function( AndOperator.INFO, Arrays.<Symbol>asList(Literal.newLiteral(true), Literal.newLiteral(true))))); collectNode.maxRowGranularity(RowGranularity.NODE); Bucket result = getBucket(collectNode); assertThat(result, contains(isRow((short) 1))); }
@Test public void testCollectShardExpressionsWhereShardIdIs0() throws Exception { EqOperator op = (EqOperator) functions.get( new FunctionIdent( EqOperator.NAME, ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER))); List<Symbol> toCollect = ImmutableList.<Symbol>of(testShardIdReference); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "shardCollect", shardRouting(0, 1), toCollect, EMPTY_PROJECTIONS); collectNode.whereClause( new WhereClause( new Function(op.info(), Arrays.asList(testShardIdReference, Literal.newLiteral(0))))); collectNode.maxRowGranularity(RowGranularity.SHARD); Bucket result = getBucket(collectNode); assertThat(result, contains(isRow(0))); }
private CollectPhase createCollectNode( Planner.Context plannerContext, boolean keepContextForFetcher) { TableInfo tableInfo = docSchemaInfo.getTableInfo("characters"); ReferenceInfo docIdRefInfo = tableInfo.getReferenceInfo(new ColumnIdent("_docid")); Symbol docIdRef = new Reference(docIdRefInfo); List<Symbol> toCollect = ImmutableList.of(docIdRef); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), plannerContext.nextExecutionPhaseId(), "collect", tableInfo.getRouting(WhereClause.MATCH_ALL, null), toCollect, ImmutableList.<Projection>of()); collectNode.maxRowGranularity(RowGranularity.DOC); collectNode.keepContextForFetcher(keepContextForFetcher); plannerContext.allocateJobSearchContextIds(collectNode.routing()); return collectNode; }
@Test public void testCollectWithNullWhereClause() throws Exception { EqOperator op = (EqOperator) functions.get( new FunctionIdent( EqOperator.NAME, ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER))); CollectPhase collectNode = new CollectPhase( UUID.randomUUID(), 0, "whereClause", testRouting, TO_COLLECT_TEST_REF, EMPTY_PROJECTIONS); collectNode.whereClause( new WhereClause( new Function(op.info(), Arrays.<Symbol>asList(Literal.NULL, Literal.NULL)))); Bucket result = getBucket(collectNode); assertThat(result.size(), is(0)); }
@Test public void testFetchAction() throws Exception { setUpCharacters(); Analysis analysis = analyze("select id, name from characters"); QueryThenFetchConsumer queryThenFetchConsumer = internalCluster().getInstance(QueryThenFetchConsumer.class); Planner.Context plannerContext = new Planner.Context(clusterService(), UUID.randomUUID()); ConsumerContext consumerContext = new ConsumerContext(analysis.rootRelation(), plannerContext); QueryThenFetch plan = (QueryThenFetch) queryThenFetchConsumer.consume(analysis.rootRelation(), consumerContext).plan(); List<Bucket> results = getBuckets(plan.collectNode()); TransportFetchNodeAction transportFetchNodeAction = internalCluster().getInstance(TransportFetchNodeAction.class); // extract docIds by nodeId and jobSearchContextId Map<String, LongArrayList> jobSearchContextDocIds = new HashMap<>(); for (Bucket rows : results) { long docId = (long) rows.iterator().next().get(0); // unpack jobSearchContextId and reader doc id from docId int jobSearchContextId = (int) (docId >> 32); String nodeId = plannerContext.nodeId(jobSearchContextId); LongArrayList docIdsPerNode = jobSearchContextDocIds.get(nodeId); if (docIdsPerNode == null) { docIdsPerNode = new LongArrayList(); jobSearchContextDocIds.put(nodeId, docIdsPerNode); } docIdsPerNode.add(docId); } Iterable<Projection> projections = Iterables.filter( plan.mergeNode().projections(), Predicates.instanceOf(FetchProjection.class)); FetchProjection fetchProjection = (FetchProjection) Iterables.getOnlyElement(projections); RowInputSymbolVisitor rowInputSymbolVisitor = new RowInputSymbolVisitor(internalCluster().getInstance(Functions.class)); RowInputSymbolVisitor.Context context = rowInputSymbolVisitor.extractImplementations(fetchProjection.outputSymbols()); final CountDownLatch latch = new CountDownLatch(jobSearchContextDocIds.size()); final List<Row> rows = new ArrayList<>(); for (Map.Entry<String, LongArrayList> nodeEntry : jobSearchContextDocIds.entrySet()) { NodeFetchRequest nodeFetchRequest = new NodeFetchRequest(); nodeFetchRequest.jobId(plan.collectNode().jobId()); nodeFetchRequest.executionPhaseId(plan.collectNode().executionPhaseId()); nodeFetchRequest.toFetchReferences(context.references()); nodeFetchRequest.closeContext(true); nodeFetchRequest.jobSearchContextDocIds(nodeEntry.getValue()); transportFetchNodeAction.execute( nodeEntry.getKey(), nodeFetchRequest, new ActionListener<NodeFetchResponse>() { @Override public void onResponse(NodeFetchResponse nodeFetchResponse) { for (Row row : nodeFetchResponse.rows()) { rows.add(row); } latch.countDown(); } @Override public void onFailure(Throwable e) { latch.countDown(); fail(e.getMessage()); } }); } latch.await(); assertThat(rows.size(), is(2)); for (Row row : rows) { assertThat((Integer) row.get(0), anyOf(is(1), is(2))); assertThat( (BytesRef) row.get(1), anyOf(is(new BytesRef("Arthur")), is(new BytesRef("Ford")))); } }
private Plan analyzeAndPlan(String stmt) { Analysis analysis = analyze(stmt); Planner planner = internalCluster().getInstance(Planner.class); return planner.plan(analysis, UUID.randomUUID()); }