Skip to content

Instantly share code, notes, and snippets.

View masayuki038's full-sized avatar

Masayuki Takahashi masayuki038

View GitHub Profile
@Override
public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable) {
int fieldCount = relOptTable.getRowType().getFieldCount();
Integer[] fields = AvroEnumerator.identityList(fieldCount);
return new AvroTableScan(context.getCluster(), relOptTable, fields);
}
public Enumerable<Object> project(DataContext root, Integer[] fields) {
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
return new AvroEnumerator(records, fields);
}
};
}
public class AvroEnumerator implements Enumerator<Object> {
private List<GenericData.Record> records;
private Integer[] fields;
private int pos;
public AvroEnumerator(List<GenericData.Record> records, Integer[] fields) {
this.records = records;
this.fields = fields;
this.pos = -1;
public class AvroTableScan extends TableScan implements EnumerableRel {
private Integer[] fields;
@Override
public RelDataType deriveRowType() {
List<RelDataTypeField> fieldList = getTable().getRowType().getFieldList();
RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
Arrays.stream(fields).forEach(field -> builder.add(fieldList.get(field)));
return builder.build();
}
@Override
public void register(RelOptPlanner planner) {
planner.addRule(AvroProjectTableScanRule.INSTANCE);
}
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
return implementor.result(physType, Blocks.toBlock(
Expressions.call(
this.table.getExpression(AvroTable.class),
"project",
implementor.getRootExpression(),
Expressions.constant(this.fields)
)
public class AvroProjectTableScanRule extends RelOptRule {
static final AvroProjectTableScanRule INSTANCE = new AvroProjectTableScanRule();
public AvroProjectTableScanRule() {
super(RelOptRule.operand(
LogicalProject.class,
RelOptRule.operand(AvroTableScan.class, RelOptRule.none())
), "AvroProjectTableScanRule");
}
@Override
public void onMatch(RelOptRuleCall call) {
LogicalProject project = call.rel(0);
AvroTableScan scan = call.rel(1);
Integer[] fields = getProjectFields(project.getProjects());
call.transformTo(
new AvroTableScan(scan.getCluster(), scan.getTable(), fields)
);
}
public class JdbcTest {
private final static String CONNECTION_URL = "jdbc:calcite:model=target/test-classes/model.json";
@BeforeClass
public static void setUpOnce() throws ClassNotFoundException {
Class.forName("org.apache.calcite.jdbc.Driver");
}
@Test