Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#FieldInfoBuilder

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#FieldInfoBuilder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexIntersectPlanGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelNode buildOriginalProject (RelNode newRel) {
  RelDataType origRowType = origProject == null ? origScan.getRowType() : origProject.getRowType();

  final RelDataTypeFactory.FieldInfoBuilder finalFieldTypeBuilder =
      origScan.getCluster().getTypeFactory().builder();

  List<RelDataTypeField> hjRowFields = newRel.getRowType().getFieldList();
  int toRemoveRowKeyCount = 1;
  if (getRowKeyIndex(origRowType, origScan)  < 0 ) {
    toRemoveRowKeyCount = 2;
  }
  finalFieldTypeBuilder.addAll(hjRowFields.subList(0, hjRowFields.size()-toRemoveRowKeyCount));
  final RelDataType finalProjectRowType = finalFieldTypeBuilder.build();

  List<RexNode> resetExprs = Lists.newArrayList();
  for (int idx=0; idx<hjRowFields.size()-toRemoveRowKeyCount; ++idx) {
    resetExprs.add(RexInputRef.of(idx, newRel.getRowType()));
  }

  final ProjectPrel resetProjectPrel = new ProjectPrel(newRel.getCluster(), newRel.getTraitSet(),
      newRel, resetExprs, finalProjectRowType);
  newRel = resetProjectPrel;

  RelNode finalRel = Prule.convert(newRel, newRel.getTraitSet());
  return finalRel;
}
 
Example 2
Source File: IndexIntersectPlanGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelNode buildRowKeyProject(RelNode inputRel, int fieldIndex) {
  List<RelDataTypeField> inputFields = inputRel.getRowType().getFieldList();
  final RelDataTypeField rowKeyField = inputFields.get(fieldIndex);
    RexNode expr = builder.makeInputRef(rowKeyField.getType(), rowKeyField.getIndex());
  List<RexNode> exprs = Lists.newArrayList();
  exprs.add(expr);

  final RelDataTypeFactory.FieldInfoBuilder rightFieldTypeBuilder =
      inputRel.getCluster().getTypeFactory().builder();

  rightFieldTypeBuilder.add(rowKeyField);
  final RelDataType projectRowType = rightFieldTypeBuilder.build();

  ProjectPrel proj = new ProjectPrel(inputRel.getCluster(), inputRel.getTraitSet(), inputRel, exprs, projectRowType);

  return proj;
}
 
Example 3
Source File: StreamEndpoint.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory)
{
  RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
  if (fieldMapping != null) {
    for (Map.Entry<String, Class> entry : fieldMapping.entrySet()) {
      builder.add(entry.getKey(), convertField(typeFactory, entry.getValue()));
    }
  } else if (pojoClass != null) {
    for (Field field : pojoClass.getDeclaredFields()) {
      builder.add(field.getName(), convertField(typeFactory, field.getType()));
    }
  } else {
    throw new RuntimeException("Either fieldMapping or pojoClass needs to be set.");
  }

  return builder.build();
}
 
Example 4
Source File: ReflectionUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static RelNode removeColumns(RelNode node, Predicate<RelDataTypeField> predicate) {
  if (node.getTraitSet() == null) {
    // for test purposes.
    return node;
  }

  // identify all fields that match passed predicate
  Set<RelDataTypeField> toRemove = FluentIterable.from(node.getRowType().getFieldList()).filter(predicate).toSet();

  if (toRemove.isEmpty()) {
    return node;
  }

  final RexBuilder rexBuilder = node.getCluster().getRexBuilder();
  final RelDataTypeFactory.FieldInfoBuilder rowTypeBuilder = new RelDataTypeFactory.FieldInfoBuilder(node.getCluster().getTypeFactory());
  final List<RexNode> projects = FluentIterable.from(node.getRowType().getFieldList())
    .filter(Predicates.not(toRemove::contains))
    .transform((RelDataTypeField field) -> {
      rowTypeBuilder.add(field);
      return (RexNode) rexBuilder.makeInputRef(field.getType(), field.getIndex());
    }).toList();

  return new LogicalProject(node.getCluster(), node.getTraitSet(), node, projects, rowTypeBuilder.build());
}
 
Example 5
Source File: TableFunctions.java    From mat-calcite-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory relDataTypeFactory) {
    RelDataTypeFactory.FieldInfoBuilder builder = relDataTypeFactory.builder();
    for (String columnName : columnNames) {
        builder.add(columnName, relDataTypeFactory.createSqlType(SqlTypeName.ANY));
    }
    return builder.build();
}
 
Example 6
Source File: SnapshotThreadStacksTable.java    From mat-calcite-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
    RelDataType anyType = typeFactory.createSqlType(SqlTypeName.ANY);
    builder.add("thread", anyType);
    builder.add("depth", typeFactory.createJavaType(int.class));
    builder.add("text", typeFactory.createJavaType(String.class));
    builder.add("objects", typeFactory.createMultisetType(anyType, -1));
    return builder.build();
}
 
