Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#ANY
The following examples show how to use
org.apache.calcite.sql.type.SqlTypeName#ANY .
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: JethroDataSqlDialect.java From Bats with Apache License 2.0 | 6 votes |
private SqlTypeName parse(String strType) { switch (strType.toLowerCase(Locale.ROOT)) { case "bigint": case "long": return SqlTypeName.BIGINT; case "integer": case "int": return SqlTypeName.INTEGER; case "double": return SqlTypeName.DOUBLE; case "float": return SqlTypeName.FLOAT; case "string": return SqlTypeName.VARCHAR; case "timestamp": return SqlTypeName.TIMESTAMP; default: return SqlTypeName.ANY; } }
Example 2
Source File: TestValuesRel.java From dremio-oss with Apache License 2.0 | 6 votes |
@Test public void testCharValuesRelRowTypeAdjustment() { final int INListLength = 20; // Build RowType & Tuples RelDataTypeField relDataType = new RelDataTypeFieldImpl("ROW_VALUE", 0, new BasicSqlType(RelDataTypeSystemImpl.REL_DATA_TYPE_SYSTEM, SqlTypeName.ANY)); RelDataType rowType = new RelRecordType(StructKind.FULLY_QUALIFIED, Arrays.asList(relDataType)); ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = new ImmutableList.Builder<>(); for (int i = 0; i < INListLength; ++i) { tuples.add(new ImmutableList.Builder<RexLiteral>().add(new RexBuilder(typeFactory).makeLiteral(charLiteralBuilder(i))).build()); } // Check original types. assertEquals(1, rowType.getFieldCount()); assertEquals(SqlTypeName.ANY, rowType.getFieldList().get(0).getType().getSqlTypeName()); // Construct ValuesRel final ValuesRel valuesRel = new ValuesRel(cluster, rowType, tuples.build(), traits); // Check the adjusted types. RelDataType adjustedRowType = valuesRel.getRowType(); assertEquals(1, adjustedRowType.getFieldCount()); assertEquals(SqlTypeName.CHAR, adjustedRowType.getFieldList().get(0).getType().getSqlTypeName()); assertEquals(INListLength - 1, adjustedRowType.getFieldList().get(0).getType().getPrecision()); }
Example 3
Source File: TestValuesRel.java From dremio-oss with Apache License 2.0 | 6 votes |
@Test public void testNumericValuesRelRowTypeAdjustment() { final int INListLength = 20; // Build RowType & Tuples RelDataTypeField relDataType = new RelDataTypeFieldImpl("ROW_VALUE", 0, new BasicSqlType(RelDataTypeSystemImpl.REL_DATA_TYPE_SYSTEM, SqlTypeName.ANY)); RelDataType rowType = new RelRecordType(StructKind.FULLY_QUALIFIED, Arrays.asList(relDataType)); ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = new ImmutableList.Builder<>(); for (int i = 0; i < INListLength; i++) { tuples.add(new ImmutableList.Builder<RexLiteral>().add(new RexBuilder(typeFactory).makeExactLiteral(new BigDecimal(i))).build()); } // Check original types. assertEquals(1, rowType.getFieldCount()); assertEquals(SqlTypeName.ANY, rowType.getFieldList().get(0).getType().getSqlTypeName()); // Construct ValuesRel final ValuesRel valuesRel = new ValuesRel(cluster, rowType, tuples.build(), traits); // Check the adjusted types. RelDataType adjustedRowType = valuesRel.getRowType(); assertEquals(1, adjustedRowType.getFieldCount()); assertEquals(SqlTypeName.INTEGER, adjustedRowType.getFieldList().get(0).getType().getSqlTypeName()); }
Example 4
Source File: ValuesRel.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Helper method that gets the type from tuples for given fieldIndex. * An IN list is represented by a single iteration through the tuples at fieldIndex. * * @param fieldIndex Field index used to retrieve the relevant RexLiteral. * @param tuples RexLiterals for the Values Rel * * @return the derived RelDataType from literal. */ private static RelDataType getFieldTypeFromInput(RelDataTypeFactory typeFactory, final int fieldIndex, final ImmutableList<ImmutableList<RexLiteral>> tuples) { // Search for a non-NULL, non-ANY type. List<RelDataType> literalTypes = Lists.newArrayListWithExpectedSize(tuples.size()); for(ImmutableList<RexLiteral> literals : tuples) { final RexLiteral literal = literals.get(fieldIndex); if (literal != null && literal.getType().getSqlTypeName() != SqlTypeName.NULL && literal.getType().getSqlTypeName() != SqlTypeName.ANY) { literalTypes.add(literal.getType()); } } // Return the least restrictive type unless it is null, in which case return the first non-null, non-ANY type. RelDataType leastRestrictiveType = typeFactory.leastRestrictive(literalTypes); return (leastRestrictiveType != null) ? leastRestrictiveType : literalTypes.get(0); }
Example 5
Source File: JethroDataSqlDialect.java From calcite with Apache License 2.0 | 6 votes |
private SqlTypeName parse(String strType) { switch (strType.toLowerCase(Locale.ROOT)) { case "bigint": case "long": return SqlTypeName.BIGINT; case "integer": case "int": return SqlTypeName.INTEGER; case "double": return SqlTypeName.DOUBLE; case "float": return SqlTypeName.FLOAT; case "string": return SqlTypeName.VARCHAR; case "timestamp": return SqlTypeName.TIMESTAMP; default: return SqlTypeName.ANY; } }
Example 6
Source File: RexBuilder.java From Bats with Apache License 2.0 | 6 votes |
/** * Ensures expression is interpreted as a specified type. The returned * expression may be wrapped with a cast. * * @param type desired type * @param node expression * @param matchNullability whether to correct nullability of specified * type to match the expression; this usually should * be true, except for explicit casts which can * override default nullability * @return a casted expression or the original expression */ public RexNode ensureType( RelDataType type, RexNode node, boolean matchNullability) { RelDataType targetType = type; if (matchNullability) { targetType = matchNullability(type, node); } if (targetType.getSqlTypeName() == SqlTypeName.ANY && (!matchNullability || targetType.isNullable() == node.getType().isNullable())) { return node; } if (!node.getType().equals(targetType)) { return makeCast(targetType, node); } return node; }
Example 7
Source File: RexBuilder.java From calcite with Apache License 2.0 | 6 votes |
/** * Ensures expression is interpreted as a specified type. The returned * expression may be wrapped with a cast. * * @param type desired type * @param node expression * @param matchNullability whether to correct nullability of specified * type to match the expression; this usually should * be true, except for explicit casts which can * override default nullability * @return a casted expression or the original expression */ public RexNode ensureType( RelDataType type, RexNode node, boolean matchNullability) { RelDataType targetType = type; if (matchNullability) { targetType = matchNullability(type, node); } if (targetType.getSqlTypeName() == SqlTypeName.ANY && (!matchNullability || targetType.isNullable() == node.getType().isNullable())) { return node; } if (!node.getType().equals(targetType)) { return makeCast(targetType, node); } return node; }
Example 8
Source File: SamzaSqlValidator.java From samza with Apache License 2.0 | 5 votes |
private boolean compareFieldTypes(RelDataType outputFieldType, SqlFieldSchema sqlFieldSchema, RelDataType selectQueryFieldType, RelSchemaProvider outputRelSchemaProvider) { SqlTypeName outputSqlType = outputFieldType.getSqlTypeName(); SqlTypeName projectSqlType = selectQueryFieldType.getSqlTypeName(); if (projectSqlType == SqlTypeName.ANY || outputSqlType == SqlTypeName.ANY) { return true; } else if (outputSqlType != SqlTypeName.ROW && outputSqlType == projectSqlType) { return true; } switch (outputSqlType) { case CHAR: return projectSqlType == SqlTypeName.VARCHAR; case VARCHAR: return projectSqlType == SqlTypeName.CHAR; case BIGINT: return projectSqlType == SqlTypeName.INTEGER; case INTEGER: return projectSqlType == SqlTypeName.BIGINT; case FLOAT: return projectSqlType == SqlTypeName.DOUBLE; case DOUBLE: return projectSqlType == SqlTypeName.FLOAT; case ROW: try { validateOutputRecords(sqlFieldSchema.getRowSchema(), (RelRecordType) outputFieldType, (RelRecordType) selectQueryFieldType, outputRelSchemaProvider); } catch (SamzaSqlValidatorException e) { LOG.error("A field in select query does not match with the output schema.", e); return false; } return true; default: return false; } }
Example 9
Source File: RelDataTypeHolder.java From calcite with Apache License 2.0 | 5 votes |
/** * Get field if exists, otherwise inserts a new field. The new field by default will have "any" * type, except for the dynamic star field. * * @param fieldName Request field name * @param caseSensitive Case Sensitive * @return A pair of RelDataTypeField and Boolean. Boolean indicates whether a new field is added * to this holder. */ Pair<RelDataTypeField, Boolean> getFieldOrInsert(String fieldName, boolean caseSensitive) { // First check if this field name exists in our field list for (RelDataTypeField f : fields) { if (Util.matches(caseSensitive, f.getName(), fieldName)) { return Pair.of(f, false); } // A dynamic star field matches any field if (f.getType().getSqlTypeName() == SqlTypeName.DYNAMIC_STAR) { return Pair.of(f, false); } } final SqlTypeName typeName = DynamicRecordType.isDynamicStarColName(fieldName) ? SqlTypeName.DYNAMIC_STAR : SqlTypeName.ANY; // This field does not exist in our field list; add it RelDataTypeField newField = new RelDataTypeFieldImpl( fieldName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(typeName), true)); // Add the name to our list of field names fields.add(newField); return Pair.of(newField, true); }
Example 10
Source File: StreamlineSqlTypeFactoryImpl.java From streamline with Apache License 2.0 | 5 votes |
@Override public RelDataType toSql(RelDataType type) { if (type instanceof JavaType) { JavaType javaType = (JavaType) type; SqlTypeName sqlTypeName = JavaToSqlTypeConversionRules.instance().lookup(javaType.getJavaClass()); if (sqlTypeName == null) { sqlTypeName = SqlTypeName.ANY; } return createTypeWithNullability(createSqlType(sqlTypeName), type.isNullable()); } return super.toSql(type); }
Example 11
Source File: TypeInferenceUtils.java From Bats with Apache License 2.0 | 5 votes |
/** * Given a Drill's TypeProtos.MinorType, return a Calcite's corresponding SqlTypeName */ public static SqlTypeName getCalciteTypeFromDrillType(final TypeProtos.MinorType type) { if(!DRILL_TO_CALCITE_TYPE_MAPPING.containsKey(type)) { return SqlTypeName.ANY; } return DRILL_TO_CALCITE_TYPE_MAPPING.get(type); }
Example 12
Source File: DrillRelOptUtil.java From Bats with Apache License 2.0 | 5 votes |
public static boolean areRowTypesCompatible(RelDataType rowType1, RelDataType rowType2, boolean compareNames, boolean allowSubstring) { if (rowType1 == rowType2) { return true; } if (compareNames) { // if types are not identity-equal, then either the names or // the types must be different return false; } if (rowType2.getFieldCount() != rowType1.getFieldCount()) { return false; } final List<RelDataTypeField> f1 = rowType1.getFieldList(); final List<RelDataTypeField> f2 = rowType2.getFieldList(); for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) { final RelDataType type1 = pair.left.getType(); final RelDataType type2 = pair.right.getType(); // If one of the types is ANY comparison should succeed if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) { continue; } if (type1.getSqlTypeName() != type2.getSqlTypeName()) { if (allowSubstring && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR) && (type1.getPrecision() <= type2.getPrecision())) { return true; } // Check if Drill implicit casting can resolve the incompatibility List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2); types.add(Types.getMinorTypeFromName(type1.getSqlTypeName().getName())); types.add(Types.getMinorTypeFromName(type2.getSqlTypeName().getName())); return TypeCastRules.getLeastRestrictiveType(types) != null; } } return true; }
Example 13
Source File: Checker.java From samza with Apache License 2.0 | 5 votes |
/** * Converts the {@link SamzaSqlFieldType} to the calcite {@link SqlTypeName}. * @param samzaSqlFieldType the samza sql field type. * @return the converted calcite SqlTypeName. */ @VisibleForTesting static SqlTypeName toCalciteSqlType(SamzaSqlFieldType samzaSqlFieldType) { switch (samzaSqlFieldType) { case ANY: case ROW: return SqlTypeName.ANY; case MAP: return SqlTypeName.MAP; case ARRAY: return SqlTypeName.ARRAY; case REAL: return SqlTypeName.REAL; case DOUBLE: return SqlTypeName.DOUBLE; case STRING: return SqlTypeName.VARCHAR; case INT16: case INT32: return SqlTypeName.INTEGER; case FLOAT: return SqlTypeName.FLOAT; case INT64: return SqlTypeName.BIGINT; case BOOLEAN: return SqlTypeName.BOOLEAN; case BYTES: return SqlTypeName.VARBINARY; default: String msg = String.format("Field Type %s is not supported", samzaSqlFieldType); LOG.error(msg); throw new SamzaException(msg); } }
Example 14
Source File: RelOptUtil.java From Bats with Apache License 2.0 | 5 votes |
/** * Returns whether two types are equal using '='. * * @param desc1 Description of first type * @param type1 First type * @param desc2 Description of second type * @param type2 Second type * @param litmus What to do if an error is detected (types are not equal) * @return Whether the types are equal */ public static boolean eq(final String desc1, RelDataType type1, final String desc2, RelDataType type2, Litmus litmus) { // if any one of the types is ANY return true if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) { return litmus.succeed(); } if (type1 != type2) { return litmus.fail("type mismatch:\n{}:\n{}\n{}:\n{}", desc1, type1.getFullTypeString(), desc2, type2.getFullTypeString()); } return litmus.succeed(); }
Example 15
Source File: RelOptUtil.java From calcite with Apache License 2.0 | 5 votes |
public static boolean areRowTypesEqual( RelDataType rowType1, RelDataType rowType2, boolean compareNames) { if (rowType1 == rowType2) { return true; } if (compareNames) { // if types are not identity-equal, then either the names or // the types must be different return false; } if (rowType2.getFieldCount() != rowType1.getFieldCount()) { return false; } final List<RelDataTypeField> f1 = rowType1.getFieldList(); final List<RelDataTypeField> f2 = rowType2.getFieldList(); for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) { final RelDataType type1 = pair.left.getType(); final RelDataType type2 = pair.right.getType(); // If one of the types is ANY comparison should succeed if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) { continue; } if (!type1.equals(type2)) { return false; } } return true; }
Example 16
Source File: CalciteArrowHelper.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Given a Dremio's TypeProtos.MinorType, return a Calcite's corresponding SqlTypeName */ public static SqlTypeName getCalciteTypeFromMinorType(final TypeProtos.MinorType type) { if(!CalciteTypeMaps.MINOR_TO_CALCITE_TYPE_MAPPING.containsKey(type)) { return SqlTypeName.ANY; } return CalciteTypeMaps.MINOR_TO_CALCITE_TYPE_MAPPING.get(type); }
Example 17
Source File: RelOptUtil.java From Bats with Apache License 2.0 | 5 votes |
public static boolean areRowTypesEqual(RelDataType rowType1, RelDataType rowType2, boolean compareNames) { if (rowType1 == rowType2) { return true; } if (compareNames) { // if types are not identity-equal, then either the names or // the types must be different return false; } if (rowType2.getFieldCount() != rowType1.getFieldCount()) { return false; } final List<RelDataTypeField> f1 = rowType1.getFieldList(); final List<RelDataTypeField> f2 = rowType2.getFieldList(); for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) { final RelDataType type1 = pair.left.getType(); final RelDataType type2 = pair.right.getType(); // If one of the types is ANY comparison should succeed if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) { continue; } if (!type1.equals(type2)) { return false; } } return true; }
Example 18
Source File: TypeInferenceUtils.java From Bats with Apache License 2.0 | 4 votes |
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) { RelDataTypeFactory factory = opBinding.getTypeFactory(); // operands count ond order is checked at parsing stage RelDataType inputType = opBinding.getOperandType(2); boolean isNullable = inputType.isNullable() || opBinding.getOperandType(1).isNullable(); SqlTypeName inputTypeName = inputType.getSqlTypeName(); TimeUnit qualifier = ((SqlLiteral) ((SqlCallBinding) opBinding).operand(0)).getValueAs(TimeUnit.class); SqlTypeName sqlTypeName; // follow up with type inference of reduced expression switch (qualifier) { case DAY: case WEEK: case MONTH: case QUARTER: case YEAR: case NANOSECOND: // NANOSECOND is not supported by Calcite SqlTimestampAddFunction. // Once it is fixed, NANOSECOND should be moved to the group below. sqlTypeName = inputTypeName; break; case MICROSECOND: case MILLISECOND: // precision should be specified for MICROSECOND and MILLISECOND return factory.createTypeWithNullability( factory.createSqlType(SqlTypeName.TIMESTAMP, 3), isNullable); case SECOND: case MINUTE: case HOUR: if (inputTypeName == SqlTypeName.TIME) { sqlTypeName = SqlTypeName.TIME; } else { sqlTypeName = SqlTypeName.TIMESTAMP; } break; default: sqlTypeName = SqlTypeName.ANY; } // preserves precision of input type if it was specified if (inputType.getSqlTypeName().allowsPrecNoScale()) { RelDataType type = factory.createSqlType(sqlTypeName, inputType.getPrecision()); return factory.createTypeWithNullability(type, isNullable); } return createCalciteTypeWithNullability( opBinding.getTypeFactory(), sqlTypeName, isNullable); }
Example 19
Source File: JdbcToEnumerableConverter.java From calcite with Apache License 2.0 | 4 votes |
private void generateGet(EnumerableRelImplementor implementor, PhysType physType, BlockBuilder builder, ParameterExpression resultSet_, int i, Expression target, Expression calendar_, SqlDialect.CalendarPolicy calendarPolicy) { final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i)); final RelDataType fieldType = physType.getRowType().getFieldList().get(i).getType(); final List<Expression> dateTimeArgs = new ArrayList<>(); dateTimeArgs.add(Expressions.constant(i + 1)); SqlTypeName sqlTypeName = fieldType.getSqlTypeName(); boolean offset = false; switch (calendarPolicy) { case LOCAL: dateTimeArgs.add(calendar_); break; case NULL: // We don't specify a calendar at all, so we don't add an argument and // instead use the version of the getXXX that doesn't take a Calendar break; case DIRECT: sqlTypeName = SqlTypeName.ANY; break; case SHIFT: switch (sqlTypeName) { case TIMESTAMP: case DATE: offset = true; } break; } final Expression source; switch (sqlTypeName) { case DATE: case TIME: case TIMESTAMP: source = Expressions.call( getMethod(sqlTypeName, fieldType.isNullable(), offset), Expressions.<Expression>list() .append( Expressions.call(resultSet_, getMethod2(sqlTypeName), dateTimeArgs)) .appendIf(offset, getTimeZoneExpression(implementor))); break; case ARRAY: final Expression x = Expressions.convert_( Expressions.call(resultSet_, jdbcGetMethod(primitive), Expressions.constant(i + 1)), java.sql.Array.class); source = Expressions.call(BuiltInMethod.JDBC_ARRAY_TO_LIST.method, x); break; default: source = Expressions.call( resultSet_, jdbcGetMethod(primitive), Expressions.constant(i + 1)); } builder.add( Expressions.statement( Expressions.assign( target, source))); // [CALCITE-596] If primitive type columns contain null value, returns null // object if (primitive != null) { builder.add( Expressions.ifThen( Expressions.call(resultSet_, "wasNull"), Expressions.statement( Expressions.assign(target, Expressions.constant(null))))); } }
Example 20
Source File: MoreRelOptUtil.java From dremio-oss with Apache License 2.0 | 4 votes |
private static boolean checkRowTypesCompatiblity( RelDataType rowType1, RelDataType rowType2, boolean compareNames, boolean allowSubstring, boolean insertOp) { if (rowType1 == rowType2) { return true; } if (compareNames) { // if types are not identity-equal, then either the names or // the types must be different return false; } if (rowType2.getFieldCount() != rowType1.getFieldCount()) { return false; } final List<RelDataTypeField> f1 = rowType1.getFieldList(); final List<RelDataTypeField> f2 = rowType2.getFieldList(); for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) { final RelDataType type1 = pair.left.getType(); final RelDataType type2 = pair.right.getType(); // If one of the types is ANY comparison should succeed if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) { continue; } if (!(type1.toString().equals(type2.toString()))) { if (allowSubstring && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR) && (type1.getPrecision() <= type2.getPrecision())) { continue; } // Check if Dremio implicit casting can resolve the incompatibility List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2); TypeProtos.MinorType minorType1 = Types.getMinorTypeFromName(type1.getSqlTypeName().getName()); TypeProtos.MinorType minorType2 = Types.getMinorTypeFromName(type2.getSqlTypeName().getName()); types.add(minorType1); types.add(minorType2); if (insertOp) { // Insert is more strict than normal select in terms of implicit casts // Return false if TypeCastRules do not allow implicit cast if (TypeCastRules.isCastable(minorType1, minorType2, true) && TypeCastRules.getLeastRestrictiveTypeForInsert(types) != null) { if (TypeCastRules.isCastSafeFromDataTruncation(type1, type2)) { continue; } } } else { if (TypeCastRules.getLeastRestrictiveType(types) != null) { continue; } } return false; } } return true; }