Search in sources :

Example 51 with RelOptTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project druid by druid-io.

the class DruidTableScanRule method onMatch.

@Override
public void onMatch(final RelOptRuleCall call) {
    final LogicalTableScan scan = call.rel(0);
    final RelOptTable table = scan.getTable();
    final DruidTable druidTable = table.unwrap(DruidTable.class);
    if (druidTable != null) {
        call.transformTo(DruidQueryRel.scanTable(scan, table, druidTable, plannerContext));
    }
}
Also used : DruidTable(org.apache.druid.sql.calcite.table.DruidTable) RelOptTable(org.apache.calcite.plan.RelOptTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 52 with RelOptTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project flink by apache.

the class CommonExecLookupJoin method translateToPlanInternal.

@Override
@SuppressWarnings("unchecked")
public Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    RelOptTable temporalTable = temporalTableSourceSpec.getTemporalTable(planner.getFlinkContext());
    // validate whether the node is valid and supported.
    validate(temporalTable);
    final ExecEdge inputEdge = getInputEdges().get(0);
    RowType inputRowType = (RowType) inputEdge.getOutputType();
    RowType tableSourceRowType = FlinkTypeFactory.toLogicalRowType(temporalTable.getRowType());
    RowType resultRowType = (RowType) getOutputType();
    validateLookupKeyType(lookupKeys, inputRowType, tableSourceRowType);
    boolean isAsyncEnabled = false;
    UserDefinedFunction userDefinedFunction = LookupJoinUtil.getLookupFunction(temporalTable, lookupKeys.keySet());
    UserDefinedFunctionHelper.prepareInstance(config, userDefinedFunction);
    if (userDefinedFunction instanceof AsyncTableFunction) {
        isAsyncEnabled = true;
    }
    boolean isLeftOuterJoin = joinType == FlinkJoinType.LEFT;
    StreamOperatorFactory<RowData> operatorFactory;
    if (isAsyncEnabled) {
        operatorFactory = createAsyncLookupJoin(temporalTable, config, lookupKeys, (AsyncTableFunction<Object>) userDefinedFunction, planner.getRelBuilder(), inputRowType, tableSourceRowType, resultRowType, isLeftOuterJoin);
    } else {
        operatorFactory = createSyncLookupJoin(temporalTable, config, lookupKeys, (TableFunction<Object>) userDefinedFunction, planner.getRelBuilder(), inputRowType, tableSourceRowType, resultRowType, isLeftOuterJoin, planner.getExecEnv().getConfig().isObjectReuseEnabled());
    }
    Transformation<RowData> inputTransformation = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    return ExecNodeUtil.createOneInputTransformation(inputTransformation, createTransformationMeta(LOOKUP_JOIN_TRANSFORMATION, config), operatorFactory, InternalTypeInfo.of(resultRowType), inputTransformation.getParallelism());
}
Also used : RowData(org.apache.flink.table.data.RowData) Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) RowType(org.apache.flink.table.types.logical.RowType) AsyncTableFunction(org.apache.flink.table.functions.AsyncTableFunction) TableFunction(org.apache.flink.table.functions.TableFunction) RelOptTable(org.apache.calcite.plan.RelOptTable) AsyncTableFunction(org.apache.flink.table.functions.AsyncTableFunction)

Example 53 with RelOptTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project flink by apache.

the class SqlValidatorImpl method createTargetRowType.

/**
 * Derives a row-type for INSERT and UPDATE operations.
 *
 * @param table Target table for INSERT/UPDATE
 * @param targetColumnList List of target columns, or null if not specified
 * @param append Whether to append fields to those in <code>
 *                         baseRowType</code>
 * @return Rowtype
 */
protected RelDataType createTargetRowType(SqlValidatorTable table, SqlNodeList targetColumnList, boolean append) {
    RelDataType baseRowType = table.getRowType();
    if (targetColumnList == null) {
        return baseRowType;
    }
    List<RelDataTypeField> targetFields = baseRowType.getFieldList();
    final List<Map.Entry<String, RelDataType>> fields = new ArrayList<>();
    if (append) {
        for (RelDataTypeField targetField : targetFields) {
            fields.add(Pair.of(SqlUtil.deriveAliasFromOrdinal(fields.size()), targetField.getType()));
        }
    }
    final Set<Integer> assignedFields = new HashSet<>();
    final RelOptTable relOptTable = table instanceof RelOptTable ? ((RelOptTable) table) : null;
    for (SqlNode node : targetColumnList) {
        SqlIdentifier id = (SqlIdentifier) node;
        RelDataTypeField targetField = SqlValidatorUtil.getTargetField(baseRowType, typeFactory, id, catalogReader, relOptTable);
        if (targetField == null) {
            throw newValidationError(id, RESOURCE.unknownTargetColumn(id.toString()));
        }
        if (!assignedFields.add(targetField.getIndex())) {
            throw newValidationError(id, RESOURCE.duplicateTargetColumn(targetField.getName()));
        }
        fields.add(targetField);
    }
    return typeFactory.createStructType(fields);
}
Also used : BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 54 with RelOptTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project flink by apache.

