Search in sources :

Example 76 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName 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 77 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class CalciteMetaImpl method getAllDefaultType.

private ImmutableList<MetaTypeInfo> getAllDefaultType() {
    final ImmutableList.Builder<MetaTypeInfo> allTypeList = ImmutableList.builder();
    final CalciteConnectionImpl conn = (CalciteConnectionImpl) connection;
    final RelDataTypeSystem typeSystem = conn.typeFactory.getTypeSystem();
    for (SqlTypeName sqlTypeName : SqlTypeName.values()) {
        allTypeList.add(new MetaTypeInfo(sqlTypeName.getName(), sqlTypeName.getJdbcOrdinal(), typeSystem.getMaxPrecision(sqlTypeName), typeSystem.getLiteral(sqlTypeName, true), typeSystem.getLiteral(sqlTypeName, false), // All types are nullable
        (short) DatabaseMetaData.typeNullable, typeSystem.isCaseSensitive(sqlTypeName), // be specific and declare under SqlTypeName
        (short) DatabaseMetaData.typeSearchable, false, false, typeSystem.isAutoincrement(sqlTypeName), (short) sqlTypeName.getMinScale(), (short) typeSystem.getMaxScale(sqlTypeName), typeSystem.getNumTypeRadix(sqlTypeName)));
    }
    return allTypeList.build();
}
Also used : RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ImmutableList(com.google.common.collect.ImmutableList)

Example 78 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testDistinct.

@Test
public void testDistinct() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.DISTINCT);
    assertEquals("DISTINCT did not map to DISTINCT", SqlTypeName.DISTINCT, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 79 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testBlob.

@Test
public void testBlob() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.BLOB);
    assertEquals("BLOB did not map to null", null, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Example 80 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class SqlTypeNameTest method testReal.

@Test
public void testReal() {
    SqlTypeName tn = SqlTypeName.getNameForJdbcType(Types.REAL);
    assertEquals("REAL did not map to REAL", SqlTypeName.REAL, tn);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Test(org.junit.Test)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)117 Test (org.junit.Test)38 RelDataType (org.apache.calcite.rel.type.RelDataType)28 RexNode (org.apache.calcite.rex.RexNode)18 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)13 List (java.util.List)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)8 ImmutableList (com.google.common.collect.ImmutableList)7 Map (java.util.Map)7 DateString (org.apache.calcite.util.DateString)7 TimeString (org.apache.calcite.util.TimeString)7 TimestampString (org.apache.calcite.util.TimestampString)7 ISE (io.druid.java.util.common.ISE)6 SqlKind (org.apache.calcite.sql.SqlKind)6 NlsString (org.apache.calcite.util.NlsString)6 Calendar (java.util.Calendar)5 Nullable (javax.annotation.Nullable)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5