Search in sources :

Example 1 with STATIC

use of io.airlift.bytecode.Access.STATIC in project hetu-core by openlookeng.

the class ConcatFunction method generateConcat.

private static Class<?> generateConcat(TypeSignature type, int arity) {
    checkCondition(arity <= 254, NOT_SUPPORTED, "Too many arguments for string concatenation");
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(type.getBase() + "_concat" + arity + "ScalarFunction"), type(Object.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PRIVATE));
    // Generate concat()
    List<Parameter> parameters = IntStream.range(0, arity).mapToObj(i -> arg("arg" + i, Slice.class)).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "concat", type(Slice.class), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable length = scope.declareVariable(int.class, "length");
    body.append(length.set(constantInt(0)));
    for (int i = 0; i < arity; ++i) {
        body.append(length.set(generateCheckedAdd(length, parameters.get(i).invoke("length", int.class))));
    }
    Variable result = scope.declareVariable(Slice.class, "result");
    body.append(result.set(invokeStatic(Slices.class, "allocate", Slice.class, length)));
    Variable position = scope.declareVariable(int.class, "position");
    body.append(position.set(constantInt(0)));
    for (int i = 0; i < arity; ++i) {
        body.append(result.invoke("setBytes", void.class, position, parameters.get(i)));
        body.append(position.set(add(position, parameters.get(i).invoke("length", int.class))));
    }
    body.getVariable(result).retObject();
    return defineClass(definition, Object.class, ImmutableMap.of(), new DynamicClassLoader(ConcatFunction.class.getClassLoader()));
}
Also used : BytecodeBlock(io.airlift.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) BoundVariables(io.prestosql.metadata.BoundVariables) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) CompilerUtils.makeClassName(io.prestosql.util.CompilerUtils.makeClassName) Access.a(io.airlift.bytecode.Access.a) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) Parameter.arg(io.airlift.bytecode.Parameter.arg) BytecodeExpressions.add(io.airlift.bytecode.expression.BytecodeExpressions.add) Slices(io.airlift.slice.Slices) MethodDefinition(io.airlift.bytecode.MethodDefinition) PrestoException(io.prestosql.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) TypeSignature(io.prestosql.spi.type.TypeSignature) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) FunctionKind(io.prestosql.spi.function.FunctionKind) BytecodeExpressions.constantInt(io.airlift.bytecode.expression.BytecodeExpressions.constantInt) Parameter(io.airlift.bytecode.Parameter) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ImmutableList(com.google.common.collect.ImmutableList) Failures.checkCondition(io.prestosql.util.Failures.checkCondition) CompilerUtils.defineClass(io.prestosql.util.CompilerUtils.defineClass) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) Signature(io.prestosql.spi.function.Signature) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) BytecodeExpressions.invokeStatic(io.airlift.bytecode.expression.BytecodeExpressions.invokeStatic) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) Math.addExact(java.lang.Math.addExact) PUBLIC(io.airlift.bytecode.Access.PUBLIC) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Slice(io.airlift.slice.Slice) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) ClassDefinition(io.airlift.bytecode.ClassDefinition)

Example 2 with STATIC

use of io.airlift.bytecode.Access.STATIC in project trino by trinodb.

the class AbstractGreatestLeast method generate.

private Class<?> generate(List<Class<?>> javaTypes, MethodHandle compareMethod) {
    Signature signature = getFunctionMetadata().getSignature();
    checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", signature.getName());
    String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + signature.getName()), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), signature.getName(), type(wrap(javaTypes.get(0))), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    CallSiteBinder binder = new CallSiteBinder();
    Variable value = scope.declareVariable(wrap(javaTypes.get(0)), "value");
    BytecodeExpression nullValue = constantNull(wrap(javaTypes.get(0)));
    body.append(value.set(nullValue));
    LabelNode done = new LabelNode("done");
    compareMethod = compareMethod.asType(methodType(boolean.class, compareMethod.type().wrap().parameterList()));
    for (int i = 0; i < javaTypes.size(); i++) {
        Parameter parameter = parameters.get(i);
        BytecodeExpression invokeCompare = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(compareMethod).getBindingId()), "compare", boolean.class, parameter, value);
        body.append(new IfStatement().condition(isNull(parameter)).ifTrue(new BytecodeBlock().append(value.set(nullValue)).gotoLabel(done)));
        body.append(new IfStatement().condition(or(isNull(value), invokeCompare)).ifTrue(value.set(parameter)));
    }
    body.visitLabel(done);
    body.append(value.ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
Also used : FunctionDependencies(io.trino.metadata.FunctionDependencies) SCALAR(io.trino.metadata.FunctionKind.SCALAR) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) FunctionNullability(io.trino.metadata.FunctionNullability) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Access.a(io.airlift.bytecode.Access.a) BOOTSTRAP_METHOD(io.trino.sql.gen.Bootstrap.BOOTSTRAP_METHOD) Parameter.arg(io.airlift.bytecode.Parameter.arg) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CompilerUtils.makeClassName(io.trino.util.CompilerUtils.makeClassName) MinMaxCompare.getMinMaxCompare(io.trino.util.MinMaxCompare.getMinMaxCompare) BytecodeExpressions.constantNull(io.airlift.bytecode.expression.BytecodeExpressions.constantNull) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) MethodDefinition(io.airlift.bytecode.MethodDefinition) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) Collectors.joining(java.util.stream.Collectors.joining) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) BytecodeExpressions.or(io.airlift.bytecode.expression.BytecodeExpressions.or) Failures.checkCondition(io.trino.util.Failures.checkCondition) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) MethodHandle(java.lang.invoke.MethodHandle) Type(io.trino.spi.type.Type) Parameter(io.airlift.bytecode.Parameter) ImmutableList(com.google.common.collect.ImmutableList) LabelNode(io.airlift.bytecode.instruction.LabelNode) Signature(io.trino.metadata.Signature) NULLABLE_RETURN(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) MethodType.methodType(java.lang.invoke.MethodType.methodType) BytecodeExpressions.invokeDynamic(io.airlift.bytecode.expression.BytecodeExpressions.invokeDynamic) BOXED_NULLABLE(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BOXED_NULLABLE) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) IfStatement(io.airlift.bytecode.control.IfStatement) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) PUBLIC(io.airlift.bytecode.Access.PUBLIC) MinMaxCompare.getMinMaxCompareFunctionDependencies(io.trino.util.MinMaxCompare.getMinMaxCompareFunctionDependencies) BoundSignature(io.trino.metadata.BoundSignature) BytecodeExpressions.isNull(io.airlift.bytecode.expression.BytecodeExpressions.isNull) Primitives.wrap(com.google.common.primitives.Primitives.wrap) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) LabelNode(io.airlift.bytecode.instruction.LabelNode) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) IfStatement(io.airlift.bytecode.control.IfStatement) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 3 with STATIC

