@Override protected List<? extends OperatorFactory> createOperatorFactories() { OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "totalprice"); RowExpression filter = call( internalOperator(GREATER_THAN_OR_EQUAL, BOOLEAN.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature())), BOOLEAN, field(0, DOUBLE), constant(50000.0, DOUBLE)); ExpressionCompiler expressionCompiler = new ExpressionCompiler(localQueryRunner.getMetadata(), new PageFunctionCompiler(localQueryRunner.getMetadata(), 0)); Supplier<PageProcessor> pageProcessor = expressionCompiler.compilePageProcessor(Optional.of(filter), ImmutableList.of(field(0, DOUBLE))); FilterAndProjectOperator.FilterAndProjectOperatorFactory filterAndProjectOperator = new FilterAndProjectOperator.FilterAndProjectOperatorFactory( 1, new PlanNodeId("test"), pageProcessor, ImmutableList.of(DOUBLE), new DataSize(0, BYTE), 0); return ImmutableList.of(tableScanOperator, filterAndProjectOperator); }
@Override protected RowExpression visitIfExpression(IfExpression node, Void context) { ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder(); arguments.add(process(node.getCondition(), context)) .add(process(node.getTrueValue(), context)); if (node.getFalseValue().isPresent()) { arguments.add(process(node.getFalseValue().get(), context)); } else { arguments.add(constantNull(getType(node))); } return call(Signatures.ifSignature(getType(node)), getType(node), arguments.build()); }
private static List<Integer> getInputChannels(Iterable<RowExpression> expressions) { TreeSet<Integer> channels = new TreeSet<>(); for (RowExpression expression : Expressions.subExpressions(expressions)) { if (expression instanceof InputReferenceExpression) { channels.add(((InputReferenceExpression) expression).getField()); } } return ImmutableList.copyOf(channels); }
@Test public void testSanityFilterOnRLE() { Signature lessThan = internalOperator(LESS_THAN, BOOLEAN, ImmutableList.of(BIGINT, BIGINT)); CallExpression filter = new CallExpression(lessThan, BOOLEAN, ImmutableList.of(field(0, BIGINT), constant(10L, BIGINT))); PageProcessor processor = compiler.compilePageProcessor(Optional.of(filter), ImmutableList.of(field(0, BIGINT)), MAX_BATCH_SIZE).get(); Page page = new Page(createRLEBlock(5L, 100)); Page outputPage = getOnlyElement( processor.process( null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertEquals(outputPage.getPositionCount(), 100); assertTrue(outputPage.getBlock(0) instanceof RunLengthEncodedBlock); RunLengthEncodedBlock rle = (RunLengthEncodedBlock) outputPage.getBlock(0); assertEquals(BIGINT.getLong(rle.getValue(), 0), 5L); }
@Override public RowExpression visitInputReference(InputReferenceExpression reference, Void context) { return field(reference.getField(), targetType); }
@Override protected RowExpression visitBooleanLiteral(BooleanLiteral node, Void context) { return constant(node.getValue(), BOOLEAN); }
@Override protected RowExpression visitIsNotNullPredicate(IsNotNullPredicate node, Void context) { RowExpression expression = process(node.getValue(), context); return call( Signatures.notSignature(), BOOLEAN, call(Signatures.isNullSignature(expression.getType()), BOOLEAN, ImmutableList.of(expression))); }
.map(argument -> argument.accept(this, null)) .collect(toImmutableList()); return call(signature, call.getType(), arguments); return call(signature, call.getType(), optimizedArgumentsBuilder.build()); .map(argument -> argument.accept(this, null)) .collect(toImmutableList()); return call(signature, call.getType(), arguments); return constantNull(call.getType()); return constant(method.invokeWithArguments(constantArguments), call.getType()); return call(signature, typeManager.getType(signature.getReturnType()), arguments);
@Override protected RowExpression visitDereferenceExpression(DereferenceExpression node, Void context) { RowType rowType = (RowType) getType(node.getBase()); String fieldName = node.getField().getValue(); List<Field> fields = rowType.getFields(); int index = -1; for (int i = 0; i < fields.size(); i++) { Field field = fields.get(i); if (field.getName().isPresent() && field.getName().get().equalsIgnoreCase(fieldName)) { checkArgument(index < 0, "Ambiguous field %s in type %s", field, rowType.getDisplayName()); index = i; } } if (legacyRowFieldOrdinalAccess && index < 0) { OptionalInt rowIndex = parseAnonymousRowFieldOrdinalAccess(fieldName, fields); if (rowIndex.isPresent()) { index = rowIndex.getAsInt(); } } checkState(index >= 0, "could not find field name: %s", node.getField()); Type returnType = getType(node); return call(dereferenceSignature(returnType, rowType), returnType, process(node.getBase(), context), constant(index, INTEGER)); }
@Override protected RowExpression visitNullLiteral(NullLiteral node, Void context) { return constantNull(UnknownType.UNKNOWN); }
@Test public void testDeterminismEvaluator() { DeterminismEvaluator determinismEvaluator = new DeterminismEvaluator(createTestMetadataManager().getFunctionRegistry()); CallExpression random = new CallExpression( new Signature( "random", SCALAR, parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT)), BIGINT, singletonList(constant(10L, BIGINT))); assertFalse(determinismEvaluator.isDeterministic(random)); InputReferenceExpression col0 = field(0, BIGINT); Signature lessThan = internalOperator(LESS_THAN, BOOLEAN, ImmutableList.of(BIGINT, BIGINT)); CallExpression lessThanExpression = new CallExpression(lessThan, BOOLEAN, ImmutableList.of(col0, constant(10L, BIGINT))); assertTrue(determinismEvaluator.isDeterministic(lessThanExpression)); CallExpression lessThanRandomExpression = new CallExpression(lessThan, BOOLEAN, ImmutableList.of(col0, random)); assertFalse(determinismEvaluator.isDeterministic(lessThanRandomExpression)); } }
@Override public RowExpression visitInputReference(InputReferenceExpression reference, Void context) { int parameter = getParameterForField(reference); return field(parameter, reference.getType()); }
@Override protected RowExpression visitDoubleLiteral(DoubleLiteral node, Void context) { return constant(node.getValue(), DOUBLE); }
private RowExpression likeFunctionCall(RowExpression value, RowExpression pattern) { if (value.getType() instanceof VarcharType) { return call(likeVarcharSignature(), BOOLEAN, value, pattern); } checkState(value.getType() instanceof CharType, "LIKE value type is neither VARCHAR or CHAR"); return call(likeCharSignature(value.getType()), BOOLEAN, value, pattern); }
.map(argument -> argument.accept(this, null)) .collect(toImmutableList()); return call(signature, call.getType(), arguments); default: function = registry.getScalarFunctionImplementation(signature); return constantNull(call.getType()); return constant(method.invokeWithArguments(constantArguments), call.getType()); return call(signature, typeManager.getType(signature.getReturnType()), arguments);
@Override protected RowExpression visitGenericLiteral(GenericLiteral node, Void context) { Type type; try { type = typeManager.getType(parseTypeSignature(node.getType())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unsupported type: " + node.getType()); } if (JSON.equals(type)) { return call( new Signature("json_parse", SCALAR, getType(node).getTypeSignature(), VARCHAR.getTypeSignature()), getType(node), constant(utf8Slice(node.getValue()), VARCHAR)); } return call( castSignature(getType(node), VARCHAR), getType(node), constant(utf8Slice(node.getValue()), VARCHAR)); }
@Override protected RowExpression visitNullLiteral(NullLiteral node, Void context) { return constantNull(UnknownType.UNKNOWN); }
RowExpression jsonCastExpression = new CallExpression(jsonCastSignature, new ArrayType(INTEGER), ImmutableList.of(call(jsonParseSignature, JSON, constant(utf8Slice("[1, 2]"), VARCHAR)))); RowExpression resultExpression = optimizer.optimize(jsonCastExpression); assertInstanceOf(resultExpression, ConstantExpression.class); jsonCastExpression = new CallExpression(jsonCastSignature, new ArrayType(VARCHAR), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression); assertEquals( resultExpression, call(internalScalarFunction(JSON_STRING_TO_ARRAY_NAME, parseTypeSignature("array(varchar)"), parseTypeSignature(StandardTypes.VARCHAR)), new ArrayType(VARCHAR), field(1, VARCHAR))); jsonCastExpression = new CallExpression(jsonCastSignature, mapType(INTEGER, VARCHAR), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression); assertEquals( resultExpression, call(internalScalarFunction(JSON_STRING_TO_MAP_NAME, parseTypeSignature("map(integer,varchar)"), parseTypeSignature(StandardTypes.VARCHAR)), mapType(INTEGER, VARCHAR), field(1, VARCHAR))); jsonCastExpression = new CallExpression(jsonCastSignature, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call(jsonParseSignature, JSON, field(1, VARCHAR)))); resultExpression = optimizer.optimize(jsonCastExpression); assertEquals( resultExpression, call(internalScalarFunction(JSON_STRING_TO_ROW_NAME, parseTypeSignature("row(varchar,bigint)"), parseTypeSignature(StandardTypes.VARCHAR)), RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), field(1, VARCHAR)));