use of io.crate.expression.symbol.Function in project crate by crate.
the class SubscriptObjectFunction method tryToInferReturnTypeFromObjectTypeAndArguments.
static Symbol tryToInferReturnTypeFromObjectTypeAndArguments(Function func) {
if (!func.valueType().equals(DataTypes.UNDEFINED)) {
return func;
}
var arguments = func.arguments();
ObjectType objectType = (ObjectType) arguments.get(0).valueType();
List<String> path = maybeCreatePath(arguments);
if (path == null) {
return func;
} else {
DataType<?> returnType = objectType.resolveInnerType(path);
return returnType.equals(DataTypes.UNDEFINED) ? func : new Function(func.signature(), func.arguments(), returnType);
}
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ScalarsAndRefsToTrue method visitFunction.
@Override
public Symbol visitFunction(Function symbol, Void context) {
String functionName = symbol.name();
if (functionName.equals(NotPredicate.NAME)) {
Symbol argument = symbol.arguments().get(0);
if (argument instanceof Reference) {
return argument.accept(this, context);
} else if (argument instanceof Function) {
if (!Operators.LOGICAL_OPERATORS.contains(((Function) argument).name())) {
return argument.accept(this, context);
}
}
}
List<Symbol> newArgs = new ArrayList<>(symbol.arguments().size());
boolean allLiterals = true;
boolean isNull = false;
for (Symbol arg : symbol.arguments()) {
Symbol processedArg = arg.accept(this, context);
newArgs.add(processedArg);
if (!processedArg.symbolType().isValueSymbol()) {
allLiterals = false;
}
if (processedArg.valueType().id() == DataTypes.UNDEFINED.id()) {
isNull = true;
}
}
if (allLiterals && !Operators.LOGICAL_OPERATORS.contains(functionName)) {
return isNull ? Literal.NULL : Literal.BOOLEAN_TRUE;
}
return new Function(symbol.signature(), newArgs, symbol.valueType());
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class InputColumnsTest method testNonDeterministicFunctionsReplacement.
@Test
public void testNonDeterministicFunctionsReplacement() throws Exception {
Function fn1 = (Function) sqlExpressions.asSymbol("random()");
Function fn2 = (Function) sqlExpressions.asSymbol("random()");
List<Symbol> inputSymbols = Arrays.<Symbol>asList(Literal.BOOLEAN_FALSE, sqlExpressions.asSymbol("upper(a)"), fn1, fn2);
Function newSameFn = (Function) sqlExpressions.asSymbol("random()");
Function newDifferentFn = (Function) sqlExpressions.asSymbol("random()");
InputColumns.SourceSymbols sourceSymbols = new InputColumns.SourceSymbols(inputSymbols);
Symbol replaced1 = InputColumns.create(fn1, sourceSymbols);
assertThat(replaced1, is(instanceOf(InputColumn.class)));
assertThat(((InputColumn) replaced1).index(), is(2));
Symbol replaced2 = InputColumns.create(fn2, sourceSymbols);
assertThat(replaced2, is(instanceOf(InputColumn.class)));
assertThat(((InputColumn) replaced2).index(), is(3));
Symbol replaced3 = InputColumns.create(newSameFn, sourceSymbols);
// not replaced
assertThat(replaced3, is(equalTo(newSameFn)));
Symbol replaced4 = InputColumns.create(newDifferentFn, sourceSymbols);
// not replaced
assertThat(replaced4, is(equalTo(newDifferentFn)));
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ProjectionToProjectorVisitorTest method testFilterProjection.
@Test
public void testFilterProjection() throws Exception {
List<Symbol> arguments = Arrays.asList(Literal.of(2), new InputColumn(1));
EqOperator op = (EqOperator) nodeCtx.functions().get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
Function function = new Function(op.signature(), arguments, EqOperator.RETURN_TYPE);
FilterProjection projection = new FilterProjection(function, Arrays.asList(new InputColumn(0), new InputColumn(1)));
Projector projector = visitor.create(projection, txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, UUID.randomUUID());
assertThat(projector, instanceOf(FilterProjector.class));
List<Object[]> rows = new ArrayList<>();
rows.add($("human", 2));
rows.add($("vogon", 1));
BatchIterator<Row> filteredBI = projector.apply(InMemoryBatchIterator.of(new CollectionBucket(rows), SENTINEL, true));
TestingRowConsumer consumer = new TestingRowConsumer();
consumer.accept(filteredBI, null);
Bucket bucket = consumer.getBucket();
assertThat(bucket.size(), is(1));
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ProjectingRowConsumerTest method testConsumerRequiresScrollAndProjectorsDontSupportScrolling.
@Test
public void testConsumerRequiresScrollAndProjectorsDontSupportScrolling() {
List<Symbol> arguments = Arrays.asList(Literal.of(2), new InputColumn(1, DataTypes.INTEGER));
EqOperator op = (EqOperator) nodeCtx.functions().get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
Function function = new Function(op.signature(), arguments, EqOperator.RETURN_TYPE);
FilterProjection filterProjection = new FilterProjection(function, Arrays.asList(new InputColumn(0), new InputColumn(1)));
RowConsumer delegateConsumerRequiresScroll = new DummyRowConsumer(true);
RowConsumer projectingConsumer = ProjectingRowConsumer.create(delegateConsumerRequiresScroll, Collections.singletonList(filterProjection), UUID.randomUUID(), txnCtx, RamAccounting.NO_ACCOUNTING, memoryManager, projectorFactory);
assertThat(projectingConsumer.requiresScroll(), is(true));
}
Aggregations