Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#createStructType()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#createStructType() . 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: ValuesRel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust the row type to remove ANY types - derive type from the literals
 *
 * @param typeFactory       RelDataTypeFactory used to create the RelDataType
 * @param rowType           Row type
 * @param tuples            RexLiterals for the Values Rel
 *
 * @return the derived RelDataType from literal.
 */
private static RelDataType adjustRowType(final RelDataTypeFactory typeFactory, final RelDataType rowType,
                                         final ImmutableList<ImmutableList<RexLiteral>> tuples) {
  final int inFieldCount = rowType.getFieldCount();
  List<RelDataType> fieldTypes = Lists.newArrayListWithExpectedSize(inFieldCount);
  List<String> fieldNames = Lists.newArrayListWithExpectedSize(inFieldCount);

  boolean changed = false;
  int i = 0;
  for (final RelDataTypeField field : rowType.getFieldList()) {
    final SqlTypeName sqlTypeName = field.getValue().getSqlTypeName();
    if (sqlTypeName == SqlTypeName.ANY) {
      fieldTypes.add(getFieldTypeFromInput(typeFactory, i, tuples));
      changed = true;
    } else {
      fieldTypes.add(field.getType());
    }
    fieldNames.add(field.getName());
  }

  if (!changed) {
    return rowType;
  }

  return typeFactory.createStructType(fieldTypes, fieldNames);
}
 
Example 2
Source File: RecordDataType.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * This method constructs a {@link org.apache.calcite.rel.type.RelDataType} based on the
 * {@link org.apache.drill.exec.store.RecordDataType}'s field sql types and field names.
 *
 * @param factory helps construct a {@link org.apache.calcite.rel.type.RelDataType}
 * @return the constructed type
 */
public final RelDataType getRowType(RelDataTypeFactory factory) {
  final List<SimpleImmutableEntry<SqlTypeName, Boolean>> types = getFieldSqlTypeNames();
  final List<String> names = getFieldNames();
  final List<RelDataType> fields = new ArrayList<>();
  for (SimpleImmutableEntry<SqlTypeName, Boolean> sqlTypePair : types) {
    final SqlTypeName typeName = sqlTypePair.getKey();
    final RelDataType tempDataType;
    switch (typeName) {
      case VARCHAR:
        tempDataType = factory.createSqlType(typeName, Integer.MAX_VALUE);
        break;
      default:
        tempDataType = factory.createSqlType(typeName);
    }
    //Add [Non]Nullable RelDataType
    fields.add(factory.createTypeWithNullability(tempDataType, sqlTypePair.getValue()));
  }
  return factory.createStructType(fields, names);
}
 
Example 3
Source File: JoinUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static RelDataType rowTypeFromProjected(RelNode left, RelNode right, RelDataType curRowType, ImmutableBitSet projected, RelDataTypeFactory typeFactory) {
  int leftSize = left.getRowType().getFieldCount();
  int rightSize = right.getRowType().getFieldCount();
  List<RelDataTypeField> fields = curRowType.getFieldList();
  if (projected.asSet().size() != fields.size()) {;
    List<RelDataType> dataTypes = new ArrayList<>();
    List<String> fieldNames = new ArrayList<>();
    for (int i = 0; i < fields.size(); i++) {
      if (projected.get(i)) {
        RelDataTypeField field = fields.get(i);
        dataTypes.add(field.getType());
        fieldNames.add(field.getName());
      }
    }
    return typeFactory.createStructType(dataTypes, fieldNames);
  }
  return curRowType;
}
 
Example 4
Source File: ProjectPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Prel prepareForLateralUnnestPipeline(List<RelNode> children) {
  RelDataTypeFactory typeFactory = this.getCluster().getTypeFactory();
  RexBuilder builder = this.getCluster().getRexBuilder();
  List<RexNode> projects = Lists.newArrayList();
  projects.add(builder.makeInputRef(typeFactory.createSqlType(SqlTypeName.INTEGER), 0));
  // right shift the previous field indices.
  projects.addAll(DrillRelOptUtil.transformExprs(builder, this.getProjects(),
                      DrillRelOptUtil.rightShiftColsInRowType(this.getInput().getRowType())));

  List<String> fieldNames = new ArrayList<>();
  List<RelDataType> fieldTypes = new ArrayList<>();

  fieldNames.add("$drill_implicit_field$");
  fieldTypes.add(typeFactory.createSqlType(SqlTypeName.INTEGER));

  for (RelDataTypeField field : this.rowType.getFieldList()) {
    fieldNames.add(field.getName());
    fieldTypes.add(field.getType());
  }

  RelDataType newRowType = typeFactory.createStructType(fieldTypes, fieldNames);

  return (Prel) this.copy(this.getTraitSet(), children.get(0), projects, newRowType);
}
 
