This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @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); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public Enumerable<Object> project(DataContext root, Integer[] fields) { | |
| return new AbstractEnumerable<Object>() { | |
| public Enumerator<Object> enumerator() { | |
| return new AvroEnumerator(records, fields); | |
| } | |
| }; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class AvroTableScan extends TableScan implements EnumerableRel { | |
| private Integer[] fields; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @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(); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @Override | |
| public void register(RelOptPlanner planner) { | |
| planner.addRule(AvroProjectTableScanRule.INSTANCE); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @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) | |
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @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) | |
| ); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 |