@Override public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { return type == JoinType.Semi || type == JoinType.Anti ? new SemiAntiJoinIterator(lhsPlan.iterator(scanGrouper), rhsPlan.iterator(scanGrouper)) : new BasicJoinIterator(lhsPlan.iterator(scanGrouper), rhsPlan.iterator(scanGrouper)); }
@Override public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { return new UnnestArrayResultIterator(delegate.iterator(scanGrouper, scan)); }
@Override public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { ResultIterator iterator = new DelegateResultIterator(delegate.iterator(scanGrouper, scan)) { @Override public Tuple next() throws SQLException { Tuple tuple = super.next(); if (tuple == null) return null; return tupleProjector.projectResults(tuple); } @Override public String toString() { return "TupleProjectionResultIterator [projector=" + tupleProjector + "]"; } }; if (postFilter != null) { iterator = new FilterResultIterator(iterator, postFilter); } return iterator; }
public UnionResultIterators(List<QueryPlan> plans, StatementContext parentStmtCtx) throws SQLException { this.parentStmtCtx = parentStmtCtx; int nPlans = plans.size(); iterators = Lists.newArrayListWithExpectedSize(nPlans); splits = Lists.newArrayListWithExpectedSize(nPlans * 30); scans = Lists.newArrayListWithExpectedSize(nPlans * 10); readMetricsList = Lists.newArrayListWithCapacity(nPlans); overAllQueryMetricsList = Lists.newArrayListWithCapacity(nPlans); for (QueryPlan plan : plans) { readMetricsList.add(plan.getContext().getReadMetricsQueue()); overAllQueryMetricsList.add(plan.getContext().getOverallQueryMetrics()); iterators.add(LookAheadResultIterator.wrap(plan.iterator())); splits.addAll(plan.getSplits()); scans.addAll(plan.getScans()); } }
public static QueryPlan getOptimizeQueryPlan(Connection conn,String sql) throws SQLException { PhoenixPreparedStatement statement = conn.prepareStatement(sql).unwrap(PhoenixPreparedStatement.class); QueryPlan queryPlan = statement.optimizeQuery(sql); queryPlan.iterator(); return queryPlan; }
private static QueryPlan getQueryPlan(Connection conn,String sql) throws SQLException { PhoenixPreparedStatement statement = conn.prepareStatement(sql).unwrap(PhoenixPreparedStatement.class); QueryPlan queryPlan = statement.optimizeQuery(sql); queryPlan.iterator(); return queryPlan; }
@Override public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { StatementContext context = delegate.getContext(); if (resultIterator == null) { context.getOverallQueryMetrics().startQuery(); resultIterator = new CursorResultIterator(LookAheadResultIterator.wrap(delegate.iterator(scanGrouper, scan)),cursorName); } return resultIterator; }
private Scan projectQuery(String query) throws SQLException { QueryPlan plan = getQueryPlan(query, Collections.emptyList()); plan.iterator(); // Forces projection return plan.getContext().getScan(); }
@Test public void testMultiCFProjection() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String ddl = "CREATE TABLE multiCF (k integer primary key, a.a varchar, b.b varchar)"; conn.createStatement().execute(ddl); String query = "SELECT COUNT(*) FROM multiCF"; QueryPlan plan = getQueryPlan(query,Collections.emptyList()); plan.iterator(); Scan scan = plan.getContext().getScan(); assertTrue(scan.getFilter() instanceof FirstKeyOnlyFilter); assertEquals(1, scan.getFamilyMap().size()); }
@Test public void testSingleColLocalIndexPruning() throws SQLException { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); try (Connection conn = DriverManager.getConnection(getUrl(), props)) { conn.createStatement().execute("CREATE TABLE T (\n" + " A CHAR(1) NOT NULL,\n" + " B CHAR(1) NOT NULL,\n" + " C CHAR(1) NOT NULL,\n" + " CONSTRAINT PK PRIMARY KEY (\n" + " A,\n" + " B,\n" + " C\n" + " )\n" + ") SPLIT ON ('A','C','E','G','I')"); conn.createStatement().execute("CREATE LOCAL INDEX IDX ON T(A,C)"); String query = "SELECT * FROM T WHERE A = 'B' and C='C'"; PhoenixStatement statement = conn.createStatement().unwrap(PhoenixStatement.class); QueryPlan plan = statement.optimizeQuery(query); assertEquals("IDX", plan.getContext().getCurrentTable().getTable().getName().getString()); plan.iterator(); List<List<Scan>> outerScans = plan.getScans(); assertEquals(1, outerScans.size()); List<Scan> innerScans = outerScans.get(0); assertEquals(1, innerScans.size()); Scan scan = innerScans.get(0); assertEquals("A", Bytes.toString(scan.getStartRow()).trim()); assertEquals("C", Bytes.toString(scan.getStopRow()).trim()); } }
QueryPlan plan = statement.optimizeQuery(query); assertEquals("IDX", plan.getContext().getCurrentTable().getTable().getName().getString()); plan.iterator(); List<List<Scan>> outerScans = plan.getScans(); assertEquals(1, outerScans.size());
QueryPlan plan = statement.optimizeQuery(query); assertEquals("IDX", plan.getContext().getCurrentTable().getTable().getName().getString()); plan.iterator(); List<List<Scan>> outerScans = plan.getScans(); assertEquals(1, outerScans.size());
@Test public void testNoLocalIndexPruning() throws SQLException { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); try (Connection conn = DriverManager.getConnection(getUrl(), props)) { conn.createStatement().execute("CREATE TABLE T (\n" + " A CHAR(1) NOT NULL,\n" + " B CHAR(1) NOT NULL,\n" + " C CHAR(1) NOT NULL,\n" + " CONSTRAINT PK PRIMARY KEY (\n" + " A,\n" + " B,\n" + " C\n" + " )\n" + ") SPLIT ON ('A','C','E','G','I')"); conn.createStatement().execute("CREATE LOCAL INDEX IDX ON T(C)"); String query = "SELECT * FROM T WHERE C='C'"; PhoenixStatement statement = conn.createStatement().unwrap(PhoenixStatement.class); QueryPlan plan = statement.optimizeQuery(query); assertEquals("IDX", plan.getContext().getCurrentTable().getTable().getName().getString()); plan.iterator(); List<List<Scan>> outerScans = plan.getScans(); assertEquals(6, outerScans.size()); } }
QueryPlan plan = statement.optimizeQuery(query); assertEquals("IDX", plan.getContext().getCurrentTable().getTable().getName().getString()); plan.iterator(); List<List<Scan>> outerScans = plan.getScans(); assertEquals(1, outerScans.size());
@Test public void testSmallScanForPointLookups() throws SQLException { Properties props = PropertiesUtil.deepCopy(new Properties()); createTestTable(getUrl(), "CREATE TABLE FOO(\n" + " a VARCHAR NOT NULL,\n" + " b VARCHAR NOT NULL,\n" + " c VARCHAR,\n" + " CONSTRAINT pk PRIMARY KEY (a, b DESC, c)\n" + " )"); props.put(QueryServices.SMALL_SCAN_THRESHOLD_ATTRIB, "2"); try (Connection conn = DriverManager.getConnection(getUrl(), props)) { String query = "select * from foo where a = 'a' and b = 'b' and c in ('x','y','z')"; PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); QueryPlan plan = stmt.optimizeQuery(query); plan.iterator(); //Fail since we have 3 rows in pointLookup assertFalse(plan.getContext().getScan().isSmall()); query = "select * from foo where a = 'a' and b = 'b' and c = 'c'"; plan = stmt.compileQuery(query); plan.iterator(); //Should be small scan, query is for single row pointLookup assertTrue(plan.getContext().getScan().isSmall()); } }
@Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); Tuple tuple = plan.iterator().next(); long rowCount = 0; if (tuple != null) { Cell kv = tuple.getValue(0); ImmutableBytesWritable tmpPtr = new ImmutableBytesWritable(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength()); // A single Cell will be returned with the count(*) - we decode that here rowCount = PLong.INSTANCE.getCodec().decodeLong(tmpPtr, SortOrder.getDefault()); } // The contract is to return a MutationState that contains the number of rows modified. In this // case, it's the number of rows in the data table which corresponds to the number of index // rows that were added. return new MutationState(0, 0, connection, rowCount); }
@Override public ResultIterator iterator(ParallelScanGrouper scanGrouper, Scan scan) throws SQLException { ResultIterator iterator = delegate.iterator(scanGrouper, scan); if (where != null) { iterator = new FilterResultIterator(iterator, where); } if (!orderBy.getOrderByExpressions().isEmpty()) { // TopN int thresholdBytes = context.getConnection().getQueryServices().getProps().getInt( QueryServices.SPOOL_THRESHOLD_BYTES_ATTRIB, QueryServicesOptions.DEFAULT_SPOOL_THRESHOLD_BYTES); iterator = new OrderedResultIterator(iterator, orderBy.getOrderByExpressions(), thresholdBytes, limit, offset, projector.getEstimatedRowByteSize()); } else { if (offset != null) { iterator = new OffsetResultIterator(iterator, offset); } if (limit != null) { iterator = new LimitingResultIterator(iterator, limit); } } if (context.getSequenceManager().getSequenceCount() > 0) { iterator = new SequenceResultIterator(iterator, context.getSequenceManager()); } return iterator; }
private void testLiteralResultIteratorPlan(Object[][] expectedResult, Integer offset, Integer limit) throws SQLException { QueryPlan plan = newLiteralResultIterationPlan(offset, limit); ResultIterator iter = plan.iterator(); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); for (Object[] row : expectedResult) { Tuple next = iter.next(); assertNotNull(next); for (int i = 0; i < row.length; i++) { PColumn column = table.getColumns().get(i); boolean eval = new ProjectedColumnExpression(column, table, column.getName().getString()).evaluate(next, ptr); Object o = eval ? column.getDataType().toObject(ptr) : null; assertEquals(row[i], o); } } assertNull(iter.next()); }
@Override public MutationState execute() throws SQLException { ResultIterator iterator = queryPlan.iterator(); if (parallelIteratorFactory == null) { return upsertSelect(new StatementContext(statement, queryPlan.getContext().getScan()), tableRef, projector, iterator, columnIndexes, pkSlotIndexes, useServerTimestamp, false);
scan.setAttribute(PhoenixIndexCodec.INDEX_PROTO_MD, ptr.get()); ResultIterator iterator = aggPlan.iterator(); try { Tuple row = iterator.next();