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

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#Builder . 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: WithItemNamespace.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override protected RelDataType validateImpl(RelDataType targetRowType) {
  final SqlValidatorNamespace childNs =
      validator.getNamespace(withItem.query);
  final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
  if (withItem.columnList == null) {
    return rowType;
  }
  final RelDataTypeFactory.Builder builder =
      validator.getTypeFactory().builder();
  for (Pair<SqlNode, RelDataTypeField> pair
      : Pair.zip(withItem.columnList, rowType.getFieldList())) {
    builder.add(((SqlIdentifier) pair.left).getSimple(),
        pair.right.getType());
  }
  return builder.build();
}
 
Example 2
Source File: RelBuilder.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Ensures that the field names match those given.
 *
 * <p>If all fields have the same name, adds nothing;
 * if any fields do not have the same name, adds a {@link Project}.
 *
 * <p>Note that the names can be short-lived. Other {@code RelBuilder}
 * operations make no guarantees about the field names of the rows they
 * produce.
 *
 * @param fieldNames List of desired field names; may contain null values or
 * have fewer fields than the current row type
 */
public RelBuilder rename(List<String> fieldNames) {
    final List<String> oldFieldNames = peek().getRowType().getFieldNames();
    Preconditions.checkArgument(fieldNames.size() <= oldFieldNames.size(), "More names than fields");
    final List<String> newFieldNames = new ArrayList<>(oldFieldNames);
    for (int i = 0; i < fieldNames.size(); i++) {
        final String s = fieldNames.get(i);
        if (s != null) {
            newFieldNames.set(i, s);
        }
    }
    if (oldFieldNames.equals(newFieldNames)) {
        return this;
    }
    if (peek() instanceof Values) {
        // Special treatment for VALUES. Re-build it rather than add a project.
        final Values v = (Values) build();
        final RelDataTypeFactory.Builder b = getTypeFactory().builder();
        for (Pair<String, RelDataTypeField> p : Pair.zip(newFieldNames, v.getRowType().getFieldList())) {
            b.add(p.left, p.right.getType());
        }
        return values(v.tuples, b.build());
    }

    return project(fields(), newFieldNames, true);
}
 
Example 3
Source File: ProvenanceTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 4
Source File: Smalls.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** A function that generates multiplication table of {@code ncol} columns x
 * {@code nrow} rows. */
public static QueryableTable multiplicationTable(final int ncol,
    final int nrow, Integer offset) {
  final int offs = offset == null ? 0 : offset;
  return new AbstractQueryableTable(Object[].class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      final RelDataTypeFactory.Builder builder = typeFactory.builder();
      builder.add("row_name", typeFactory.createJavaType(String.class));
      final RelDataType int_ = typeFactory.createJavaType(int.class);
      for (int i = 1; i <= ncol; i++) {
        builder.add("c" + i, int_);
      }
      return builder.build();
    }

    public Queryable<Object[]> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      final List<Object[]> table = new AbstractList<Object[]>() {
        @Override public Object[] get(int index) {
          Object[] cur = new Object[ncol + 1];
          cur[0] = "row " + index;
          for (int j = 1; j <= ncol; j++) {
            cur[j] = j * (index + 1) + offs;
          }
          return cur;
        }

        @Override public int size() {
          return nrow;
        }
      };
      return Linq4j.asEnumerable(table).asQueryable();
    }
  };
}
 
Example 5
Source File: FlowFileTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 6
Source File: PigTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  for (String fieldName : fieldNames) {
    // only supports CHARARRAY types for now
    final RelDataType relDataType = typeFactory
        .createSqlType(PigDataType.valueOf(DataType.CHARARRAY).getSqlType());
    final RelDataType nullableRelDataType = typeFactory
        .createTypeWithNullability(relDataType, true);
    builder.add(fieldName, nullableRelDataType);
  }
  return builder.build();
}
 
Example 7
Source File: SqlTypeUtilTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelDataType struct(RelDataType...relDataTypes) {
  final RelDataTypeFactory.Builder builder = f.typeFactory.builder();
  for (int i = 0; i < relDataTypes.length; i++) {
    builder.add("field" + i, relDataTypes[i]);
  }
  return builder.build();
}
 