Example 7
Source File: OLAPTable.java    From kylin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private RelDataType deriveRowType(RelDataTypeFactory typeFactory) {
    RelDataTypeFactory.FieldInfoBuilder fieldInfo = typeFactory.builder();
    for (ColumnDesc column : sourceColumns) {
        RelDataType sqlType = createSqlType(typeFactory, column.getUpgradedType(), column.isNullable());
        sqlType = SqlTypeUtil.addCharsetAndCollation(sqlType, typeFactory);
        fieldInfo.add(column.getName(), sqlType);
    }
    return typeFactory.createStructType(fieldInfo);
}
 
Example 8
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.FieldInfoBuilder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return getCluster().getTypeFactory().createStructType(builder);
}
 
Example 9
Source File: OLAPNonEquiJoinRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void implementRewrite(RewriteImplementor implementor) {
    implementor.visitChild(this, this.left);
    implementor.visitChild(this, this.right);

    this.rowType = this.deriveRowType();
    if (this.isTopJoin) {

        // add dynamic field
        Map<TblColRef, RelDataType> dynFields = this.context.dynamicFields;
        if (!dynFields.isEmpty()) {
            List<TblColRef> newCols = Lists.newArrayList(this.columnRowType.getAllColumns());
            List<RelDataTypeField> newFieldList = Lists.newArrayList();
            int paramIndex = this.rowType.getFieldList().size();
            for (TblColRef fieldCol : dynFields.keySet()) {
                RelDataType fieldType = dynFields.get(fieldCol);

                RelDataTypeField newField = new RelDataTypeFieldImpl(fieldCol.getName(), paramIndex++, fieldType);
                newFieldList.add(newField);

                newCols.add(fieldCol);
            }

            // rebuild row type
            RelDataTypeFactory.FieldInfoBuilder fieldInfo = getCluster().getTypeFactory().builder();
            fieldInfo.addAll(this.rowType.getFieldList());
            fieldInfo.addAll(newFieldList);
            this.rowType = getCluster().getTypeFactory().createStructType(fieldInfo);

            this.columnRowType = new ColumnRowType(newCols);
        }
    }
}
 
Example 10
Source File: CSVMessageFormat.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory)
{
  String schema = (String)operands.get(CSV_SCHEMA);
  RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();

  DelimitedSchema delimitedSchema = new DelimitedSchema(schema);
  for (DelimitedSchema.Field field : delimitedSchema.getFields()) {
    builder.add(field.getName(), convertField(typeFactory, field.getType()));
  }

  return builder.build();
}
 
Example 11
Source File: GremlinTableScan.java    From sql-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.FieldInfoBuilder builder =
            getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 12
Source File: PrelWithDictionaryInfo.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
static RelDataType toRowDataType(Collection<RelDataTypeField> fields, RelDataTypeFactory factory) {
  final RelDataTypeFactory.FieldInfoBuilder builder = new RelDataTypeFactory.FieldInfoBuilder(factory);
  for (RelDataTypeField field: fields) {
    builder.add(field);
  }
  return builder.build();
}
 
Example 13
Source File: JournalledJdbcSchema.java    From calcite-sql-rewriter with Apache License 2.0 5 votes vote down vote up
@Override
RelProtoDataType getRelDataType(
		DatabaseMetaData metaData,
		String catalogName,
		String schemaName,
		String tableName
) throws SQLException {
	if (journalledTableKeys.containsKey(tableName)) {
		// 1: Find columns for journal table
		RelDataType relDataType = super
				.getRelDataType(metaData, catalogName, schemaName, journalNameFor(tableName))
				.apply(new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT) {
					@Override
					public RelDataType copyType(RelDataType type) {
						return type;
					}
				});

		RelDataTypeFactory.FieldInfoBuilder fieldInfo = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT).builder();

		// 2: Filter out journal-implementation columns
		for (RelDataTypeField field : relDataType.getFieldList()) {
			String fieldName = field.getName();
			if (fieldName.equals(versionField) || fieldName.equals(subsequentVersionField)) {
				continue;
			}
			fieldInfo.add(field);
		}

		return RelDataTypeImpl.proto(fieldInfo.build());
	} else {
		return super.getRelDataType(metaData, catalogName, schemaName, tableName);
	}
}
 
Example 14
Source File: JSqlTable.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
	if (dataType == null) {
		RelDataTypeFactory.FieldInfoBuilder fieldInfo = typeFactory.builder();
		for (JSqlMapData.Column column : this.sourceTable.columns) {
			RelDataType sqlType = typeFactory.createJavaType(JSqlMapData.JAVATYPE_MAPPING.get(column.type));
			sqlType = SqlTypeUtil.addCharsetAndCollation(sqlType, typeFactory);
			fieldInfo.add(column.name, sqlType);
		}
		this.dataType = typeFactory.createStructType(fieldInfo);
	}
	return this.dataType;
}
 
