Java Code Examples for org.apache.calcite.rel.type.RelDataTypeImpl#proto()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeImpl#proto() . 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: ViewTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ModifiableViewTable}
 * by overriding this method. */
protected ModifiableViewTable modifiableViewTable(CalciteResult.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath,
    CalciteSchema schema) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ModifiableViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath,
      parsed.table, Schemas.path(schema.root(), parsed.tablePath),
      parsed.constraint, parsed.columnMapping);
}
 
Example 2
Source File: ViewTableMacro.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ViewTable} by
 * overriding this method. */
protected ViewTable viewTable(CalciteResult.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath);
}
 
Example 3
Source File: Table.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    if (getRelDef() == null) {
        return null;
    }
    RelProtoDataType protoRowType = RelDataTypeImpl.proto(getRowType());
    return protoRowType.apply(typeFactory);
}
 
Example 4
Source File: DataTable.java    From marble with Apache License 2.0 5 votes vote down vote up
/**
 * new ArrayList<>()
 * Creates a DataTable.
 */
public DataTable(RelDataType rowType, List rows) {
  super();
  this.rowType = Objects.requireNonNull(rowType);
  this.protoRowType = RelDataTypeImpl.proto(rowType);
  this.rows = rows;

}
 
Example 5
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 6
Source File: CsvTableFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public CsvTable create(SchemaPlus schema, String name,
    Map<String, Object> operand, RelDataType rowType) {
  String fileName = (String) operand.get("file");
  final File base =
      (File) operand.get(ModelHandler.ExtraOperand.BASE_DIRECTORY.camelName);
  final Source source = Sources.file(base, fileName);
  final RelProtoDataType protoRowType =
      rowType != null ? RelDataTypeImpl.proto(rowType) : null;
  return new CsvScannableTable(source, protoRowType);
}
 
Example 7
Source File: RedisTableFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Table create(SchemaPlus schema, String tableName, Map operand,
    RelDataType rowType) {
  final RedisSchema redisSchema = schema.unwrap(RedisSchema.class);
  final RelProtoDataType protoRowType =
      rowType != null ? RelDataTypeImpl.proto(rowType) : null;
  return RedisTable.create(redisSchema, tableName, operand, protoRowType);
}
 
Example 8
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code CREATE MATERIALIZED VIEW} command. */
public void execute(SqlCreateMaterializedView create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  if (pair.left.plus().getTable(pair.right) != null) {
    // Materialized view exists.
    if (!create.ifNotExists) {
      // They did not specify IF NOT EXISTS, so give error.
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.tableExists(pair.right));
    }
    return;
  }
  final SqlNode q = renameColumns(create.columnList, create.query);
  final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
  final List<String> schemaPath = pair.left.path(null);
  final ViewTableMacro viewTableMacro =
      ViewTable.viewMacro(pair.left.plus(), sql, schemaPath,
          context.getObjectPath(), false);
  final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
  final RelDataType rowType = x.getRowType(context.getTypeFactory());

  // Table does not exist. Create it.
  final MaterializedViewTable table =
      new MaterializedViewTable(pair.right, RelDataTypeImpl.proto(rowType));
  pair.left.add(pair.right, table);
  populate(create.name, create.query, context);
  table.key =
      MaterializationService.instance().defineMaterialization(pair.left, null,
          sql, schemaPath, pair.right, true, true);
}
 
Example 9
Source File: JdbcSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
RelProtoDataType getRelDataType(DatabaseMetaData metaData, String catalogName,
    String schemaName, String tableName) throws SQLException {
  final ResultSet resultSet =
      metaData.getColumns(catalogName, schemaName, tableName, null);

  // 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();
  while (resultSet.next()) {
    final String columnName = resultSet.getString(4);
    final int dataType = resultSet.getInt(5);
    final String typeString = resultSet.getString(6);
    final int precision;
    final int scale;
    switch (SqlType.valueOf(dataType)) {
    case TIMESTAMP:
    case TIME:
      precision = resultSet.getInt(9); // SCALE
      scale = 0;
      break;
    default:
      precision = resultSet.getInt(7); // SIZE
      scale = resultSet.getInt(9); // SCALE
      break;
    }
    RelDataType sqlType =
        sqlType(typeFactory, dataType, precision, scale, typeString);
    boolean nullable = resultSet.getInt(11) != DatabaseMetaData.columnNoNulls;
    fieldInfo.add(columnName, sqlType).nullable(nullable);
  }
  resultSet.close();
  return RelDataTypeImpl.proto(fieldInfo.build());
}
 
