public SubqueryCursor(TableFilter filter, SearchRow first, SearchRow last) { StringBuilder buff = new StringBuilder("SELECT * FROM "); buff.append(filter.getTable().getSQL()); Expression filterCondition = filter.getFilterCondition(); if (filterCondition != null) { buff.append(" WHERE ").append(StringUtils.unEnclose(filterCondition.getSQL())); } Prepared prepared = filter.getSession().prepare(buff.toString(), true); subqueryResult = prepared.query(-1); }
/** Calculate the best query plan to use. */ void optimize() { calculateBestPlan(); bestPlan.removeUnusableIndexConditions(); TableFilter[] f2 = bestPlan.getFilters(); topFilter = f2[0]; for (int i = 0; i < f2.length - 1; i++) { f2[i].addJoin(f2[i + 1], false, false, null); } for (TableFilter f : f2) { PlanItem item = bestPlan.getItem(f); f.setPlanItem(item); } }
private void calculateBruteForceSome() { int bruteForce = getMaxBruteForceFilters(filters.length); TableFilter[] list = new TableFilter[filters.length]; Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce); for (int x = 0; !canStop(x) && p.next(); x++) { // find out what filters are not used yet for (TableFilter f : filters) { f.setUsed(false); } for (int i = 0; i < bruteForce; i++) { list[i].setUsed(true); } // fill the remaining elements with the unused elements (greedy) for (int i = bruteForce; i < filters.length; i++) { double costPart = -1.0; int bestPart = -1; for (int j = 0; j < filters.length; j++) { if (!filters[j].isUsed()) { if (i == filters.length - 1) { bestPart = j; break; } list[i] = filters[j]; Plan part = new Plan(list, i + 1, condition); double costNow = part.calculateCost(session); if (costPart < 0 || costNow < costPart) { costPart = costNow; bestPart = j; } } } filters[bestPart].setUsed(true); list[i] = filters[bestPart]; } testPlan(list); } }
public HBaseSecondaryIndexCursor( HBaseSecondaryIndex index, TableFilter filter, byte[] startKey, byte[] endKey) { defaultColumnFamilyName = Bytes.toBytes(((HBaseTable) filter.getTable()).getDefaultColumnFamilyName()); secondaryIndex = index; session = (HBaseSession) filter.getSession(); Prepared p = filter.getPrepared(); if (p instanceof WithWhereClause) { regionName = Bytes.toBytes(((WithWhereClause) p).getWhereClauseSupport().getRegionName()); } if (regionName == null) throw new RuntimeException("regionName is null"); fetchSize = filter.getPrepared().getFetchSize(); // 非查询的操作一般不设置fetchSize,此时fetchSize为0,所以要设置一个默认值 // if (fetchSize < 1 && !filter.getPrepared().isQuery()) if (fetchSize < 1) fetchSize = SysProperties.SERVER_RESULT_SET_FETCH_SIZE; if (filter.getSelect() != null) columns = filter.getSelect().getColumns(filter); else columns = Arrays.asList(filter.getTable().getColumns()); if (startKey == null) startKey = HConstants.EMPTY_BYTE_ARRAY; if (endKey == null) endKey = HConstants.EMPTY_BYTE_ARRAY; Scan scan = new Scan(); scan.setMaxVersions(1); try { HRegionInfo info = session.getRegionServer().getRegionInfo(regionName); if (Bytes.compareTo(startKey, info.getStartKey()) >= 0) scan.setStartRow(startKey); else scan.setStartRow(info.getStartKey()); if (Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY)) scan.setStopRow(info.getEndKey()); else if (Bytes.compareTo(endKey, info.getEndKey()) < 0) scan.setStopRow(endKey); else scan.setStopRow(info.getEndKey()); scan.addColumn(HBaseSecondaryIndex.PSEUDO_FAMILY, HBaseSecondaryIndex.PSEUDO_COLUMN); scannerId = session.getRegionServer().openScanner(regionName, scan); } catch (Exception e) { throw new RuntimeException(e); } }