Example 15
Source File: OLAPTable.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private RelDataType deriveRowType(RelDataTypeFactory typeFactory) {
    RelDataTypeFactory.FieldInfoBuilder fieldInfo = typeFactory.builder();
    for (ColumnDesc column : sourceColumns) {
        RelDataType sqlType = createSqlType(typeFactory, column.getUpgradedType(), column.isNullable());
        sqlType = SqlTypeUtil.addCharsetAndCollation(sqlType, typeFactory);
        fieldInfo.add(column.getName(), sqlType);
    }
    return typeFactory.createStructType(fieldInfo);
}
 
Example 16
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.FieldInfoBuilder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return getCluster().getTypeFactory().createStructType(builder);
}
 
Example 17
Source File: ElasticsearchTable.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public RelDataType[] getRowTypeFromEs(RelDataTypeFactory typeFactory) throws IOException {
    RelDataType[] relDataTypes = new RelDataType[2];
    RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
    RelDataTypeFactory.FieldInfoBuilder builder1 = typeFactory.builder();
    GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings(esIndexName).get();
    MappingMetaData typeMapping = getMappingsResponse.getMappings().get(esIndexName).get(esTypeName);
    //{"person":{"properties":{"age":{"type":"integer"},"name":{"type":"text","fields":{"raw":{"type":"keyword"}}}}}}
    String json = typeMapping.source().string();
    Map<String, Object> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>() {
    });
    Map<String, Object> properties = ((Map<String, Map<String, Object>>) map.get(esTypeName)).get("properties");
    int index = 0;
    int index1 = 0;
    //(base-field-name, field-map)
    Stack<Pair<String, Map<String, Object>>> mapStack = new Stack<>();
    mapStack.push(Pair.of(null, properties));
    while (!mapStack.isEmpty()) {
        Pair<String, Map<String, Object>> pair = mapStack.pop();
        String baseFieldName = pair.left;
        for (Map.Entry<String, Object> entry : pair.right.entrySet()) {
            String name = entry.getKey().toUpperCase();
            if (baseFieldName != null) name = baseFieldName + "." + name;

            Map<String, Object> fieldMap = (Map<String, Object>) entry.getValue();
            String type = fieldMap.get("type") != null ? fieldMap.get("type").toString() : null;
            if (type == null)
                throw new IllegalStateException(String.format("type of elasticsearch field '%s' is null", name));
            builder.add(new RelDataTypeFieldImpl(name, index++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));

            if(!name.endsWith(".KEYWORD")){
                builder1.add(new RelDataTypeFieldImpl(name, index1++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));
            }
            //multi-field, that means containing 'fields' attribute
            Map<String, Object> moreFields = fieldMap.get("fields") != null ? (Map<String, Object>) fieldMap.get("fields") : null;
            if (moreFields != null) mapStack.push(Pair.of(name, moreFields));
        }
    }
    relDataTypes[0] = builder.build();
    relDataTypes[1] = builder1.build();
    return relDataTypes;
}
 
Example 18
Source File: ElasticsearchTable.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public RelDataType[] getRowTypeFromEs(RelDataTypeFactory typeFactory) throws IOException {
    RelDataType[] relDataTypes = new RelDataType[2];
    RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
    RelDataTypeFactory.FieldInfoBuilder builder1 = typeFactory.builder();
    GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings(esIndexName).get();
    MappingMetaData typeMapping = getMappingsResponse.getMappings().get(esIndexName).get(esTypeName);
    //{"person":{"properties":{"age":{"type":"integer"},"name":{"type":"text","fields":{"raw":{"type":"keyword"}}}}}}
    String json = typeMapping.source().string();
    Map<String, Object> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>() {
    });
    Map<String, Object> properties = ((Map<String, Map<String, Object>>) map.get(esTypeName)).get("properties");
    int index = 0;
    int index1 = 0;
    //(base-field-name, field-map)
    Stack<Pair<String, Map<String, Object>>> mapStack = new Stack<>();
    mapStack.push(Pair.of(null, properties));
    while (!mapStack.isEmpty()) {
        Pair<String, Map<String, Object>> pair = mapStack.pop();
        String baseFieldName = pair.left;
        for (Map.Entry<String, Object> entry : pair.right.entrySet()) {
            String name = entry.getKey().toUpperCase();
            if (baseFieldName != null) name = baseFieldName + "." + name;

            Map<String, Object> fieldMap = (Map<String, Object>) entry.getValue();
            String type = fieldMap.get("type") != null ? fieldMap.get("type").toString() : null;
            if (type == null)
                throw new IllegalStateException(String.format("type of elasticsearch field '%s' is null", name));
            builder.add(new RelDataTypeFieldImpl(name, index++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));

            if(!name.endsWith(".KEYWORD")){
                builder1.add(new RelDataTypeFieldImpl(name, index1++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));
            }
            //multi-field, that means containing 'fields' attribute
            Map<String, Object> moreFields = fieldMap.get("fields") != null ? (Map<String, Object>) fieldMap.get("fields") : null;
            if (moreFields != null) mapStack.push(Pair.of(name, moreFields));
        }
    }
    relDataTypes[0] = builder.build();
    relDataTypes[1] = builder1.build();
    return relDataTypes;
}
 