Example 5
Source File: UnnestPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Prel prepareForLateralUnnestPipeline(List<RelNode> children) {
  RelDataTypeFactory typeFactory = this.getCluster().getTypeFactory();
  List<String> fieldNames = new ArrayList<>();
  List<RelDataType> fieldTypes = new ArrayList<>();

  fieldNames.add(IMPLICIT_COLUMN);
  fieldTypes.add(typeFactory.createSqlType(SqlTypeName.INTEGER));

  for (RelDataTypeField field : this.rowType.getFieldList()) {
    fieldNames.add(field.getName());
    fieldTypes.add(field.getType());
  }

  RelDataType newRowType = typeFactory.createStructType(fieldTypes, fieldNames);
  return new UnnestPrel(this.getCluster(), this.getTraitSet(), newRowType, ref);
}
 
Example 6
Source File: TestMaterializationExpander.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * row types with TIMESTAMP fields with different precisions should not be equal
 */
@Test
public void testTimestampPrecisionMismatch() {
  // Don't use Dremio's type factory as it enforces a precision of 3
  final RelDataTypeFactory calciteFactory = new org.apache.calcite.jdbc.JavaTypeFactoryImpl();
  final RelDataType type1 = calciteFactory.createStructType(
    Collections.singletonList(calciteFactory.createSqlType(SqlTypeName.TIMESTAMP, 0)),
    Collections.singletonList("ts0")
  );

  final RelDataType type2 = typeFactory.createStructType(
    Collections.singletonList(typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 3)),
    Collections.singletonList("ts3")
  );

  Assert.assertFalse(MaterializationExpander.areRowTypesEqual(type1, type2));
}
 
Example 7
Source File: View.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory factory) {

    List<RelDataType> types = Lists.newArrayList();
    List<String> names = Lists.newArrayList();

    for (FieldType field : fields) {
      names.add(field.getName());
      RelDataType type;
      if (   SqlTypeFamily.INTERVAL_YEAR_MONTH == field.getType().getFamily()
          || SqlTypeFamily.INTERVAL_DAY_TIME   == field.getType().getFamily() ) {
       type = factory.createSqlIntervalType( field.getIntervalQualifier() );
      } else if (field.getType().equals(SqlTypeName.ARRAY) || field.getType().equals(SqlTypeName.MAP)) {
        type = factory.createSqlType(SqlTypeName.ANY);
      } else if (field.getPrecision() == null && field.getScale() == null) {
        type = factory.createSqlType(field.getType());
      } else if (field.getPrecision() != null && field.getScale() == null) {
        type = factory.createSqlType(field.getType(), field.getPrecision());
      } else {
        type = factory.createSqlType(field.getType(), field.getPrecision(), field.getScale());
      }

      if (field.getIsNullable()) {
        types.add(factory.createTypeWithNullability(type, true));
      } else {
        types.add(type);
      }
    }
    return factory.createStructType(types, names);
  }
 
Example 8
Source File: ExtendedSqlRowTypeNameSpec.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator sqlValidator) {
	final RelDataTypeFactory typeFactory = sqlValidator.getTypeFactory();
	return typeFactory.createStructType(
		fieldTypes.stream()
			.map(dt -> dt.deriveType(sqlValidator))
			.collect(Collectors.toList()),
		fieldNames.stream()
			.map(SqlIdentifier::toString)
			.collect(Collectors.toList()));
}
 
Example 9
Source File: GremlinTable.java    From sql-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory relDataTypeFactory) {
    List<String> names = new ArrayList<>();
    List<RelDataType> types = new ArrayList<>();

    for(Map.Entry<String, TableColumn> entry : tableDef.columns.entrySet()) {
        names.add(entry.getKey());
        types.add(relDataTypeFactory.createJavaType(
                getType(entry.getValue().getType())));
    }

    return relDataTypeFactory.createStructType(Pair.zip(names, types));
}
 
