Search in sources :

Example 66 with TableException

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);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) DecimalType(org.apache.flink.table.types.logical.DecimalType) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CallExpression(org.apache.flink.table.expressions.CallExpression) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 67 with TableException

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));
}
Also used : DecimalData(org.apache.flink.table.data.DecimalData) TableException(org.apache.flink.table.api.TableException) BigDecimal(java.math.BigDecimal)

Example 68 with TableException

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);
}
Also used : TableException(org.apache.flink.table.api.TableException) SqlRegularColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlRegularColumn) LogicalType(org.apache.flink.table.types.logical.LogicalType) DataType(org.apache.flink.table.types.DataType) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec)

Example 69 with TableException

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));
}
Also used : TableException(org.apache.flink.table.api.TableException) RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) InputFormatTableSource(org.apache.flink.table.sources.InputFormatTableSource) RexNode(org.apache.calcite.rex.RexNode)

Example 70 with TableException

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);
    }
}
Also used : PythonFunctionInfo(org.apache.flink.table.functions.python.PythonFunctionInfo) TableException(org.apache.flink.table.api.TableException) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) RowType(org.apache.flink.table.types.logical.RowType) TableException(org.apache.flink.table.api.TableException)

Aggregations

TableException (org.apache.flink.table.api.TableException)163 RowData (org.apache.flink.table.data.RowData)35 RowType (org.apache.flink.table.types.logical.RowType)35 Transformation (org.apache.flink.api.dag.Transformation)28 ArrayList (java.util.ArrayList)27 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)24 LogicalType (org.apache.flink.table.types.logical.LogicalType)24 List (java.util.List)22 DataType (org.apache.flink.table.types.DataType)19 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)18 ValidationException (org.apache.flink.table.api.ValidationException)17 IOException (java.io.IOException)13 AggregateCall (org.apache.calcite.rel.core.AggregateCall)13 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)13 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)13 Optional (java.util.Optional)11 Configuration (org.apache.flink.configuration.Configuration)11 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)11 Constructor (java.lang.reflect.Constructor)10 Arrays (java.util.Arrays)9