Search in sources :

Example 16 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project samza by apache.

the class SamzaSqlValidator method validateDeleteOp.

private void validateDeleteOp(RelRoot relRoot) throws SamzaSqlValidatorException {
    LogicalProject project = (LogicalProject) relRoot.rel;
    RelRecordType projectRecord = (RelRecordType) project.getRowType();
    if (projectRecord.getFieldCount() != 2) {
        throw new SamzaSqlValidatorException(String.format("Only two select query fields are expected for DELETE op." + " But there are %d fields given in the query.", projectRecord.getFieldCount()));
    }
    RelDataTypeField keyField = projectRecord.getField(SamzaSqlRelMessage.KEY_NAME, true, true);
    if (keyField == null) {
        throw new SamzaSqlValidatorException(String.format("Select query needs to specify '%s' field while using DELETE" + " op. Eg: 'SELECT myKey AS %s, '%s' AS %s FROM myTable'", SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.OP_NAME));
    }
    int keyIdx = projectRecord.getFieldList().indexOf(keyField);
    // Get the node corresponding to the special op.
    RexNode node = project.getProjects().get(1 - keyIdx);
    if (!node.toString().equals(String.format("'%s'", SamzaSqlRelMessage.DELETE_OP))) {
        throw new SamzaSqlValidatorException(String.format("%s op is not supported. Please note that only '%s' op is" + " currently supported. Eg:'SELECT myKey AS %s, '%s' AS %s FROM myStream'", node.toString(), SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.OP_NAME));
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project calcite by apache.

the class RelOptTableImpl method toRel.

public RelNode toRel(ToRelContext context) {
    // RelOptTable by replacing with immutable RelRecordType using the same field list.
    if (this.getRowType().isDynamicStruct()) {
        final RelDataType staticRowType = new RelRecordType(getRowType().getFieldList());
        final RelOptTable relOptTable = this.copy(staticRowType);
        return relOptTable.toRel(context);
    }
    // If there are any virtual columns, create a copy of this table without
    // those virtual columns.
    final List<ColumnStrategy> strategies = getColumnStrategies();
    if (strategies.contains(ColumnStrategy.VIRTUAL)) {
        final RelDataTypeFactory.Builder b = context.getCluster().getTypeFactory().builder();
        for (RelDataTypeField field : rowType.getFieldList()) {
            if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
                b.add(field.getName(), field.getType());
            }
        }
        final RelOptTable relOptTable = new RelOptTableImpl(this.schema, b.build(), this.names, this.table, this.expressionFunction, this.rowCount) {

            @Override
            public <T> T unwrap(Class<T> clazz) {
                if (clazz.isAssignableFrom(InitializerExpressionFactory.class)) {
                    return clazz.cast(NullInitializerExpressionFactory.INSTANCE);
                }
                return super.unwrap(clazz);
            }
        };
        return relOptTable.toRel(context);
    }
    if (table instanceof TranslatableTable) {
        return ((TranslatableTable) table).toRel(context, this);
    }
    final RelOptCluster cluster = context.getCluster();
    if (Hook.ENABLE_BINDABLE.get(false)) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE && table instanceof QueryableTable) {
        return EnumerableTableScan.create(cluster, this);
    }
    if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE) {
        return EnumerableTableScan.create(cluster, this);
    }
    throw new AssertionError();
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) QueryableTable(org.apache.calcite.schema.QueryableTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TranslatableTable(org.apache.calcite.schema.TranslatableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 18 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project calcite by apache.

the class JavaTypeFactoryImpl method createSyntheticType.

/**
 * Creates a synthetic Java class whose fields have the same names and
 * relational types.
 */
private Type createSyntheticType(RelRecordType type) {
    final String name = "Record" + type.getFieldCount() + "_" + syntheticTypes.size();
    final SyntheticRecordType syntheticType = new SyntheticRecordType(type, name);
    for (final RelDataTypeField recordField : type.getFieldList()) {
        final Type javaClass = getJavaClass(recordField.getType());
        syntheticType.fields.add(new RecordFieldImpl(syntheticType, recordField.getName(), javaClass, recordField.getType().isNullable() && !Primitive.is(javaClass), Modifier.PUBLIC));
    }
    return register(syntheticType);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) Type(java.lang.reflect.Type) BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) IntervalSqlType(org.apache.calcite.sql.type.IntervalSqlType) ByteString(org.apache.calcite.avatica.util.ByteString)

Example 19 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project flink by apache.

the class SqlValidatorImpl method handleScalarSubQuery.

/**
 * Processes SubQuery found in Select list. Checks that is actually Scalar sub-query and makes
 * proper entries in each of the 3 lists used to create the final rowType entry.
 *
 * @param parentSelect base SqlSelect item
 * @param selectItem child SqlSelect from select list
 * @param expandedSelectItems Select items after processing
 * @param aliasList built from user or system values
 * @param fieldList Built up entries for each select list entry
 */
private void handleScalarSubQuery(SqlSelect parentSelect, SqlSelect selectItem, List<SqlNode> expandedSelectItems, Set<String> aliasList, List<Map.Entry<String, RelDataType>> fieldList) {
    // A scalar sub-query only has one output column.
    if (1 != selectItem.getSelectList().size()) {
        throw newValidationError(selectItem, RESOURCE.onlyScalarSubQueryAllowed());
    }
    // No expansion in this routine just append to list.
    expandedSelectItems.add(selectItem);
    // Get or generate alias and add to list.
    final String alias = deriveAlias(selectItem, aliasList.size());
    aliasList.add(alias);
    final SelectScope scope = (SelectScope) getWhereScope(parentSelect);
    final RelDataType type = deriveType(scope, selectItem);
    setValidatedNodeType(selectItem, type);
    // in the sub-query select list.
    assert type instanceof RelRecordType;
    RelRecordType rec = (RelRecordType) type;
    RelDataType nodeType = rec.getFieldList().get(0).getType();
    nodeType = typeFactory.createTypeWithNullability(nodeType, true);
    fieldList.add(Pair.of(alias, nodeType));
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 20 with RelRecordType

use of org.apache.calcite.rel.type.RelRecordType in project calcite by apache.

the class SqlValidatorImpl method handleScalarSubQuery.

/**
 * Processes SubQuery found in Select list. Checks that is actually Scalar
 * sub-query and makes proper entries in each of the 3 lists used to create
 * the final rowType entry.
 *
 * @param parentSelect        base SqlSelect item
 * @param selectItem          child SqlSelect from select list
 * @param expandedSelectItems Select items after processing
 * @param aliasList           built from user or system values
 * @param fieldList           Built up entries for each select list entry
 */
private void handleScalarSubQuery(SqlSelect parentSelect, SqlSelect selectItem, List<SqlNode> expandedSelectItems, Set<String> aliasList, List<Map.Entry<String, RelDataType>> fieldList) {
    // A scalar sub-query only has one output column.
    if (1 != selectItem.getSelectList().size()) {
        throw newValidationError(selectItem, RESOURCE.onlyScalarSubQueryAllowed());
    }
    // No expansion in this routine just append to list.
    expandedSelectItems.add(selectItem);
    // Get or generate alias and add to list.
    final String alias = deriveAlias(selectItem, aliasList.size());
    aliasList.add(alias);
    final SelectScope scope = (SelectScope) getWhereScope(parentSelect);
    final RelDataType type = deriveType(scope, selectItem);
    setValidatedNodeType(selectItem, type);
    // in the sub-query select list.
    assert type instanceof RelRecordType;
    RelRecordType rec = (RelRecordType) type;
    RelDataType nodeType = rec.getFieldList().get(0).getType();
    nodeType = typeFactory.createTypeWithNullability(nodeType, true);
    fieldList.add(Pair.of(alias, nodeType));
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Aggregations

RelRecordType (org.apache.calcite.rel.type.RelRecordType)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)20 ArrayList (java.util.ArrayList)13 RexNode (org.apache.calcite.rex.RexNode)13 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)12 RelDataType (org.apache.calcite.rel.type.RelDataType)10 RelNode (org.apache.calcite.rel.RelNode)8 RexBuilder (org.apache.calcite.rex.RexBuilder)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 Prel (org.apache.drill.exec.planner.physical.Prel)4 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)4 RelDataTypeDrillImpl (org.apache.drill.exec.planner.types.RelDataTypeDrillImpl)4 RelDataTypeHolder (org.apache.drill.exec.planner.types.RelDataTypeHolder)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 Type (java.lang.reflect.Type)2 LinkedHashSet (java.util.LinkedHashSet)2 RelDataTypeField (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField)2 RelRecordType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelRecordType)2 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)2 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)2