the class LogicalTableScan method create.

// END FLINK MODIFICATION
/**
 * Creates a LogicalTableScan.
 *
 * @param cluster Cluster
 * @param relOptTable Table
 * @param hints The hints
 */
public static LogicalTableScan create(RelOptCluster cluster, final RelOptTable relOptTable, List<RelHint> hints) {
    final Table table = relOptTable.unwrap(Table.class);
    final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE).replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
        if (table != null) {
            return table.getStatistic().getCollations();
        }
        return ImmutableList.of();
    });
    return new LogicalTableScan(cluster, traitSet, hints, relOptTable);
}
Also used : Table(org.apache.calcite.schema.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 55 with RelOptTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project flink by apache.

the class LookupJoinUtil method getLookupFunction.

/**
 * Gets LookupFunction from temporal table according to the given lookup keys.
 */
public static UserDefinedFunction getLookupFunction(RelOptTable temporalTable, Collection<Integer> lookupKeys) {
    int[] lookupKeyIndicesInOrder = getOrderedLookupKeys(lookupKeys);
    if (temporalTable instanceof TableSourceTable) {
        // TODO: support nested lookup keys in the future,
        // currently we only support top-level lookup keys
        int[][] indices = IntStream.of(lookupKeyIndicesInOrder).mapToObj(i -> new int[] { i }).toArray(int[][]::new);
        LookupTableSource tableSource = (LookupTableSource) ((TableSourceTable) temporalTable).tableSource();
        LookupRuntimeProviderContext providerContext = new LookupRuntimeProviderContext(indices);
        LookupTableSource.LookupRuntimeProvider provider = tableSource.getLookupRuntimeProvider(providerContext);
        if (provider instanceof TableFunctionProvider) {
            return ((TableFunctionProvider<?>) provider).createTableFunction();
        } else if (provider instanceof AsyncTableFunctionProvider) {
            return ((AsyncTableFunctionProvider<?>) provider).createAsyncTableFunction();
        }
    }
    if (temporalTable instanceof LegacyTableSourceTable) {
        String[] lookupFieldNamesInOrder = IntStream.of(lookupKeyIndicesInOrder).mapToObj(temporalTable.getRowType().getFieldNames()::get).toArray(String[]::new);
        LegacyTableSourceTable<?> legacyTableSourceTable = (LegacyTableSourceTable<?>) temporalTable;
        LookupableTableSource<?> tableSource = (LookupableTableSource<?>) legacyTableSourceTable.tableSource();
        if (tableSource.isAsyncEnabled()) {
            return tableSource.getAsyncLookupFunction(lookupFieldNamesInOrder);
        } else {
            return tableSource.getLookupFunction(lookupFieldNamesInOrder);
        }
    }
    throw new TableException(String.format("table %s is neither TableSourceTable not LegacyTableSourceTable", temporalTable.getQualifiedName()));
}
Also used : IntStream(java.util.stream.IntStream) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) JsonSubTypes(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) TableException(org.apache.flink.table.api.TableException) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LogicalType(org.apache.flink.table.types.logical.LogicalType) JsonTypeInfo(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeInfo) Internal(org.apache.flink.annotation.Internal) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) JsonIgnoreProperties(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) TableException(org.apache.flink.table.api.TableException) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) LookupableTableSource(org.apache.flink.table.sources.LookupableTableSource) LookupRuntimeProviderContext(org.apache.flink.table.runtime.connector.source.LookupRuntimeProviderContext) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) LegacyTableSourceTable(org.apache.flink.table.planner.plan.schema.LegacyTableSourceTable) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable)

Aggregations

RelOptTable (org.apache.calcite.plan.RelOptTable)63 RelDataType (org.apache.calcite.rel.type.RelDataType)20 RexNode (org.apache.calcite.rex.RexNode)18 RelNode (org.apache.calcite.rel.RelNode)17 Table (org.apache.calcite.schema.Table)15 ArrayList (java.util.ArrayList)14 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)12 RelTraitSet (org.apache.calcite.plan.RelTraitSet)10 SqlNode (org.apache.calcite.sql.SqlNode)10 RelOptCluster (org.apache.calcite.plan.RelOptCluster)9 ImmutableList (com.google.common.collect.ImmutableList)8 NlsString (org.apache.calcite.util.NlsString)8 List (java.util.List)6 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)6 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 RelOptSchema (org.apache.calcite.plan.RelOptSchema)5 Project (org.apache.calcite.rel.core.Project)5 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)5 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)5 Test (org.junit.Test)5