Search in sources :

Example 1 with UdfMetadata

use of org.apache.samza.sql.interfaces.UdfMetadata in project samza by apache.

the class TestReflectionBasedUdfResolver method testUDfResolverShouldReturnAllUDFInClassPath.

@Test
public void testUDfResolverShouldReturnAllUDFInClassPath() throws NoSuchMethodException {
    Config config = new MapConfig(ImmutableMap.of("samza.sql.udf.resolver.package.filter", "org.apache.samza.sql.udf.impl"));
    ReflectionBasedUdfResolver reflectionBasedUdfResolver = new ReflectionBasedUdfResolver(config);
    Collection<UdfMetadata> udfMetadataList = reflectionBasedUdfResolver.getUdfs();
    Method method = TestSamzaSqlUdf.class.getMethod("execute", String.class);
    UdfMetadata udfMetadata = new UdfMetadata("TestSamzaSqlUdf", "Test samza sql udf implementation", method, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.STRING), SamzaSqlFieldType.STRING, true);
    Assert.assertArrayEquals(new UdfMetadata[] { udfMetadata }, udfMetadataList.toArray(new UdfMetadata[0]));
}
Also used : ReflectionBasedUdfResolver(org.apache.samza.sql.udf.ReflectionBasedUdfResolver) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) MapConfig(org.apache.samza.config.MapConfig) Method(java.lang.reflect.Method) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 2 with UdfMetadata

use of org.apache.samza.sql.interfaces.UdfMetadata in project samza by apache.

the class TestReflectionBasedUdfResolver method testShouldReturnNothingWhenNoUDFIsInPackagePrefix.

@Test
public void testShouldReturnNothingWhenNoUDFIsInPackagePrefix() {
    Config config = new MapConfig(ImmutableMap.of("samza.sql.udf.resolver.package.prefix", "org.apache.samza.udf.blah.blah"));
    ReflectionBasedUdfResolver reflectionBasedUdfResolver = new ReflectionBasedUdfResolver(config);
    Collection<UdfMetadata> udfMetadataList = reflectionBasedUdfResolver.getUdfs();
    Assert.assertEquals(0, udfMetadataList.size());
}
Also used : ReflectionBasedUdfResolver(org.apache.samza.sql.udf.ReflectionBasedUdfResolver) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 3 with UdfMetadata

use of org.apache.samza.sql.interfaces.UdfMetadata in project samza by apache.

the class Checker method checkOperandTypes.