Example 10
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ModifiableViewTable}
 * by overriding this method. */
protected ModifiableViewTable modifiableViewTable(CalcitePrepare.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath,
    CalciteSchema schema) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ModifiableViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath,
      parsed.table, Schemas.path(schema.root(), parsed.tablePath),
      parsed.constraint, parsed.columnMapping);
}
 
Example 11
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ViewTable} by
 * overriding this method. */
protected ViewTable viewTable(CalcitePrepare.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath);
}
 
Example 12
Source File: ArrayTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLoadSorted() {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType rowType =
      typeFactory.builder()
          .add("empid", typeFactory.createType(int.class))
          .add("deptno", typeFactory.createType(int.class))
          .add("name", typeFactory.createType(String.class))
          .build();
  final Enumerable<Object[]> enumerable =
      Linq4j.asEnumerable(
          Arrays.asList(
              new Object[]{100, 10, "Bill"},
              new Object[]{200, 20, "Eric"},
              new Object[]{150, 10, "Sebastian"},
              new Object[]{160, 10, "Theodore"}));
  final ColumnLoader<Object[]> loader =
      new ColumnLoader<Object[]>(typeFactory, enumerable,
          RelDataTypeImpl.proto(rowType), null);
  checkColumn(
      loader.representationValues.get(0),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=0, bitCount=8, primitive=INT, signed=false), value=[100, 150, 160, 200, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(1),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=1, bitCount=5, primitive=INT, signed=false), value=[10, 10, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(2),
      ArrayTable.RepresentationType.OBJECT_ARRAY,
      "Column(representation=ObjectArray(ordinal=2), value=[Bill, Sebastian, Theodore, Eric])");
}
 
Example 13
Source File: ArrayTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** As {@link #testLoadSorted()} but column #1 is the unique column, not
 * column #0. The algorithm needs to go back and permute the values of
 * column #0 after it discovers that column #1 is unique and sorts by it. */
@Test void testLoadSorted2() {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType rowType =
      typeFactory.builder()
          .add("deptno", typeFactory.createType(int.class))
          .add("empid", typeFactory.createType(int.class))
          .add("name", typeFactory.createType(String.class))
          .build();
  final Enumerable<Object[]> enumerable =
      Linq4j.asEnumerable(
          Arrays.asList(
              new Object[]{10, 100, "Bill"},
              new Object[]{20, 200, "Eric"},
              new Object[]{30, 150, "Sebastian"},
              new Object[]{10, 160, "Theodore"}));
  final ColumnLoader<Object[]> loader =
      new ColumnLoader<Object[]>(typeFactory, enumerable,
          RelDataTypeImpl.proto(rowType), null);
  // Note that values have been sorted with {20, 200, Eric} last because the
  // value 200 is the highest value of empid, the unique column.
  checkColumn(
      loader.representationValues.get(0),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=0, bitCount=5, primitive=INT, signed=false), value=[10, 30, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(1),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=1, bitCount=8, primitive=INT, signed=false), value=[100, 150, 160, 200, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(2),
      ArrayTable.RepresentationType.OBJECT_ARRAY,
      "Column(representation=ObjectArray(ordinal=2), value=[Bill, Sebastian, Theodore, Eric])");
}
 
Example 14
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected ModifiableViewTable modifiableViewTable(
    CalcitePrepare.AnalyzeViewResult parsed, String viewSql,
    List<String> schemaPath, List<String> viewPath, CalciteSchema schema) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new MockModifiableViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath,
      parsed.table, Schemas.path(schema.root(), parsed.tablePath),
      parsed.constraint, parsed.columnMapping);
}
 
Example 15
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 16
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void onRegister(RelDataTypeFactory typeFactory) {
  super.onRegister(typeFactory);
  // To simulate getRowType() behavior in ViewTable.
  final RelProtoDataType protoRowType = RelDataTypeImpl.proto(rowType);
  rowType = protoRowType.apply(typeFactory);
}