Example 19
Source File: IndexIntersectPlanGenerator.java    From Bats with Apache License 2.0 4 votes vote down vote up
private Pair<RelNode, DbGroupScan> buildRestrictedDBScan(RexNode remnant,
    boolean isAnyIndexAsync) {

  DbGroupScan origDbGroupScan = (DbGroupScan)IndexPlanUtils.getGroupScan(origScan);
  List<SchemaPath> cols = new ArrayList<SchemaPath>(origDbGroupScan.getColumns());
  if (!checkRowKey(cols)) {
    cols.add(origDbGroupScan.getRowKeyPath());
  }

  // Create a restricted groupscan from the primary table's groupscan
  DbGroupScan restrictedGroupScan  = origDbGroupScan.getRestrictedScan(cols);
  if (restrictedGroupScan == null) {
    logger.error("Null restricted groupscan in IndexIntersectPlanGenerator.convertChild");
    return null;
  }
  DrillDistributionTrait partition = IndexPlanUtils.scanIsPartition(IndexPlanUtils.getGroupScan(origScan))?
      DrillDistributionTrait.RANDOM_DISTRIBUTED : DrillDistributionTrait.SINGLETON;

  RelNode lastRelNode;
  RelDataType dbscanRowType = convertRowType(origScan.getRowType(), origScan.getCluster().getTypeFactory());
  ScanPrel dbScan = new ScanPrel(origScan.getCluster(),
      origScan.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(partition), restrictedGroupScan, dbscanRowType, origScan.getTable());
  lastRelNode = dbScan;
  // build the row type for the left Project
  List<RexNode> leftProjectExprs = Lists.newArrayList();
  int leftRowKeyIndex = getRowKeyIndex(dbScan.getRowType(), origScan);
  final RelDataTypeField leftRowKeyField = dbScan.getRowType().getFieldList().get(leftRowKeyIndex);
  final RelDataTypeFactory.FieldInfoBuilder leftFieldTypeBuilder =
      dbScan.getCluster().getTypeFactory().builder();

  FilterPrel leftIndexFilterPrel = null;

  // See NonCoveringIndexPlanGenerator for why we are re-applying index filter condition in case of async indexes.
  // For intersect planning, any one of the intersected indexes may be async but to keep it simple we re-apply the
  // full original condition.
  if (isAnyIndexAsync) {
    new FilterPrel(dbScan.getCluster(), dbScan.getTraitSet(),
          dbScan, indexContext.getOrigCondition());
    lastRelNode = leftIndexFilterPrel;
  }

  // new Project's rowtype is original Project's rowtype [plus rowkey if rowkey is not in original rowtype]
  ProjectPrel leftIndexProjectPrel = null;
  if (origProject != null) {
    RelDataType origRowType = origProject.getRowType();
    List<RelDataTypeField> origProjFields = origRowType.getFieldList();
    leftFieldTypeBuilder.addAll(origProjFields);
    // get the exprs from the original Project IFF there is a project

    leftProjectExprs.addAll(IndexPlanUtils.getProjects(origProject));
    // add the rowkey IFF rowkey is not in orig scan
    if (getRowKeyIndex(origRowType, origScan) < 0) {
      leftFieldTypeBuilder.add(leftRowKeyField);
      leftProjectExprs.add(RexInputRef.of(leftRowKeyIndex, dbScan.getRowType()));
    }

    final RelDataType leftProjectRowType = leftFieldTypeBuilder.build();
    leftIndexProjectPrel = new ProjectPrel(dbScan.getCluster(), dbScan.getTraitSet(),
        leftIndexFilterPrel == null ? dbScan : leftIndexFilterPrel, leftProjectExprs, leftProjectRowType);
    lastRelNode = leftIndexProjectPrel;
  }

  final RelTraitSet leftTraits = dbScan.getTraitSet().plus(Prel.DRILL_PHYSICAL);
  // final RelNode convertedLeft = convert(leftIndexProjectPrel, leftTraits);
  final RelNode convertedLeft = Prule.convert(lastRelNode, leftTraits);

  return new Pair<>(convertedLeft, restrictedGroupScan);
}