Search in sources :

Example 11 with RelOptTable

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

the class SqlToRelConverter method convertMerge.

private RelNode convertMerge(SqlMerge call) {
    RelOptTable targetTable = getTargetTable(call);
    // convert update column list from SqlIdentifier to String
    final List<String> targetColumnNameList = new ArrayList<>();
    final RelDataType targetRowType = targetTable.getRowType();
    SqlUpdate updateCall = call.getUpdateCall();
    if (updateCall != null) {
        for (SqlNode targetColumn : updateCall.getTargetColumnList()) {
            SqlIdentifier id = (SqlIdentifier) targetColumn;
            RelDataTypeField field = SqlValidatorUtil.getTargetField(targetRowType, typeFactory, id, catalogReader, targetTable);
            assert field != null : "column " + id.toString() + " not found";
            targetColumnNameList.add(field.getName());
        }
    }
    // replace the projection of the source select with a
    // projection that contains the following:
    // 1) the expressions corresponding to the new insert row (if there is
    // an insert)
    // 2) all columns from the target table (if there is an update)
    // 3) the set expressions in the update call (if there is an update)
    // first, convert the merge's source select to construct the columns
    // from the target table and the set expressions in the update call
    RelNode mergeSourceRel = convertSelect(call.getSourceSelect(), false);
    // then, convert the insert statement so we can get the insert
    // values expressions
    SqlInsert insertCall = call.getInsertCall();
    int nLevel1Exprs = 0;
    List<RexNode> level1InsertExprs = null;
    List<RexNode> level2InsertExprs = null;
    if (insertCall != null) {
        RelNode insertRel = convertInsert(insertCall);
        // if there are 2 level of projections in the insert source, combine
        // them into a single project; level1 refers to the topmost project;
        // the level1 projection contains references to the level2
        // expressions, except in the case where no target expression was
        // provided, in which case, the expression is the default value for
        // the column; or if the expressions directly map to the source
        // table
        level1InsertExprs = ((LogicalProject) insertRel.getInput(0)).getProjects();
        if (insertRel.getInput(0).getInput(0) instanceof LogicalProject) {
            level2InsertExprs = ((LogicalProject) insertRel.getInput(0).getInput(0)).getProjects();
        }
        nLevel1Exprs = level1InsertExprs.size();
    }
    LogicalJoin join = (LogicalJoin) mergeSourceRel.getInput(0);
    int nSourceFields = join.getLeft().getRowType().getFieldCount();
    final List<RexNode> projects = new ArrayList<>();
    for (int level1Idx = 0; level1Idx < nLevel1Exprs; level1Idx++) {
        if ((level2InsertExprs != null) && (level1InsertExprs.get(level1Idx) instanceof RexInputRef)) {
            int level2Idx = ((RexInputRef) level1InsertExprs.get(level1Idx)).getIndex();
            projects.add(level2InsertExprs.get(level2Idx));
        } else {
            projects.add(level1InsertExprs.get(level1Idx));
        }
    }
    if (updateCall != null) {
        final LogicalProject project = (LogicalProject) mergeSourceRel;
        projects.addAll(Util.skip(project.getProjects(), nSourceFields));
    }
    relBuilder.push(join).project(projects);
    return LogicalTableModify.create(targetTable, catalogReader, relBuilder.build(), LogicalTableModify.Operation.MERGE, targetColumnNameList, null, false);
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlUpdate(org.apache.calcite.sql.SqlUpdate) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptTable(org.apache.calcite.plan.RelOptTable) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with RelOptTable

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

the class SqlToRelConverter method convertInsert.

protected RelNode convertInsert(SqlInsert call) {
    RelOptTable targetTable = getTargetTable(call);
    final RelDataType targetRowType = validator.getValidatedNodeType(call);
    assert targetRowType != null;
    RelNode sourceRel = convertQueryRecursive(call.getSource(), false, targetRowType).project();
    RelNode massagedRel = convertColumnList(call, sourceRel);
    return createModify(targetTable, massagedRel);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 13 with RelOptTable

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

the class SqlToRelConverter method collectInsertTargets.

/**
 * Given an INSERT statement, collects the list of names to be populated and
 * the expressions to put in them.
 *
 * @param call              Insert statement
 * @param sourceRef         Expression representing a row from the source
 *                          relational expression
 * @param targetColumnNames List of target column names, to be populated
 * @param columnExprs       List of expressions, to be populated
 */
protected void collectInsertTargets(SqlInsert call, final RexNode sourceRef, final List<String> targetColumnNames, List<RexNode> columnExprs) {
    final RelOptTable targetTable = getTargetTable(call);
    final RelDataType tableRowType = targetTable.getRowType();
    SqlNodeList targetColumnList = call.getTargetColumnList();
    if (targetColumnList == null) {
        if (validator.getConformance().isInsertSubsetColumnsAllowed()) {
            final RelDataType targetRowType = typeFactory.createStructType(tableRowType.getFieldList().subList(0, sourceRef.getType().getFieldCount()));
            targetColumnNames.addAll(targetRowType.getFieldNames());
        } else {
            targetColumnNames.addAll(tableRowType.getFieldNames());
        }
    } else {
        for (int i = 0; i < targetColumnList.size(); i++) {
            SqlIdentifier id = (SqlIdentifier) targetColumnList.get(i);
            RelDataTypeField field = SqlValidatorUtil.getTargetField(tableRowType, typeFactory, id, catalogReader, targetTable);
            assert field != null : "column " + id.toString() + " not found";
            targetColumnNames.add(field.getName());
        }
    }
    final Blackboard bb = createInsertBlackboard(targetTable, sourceRef, targetColumnNames);
    // Next, assign expressions for generated columns.
    final List<ColumnStrategy> strategies = targetTable.getColumnStrategies();
    for (String columnName : targetColumnNames) {
        final int i = tableRowType.getFieldNames().indexOf(columnName);
        final RexNode expr;
        switch(strategies.get(i)) {
            case STORED:
                final InitializerExpressionFactory f = Util.first(targetTable.unwrap(InitializerExpressionFactory.class), NullInitializerExpressionFactory.INSTANCE);
                expr = f.newColumnDefaultValue(targetTable, i, bb);
                break;
            case VIRTUAL:
                expr = null;
                break;
            default:
                expr = bb.nameToNodeMap.get(columnName);
        }
        columnExprs.add(expr);
    }
    // Remove virtual columns from the list.
    for (int i = 0; i < targetColumnNames.size(); i++) {
        if (columnExprs.get(i) == null) {
            columnExprs.remove(i);
            targetColumnNames.remove(i);
            --i;
        }
    }
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) RexNode(org.apache.calcite.rex.RexNode)

Example 14 with RelOptTable

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

the class SqlToRelConverter method convertIdentifier.

private void convertIdentifier(Blackboard bb, SqlIdentifier id, SqlNodeList extendedColumns) {
    final SqlValidatorNamespace fromNamespace = validator.getNamespace(id).resolve();
    if (fromNamespace.getNode() != null) {
        convertFrom(bb, fromNamespace.getNode());
        return;
    }
    final String datasetName = datasetStack.isEmpty() ? null : datasetStack.peek();
    final boolean[] usedDataset = { false };
    RelOptTable table = SqlValidatorUtil.getRelOptTable(fromNamespace, catalogReader, datasetName, usedDataset);
    if (extendedColumns != null && extendedColumns.size() > 0) {
        assert table != null;
        final SqlValidatorTable validatorTable = table.unwrap(SqlValidatorTable.class);
        final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), validatorTable, extendedColumns);
        table = table.extend(extendedFields);
    }
    final RelNode tableRel;
    if (config.isConvertTableAccess()) {
        tableRel = toRel(table);
    } else {
        tableRel = LogicalTableScan.create(cluster, table);
    }
    bb.setRoot(tableRel, true);
    if (usedDataset[0]) {
        bb.setDataset(datasetName);
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) NlsString(org.apache.calcite.util.NlsString) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace)

Example 15 with RelOptTable

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

the class RelBuilder method scan.

// Methods that create relational expressions
/**
 * Creates a {@link org.apache.calcite.rel.core.TableScan} of the table
 * with a given name.
 *
 * <p>Throws if the table does not exist.
 *
 * <p>Returns this builder.
 *
 * @param tableNames Name of table (can optionally be qualified)
 */
public RelBuilder scan(Iterable<String> tableNames) {
    final List<String> names = ImmutableList.copyOf(tableNames);
    final RelOptTable relOptTable = relOptSchema.getTableForMember(names);
    if (relOptTable == null) {
        throw RESOURCE.tableNotFound(Joiner.on(".").join(names)).ex();
    }
    final RelNode scan = scanFactory.createScan(cluster, relOptTable);
    push(scan);
    return this;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) NlsString(org.apache.calcite.util.NlsString) RelOptTable(org.apache.calcite.plan.RelOptTable)

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