use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project herddb by diennea.
the class CalcitePlanner method planValues.
private PlannerOp planValues(EnumerableValues op) {
List<List<CompiledSQLExpression>> tuples = new ArrayList<>(op.getTuples().size());
RelDataType rowType = op.getRowType();
List<RelDataTypeField> fieldList = rowType.getFieldList();
Column[] columns = new Column[fieldList.size()];
for (ImmutableList<RexLiteral> tuple : op.getTuples()) {
List<CompiledSQLExpression> row = new ArrayList<>(tuple.size());
for (RexLiteral node : tuple) {
CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
row.add(exp);
}
tuples.add(row);
}
int i = 0;
String[] fieldNames = new String[fieldList.size()];
for (RelDataTypeField field : fieldList) {
Column col = Column.column(field.getName(), convertToHerdType(field.getType()));
fieldNames[i] = field.getName();
columns[i++] = col;
}
return new ValuesOp(manager.getNodeId(), fieldNames, columns, tuples);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project herddb by diennea.
the class CalcitePlanner method planEnumerableNestedLoopJoin.
private PlannerOp planEnumerableNestedLoopJoin(EnumerableNestedLoopJoin op, RelDataType rowType) {
PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
PlannerOp right = convertRelNode(op.getRight(), null, false, false);
CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
List<RelDataTypeField> fieldList = _rowType.getFieldList();
Column[] columns = new Column[fieldList.size()];
String[] fieldNames = new String[columns.length];
int i = 0;
for (RelDataTypeField field : fieldList) {
Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
fieldNames[i] = col.name;
columns[i++] = col;
}
return new NestedLoopJoinOp(fieldNames, columns, left, right, condition, op.getJoinType(), false);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class CassandraTable method query.
/**
* Executes a CQL query on the underlying table.
*
* @param session Cassandra session
* @param fields List of fields to project
* @param predicates A list of predicates which should be used in the query
* @return Enumerator of results
*/
public Enumerable<Object> query(final Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
// Build the type of the resulting row based on the provided fields
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
final RelDataType rowType = getRowType(typeFactory);
Function1<String, Void> addField = new Function1<String, Void>() {
public Void apply(String fieldName) {
SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
return null;
}
};
if (selectFields.isEmpty()) {
for (Map.Entry<String, Class> field : fields) {
addField.apply(field.getKey());
}
} else {
for (Map.Entry<String, String> field : selectFields) {
addField.apply(field.getKey());
}
}
final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
// Construct the list of fields to project
final String selectString;
if (selectFields.isEmpty()) {
selectString = "*";
} else {
selectString = Util.toString(new Iterable<String>() {
public Iterator<String> iterator() {
final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
return new Iterator<String>() {
@Override
public boolean hasNext() {
return selectIterator.hasNext();
}
@Override
public String next() {
Map.Entry<String, String> entry = selectIterator.next();
return entry.getKey() + " AS " + entry.getValue();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, "", ", ", "");
}
// Combine all predicates conjunctively
String whereClause = "";
if (!predicates.isEmpty()) {
whereClause = " WHERE ";
whereClause += Util.toString(predicates, "", " AND ", "");
}
// Build and issue the query and return an Enumerator over the results
StringBuilder queryBuilder = new StringBuilder("SELECT ");
queryBuilder.append(selectString);
queryBuilder.append(" FROM \"" + columnFamily + "\"");
queryBuilder.append(whereClause);
if (!order.isEmpty()) {
queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
}
int limit = offset;
if (fetch >= 0) {
limit += fetch;
}
if (limit > 0) {
queryBuilder.append(" LIMIT " + limit);
}
queryBuilder.append(" ALLOW FILTERING");
final String query = queryBuilder.toString();
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final ResultSet results = session.execute(query);
// Skip results until we get to the right offset
int skip = 0;
Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
while (skip < offset && enumerator.moveNext()) {
skip++;
}
return enumerator;
}
};
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class TableScanNode method createEnumerable.
private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
if (!rejectedFilters.isEmpty()) {
final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
assert filter != null;
// Re-map filter for the projects that have been applied already
final RexNode filter2;
final RelDataType inputRowType;
if (acceptedProjects == null) {
filter2 = filter;
inputRowType = rel.getRowType();
} else {
final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
filter2 = RexUtil.apply(mapping, filter);
final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
for (int acceptedProject : acceptedProjects) {
builder.add(fieldList.get(acceptedProject));
}
inputRowType = builder.build();
}
final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
final Context context = compiler.createContext();
enumerable = enumerable.where(new Predicate1<Row>() {
@Override
public boolean apply(Row row) {
context.values = row.getValues();
Boolean b = (Boolean) condition.execute(context);
return b != null && b;
}
});
}
if (rejectedProjects != null) {
enumerable = enumerable.select(new Function1<Row, Row>() {
final Object[] values = new Object[rejectedProjects.size()];
@Override
public Row apply(Row row) {
final Object[] inValues = row.getValues();
for (int i = 0; i < rejectedProjects.size(); i++) {
values[i] = inValues[rejectedProjects.get(i)];
}
return Row.asCopy(values);
}
});
}
return new TableScanNode(compiler, rel, enumerable);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.
the class JdbcSchema method getRelDataType.
RelProtoDataType getRelDataType(DatabaseMetaData metaData, String catalogName, String schemaName, String tableName) throws SQLException {
final ResultSet resultSet = metaData.getColumns(catalogName, schemaName, tableName, null);
// Temporary type factory, just for the duration of this method. Allowable
// because we're creating a proto-type, not a type; before being used, the
// proto-type will be copied into a real type factory.
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
while (resultSet.next()) {
final String columnName = resultSet.getString(4);
final int dataType = resultSet.getInt(5);
final String typeString = resultSet.getString(6);
final int precision;
final int scale;
switch(SqlType.valueOf(dataType)) {
case TIMESTAMP:
case TIME:
// SCALE
precision = resultSet.getInt(9);
scale = 0;
break;
default:
// SIZE
precision = resultSet.getInt(7);
// SCALE
scale = resultSet.getInt(9);
break;
}
RelDataType sqlType = sqlType(typeFactory, dataType, precision, scale, typeString);
boolean nullable = resultSet.getInt(11) != DatabaseMetaData.columnNoNulls;
fieldInfo.add(columnName, sqlType).nullable(nullable);
}
resultSet.close();
return RelDataTypeImpl.proto(fieldInfo.build());
}
Aggregations