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));
}
}
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();
}
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);
}
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));
}
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));
}
Aggregations