use of org.apache.calcite.rel.type.RelDataTypeFactory in project flink by apache.
the class FlinkConvertletTable method convertTryCast.
// Slightly modified version of StandardConvertletTable::convertCast
private RexNode convertTryCast(SqlRexContext cx, final SqlCall call) {
RelDataTypeFactory typeFactory = cx.getTypeFactory();
final SqlNode leftNode = call.operand(0);
final SqlNode rightNode = call.operand(1);
final RexNode valueRex = cx.convertExpression(leftNode);
RelDataType type;
if (rightNode instanceof SqlIntervalQualifier) {
type = typeFactory.createSqlIntervalType((SqlIntervalQualifier) rightNode);
} else if (rightNode instanceof SqlDataTypeSpec) {
SqlDataTypeSpec dataType = ((SqlDataTypeSpec) rightNode);
type = dataType.deriveType(cx.getValidator());
if (type == null) {
type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
}
} else {
throw new IllegalStateException("Invalid right argument type for TRY_CAST: " + rightNode);
}
type = typeFactory.createTypeWithNullability(type, true);
if (SqlUtil.isNullLiteral(leftNode, false)) {
final SqlValidatorImpl validator = (SqlValidatorImpl) cx.getValidator();
validator.setValidatedNodeType(leftNode, type);
return cx.convertExpression(leftNode);
}
return cx.getRexBuilder().makeCall(type, FlinkSqlOperatorTable.TRY_CAST, Collections.singletonList(valueRex));
}
use of org.apache.calcite.rel.type.RelDataTypeFactory 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;
}
};
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project beam by apache.
the class SqlOperators method createSqlType.
private static RelDataType createSqlType(SqlTypeName typeName, boolean withNullability) {
final RelDataTypeFactory typeFactory = createTypeFactory();
RelDataType type = typeFactory.createSqlType(typeName);
if (withNullability) {
type = typeFactory.createTypeWithNullability(type, true);
}
return type;
}
use of org.apache.calcite.rel.type.RelDataTypeFactory 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());
}
use of org.apache.calcite.rel.type.RelDataTypeFactory 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());
}
Aggregations