use of io.crate.expression.symbol.FetchReference in project crate by crate.
the class ReaderBucketsTest method test_reader_bucket_accounts_memory_for_added_rows.
@Test
public void test_reader_bucket_accounts_memory_for_added_rows() throws Exception {
var e = SQLExecutor.builder(clusterService).addTable("create table t1 (x text)").build();
var t1 = e.resolveTableInfo("t1");
var x = (Reference) e.asSymbol("x");
var fetchSource = new FetchSource();
fetchSource.addFetchIdColumn(new InputColumn(0, DataTypes.LONG));
fetchSource.addRefToFetch(x);
var fetchRows = FetchRows.create(CoordinatorTxnCtx.systemTransactionContext(), TestingHelpers.createNodeContext(), Map.of(t1.ident(), fetchSource), List.of(new FetchReference(new InputColumn(0, DataTypes.LONG), x), new InputColumn(1, DataTypes.INTEGER)));
var bytesAccounted = new AtomicLong();
var ramAccounting = new BlockBasedRamAccounting(bytes -> bytesAccounted.addAndGet(bytes), 1024);
int readerId = 1;
var readerBuckets = new ReaderBuckets(fetchRows, reader -> fetchSource, new EstimateCellsSize(List.of(DataTypes.LONG, DataTypes.INTEGER)), ramAccounting);
long fetchId = FetchId.encode(readerId, 1);
readerBuckets.add(new RowN(fetchId, 42));
assertThat(bytesAccounted.get(), is(1024L));
assertThat(readerBuckets.ramBytesUsed(), is(40L));
IntObjectHashMap<Bucket> bucketsByReader = new IntObjectHashMap<>();
bucketsByReader.put(readerId, new CollectionBucket(List.<Object[]>of(new Object[] { "I eat memory for breakfast" })));
IntHashSet readerIds = new IntHashSet(2);
readerIds.add(readerId);
readerBuckets.generateToFetch(readerIds);
try (var outputRows = readerBuckets.getOutputRows(List.of(bucketsByReader))) {
assertThat(bytesAccounted.get(), is(1024L));
assertThat(readerBuckets.ramBytesUsed(), is(136L));
}
assertThat("After outputRows are closed the readerBuckets are released", readerBuckets.ramBytesUsed(), is(0L));
}
use of io.crate.expression.symbol.FetchReference in project crate by crate.
the class FetchRows method create.
public static FetchRows create(TransactionContext txnCtx, NodeContext nodeCtx, Map<RelationName, FetchSource> fetchSourceByTable, List<Symbol> outputSymbols) {
IntArrayList fetchIdPositions = new IntArrayList();
ArrayList<Object[]> nullRows = new ArrayList<>();
IntObjectHashMap<UnsafeArrayRow> fetchedRows = new IntObjectHashMap<>();
for (var fetchSource : fetchSourceByTable.values()) {
Object[] nullRow = new Object[fetchSource.references().size()];
for (InputColumn ic : fetchSource.fetchIdCols()) {
fetchIdPositions.add(ic.index());
nullRows.add(nullRow);
fetchedRows.put(ic.index(), new UnsafeArrayRow());
}
}
final UnsafeArrayRow inputRow = new UnsafeArrayRow();
var visitor = new BaseImplementationSymbolVisitor<Void>(txnCtx, nodeCtx) {
@Override
public Input<?> visitInputColumn(final InputColumn inputColumn, final Void context) {
final int idx = inputColumn.index();
return () -> inputRow.get(idx);
}
@Override
public Input<?> visitFetchReference(final FetchReference fetchReference, final Void context) {
var ref = fetchReference.ref();
UnsafeArrayRow row = fetchedRows.get(fetchReference.fetchId().index());
int posInFetchedRow = fetchSourceByTable.get(ref.ident().tableIdent()).references().indexOf(ref);
return () -> row.get(posInFetchedRow);
}
};
List<Input<?>> outputExpressions = Lists2.map(outputSymbols, x -> x.accept(visitor, null));
return new FetchRows(fetchIdPositions, outputExpressions, inputRow, fetchedRows, nullRows);
}
use of io.crate.expression.symbol.FetchReference in project crate by crate.
the class SymbolPrinterTest method testPrintFetchRefs.
@Test
public void testPrintFetchRefs() throws Exception {
Symbol field = sqlExpressions.asSymbol("bar");
assertThat(field, isReference("bar"));
Reference ref = (Reference) field;
FetchReference fetchRef = new FetchReference(new InputColumn(0, field.valueType()), ref);
assertPrint(fetchRef, "FETCH(INPUT(0), doc.formatter.bar)");
}
use of io.crate.expression.symbol.FetchReference in project crate by crate.
the class FetchRowsTest method test_fetch_rows_can_map_inputs_and_buckets_to_outputs.
@Test
public void test_fetch_rows_can_map_inputs_and_buckets_to_outputs() throws Exception {
var e = SQLExecutor.builder(clusterService).addTable("create table t1 (x text)").addTable("create table t2 (y text, z int)").build();
var t1 = e.resolveTableInfo("t1");
var x = (Reference) e.asSymbol("x");
var fetchSource1 = new FetchSource();
fetchSource1.addFetchIdColumn(new InputColumn(0, DataTypes.LONG));
fetchSource1.addRefToFetch(x);
var t2 = e.resolveTableInfo("t2");
var y = (Reference) e.asSymbol("y");
var fetchSource2 = new FetchSource();
fetchSource2.addFetchIdColumn(new InputColumn(1, DataTypes.LONG));
fetchSource2.addRefToFetch(y);
var fetchSources = Map.of(t1.ident(), fetchSource1, t2.ident(), fetchSource2);
var fetchRows = FetchRows.create(CoordinatorTxnCtx.systemTransactionContext(), createNodeContext(), fetchSources, List.of(new FetchReference(new InputColumn(0, DataTypes.LONG), x), new FetchReference(new InputColumn(1, DataTypes.LONG), y), new InputColumn(2, DataTypes.INTEGER)));
long fetchIdRel1 = FetchId.encode(1, 1);
long fetchIdRel2 = FetchId.encode(2, 1);
var readerBuckets = new ReaderBuckets(fetchRows, reader -> reader == 1 ? fetchSource1 : fetchSource2, cells -> 0, RamAccounting.NO_ACCOUNTING);
IntHashSet readerIds = new IntHashSet(2);
readerIds.add(1);
readerIds.add(2);
readerBuckets.add(new RowN(fetchIdRel1, fetchIdRel2, 42));
readerBuckets.generateToFetch(readerIds);
IntObjectHashMap<Bucket> results = new IntObjectHashMap<>();
results.put(1, new ArrayBucket($$($("Arthur"))));
results.put(2, new ArrayBucket($$($("Trillian"))));
var it = readerBuckets.getOutputRows(List.of(results));
assertThat(it.hasNext(), is(true));
var outputRow = it.next();
assertThat(outputRow.get(0), is("Arthur"));
assertThat(outputRow.get(1), is("Trillian"));
assertThat(outputRow.get(2), is(42));
}
use of io.crate.expression.symbol.FetchReference in project crate by crate.
the class InputColumns method visitFetchStub.
@Override
public Symbol visitFetchStub(FetchStub fetchStub, SourceSymbols sourceSymbols) {
FetchMarker fetchMarker = fetchStub.fetchMarker();
InputColumn fetchId = sourceSymbols.inputs.get(fetchMarker);
if (fetchId == null) {
throw new IllegalArgumentException("Could not find fetchMarker " + fetchMarker + " in sources: " + sourceSymbols);
}
return new FetchReference(fetchId, fetchStub.ref());
}
Aggregations