use of org.apache.calcite.sql.SqlCallBinding in project hazelcast by hazelcast.
the class HazelcastDynamicTableFunction method toArguments.
private static List<Object> toArguments(String functionName, List<HazelcastTableFunctionParameter> parameters, SqlOperatorBinding callBinding) {
SqlCallBinding binding = (SqlCallBinding) callBinding;
SqlCall call = binding.getCall();
HazelcastSqlValidator validator = (HazelcastSqlValidator) binding.getValidator();
return ValidationUtil.hasAssignment(call) ? fromNamedArguments(functionName, parameters, call, validator) : fromPositionalArguments(functionName, parameters, call, validator);
}
use of org.apache.calcite.sql.SqlCallBinding in project hazelcast by hazelcast.
the class HazelcastCaseOperator method deriveType.
@Override
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
preValidateCall(validator, scope, call);
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);
checkOperandTypes(opBinding, true);
return inferReturnType(opBinding);
}
use of org.apache.calcite.sql.SqlCallBinding 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;
}
use of org.apache.calcite.sql.SqlCallBinding 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));
}
use of org.apache.calcite.sql.SqlCallBinding in project samza by apache.
the class CheckerTest method testCheckOperandTypesShouldReturnTrueWhenArgumentCheckIsDisabled.
@Test
public void testCheckOperandTypesShouldReturnTrueWhenArgumentCheckIsDisabled() throws NoSuchMethodException {
Method udfMethod = TestUdfWithWrongTypes.class.getMethod("execute", String.class);
UdfMetadata udfMetadata = new UdfMetadata("TestUdfWithWrongTypes", "TestUDFClass", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.INT32), SamzaSqlFieldType.INT64, true);
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.VARCHAR, 12));
assertTrue(operandTypeChecker.checkOperandTypes(callBinding, true));
}
Aggregations