use of org.apache.flink.table.api.TableException in project flink by apache.
the class OverConvertRule method createBound.
private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) {
if (bound instanceof CallExpression) {
CallExpression callExpr = (CallExpression) bound;
FunctionDefinition func = callExpr.getFunctionDefinition();
if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE.equals(func)) {
SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
return RexWindowBound.create(unbounded, null);
} else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE.equals(func)) {
SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
return RexWindowBound.create(currentRow, null);
} else {
throw new IllegalArgumentException("Unexpected expression: " + bound);
}
} else if (bound instanceof ValueLiteralExpression) {
RelDataType returnType = context.getTypeFactory().createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
SqlOperator sqlOperator = new SqlPostfixOperator(sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null);
SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);
ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class).map(v -> context.getRelBuilder().literal(v)).orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class)));
List<RexNode> expressions = new ArrayList<>();
expressions.add(literalRexNode);
RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall(returnType, sqlOperator, expressions);
return RexWindowBound.create(node, rexNode);
} else {
throw new TableException("Unexpected expression: " + bound);
}
}
use of org.apache.flink.table.api.TableException in project flink by apache.
the class ExpressionConverter method extractValue.
/**
* Extracts a value from a literal. Including planner-specific instances such as {@link
* DecimalData}.
*/
@SuppressWarnings("unchecked")
public static <T> T extractValue(ValueLiteralExpression literal, Class<T> clazz) {
final Optional<Object> possibleObject = literal.getValueAs(Object.class);
if (!possibleObject.isPresent()) {
throw new TableException("Invalid literal.");
}
final Object object = possibleObject.get();
if (clazz.equals(BigDecimal.class)) {
final Optional<BigDecimal> possibleDecimal = literal.getValueAs(BigDecimal.class);
if (possibleDecimal.isPresent()) {
return (T) possibleDecimal.get();
}
if (object instanceof DecimalData) {
return (T) ((DecimalData) object).toBigDecimal();
}
}
return literal.getValueAs(clazz).orElseThrow(() -> new TableException("Unsupported literal class: " + clazz));
}
use of org.apache.flink.table.api.TableException in project flink by apache.
the class OperationConverterUtils method toTableColumn.
private static TableColumn toTableColumn(SqlTableColumn tableColumn, SqlValidator sqlValidator) {
if (!(tableColumn instanceof SqlRegularColumn)) {
throw new TableException("Only regular columns are supported for this operation yet.");
}
SqlRegularColumn regularColumn = (SqlRegularColumn) tableColumn;
String name = regularColumn.getName().getSimple();
SqlDataTypeSpec typeSpec = regularColumn.getType();
boolean nullable = typeSpec.getNullable() == null ? true : typeSpec.getNullable();
LogicalType logicalType = FlinkTypeFactory.toLogicalType(typeSpec.deriveType(sqlValidator, nullable));
DataType dataType = TypeConversions.fromLogicalToDataType(logicalType);
return TableColumn.physical(name, dataType);
}
use of org.apache.flink.table.api.TableException in project flink by apache.
the class CommonExecLegacyTableSourceScan method translateToPlanInternal.
@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
final Transformation<?> sourceTransform;
final StreamExecutionEnvironment env = planner.getExecEnv();
if (tableSource instanceof InputFormatTableSource) {
InputFormatTableSource<Object> inputFormat = (InputFormatTableSource<Object>) tableSource;
TypeInformation<Object> typeInfo = (TypeInformation<Object>) fromDataTypeToTypeInfo(inputFormat.getProducedDataType());
InputFormat<Object, ?> format = inputFormat.getInputFormat();
sourceTransform = createInput(env, format, typeInfo);
} else if (tableSource instanceof StreamTableSource) {
sourceTransform = ((StreamTableSource<?>) tableSource).getDataStream(env).getTransformation();
} else {
throw new UnsupportedOperationException(tableSource.getClass().getSimpleName() + " is unsupported.");
}
final TypeInformation<?> inputType = sourceTransform.getOutputType();
final DataType producedDataType = tableSource.getProducedDataType();
// check that declared and actual type of table source DataStream are identical
if (!inputType.equals(TypeInfoDataTypeConverter.fromDataTypeToTypeInfo(producedDataType))) {
throw new TableException(String.format("TableSource of type %s " + "returned a DataStream of data type %s that does not match with the " + "data type %s declared by the TableSource.getProducedDataType() method. " + "Please validate the implementation of the TableSource.", tableSource.getClass().getCanonicalName(), inputType, producedDataType));
}
final RowType outputType = (RowType) getOutputType();
final RelDataType relDataType = FlinkTypeFactory.INSTANCE().buildRelNodeRowType(outputType);
// get expression to extract rowtime attribute
final Optional<RexNode> rowtimeExpression = JavaScalaConversionUtil.toJava(TableSourceUtil.getRowtimeAttributeDescriptor(tableSource, relDataType)).map(desc -> TableSourceUtil.getRowtimeExtractionExpression(desc.getTimestampExtractor(), producedDataType, planner.getRelBuilder(), getNameRemapping()));
return createConversionTransformationIfNeeded(planner.getExecEnv(), config, sourceTransform, rowtimeExpression.orElse(null));
}
use of org.apache.flink.table.api.TableException in project flink by apache.
the class CommonExecPythonCalc method getPythonScalarFunctionOperator.
@SuppressWarnings("unchecked")
private OneInputStreamOperator<RowData, RowData> getPythonScalarFunctionOperator(ExecNodeConfig config, Configuration pythonConfig, InternalTypeInfo<RowData> inputRowTypeInfo, InternalTypeInfo<RowData> outputRowTypeInfo, int[] udfInputOffsets, PythonFunctionInfo[] pythonFunctionInfos, int[] forwardedFields, boolean isArrow) {
Class<?> clazz;
boolean isInProcessMode = CommonPythonUtil.isPythonWorkerInProcessMode(pythonConfig);
if (isArrow) {
clazz = CommonPythonUtil.loadClass(ARROW_PYTHON_SCALAR_FUNCTION_OPERATOR_NAME);
} else {
if (isInProcessMode) {
clazz = CommonPythonUtil.loadClass(PYTHON_SCALAR_FUNCTION_OPERATOR_NAME);
} else {
clazz = CommonPythonUtil.loadClass(EMBEDDED_PYTHON_SCALAR_FUNCTION_OPERATOR_NAME);
}
}
final RowType inputType = inputRowTypeInfo.toRowType();
final RowType outputType = outputRowTypeInfo.toRowType();
final RowType udfInputType = (RowType) Projection.of(udfInputOffsets).project(inputType);
final RowType forwardedFieldType = (RowType) Projection.of(forwardedFields).project(inputType);
final RowType udfOutputType = (RowType) Projection.range(forwardedFields.length, outputType.getFieldCount()).project(outputType);
try {
if (isInProcessMode) {
Constructor<?> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, GeneratedProjection.class, GeneratedProjection.class);
return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, pythonFunctionInfos, inputType, udfInputType, udfOutputType, ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "UdfInputProjection", inputType, udfInputType, udfInputOffsets), ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "ForwardedFieldProjection", inputType, forwardedFieldType, forwardedFields));
} else {
if (forwardedFields.length > 0) {
Constructor<?> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, int[].class, GeneratedProjection.class);
return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, pythonFunctionInfos, inputType, udfInputType, udfOutputType, udfInputOffsets, ProjectionCodeGenerator.generateProjection(CodeGeneratorContext.apply(config.getTableConfig()), "ForwardedFieldProjection", inputType, forwardedFieldType, forwardedFields));
} else {
Constructor<?> ctor = clazz.getConstructor(Configuration.class, PythonFunctionInfo[].class, RowType.class, RowType.class, RowType.class, int[].class);
return (OneInputStreamOperator<RowData, RowData>) ctor.newInstance(pythonConfig, pythonFunctionInfos, inputType, udfInputType, udfOutputType, udfInputOffsets);
}
}
} catch (Exception e) {
throw new TableException("Python Scalar Function Operator constructed failed.", e);
}
}
Aggregations