use of io.crate.expression.InputFactory in project crate by crate.
the class RowsTransformer method toRowsIterable.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Iterable<Row> toRowsIterable(TransactionContext txnCtx, InputFactory inputFactory, ReferenceResolver<?> referenceResolver, RoutedCollectPhase collectPhase, Iterable<?> iterable, boolean sort) {
if (!WhereClause.canMatch(collectPhase.where())) {
return Collections.emptyList();
}
InputFactory.Context ctx = inputFactory.ctxForRefs(txnCtx, referenceResolver);
ctx.add(collectPhase.toCollect());
OrderBy orderBy = collectPhase.orderBy();
if (orderBy != null) {
for (Symbol symbol : orderBy.orderBySymbols()) {
ctx.add(symbol);
}
}
ValueAndInputRow<Object> inputRow = new ValueAndInputRow<>(ctx.topLevelInputs(), ctx.expressions());
assert DataTypes.BOOLEAN.equals(collectPhase.where().valueType()) : "whereClause.query() must be of type " + DataTypes.BOOLEAN;
Predicate<Row> predicate = InputCondition.asPredicate(ctx.add(collectPhase.where()));
if (sort == false || orderBy == null) {
return () -> StreamSupport.stream((Spliterator<Object>) iterable.spliterator(), false).map(inputRow).filter(predicate).iterator();
}
ArrayList<Object[]> items = new ArrayList<>();
for (var item : iterable) {
var row = inputRow.apply(item);
if (predicate.test(row)) {
items.add(row.materialize());
}
}
items.sort(OrderingByPosition.arrayOrdering(collectPhase));
return Lists2.mapLazy(items, Buckets.arrayToSharedRow());
}
use of io.crate.expression.InputFactory in project crate by crate.
the class S3FileReadingCollectorTest method prepare.
@Before
public void prepare() throws Exception {
NodeContext nodeCtx = new NodeContext(new Functions(Map.of()));
inputFactory = new InputFactory(nodeCtx);
}
use of io.crate.expression.InputFactory in project crate by crate.
the class InsertSourceFromCells method addDefaults.
private static Tuple<List<Reference>, List<Input<?>>> addDefaults(List<Reference> targets, DocTableInfo table, TransactionContext txnCtx, NodeContext nodeCtx) {
if (table.defaultExpressionColumns().isEmpty()) {
return new Tuple<>(targets, List.of());
}
InputFactory inputFactory = new InputFactory(nodeCtx);
Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
ArrayList<Reference> defaultColumns = new ArrayList<>(table.defaultExpressionColumns().size());
ArrayList<Input<?>> defaultValues = new ArrayList<>();
for (Reference ref : table.defaultExpressionColumns()) {
if (targets.contains(ref) == false) {
defaultColumns.add(ref);
defaultValues.add(ctx.add(ref.defaultExpression()));
}
}
List<Reference> allColumns;
if (defaultColumns.isEmpty()) {
allColumns = targets;
} else {
allColumns = Lists2.concat(targets, defaultColumns);
}
return new Tuple<>(allColumns, defaultValues);
}
use of io.crate.expression.InputFactory in project crate by crate.
the class WindowProjector method fromProjection.
public static Projector fromProjection(WindowAggProjection projection, NodeContext nodeCtx, InputFactory inputFactory, TransactionContext txnCtx, RamAccounting ramAccounting, MemoryManager memoryManager, Version minNodeVersion, Version indexVersionCreated, IntSupplier numThreads, Executor executor) {
var windowFunctionSymbols = projection.windowFunctions();
var numWindowFunctions = windowFunctionSymbols.size();
assert numWindowFunctions > 0 : "WindowAggProjection must have at least 1 window function.";
ArrayList<WindowFunction> windowFunctions = new ArrayList<>(numWindowFunctions);
ArrayList<CollectExpression<Row, ?>> windowFuncArgsExpressions = new ArrayList<>(numWindowFunctions);
Input[][] windowFuncArgsInputs = new Input[numWindowFunctions][];
Boolean[] ignoreNulls = new Boolean[numWindowFunctions];
for (int idx = 0; idx < numWindowFunctions; idx++) {
var windowFunctionSymbol = windowFunctionSymbols.get(idx);
InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
ctx.add(windowFunctionSymbol.arguments());
FunctionImplementation impl = nodeCtx.functions().getQualified(windowFunctionSymbol, txnCtx.sessionSettings().searchPath());
assert impl != null : "Function implementation not found using full qualified lookup";
if (impl instanceof AggregationFunction) {
var filterInputFactoryCtx = inputFactory.ctxForInputColumns(txnCtx);
var filterSymbol = windowFunctionSymbol.filter();
// noinspection unchecked
Input<Boolean> filterInput = filterSymbol == null ? Literal.BOOLEAN_TRUE : (Input<Boolean>) filterInputFactoryCtx.add(filterSymbol);
ExpressionsInput<Row, Boolean> filter = new ExpressionsInput<>(filterInput, filterInputFactoryCtx.expressions());
windowFunctions.add(new AggregateToWindowFunctionAdapter((AggregationFunction) impl, filter, indexVersionCreated, ramAccounting, memoryManager, minNodeVersion));
} else if (impl instanceof WindowFunction) {
windowFunctions.add((WindowFunction) impl);
} else {
throw new AssertionError("Function needs to be either a window or an aggregate function");
}
windowFuncArgsExpressions.addAll(ctx.expressions());
windowFuncArgsInputs[idx] = ctx.topLevelInputs().toArray(new Input[0]);
ignoreNulls[idx] = windowFunctionSymbol.ignoreNulls();
}
var windowDefinition = projection.windowDefinition();
var partitions = windowDefinition.partitions();
Supplier<InputFactory.Context<CollectExpression<Row, ?>>> createInputFactoryContext = () -> inputFactory.ctxForInputColumns(txnCtx);
int arrayListElementOverHead = 32;
RowAccountingWithEstimators accounting = new RowAccountingWithEstimators(Symbols.typeView(projection.standalone()), ramAccounting, arrayListElementOverHead);
Comparator<Object[]> cmpPartitionBy = partitions.isEmpty() ? null : createComparator(createInputFactoryContext, new OrderBy(windowDefinition.partitions()));
Comparator<Object[]> cmpOrderBy = createComparator(createInputFactoryContext, windowDefinition.orderBy());
int numCellsInSourceRow = projection.standalone().size();
ComputeFrameBoundary<Object[]> computeFrameStart = createComputeStartFrameBoundary(numCellsInSourceRow, txnCtx, nodeCtx, windowDefinition, cmpOrderBy);
ComputeFrameBoundary<Object[]> computeFrameEnd = createComputeEndFrameBoundary(numCellsInSourceRow, txnCtx, nodeCtx, windowDefinition, cmpOrderBy);
return sourceRows -> WindowFunctionBatchIterator.of(sourceRows, accounting, computeFrameStart, computeFrameEnd, cmpPartitionBy, cmpOrderBy, numCellsInSourceRow, numThreads, executor, windowFunctions, windowFuncArgsExpressions, ignoreNulls, windowFuncArgsInputs);
}
use of io.crate.expression.InputFactory in project crate by crate.
the class ProjectionToProjectorVisitor method visitSourceIndexWriterProjection.
@Override
public Projector visitSourceIndexWriterProjection(SourceIndexWriterProjection projection, Context context) {
InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
List<Input<?>> partitionedByInputs = new ArrayList<>(projection.partitionedBySymbols().size());
for (Symbol partitionedBySymbol : projection.partitionedBySymbols()) {
partitionedByInputs.add(ctx.add(partitionedBySymbol));
}
Input<?> sourceInput = ctx.add(projection.rawSource());
Supplier<String> indexNameResolver = IndexNameResolver.create(projection.tableIdent(), projection.partitionIdent(), partitionedByInputs);
ClusterState state = clusterService.state();
Settings tableSettings = TableSettingsResolver.get(state.getMetadata(), projection.tableIdent(), !projection.partitionedBySymbols().isEmpty());
int targetTableNumShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(tableSettings);
int targetTableNumReplicas = NumberOfReplicas.fromSettings(tableSettings, state.getNodes().getSize());
UpsertResultContext upsertResultContext;
if (projection instanceof SourceIndexWriterReturnSummaryProjection) {
upsertResultContext = UpsertResultContext.forReturnSummary(context.txnCtx, (SourceIndexWriterReturnSummaryProjection) projection, clusterService.localNode(), inputFactory);
} else {
upsertResultContext = UpsertResultContext.forRowCount();
}
return new IndexWriterProjector(clusterService, nodeJobsCounter, circuitBreakerService.getBreaker(HierarchyCircuitBreakerService.QUERY), context.ramAccounting, threadPool.scheduler(), threadPool.executor(ThreadPool.Names.SEARCH), context.txnCtx, nodeCtx, state.metadata().settings(), targetTableNumShards, targetTableNumReplicas, transportActionProvider.transportBulkCreateIndicesAction(), transportActionProvider.transportShardUpsertAction()::execute, indexNameResolver, projection.rawSourceReference(), projection.primaryKeys(), projection.ids(), projection.clusteredBy(), projection.clusteredByIdent(), sourceInput, ctx.expressions(), projection.bulkActions(), projection.includes(), projection.excludes(), projection.autoCreateIndices(), projection.overwriteDuplicates(), context.jobId, upsertResultContext, projection.failFast());
}
Aggregations