use of org.apache.calcite.schema.ColumnStrategy 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();
}
use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.
the class RelOptTableImpl method realOrdinal.
/**
* Converts the ordinal of a field into the ordinal of a stored field.
* That is, it subtracts the number of virtual fields that come before it.
*/
public static int realOrdinal(final RelOptTable table, int i) {
List<ColumnStrategy> strategies = table.getColumnStrategies();
int n = 0;
for (int j = 0; j < i; j++) {
switch(strategies.get(j)) {
case VIRTUAL:
++n;
}
}
return i - n;
}
use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.
the class RelOptTableImpl method realRowType.
/**
* Returns the row type of a table after any {@link ColumnStrategy#VIRTUAL}
* columns have been removed. This is the type of the records that are
* actually stored.
*/
public static RelDataType realRowType(RelOptTable table) {
final RelDataType rowType = table.getRowType();
final List<ColumnStrategy> strategies = columnStrategies(table);
if (!strategies.contains(ColumnStrategy.VIRTUAL)) {
return rowType;
}
final RelDataTypeFactory.Builder builder = table.getRelOptSchema().getTypeFactory().builder();
for (RelDataTypeField field : rowType.getFieldList()) {
if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
builder.add(field);
}
}
return builder.build();
}
use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.
the class SqlValidatorImpl method checkFieldCount.
private void checkFieldCount(SqlNode node, SqlValidatorTable table, SqlNode source, RelDataType logicalSourceRowType, RelDataType logicalTargetRowType) {
final int sourceFieldCount = logicalSourceRowType.getFieldCount();
final int targetFieldCount = logicalTargetRowType.getFieldCount();
if (sourceFieldCount != targetFieldCount) {
throw newValidationError(node, RESOURCE.unmatchInsertColumn(targetFieldCount, sourceFieldCount));
}
// Ensure that non-nullable fields are targeted.
final InitializerContext rexBuilder = new InitializerContext() {
public RexBuilder getRexBuilder() {
return new RexBuilder(typeFactory);
}
public RexNode convertExpression(SqlNode e) {
throw new UnsupportedOperationException();
}
};
final List<ColumnStrategy> strategies = table.unwrap(RelOptTable.class).getColumnStrategies();
for (final RelDataTypeField field : table.getRowType().getFieldList()) {
final RelDataTypeField targetField = logicalTargetRowType.getField(field.getName(), true, false);
switch(strategies.get(field.getIndex())) {
case NOT_NULLABLE:
assert !field.getType().isNullable();
if (targetField == null) {
throw newValidationError(node, RESOURCE.columnNotNullable(field.getName()));
}
break;
case NULLABLE:
assert field.getType().isNullable();
break;
case VIRTUAL:
case STORED:
if (targetField != null && !isValuesWithDefault(source, targetField.getIndex())) {
throw newValidationError(node, RESOURCE.insertIntoAlwaysGenerated(field.getName()));
}
}
}
}
Aggregations