org.apache.calcite.rel.type.RelDataTypeImpl Java Examples

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeImpl. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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);
  }
}
 
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);
}
 
Example #17
Source File: ReturnTypes.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an inference rule which returns a copy of a given data type.
 */
public static ExplicitReturnTypeInference explicit(RelDataType type) {
  return explicit(RelDataTypeImpl.proto(type));
}
 
Example #18
Source File: ReturnTypes.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an inference rule which returns a type with precision but no scale,
 * such as {@code VARCHAR(100)}.
 */
public static ExplicitReturnTypeInference explicit(SqlTypeName typeName,
    int precision) {
  return explicit(RelDataTypeImpl.proto(typeName, precision, false));
}
 
Example #19
Source File: ReturnTypes.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an inference rule which returns a copy of a given data type.
 */
public static ExplicitReturnTypeInference explicit(RelDataType type) {
  return explicit(RelDataTypeImpl.proto(type));
}
 
Example #20
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 #21
Source File: ReturnTypes.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an inference rule which returns a type with precision but no scale,
 * such as {@code VARCHAR(100)}.
 */
public static ExplicitReturnTypeInference explicit(SqlTypeName typeName,
    int precision) {
  return explicit(RelDataTypeImpl.proto(typeName, precision, false));
}
 
Example #22
Source File: ReturnTypes.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an inference rule which returns a type with no precision or scale,
 * such as {@code DATE}.
 */
public static ExplicitReturnTypeInference explicit(SqlTypeName typeName) {
  return explicit(RelDataTypeImpl.proto(typeName, false));
}
 
Example #23
Source File: ReturnTypes.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an inference rule which returns a type with no precision or scale,
 * such as {@code DATE}.
 */
public static ExplicitReturnTypeInference explicit(SqlTypeName typeName) {
  return explicit(RelDataTypeImpl.proto(typeName, false));
}