@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    if (!udfMetadataOptional.isPresent() || udfMetadataOptional.get().isDisableArgCheck() || !throwOnFailure) {
        return true;
    } else {
        // 1. Generate a mapping from argument index to parsed calcite-type for the sql UDF.
        Map<Integer, RelDataType> argumentIndexToCalciteType = IntStream.range(0, callBinding.getOperandCount()).boxed().collect(Collectors.toMap(operandIndex -> operandIndex, callBinding::getOperandType, (a, b) -> b));
        UdfMetadata udfMetadata = udfMetadataOptional.get();
        List<SamzaSqlFieldType> udfArguments = udfMetadata.getArguments();
        // calcite parser engine.
        for (int udfArgumentIndex = 0; udfArgumentIndex < udfArguments.size(); ++udfArgumentIndex) {
            SamzaSqlFieldType udfArgumentType = udfArguments.get(udfArgumentIndex);
            SqlTypeName udfArgumentAsSqlType = toCalciteSqlType(udfArgumentType);
            RelDataType parsedSqlArgType = argumentIndexToCalciteType.get(udfArgumentIndex);
            // 3(a). Special-case, where static strings used as method-arguments in udf-methods during invocation are parsed as the Char type by calcite.
            if (parsedSqlArgType.getSqlTypeName() == SqlTypeName.CHAR && udfArgumentAsSqlType == SqlTypeName.VARCHAR) {
                return true;
            } else if (!Objects.equals(parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType) && !ANY_SQL_TYPE_NAMES.contains(udfArgumentAsSqlType) && !ANY_SQL_TYPE_NAMES.contains(parsedSqlArgType.getSqlTypeName()) && hasOneUdfMethod(udfMetadata)) {
                // 3(b). Throw up and fail on mismatch between the SamzaSqlType and CalciteType for any argument.
                String msg = String.format("Type mismatch in udf class: %s at argument index: %d." + "Expected type: %s, actual type: %s.", udfMetadata.getName(), udfArgumentIndex, parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType);
                LOG.error(msg);
                throw new SamzaSqlValidatorException(msg);
            }
        }
    }
    // 4. The SamzaSqlFieldType and CalciteType has matched for all the arguments in the UDF.
    return true;
}
Also used : IntStream(java.util.stream.IntStream) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) RelDataType(org.apache.calcite.rel.type.RelDataType) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlOperandCountRanges(org.apache.calcite.sql.type.SqlOperandCountRanges) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) LoggerFactory(org.slf4j.LoggerFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlFieldType(org.apache.samza.sql.schema.SamzaSqlFieldType) Collectors(java.util.stream.Collectors) SamzaException(org.apache.samza.SamzaException) Objects(java.util.Objects) List(java.util.List) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SqlOperator(org.apache.calcite.sql.SqlOperator) Method(java.lang.reflect.Method) SamzaSqlFieldType(org.apache.samza.sql.schema.SamzaSqlFieldType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata)

Example 4 with UdfMetadata

use of org.apache.samza.sql.interfaces.UdfMetadata in project samza by apache.

the class SamzaSqlUdfOperatorTable method getSqlOperator.

private SqlOperator getSqlOperator(SamzaSqlScalarFunctionImpl scalarFunction, List<UdfMetadata> udfMetadataList) {
    int numArguments = scalarFunction.numberOfArguments();
    UdfMetadata udfMetadata = scalarFunction.getUdfMetadata();
    if (udfMetadata.isDisableArgCheck()) {
        return new SqlUserDefinedFunction(new SqlIdentifier(scalarFunction.getUdfName(), SqlParserPos.ZERO), o -> scalarFunction.getReturnType(o.getTypeFactory()), null, Checker.ANY_CHECKER, null, scalarFunction);
    } else {
        return new SqlUserDefinedFunction(new SqlIdentifier(scalarFunction.getUdfName(), SqlParserPos.ZERO), o -> scalarFunction.getReturnType(o.getTypeFactory()), null, Checker.getChecker(numArguments, numArguments, udfMetadata), null, scalarFunction);
    }
}
Also used : SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata)

Example 5 with UdfMetadata

use of org.apache.samza.sql.interfaces.UdfMetadata in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldReturnTrueOnAnyTypeInArg.

@Test
public void testCheckOperandTypesShouldReturnTrueOnAnyTypeInArg() throws NoSuchMethodException {
    Method udfMethod = TestUdfWithAnyType.class.getMethod("execute", Object.class);
    UdfMetadata udfMetadata = new UdfMetadata("TestUdfWithAnyType", "TestUDFClass", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.ANY), SamzaSqlFieldType.INT64, false);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.ARRAY));
    assertTrue(operandTypeChecker.checkOperandTypes(callBinding, true));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Aggregations

UdfMetadata (org.apache.samza.sql.interfaces.UdfMetadata)9 Method (java.lang.reflect.Method)7 MapConfig (org.apache.samza.config.MapConfig)7 Test (org.junit.Test)7 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)6 SamzaSqlUdfMethod (org.apache.samza.sql.udfs.SamzaSqlUdfMethod)6 BasicSqlType (org.apache.calcite.sql.type.BasicSqlType)5 Config (org.apache.samza.config.Config)2 ReflectionBasedUdfResolver (org.apache.samza.sql.udf.ReflectionBasedUdfResolver)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)1