Example 8
Source File: BulletinTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 9
Source File: ConnectionStatusPredictionsTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 10
Source File: JvmMetricsTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 11
Source File: ConnectionStatusTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType deriveRowType() {
    final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
    final RelDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
    for (int field : fields) {
        builder.add(fieldList.get(field));
    }
    return builder.build();
}
 
Example 12
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
static PhysType of(
    final JavaTypeFactory typeFactory,
    Type javaRowClass) {
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  if (javaRowClass instanceof Types.RecordType) {
    final Types.RecordType recordType = (Types.RecordType) javaRowClass;
    for (Types.RecordField field : recordType.getRecordFields()) {
      builder.add(field.getName(), typeFactory.createType(field.getType()));
    }
  }
  RelDataType rowType = builder.build();
  // Do not optimize if there are 0 or 1 fields.
  return new PhysTypeImpl(typeFactory, rowType, javaRowClass,
      JavaRowFormat.CUSTOM);
}
 
Example 13
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static RelDataType toType(List<FieldType> fieldSchemaList) {
    final RelDataTypeFactory typeFactory = MycatCalciteSupport.INSTANCE.TypeFactory;
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    for (FieldType fieldSchema : fieldSchemaList) {
        boolean nullable = fieldSchema.isNullable();
        Integer precision = fieldSchema.getPrecision();
        Integer scale = fieldSchema.getScale();
        builder.add(fieldSchema.getColumnName(), toType(fieldSchema.getColumnType(), nullable, precision, scale));
    }
    return builder.build();
}
 
