Search in sources :

Example 1 with ADD

use of com.facebook.presto.common.function.OperatorType.ADD in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSetsHiddenToTrueForOperators.

@Test
public void testSetsHiddenToTrueForOperators() {
    Signature signature = SignatureBuilder.builder().operatorType(ADD).kind(SCALAR).returnType(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).argumentTypes(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).build();
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))).build();
    BuiltInScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, FUNCTION_AND_TYPE_MANAGER);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) Slice(io.airlift.slice.Slice) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) ADD(com.facebook.presto.common.function.OperatorType.ADD) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) Slices(io.airlift.slice.Slices) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) Assert.assertFalse(org.testng.Assert.assertFalse) BOOLEAN(com.facebook.presto.common.type.StandardTypes.BOOLEAN) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) ImmutableSet(com.google.common.collect.ImmutableSet) Signature.comparableWithVariadicBound(com.facebook.presto.spi.function.Signature.comparableWithVariadicBound) ImmutableMap(com.google.common.collect.ImmutableMap) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) BLOCK_AND_POSITION(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.BLOCK_AND_POSITION) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.common.type.StandardTypes.VARCHAR) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) Test(org.testng.annotations.Test)

Example 2 with ADD

use of com.facebook.presto.common.function.OperatorType.ADD in project presto by prestodb.

the class TestCursorProcessorCompiler method testRewriteRowExpressionWithCSE.

@Test
public void testRewriteRowExpressionWithCSE() {
    CursorProcessorCompiler cseCursorCompiler = new CursorProcessorCompiler(METADATA, true, emptyMap());
    ClassDefinition cursorProcessorClassDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(CursorProcessor.class.getSimpleName()), type(Object.class), type(CursorProcessor.class));
    RowExpression filter = new SpecialFormExpression(AND, BIGINT, ADD_X_Y_GREATER_THAN_2);
    List<RowExpression> projections = ImmutableList.of(ADD_X_Y_Z);
    List<RowExpression> rowExpressions = ImmutableList.<RowExpression>builder().addAll(projections).add(filter).build();
    Map<Integer, Map<RowExpression, VariableReferenceExpression>> commonSubExpressionsByLevel = collectCSEByLevel(rowExpressions);
    Map<VariableReferenceExpression, CommonSubExpressionRewriter.CommonSubExpressionFields> cseFields = declareCommonSubExpressionFields(cursorProcessorClassDefinition, commonSubExpressionsByLevel);
    Map<RowExpression, VariableReferenceExpression> commonSubExpressions = commonSubExpressionsByLevel.values().stream().flatMap(m -> m.entrySet().stream()).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
    // X+Y as CSE
    assertEquals(1, cseFields.size());
    VariableReferenceExpression cseVariable = cseFields.keySet().iterator().next();
    RowExpression rewrittenFilter = cseCursorCompiler.rewriteRowExpressionsWithCSE(ImmutableList.of(filter), commonSubExpressions).get(0);
    List<RowExpression> rewrittenProjections = cseCursorCompiler.rewriteRowExpressionsWithCSE(projections, commonSubExpressions);
    // X+Y+Z contains CSE X+Y
    assertTrue(((CallExpression) rewrittenProjections.get(0)).getArguments().contains(cseVariable));
    // X+Y > 2 consists CSE X+Y
    assertTrue(((CallExpression) ((SpecialFormExpression) rewrittenFilter).getArguments().get(0)).getArguments().contains(cseVariable));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) Page(com.facebook.presto.common.Page) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) PageBuilder(com.facebook.presto.common.PageBuilder) Expressions.field(com.facebook.presto.sql.relational.Expressions.field) ADD(com.facebook.presto.common.function.OperatorType.ADD) Access.a(com.facebook.presto.bytecode.Access.a) CursorProcessor(com.facebook.presto.operator.project.CursorProcessor) ParameterizedType.type(com.facebook.presto.bytecode.ParameterizedType.type) IF(com.facebook.presto.spi.relation.SpecialFormExpression.Form.IF) CommonSubExpressionRewriter.collectCSEByLevel(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.collectCSEByLevel) SESSION(com.facebook.presto.testing.TestingConnectorSession.SESSION) Map(java.util.Map) AND(com.facebook.presto.spi.relation.SpecialFormExpression.Form.AND) LESS_THAN(com.facebook.presto.common.function.OperatorType.LESS_THAN) CompilerUtils.makeClassName(com.facebook.presto.util.CompilerUtils.makeClassName) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) FINAL(com.facebook.presto.bytecode.Access.FINAL) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Assert.assertEquals(org.testng.Assert.assertEquals) PageRecordSet(com.facebook.presto.operator.index.PageRecordSet) Supplier(java.util.function.Supplier) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) ImmutableList(com.google.common.collect.ImmutableList) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) CommonSubExpressionFields.declareCommonSubExpressionFields(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.declareCommonSubExpressionFields) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) PUBLIC(com.facebook.presto.bytecode.Access.PUBLIC) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Collections.emptyMap(java.util.Collections.emptyMap) RecordSet(com.facebook.presto.spi.RecordSet) GREATER_THAN(com.facebook.presto.common.function.OperatorType.GREATER_THAN) Collectors.toList(java.util.stream.Collectors.toList) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Metadata(com.facebook.presto.metadata.Metadata) CursorProcessor(com.facebook.presto.operator.project.CursorProcessor) RowExpression(com.facebook.presto.spi.relation.RowExpression) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) CommonSubExpressionFields.declareCommonSubExpressionFields(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.declareCommonSubExpressionFields) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Collections.emptyMap(java.util.Collections.emptyMap) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 3 with ADD