use of io.airlift.bytecode.Access.STATIC in project hetu-core by openlookeng.

the class AbstractGreatestLeast method generate.

private Class<?> generate(List<Class<?>> javaTypes, Type type, MethodHandle compareMethod) {
    checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", getSignature().getName());
    String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + getSignature().getName()), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), getSignature().getNameSuffix(), type(javaTypes.get(0)), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    CallSiteBinder binder = new CallSiteBinder();
    if (type.getTypeSignature().getBase().equals(StandardTypes.DOUBLE)) {
        for (Parameter parameter : parameters) {
            body.append(parameter);
            body.append(invoke(binder.bind(CHECK_NOT_NAN.bindTo(getSignature().getName().toString())), "checkNotNaN"));
        }
    }
    Variable value = scope.declareVariable(javaTypes.get(0), "value");
    body.append(value.set(parameters.get(0)));
    for (int i = 1; i < javaTypes.size(); i++) {
        body.append(new IfStatement().condition(new BytecodeBlock().append(parameters.get(i)).append(value).append(invoke(binder.bind(compareMethod), "compare"))).ifTrue(value.set(parameters.get(i))));
    }
    body.append(value.ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
Also used : BytecodeUtils.invoke(io.prestosql.sql.gen.BytecodeUtils.invoke) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) BoundVariables(io.prestosql.metadata.BoundVariables) CompilerUtils.makeClassName(io.prestosql.util.CompilerUtils.makeClassName) Access.a(io.airlift.bytecode.Access.a) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) Parameter.arg(io.airlift.bytecode.Parameter.arg) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) MethodDefinition(io.airlift.bytecode.MethodDefinition) PrestoException(io.prestosql.spi.PrestoException) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) FunctionKind(io.prestosql.spi.function.FunctionKind) StandardTypes(io.prestosql.spi.type.StandardTypes) Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) TypeSignatureProvider(io.prestosql.sql.analyzer.TypeSignatureProvider) Parameter(io.airlift.bytecode.Parameter) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ImmutableList(com.google.common.collect.ImmutableList) Failures.checkCondition(io.prestosql.util.Failures.checkCondition) CompilerUtils.defineClass(io.prestosql.util.CompilerUtils.defineClass) Objects.requireNonNull(java.util.Objects.requireNonNull) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) Signature(io.prestosql.spi.function.Signature) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) IfStatement(io.airlift.bytecode.control.IfStatement) PUBLIC(io.airlift.bytecode.Access.PUBLIC) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) IfStatement(io.airlift.bytecode.control.IfStatement) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) Parameter(io.airlift.bytecode.Parameter)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 FINAL (io.airlift.bytecode.Access.FINAL)3 PRIVATE (io.airlift.bytecode.Access.PRIVATE)3 PUBLIC (io.airlift.bytecode.Access.PUBLIC)3 STATIC (io.airlift.bytecode.Access.STATIC)3 Access.a (io.airlift.bytecode.Access.a)3 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)3 ClassDefinition (io.airlift.bytecode.ClassDefinition)3 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)3 MethodDefinition (io.airlift.bytecode.MethodDefinition)3 Parameter (io.airlift.bytecode.Parameter)3 Parameter.arg (io.airlift.bytecode.Parameter.arg)3 ParameterizedType.type (io.airlift.bytecode.ParameterizedType.type)3 Scope (io.airlift.bytecode.Scope)3 Variable (io.airlift.bytecode.Variable)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 IfStatement (io.airlift.bytecode.control.IfStatement)2 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)2 BoundVariables (io.prestosql.metadata.BoundVariables)2