Example 14
Source File: TableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
public DataTable fromJdbcResultSet(
    ResultSet resultSet) {
  try {
    final RelDataTypeFactory.Builder builder = calciteCatalogReader
        .getTypeFactory()
        .builder();
    ResultSetMetaData metaData = resultSet.getMetaData();
    int columnCount = metaData.getColumnCount();
    for (int i = 1; i <= columnCount; i++) {
      String columnName = metaData.getColumnLabel(i);
      int jdbcType = metaData.getColumnType(i);
      builder.add(columnName.toUpperCase(),
          getSqlTypeNameForJdbcType(jdbcType)).nullable(true);
    }
    RelDataType rowType = builder.build();
    List<Object[]> convertRows = new ArrayList<>();
    Calendar calendar = Calendar
        .getInstance(tableConfig.getTimeZone(), Locale.ROOT);
    while (resultSet.next()) {
      Object[] row = new Object[columnCount];
      for (int i = 1; i <= columnCount; i++) {
        Object jdbcObject = resultSet.getObject(i);
        Object convertedCalciteObject = TypedValue.ofJdbc(jdbcObject,
            calendar).value;
        row[i - 1] = convertedCalciteObject;
      }
      convertRows.add(row);
    }
    return new DataTable(rowType, convertRows);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 15
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveRowType() {
  final RelDataTypeFactory.Builder builder =
      getCluster().getTypeFactory().builder();
  final List<RelDataTypeField> fieldList =
      table.getRowType().getFieldList();
  for (int project : projects) {
    builder.add(fieldList.get(project));
  }
  return builder.build();
}
 
Example 16
Source File: Interpreter.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType combinedRowType(List<RelNode> inputs) {
  final RelDataTypeFactory.Builder builder =
      interpreter.dataContext.getTypeFactory().builder();
  for (RelNode input : inputs) {
    builder.addAll(input.getRowType().getFieldList());
  }
  return builder.build();
}
 
Example 17
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns the row type of a table after any {@link ColumnStrategy#VIRTUAL}
 * columns have been removed. This is the type of the records that are
 * actually stored. */
public static RelDataType realRowType(RelOptTable table) {
    final RelDataType rowType = table.getRowType();
    final List<ColumnStrategy> strategies = columnStrategies(table);
    if (!strategies.contains(ColumnStrategy.VIRTUAL)) {
        return rowType;
    }
    final RelDataTypeFactory.Builder builder = table.getRelOptSchema().getTypeFactory().builder();
    for (RelDataTypeField field : rowType.getFieldList()) {
        if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
            builder.add(field);
        }
    }
    return builder.build();
}
 
Example 18
Source File: SchemaNamespace.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected RelDataType validateImpl(RelDataType targetRowType) {
  final RelDataTypeFactory.Builder builder =
      validator.getTypeFactory().builder();
  for (SqlMoniker moniker
      : validator.catalogReader.getAllSchemaObjectNames(names)) {
    final List<String> names1 = moniker.getFullyQualifiedNames();
    final SqlValidatorTable table = validator.catalogReader.getTable(names1);
    builder.add(Util.last(names1), table.getRowType());
  }
  return builder.build();
}
 
Example 19
Source File: SolrSchema.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
RelProtoDataType getRelDataType(String collection) {
  // 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();
  Map<String, LukeResponse.FieldInfo> luceneFieldInfoMap = getFieldInfo(collection);

  for(Map.Entry<String, LukeResponse.FieldInfo> entry : luceneFieldInfoMap.entrySet()) {
    LukeResponse.FieldInfo luceneFieldInfo = entry.getValue();

    String luceneFieldType = luceneFieldInfo.getType();
    // SOLR-13414: Luke can return a field definition with no type in rare situations
    if(luceneFieldType == null) {
      continue;
    }

    RelDataType type;
    switch (luceneFieldType) {
      case "string":
        type = typeFactory.createJavaType(String.class);
        break;
      case "tint":
      case "tlong":
      case "int":
      case "long":
      case "pint":
      case "plong":
        type = typeFactory.createJavaType(Long.class);
        break;
      case "tfloat":
      case "tdouble":
      case "float":
      case "double":
      case "pfloat":
      case "pdouble":
        type = typeFactory.createJavaType(Double.class);
        break;
      default:
        type = typeFactory.createJavaType(String.class);
    }

    /*
    EnumSet<FieldFlag> flags = luceneFieldInfo.parseFlags(luceneFieldInfo.getSchema());
    if(flags != null && flags.contains(FieldFlag.MULTI_VALUED)) {
      type = typeFactory.createArrayType(type, -1);
    }
    */

    fieldInfo.add(entry.getKey(), type).nullable(true);
  }
  fieldInfo.add("_query_",typeFactory.createJavaType(String.class));
  fieldInfo.add("score",typeFactory.createJavaType(Double.class));

  return RelDataTypeImpl.proto(fieldInfo.build());
}
 
Example 20
Source File: ExtensionDdlExecutor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Executes a {@code CREATE TABLE} command. Called via reflection. */
public void execute(SqlCreateTable create, CalcitePrepare.Context context) {
  final CalciteSchema schema =
      Schemas.subSchema(context.getRootSchema(),
          context.getDefaultSchemaPath());
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  final RelDataType queryRowType;
  if (create.query != null) {
    // A bit of a hack: pretend it's a view, to get its row type
    final String sql =
        create.query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
    final ViewTableMacro viewTableMacro =
        ViewTable.viewMacro(schema.plus(), sql, schema.path(null),
            context.getObjectPath(), false);
    final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
    queryRowType = x.getRowType(typeFactory);

    if (create.columnList != null
        && queryRowType.getFieldCount() != create.columnList.size()) {
      throw SqlUtil.newContextException(create.columnList.getParserPosition(),
          RESOURCE.columnCountMismatch());
    }
  } else {
    queryRowType = null;
  }
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  if (create.columnList != null) {
    final SqlValidator validator = new ContextSqlValidator(context, false);
    create.forEachNameType((name, typeSpec) ->
        builder.add(name.getSimple(), typeSpec.deriveType(validator, true)));
  } else {
    if (queryRowType == null) {
      // "CREATE TABLE t" is invalid; because there is no "AS query" we need
      // a list of column names and types, "CREATE TABLE t (INT c)".
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.createTableRequiresColumnList());
    }
    builder.addAll(queryRowType.getFieldList());
  }
  final RelDataType rowType = builder.build();
  schema.add(create.name.getSimple(),
      new MutableArrayTable(create.name.getSimple(),
          RelDataTypeImpl.proto(rowType)));
  if (create.query != null) {
    populate(create.name, create.query, context);
  }
}