use of com.facebook.presto.common.function.OperatorType.ADD in project presto by prestodb.

the class DecimalOperators method decimalAddOperator.

private static SqlScalarFunction decimalAddOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Also used : SUBTRACT(com.facebook.presto.common.function.OperatorType.SUBTRACT) INDETERMINATE(com.facebook.presto.common.function.OperatorType.INDETERMINATE) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.throwIfOverflows) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) TypeSignature(com.facebook.presto.common.type.TypeSignature) ADD(com.facebook.presto.common.function.OperatorType.ADD) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DIVIDE(com.facebook.presto.common.function.OperatorType.DIVIDE) BigInteger(java.math.BigInteger) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Long.signum(java.lang.Long.signum) HASH_CODE(com.facebook.presto.common.function.OperatorType.HASH_CODE) ImmutableSet(com.google.common.collect.ImmutableSet) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(com.facebook.presto.common.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.rescale) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) List(java.util.List) UnscaledDecimal128Arithmetic(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic) PolymorphicScalarFunctionBuilder(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder) SqlType(com.facebook.presto.spi.function.SqlType) StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) Slice(io.airlift.slice.Slice) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) Decimals.encodeUnscaledValue(com.facebook.presto.common.type.Decimals.encodeUnscaledValue) MULTIPLY(com.facebook.presto.common.function.OperatorType.MULTIPLY) PrestoException(com.facebook.presto.spi.PrestoException) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimal) Objects.requireNonNull(java.util.Objects.requireNonNull) Math.toIntExact(java.lang.Math.toIntExact) Decimals.longTenToNth(com.facebook.presto.common.type.Decimals.longTenToNth) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature.longVariableExpression(com.facebook.presto.spi.function.Signature.longVariableExpression) XxHash64(io.airlift.slice.XxHash64) Decimals(com.facebook.presto.common.type.Decimals) SpecializeContext(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Integer.max(java.lang.Integer.max) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) IsNull(com.facebook.presto.spi.function.IsNull) Signature(com.facebook.presto.spi.function.Signature) MODULUS(com.facebook.presto.common.function.OperatorType.MODULUS) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) XX_HASH_64(com.facebook.presto.common.function.OperatorType.XX_HASH_64) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature)

Aggregations

ADD (com.facebook.presto.common.function.OperatorType.ADD)3 ImmutableList (com.google.common.collect.ImmutableList)3 Block (com.facebook.presto.common.block.Block)2 StandardTypes (com.facebook.presto.common.type.StandardTypes)2 TypeSignature (com.facebook.presto.common.type.TypeSignature)2 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 List (java.util.List)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 FINAL (com.facebook.presto.bytecode.Access.FINAL)1 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)1 Access.a (com.facebook.presto.bytecode.Access.a)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 ParameterizedType.type (com.facebook.presto.bytecode.ParameterizedType.type)1 Page (com.facebook.presto.common.Page)1 PageBuilder (com.facebook.presto.common.PageBuilder)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)1 DIVIDE (com.facebook.presto.common.function.OperatorType.DIVIDE)1 GREATER_THAN (com.facebook.presto.common.function.OperatorType.GREATER_THAN)1 HASH_CODE (com.facebook.presto.common.function.OperatorType.HASH_CODE)1