Example 10
Source File: BulletinTable.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
    if (relDataType != null) {
        return relDataType;
    }

    final List<String> names = Arrays.asList(
            "bulletinId",
            "bulletinCategory",
            "bulletinGroupId",
            "bulletinGroupName",
            "bulletinGroupPath",
            "bulletinLevel",
            "bulletinMessage",
            "bulletinNodeAddress",
            "bulletinNodeId",
            "bulletinSourceId",
            "bulletinSourceName",
            "bulletinSourceType",
            "bulletinTimestamp"
    );
    final List<RelDataType> types = Arrays.asList(
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(Date.class)
    );

    relDataType = typeFactory.createStructType(Pair.zip(names, types));
    return relDataType;
}
 
Example 11
Source File: InfoSchemaTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {

    // Convert the array of Drill types to an array of Optiq types
    List<RelDataType> relTypes = Lists.newArrayList();
    List<String> fieldNames = Lists.newArrayList();
    for (Field field : fields) {
      relTypes.add(getRelDataType(typeFactory, field.type));
      fieldNames.add(field.name);
    }

    return typeFactory.createStructType(relTypes, fieldNames);
  }
 
Example 12
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Records a struct type with no fields.
 *
 * @param typeFactory Type factory
 * @return Struct type with no fields
 */
public static RelDataType createEmptyStructType(
    RelDataTypeFactory typeFactory) {
  return typeFactory.createStructType(
      ImmutableList.of(),
      ImmutableList.of());
}
 
Example 13
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Flattens a record type by recursively expanding any fields which are
 * themselves record types. For each record type, a representative null
 * value field is also prepended (with state NULL for a null value and FALSE
 * for non-null), and all component types are asserted to be nullable, since
 * SQL doesn't allow NOT NULL to be specified on attributes.
 *
 * @param typeFactory   factory which should produced flattened type
 * @param recordType    type with possible nesting
 * @param flatteningMap if non-null, receives map from unflattened ordinal
 *                      to flattened ordinal (must have length at least
 *                      recordType.getFieldList().size())
 * @return flattened equivalent
 */
public static RelDataType flattenRecordType(
    RelDataTypeFactory typeFactory,
    RelDataType recordType,
    int[] flatteningMap) {
  if (!recordType.isStruct()) {
    return recordType;
  }
  List<RelDataTypeField> fieldList = new ArrayList<>();
  boolean nested =
      flattenFields(
          typeFactory,
          recordType,
          fieldList,
          flatteningMap);
  if (!nested) {
    return recordType;
  }
  List<RelDataType> types = new ArrayList<>();
  List<String> fieldNames = new ArrayList<>();
  Map<String, Long> fieldCnt = fieldList.stream()
      .map(RelDataTypeField::getName)
      .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  int i = -1;
  for (RelDataTypeField field : fieldList) {
    ++i;
    types.add(field.getType());
    String oriFieldName = field.getName();
    // Patch up the field name with index if there are duplicates.
    // There is still possibility that the patched name conflicts with existing ones,
    // but that should be rare case.
    String fieldName = fieldCnt.get(oriFieldName) > 1
        ? oriFieldName + "_" + i
        : oriFieldName;
    fieldNames.add(fieldName);
  }
  return typeFactory.createStructType(types, fieldNames);
}
 
Example 14
Source File: StarTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  final List<RelDataType> typeList = new ArrayList<>();
  final List<Integer> fieldCounts = new ArrayList<>();
  for (Table table : tables) {
    final RelDataType rowType = table.getRowType(typeFactory);
    typeList.addAll(RelOptUtil.getFieldTypeList(rowType));
    fieldCounts.add(rowType.getFieldCount());
  }
  // Compute fieldCounts the first time this method is called. Safe to assume
  // that the field counts will be the same whichever type factory is used.
  if (this.fieldCounts == null) {
    this.fieldCounts = ImmutableIntList.copyOf(fieldCounts);
  }
  return typeFactory.createStructType(typeList, lattice.uniqueColumnNames());
}
 
Example 15
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Table extend(final List<RelDataTypeField> fields) {
  return new ModifiableTable(Util.last(names)) {
    @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      ImmutableList<RelDataTypeField> allFields = ImmutableList.copyOf(
          Iterables.concat(
              ModifiableTable.this.getRowType(typeFactory).getFieldList(),
              fields));
      return typeFactory.createStructType(allFields);
    }
  };
}
 
