Search in sources :

Example 1 with FunctionParameter

use of org.apache.calcite.schema.FunctionParameter in project drill by apache.

the class WithOptionsTableMacro method getParameters.

@Override
public List<FunctionParameter> getParameters() {
    List<FunctionParameter> result = new ArrayList<>();
    for (int i = 0; i < sig.getParams().size(); i++) {
        final TableParamDef p = sig.getParams().get(i);
        final int ordinal = i;
        FunctionParameter functionParameter = new FunctionParameter() {

            @Override
            public int getOrdinal() {
                return ordinal;
            }

            @Override
            public String getName() {
                return p.getName();
            }

            @Override
            public RelDataType getType(RelDataTypeFactory typeFactory) {
                return typeFactory.createJavaType(p.getType());
            }

            @Override
            public boolean isOptional() {
                return p.isOptional();
            }
        };
        result.add(functionParameter);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) FunctionParameter(org.apache.calcite.schema.FunctionParameter)

Example 2 with FunctionParameter

use of org.apache.calcite.schema.FunctionParameter in project beam by apache.

the class SqlOperators method createUdfOperator.

private static SqlUserDefinedFunction createUdfOperator(String name, Method method, final SqlSyntax syntax, String funGroup, String jarPath) {
    Function function = ZetaSqlScalarFunctionImpl.create(method, funGroup, jarPath);
    final RelDataTypeFactory typeFactory = createTypeFactory();
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, i -> function.getParameters().get(i).isOptional());
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    return new SqlUserDefinedFunction(new SqlIdentifier(name, SqlParserPos.ZERO), infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function) {

        @Override
        public SqlSyntax getSyntax() {
            return syntax;
        }
    };
}
Also used : FamilyOperandTypeChecker(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) SqlFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction) AggregateFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlUserDefinedAggFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlUserDefinedFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScalarFunction) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)

Example 3 with FunctionParameter

use of org.apache.calcite.schema.FunctionParameter in project beam by apache.

the class TypedCombineFnDelegateTest method testParameterExtractionFromCombineFn_CombineFnDelegate_WithListInsteadOfArray.

@Test
public void testParameterExtractionFromCombineFn_CombineFnDelegate_WithListInsteadOfArray() {
    Combine.BinaryCombineFn<List<List<String>>> max = Max.of((Comparator<List<List<String>>> & Serializable) (a, b) -> Integer.compare(a.get(0).get(0).length(), b.get(0).get(0).length()));
    UdafImpl<List<List<String>>, Combine.Holder<List<List<String>>>, List<List<String>>> udaf = new UdafImpl<>(new TypedCombineFnDelegate<List<List<String>>, Combine.Holder<List<List<String>>>, List<List<String>>>(max) {
    });
    RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    List<FunctionParameter> parameters = udaf.getParameters();
    assertEquals(1, parameters.size());
    assertEquals(SqlTypeName.ARRAY, parameters.get(0).getType(typeFactory).getSqlTypeName());
}
Also used : RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter) Combine(org.apache.beam.sdk.transforms.Combine) Test(org.junit.Test) JavaTypeFactoryImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl) Serializable(java.io.Serializable) SqlTypeName(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName) List(java.util.List) Max(org.apache.beam.sdk.transforms.Max) Rule(org.junit.Rule) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) Comparator(java.util.Comparator) RelDataTypeSystem(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeSystem) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) Serializable(java.io.Serializable) Combine(org.apache.beam.sdk.transforms.Combine) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) Comparator(java.util.Comparator) JavaTypeFactoryImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) List(java.util.List) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter) Test(org.junit.Test)

Example 4 with FunctionParameter

use of org.apache.calcite.schema.FunctionParameter in project beam by apache.

the class TypedCombineFnDelegateTest method testParameterExtractionFromCombineFn_CombineFnDelegate.

@Test
public void testParameterExtractionFromCombineFn_CombineFnDelegate() {
    Combine.BinaryCombineFn<String> max = Max.of((Comparator<String> & Serializable) (a, b) -> Integer.compare(a.length(), b.length()));
    UdafImpl<String, Combine.Holder<String>, String> udaf = new UdafImpl<>(new TypedCombineFnDelegate<String, Combine.Holder<String>, String>(max) {
    });
    RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    List<FunctionParameter> parameters = udaf.getParameters();
    assertEquals(1, parameters.size());
    assertEquals(SqlTypeName.VARCHAR, parameters.get(0).getType(typeFactory).getSqlTypeName());
}
Also used : RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter) Combine(org.apache.beam.sdk.transforms.Combine) Test(org.junit.Test) JavaTypeFactoryImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl) Serializable(java.io.Serializable) SqlTypeName(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName) List(java.util.List) Max(org.apache.beam.sdk.transforms.Max) Rule(org.junit.Rule) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) Comparator(java.util.Comparator) RelDataTypeSystem(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeSystem) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) Serializable(java.io.Serializable) Combine(org.apache.beam.sdk.transforms.Combine) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) Comparator(java.util.Comparator) JavaTypeFactoryImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter) Test(org.junit.Test)

Example 5 with FunctionParameter

use of org.apache.calcite.schema.FunctionParameter in project calcite by apache.

the class CalciteCatalogReader method toOp.

/**
 * Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor.
 */
private static SqlOperator toOp(RelDataTypeFactory typeFactory, SqlIdentifier name, final Function function) {
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final Predicate<Integer> optional = new PredicateImpl<Integer>() {

        public boolean test(Integer input) {
            return function.getParameters().get(input).isOptional();
        }
    };
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, optional);
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    if (function instanceof ScalarFunction) {
        return new SqlUserDefinedFunction(name, infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
    } else if (function instanceof AggregateFunction) {
        return new SqlUserDefinedAggFunction(name, infer((AggregateFunction) function), InferTypes.explicit(argTypes), typeChecker, (AggregateFunction) function, false, false, typeFactory);
    } else if (function instanceof TableMacro) {
        return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableMacro) function);
    } else if (function instanceof TableFunction) {
        return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableFunction) function);
    } else {
        throw new AssertionError("unknown function type " + function);
    }
}
Also used : SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) TableMacro(org.apache.calcite.schema.TableMacro) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) ArrayList(java.util.ArrayList) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) FamilyOperandTypeChecker(org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) FunctionParameter(org.apache.calcite.schema.FunctionParameter)

Aggregations

FunctionParameter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter)6 ArrayList (java.util.ArrayList)5 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)5 UdafImpl (org.apache.beam.sdk.extensions.sql.impl.UdafImpl)3 JavaTypeFactoryImpl (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 FunctionParameter (org.apache.calcite.schema.FunctionParameter)3 Test (org.junit.Test)3 Serializable (java.io.Serializable)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Combine (org.apache.beam.sdk.transforms.Combine)2 Max (org.apache.beam.sdk.transforms.Max)2 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)2 RelDataTypeSystem (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeSystem)2 AggregateFunction (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction)2 SqlTypeName (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Rule (org.junit.Rule)2 ExpectedException (org.junit.rules.ExpectedException)2