Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#createMapType()
The following examples show how to use
org.apache.calcite.rel.type.RelDataTypeFactory#createMapType() .
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: SqlTypeUtil.java From Bats with Apache License 2.0 | 5 votes |
public static RelDataType createMapType( RelDataTypeFactory typeFactory, RelDataType keyType, RelDataType valueType, boolean nullable) { RelDataType ret = typeFactory.createMapType(keyType, valueType); return typeFactory.createTypeWithNullability(ret, nullable); }
Example 2
Source File: View.java From Bats with Apache License 2.0 | 5 votes |
private RelDataType getType(Field field, RelDataTypeFactory factory) { RelDataType type; final SqlTypeName typeName = field.getType(); final Integer precision = field.getPrecision(); final Integer scale = field.getScale(); if (field.isInterval()) { type = factory.createSqlIntervalType(field.getIntervalQualifier()); } else if (precision != null) { type = scale != null ? factory.createSqlType(typeName, precision, scale) : factory.createSqlType(typeName, precision); } else if (typeName == SqlTypeName.MAP) { if (field.isMapTypesPresent()) { type = factory.createMapType(getType(field.getKeyType(), factory), getType(field.getValueType(), factory)); } else { /* For older views that doesn't have info about map key and value types, chosen type is ANY. Because use of raw MAP type causes creation of MAP cast expression that can't be serialized by ExpressionStringBuilder's visitCastExpression(CastExpression e, StringBuilder sb) method. See DRILL-6944 for more details. */ type = factory.createSqlType(SqlTypeName.ANY); } } else { type = factory.createSqlType(field.getType()); } return field.getIsNullable() ? factory.createTypeWithNullability(type, true) : type; }
Example 3
Source File: ElasticsearchTable.java From calcite with Apache License 2.0 | 5 votes |
@Override public RelDataType getRowType(RelDataTypeFactory relDataTypeFactory) { final RelDataType mapType = relDataTypeFactory.createMapType( relDataTypeFactory.createSqlType(SqlTypeName.VARCHAR), relDataTypeFactory.createTypeWithNullability( relDataTypeFactory.createSqlType(SqlTypeName.ANY), true)); return relDataTypeFactory.builder().add("_MAP", mapType).build(); }
Example 4
Source File: MongoTable.java From calcite with Apache License 2.0 | 5 votes |
public RelDataType getRowType(RelDataTypeFactory typeFactory) { final RelDataType mapType = typeFactory.createMapType( typeFactory.createSqlType(SqlTypeName.VARCHAR), typeFactory.createTypeWithNullability( typeFactory.createSqlType(SqlTypeName.ANY), true)); return typeFactory.builder().add("_MAP", mapType).build(); }
Example 5
Source File: JavaTypeFactoryImpl.java From calcite with Apache License 2.0 | 5 votes |
/** Converts a type in Java format to a SQL-oriented type. */ public static RelDataType toSql(final RelDataTypeFactory typeFactory, RelDataType type) { if (type instanceof RelRecordType) { return typeFactory.createTypeWithNullability( typeFactory.createStructType( type.getFieldList() .stream() .map(field -> toSql(typeFactory, field.getType())) .collect(Collectors.toList()), type.getFieldNames()), type.isNullable()); } else if (type instanceof JavaType) { SqlTypeName sqlTypeName = type.getSqlTypeName(); final RelDataType relDataType; if (SqlTypeUtil.isArray(type)) { // Transform to sql type, take care for two cases: // 1. type.getJavaClass() is collection with erased generic type // 2. ElementType returned by JavaType is also of JavaType, // and needs conversion using typeFactory final RelDataType elementType = toSqlTypeWithNullToAny( typeFactory, type.getComponentType()); relDataType = typeFactory.createArrayType(elementType, -1); } else if (SqlTypeUtil.isMap(type)) { final RelDataType keyType = toSqlTypeWithNullToAny( typeFactory, type.getKeyType()); final RelDataType valueType = toSqlTypeWithNullToAny( typeFactory, type.getValueType()); relDataType = typeFactory.createMapType(keyType, valueType); } else { relDataType = typeFactory.createSqlType(sqlTypeName); } return typeFactory.createTypeWithNullability(relDataType, type.isNullable()); } return type; }
Example 6
Source File: SqlTypeUtil.java From calcite with Apache License 2.0 | 5 votes |
public static RelDataType createMapType( RelDataTypeFactory typeFactory, RelDataType keyType, RelDataType valueType, boolean nullable) { RelDataType ret = typeFactory.createMapType(keyType, valueType); return typeFactory.createTypeWithNullability(ret, nullable); }
Example 7
Source File: AvroDrillTable.java From Bats with Apache License 2.0 | 4 votes |
private RelDataType getNullableRelDataTypeFromAvroType( RelDataTypeFactory typeFactory, Schema fieldSchema) { LogicalType logicalType = fieldSchema.getLogicalType(); String logicalTypeName = logicalType != null ? logicalType.getName() : ""; RelDataType relDataType = null; switch (fieldSchema.getType()) { case ARRAY: RelDataType eleType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getElementType()); relDataType = typeFactory.createArrayType(eleType, -1); break; case BOOLEAN: relDataType = typeFactory.createSqlType(SqlTypeName.BOOLEAN); break; case BYTES: switch (logicalTypeName) { case "decimal": relDataType = typeFactory.createSqlType(SqlTypeName.DECIMAL); break; default: relDataType = typeFactory.createSqlType(SqlTypeName.BINARY); } break; case DOUBLE: relDataType = typeFactory.createSqlType(SqlTypeName.DOUBLE); break; case FIXED: switch (logicalTypeName) { case "decimal": relDataType = typeFactory.createSqlType(SqlTypeName.DECIMAL); break; default: logger.error("{} type not supported", fieldSchema.getType()); throw UserException.unsupportedError().message("FIXED type not supported yet").build(logger); } break; case FLOAT: relDataType = typeFactory.createSqlType(SqlTypeName.FLOAT); break; case INT: switch (logicalTypeName) { case "date": relDataType = typeFactory.createSqlType(SqlTypeName.DATE); break; case "time-millis": relDataType = typeFactory.createSqlType(SqlTypeName.TIME); break; default: relDataType = typeFactory.createSqlType(SqlTypeName.INTEGER); } break; case LONG: switch (logicalTypeName) { case "date": relDataType = typeFactory.createSqlType(SqlTypeName.DATE); break; case "time-micros": relDataType = typeFactory.createSqlType(SqlTypeName.TIME); break; case "timestamp-millis": case "timestamp-micros": relDataType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP); break; default: relDataType = typeFactory.createSqlType(SqlTypeName.BIGINT); } break; case MAP: RelDataType valueType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getValueType()); RelDataType keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR); relDataType = typeFactory.createMapType(keyType, valueType); break; case NULL: relDataType = typeFactory.createSqlType(SqlTypeName.NULL); break; case RECORD: // List<String> fieldNameList = Lists.newArrayList(); // List<RelDataType> fieldRelDataTypeList = Lists.newArrayList(); // for(Field field : fieldSchema.getFields()) { // fieldNameList.add(field.name()); // fieldRelDataTypeList.add(getNullableRelDataTypeFromAvroType(typeFactory, field.schema())); // } // relDataType = typeFactory.createStructType(fieldRelDataTypeList, fieldNameList); //TODO This has to be mapped to struct type but because of calcite issue, //for now mapping it to map type. keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR); valueType = typeFactory.createSqlType(SqlTypeName.ANY); relDataType = typeFactory.createMapType(keyType, valueType); break; case ENUM: case STRING: relDataType = typeFactory.createSqlType(SqlTypeName.VARCHAR); break; case UNION: RelDataType optinalType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getTypes().get(1)); relDataType = typeFactory.createTypeWithNullability(optinalType, true); break; } return relDataType; }
Example 8
Source File: FlinkSqlDataTypeSpec.java From flink with Apache License 2.0 | 4 votes |
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 9
Source File: CollectionsFunctions.java From mat-calcite-plugin with Apache License 2.0 | 4 votes |
@Override public RelDataType getReturnType(RelDataTypeFactory relDataTypeFactory) { return relDataTypeFactory.createMapType(relDataTypeFactory.createJavaType(String.class), relDataTypeFactory.createJavaType(HeapReference.class)); }
Example 10
Source File: Handler.java From calcite with Apache License 2.0 | 4 votes |
private RelDataType toType(Ast.MapType type) { final RelDataTypeFactory typeFactory = builder.getTypeFactory(); final RelDataType k = toType(type.keyType); final RelDataType v = toType(type.valueType); return typeFactory.createMapType(k, v); }
Example 11
Source File: MatchTest.java From calcite with Apache License 2.0 | 4 votes |
/** * Tests the ElasticSearch match query. The match query is translated from * CONTAINS query which is build using RelBuilder, RexBuilder because the * normal SQL query assumes CONTAINS query is for date/period range. * * <p>Equivalent SQL query: * * <blockquote> * <code>select * from zips where city contains 'waltham'</code> * </blockquote> * * <p>ElasticSearch query for it: * * <blockquote><code> * {"query":{"constant_score":{"filter":{"match":{"city":"waltham"}}}}} * </code></blockquote> */ @Test void testMatchQuery() throws Exception { CalciteConnection con = (CalciteConnection) newConnectionFactory() .createConnection(); SchemaPlus postSchema = con.getRootSchema().getSubSchema("elastic"); FrameworkConfig postConfig = Frameworks.newConfigBuilder() .parserConfig(SqlParser.Config.DEFAULT) .defaultSchema(postSchema) .build(); final RelBuilder builder = RelBuilder.create(postConfig); builder.scan(ZIPS); final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); final RexBuilder rexBuilder = new RexBuilder(typeFactory); RexNode nameRexNode = rexBuilder.makeCall(SqlStdOperatorTable.ITEM, rexBuilder.makeInputRef(typeFactory.createSqlType(SqlTypeName.ANY), 0), rexBuilder.makeCharLiteral( new NlsString("city", typeFactory.getDefaultCharset().name(), SqlCollation.COERCIBLE))); RelDataType mapType = typeFactory.createMapType( typeFactory.createSqlType(SqlTypeName.VARCHAR), typeFactory.createTypeWithNullability( typeFactory.createSqlType(SqlTypeName.ANY), true)); List<RexNode> namedList = ImmutableList.of(rexBuilder.makeInputRef(mapType, 0), nameRexNode); // Add fields in builder stack so it is accessible while filter preparation builder.projectNamed(namedList, Arrays.asList("_MAP", "city"), true); RexNode filterRexNode = builder .call(SqlStdOperatorTable.CONTAINS, builder.field("city"), builder.literal("waltham")); builder.filter(filterRexNode); String builderExpected = "" + "LogicalFilter(condition=[CONTAINS($1, 'waltham')])\n" + " LogicalProject(_MAP=[$0], city=[ITEM($0, 'city')])\n" + " ElasticsearchTableScan(table=[[elastic, " + ZIPS + "]])\n"; RelNode root = builder.build(); RelRunner ru = (RelRunner) con.unwrap(Class.forName("org.apache.calcite.tools.RelRunner")); try (PreparedStatement preparedStatement = ru.prepare(root)) { String s = CalciteAssert.toString(preparedStatement.executeQuery()); final String result = "" + "_MAP={id=02154, city=NORTH WALTHAM, loc=[-71.236497, 42.382492], " + "pop=57871, state=MA}; city=NORTH WALTHAM\n"; // Validate query prepared assertThat(root, hasTree(builderExpected)); // Validate result returned from ES assertThat(s, is(result)); } }