Example 16
Source File: SqlRowTypeNameSpec.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator sqlValidator) {
  final RelDataTypeFactory typeFactory = sqlValidator.getTypeFactory();
  return typeFactory.createStructType(
      fieldTypes.stream()
          .map(dt -> dt.deriveType(sqlValidator))
          .collect(Collectors.toList()),
      fieldNames.stream()
          .map(SqlIdentifier::toString)
          .collect(Collectors.toList()));
}
 
Example 17
Source File: FlinkSqlDataTypeSpec.java    From flink with Apache License 2.0 4 votes vote down vote up
private RelDataType getExtendedType(RelDataTypeFactory typeFactory, SqlIdentifier typeName) {
	// quick check.
	if (!(typeName instanceof ExtendedSqlType)) {
		return null;
	}
	if (typeName instanceof SqlBytesType) {
		return typeFactory.createSqlType(SqlTypeName.VARBINARY, Integer.MAX_VALUE);
	} else if (typeName instanceof SqlStringType) {
		return typeFactory.createSqlType(SqlTypeName.VARCHAR, Integer.MAX_VALUE);
	} else if (typeName instanceof SqlArrayType) {
		final SqlArrayType arrayType = (SqlArrayType) typeName;
		return typeFactory.createArrayType(arrayType.getElementType()
			.deriveType(typeFactory), -1);
	} else if (typeName instanceof SqlMultisetType) {
		final SqlMultisetType multiSetType = (SqlMultisetType) typeName;
		return typeFactory.createMultisetType(multiSetType.getElementType()
			.deriveType(typeFactory), -1);
	} else if (typeName instanceof SqlMapType) {
		final SqlMapType mapType = (SqlMapType) typeName;
		return typeFactory.createMapType(
			mapType.getKeyType().deriveType(typeFactory),
			mapType.getValType().deriveType(typeFactory));
	} else if (typeName instanceof SqlRowType) {
		final SqlRowType rowType = (SqlRowType) typeName;
		return typeFactory.createStructType(
			rowType.getFieldTypes().stream().map(ft -> ft.deriveType(typeFactory))
				.collect(Collectors.toList()),
			rowType.getFieldNames().stream().map(SqlIdentifier::getSimple)
				.collect(Collectors.toList()));
	} else if (typeName instanceof SqlTimeType) {
		final SqlTimeType zonedTimeType = (SqlTimeType) typeName;
		if (zonedTimeType.getPrecision() >= 0) {
			return typeFactory.createSqlType(zonedTimeType.getSqlTypeName(),
				zonedTimeType.getPrecision());
		} else {
			// Use default precision.
			return typeFactory.createSqlType(zonedTimeType.getSqlTypeName());
		}
	} else if (typeName instanceof SqlTimestampType) {
		final SqlTimestampType zonedTimestampType = (SqlTimestampType) typeName;
		if (zonedTimestampType.getPrecision() >= 0) {
			return typeFactory.createSqlType(zonedTimestampType.getSqlTypeName(),
				zonedTimestampType.getPrecision());
		} else {
			// Use default precision.
			return typeFactory.createSqlType(zonedTimestampType.getSqlTypeName());
		}
	}
	return null;
}
 
Example 18
Source File: ConnectionStatusTable.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
    if (relDataType != null) {
        return relDataType;
    }

    final List<String> names = Arrays.asList(
            "id",
            "groupId",
            "name",
            "sourceId",
            "sourceName",
            "destinationId",
            "destinationName",
            "backPressureDataSizeThreshold",
            "backPressureBytesThreshold",
            "backPressureObjectThreshold",
            "isBackPressureEnabled",
            "inputCount",
            "inputBytes",
            "queuedCount",
            "queuedBytes",
            "outputCount",
            "outputBytes",
            "maxQueuedCount",
            "maxQueuedBytes"
    );
    final List<RelDataType> types = Arrays.asList(
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(String.class),
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(boolean.class),
            typeFactory.createJavaType(int.class),
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(int.class),
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(int.class),
            typeFactory.createJavaType(long.class),
            typeFactory.createJavaType(int.class),
            typeFactory.createJavaType(long.class)
    );

    relDataType = typeFactory.createStructType(Pair.zip(names, types));
    return relDataType;
}
 
Example 19
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  return typeFactory.createStructType(MockTable.this.getRowType().getFieldList());
}
 
Example 20
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Creates new row type based on stores types and field names.
 *
 * @param factory factory for data type descriptors.
 * @return new row type
 */
public RelDataType createNewRowType(RelDataTypeFactory factory) {
    return factory.createStructType(types, fieldNames);
}