@Override public List<CompatibilityError> accept( Context context, Optional<Field> left, Optional<Field> right) { if (!left.isPresent() && !right.isPresent()) { return Collections.emptyList(); } else if (left.isPresent() && !right.isPresent()) { return Collections.emptyList(); } else if (!left.isPresent() && right.isPresent()) { return Collections.singletonList( CompatibilityError.create(context.path(), "Field is missing in output schema")); } else { if (left.get().getNullable() && !right.get().getNullable()) { return Collections.singletonList( CompatibilityError.create( context.path(), "Can't cast nullable field to non-nullable field")); } } return Collections.emptyList(); }
private List<Object> verify(Schema schema, List<Object> values) { List<Object> verifiedValues = Lists.newArrayListWithCapacity(values.size()); if (schema.getFieldCount() != values.size()) { throw new IllegalArgumentException( String.format( "Field count in Schema (%s) and values (%s) must match", schema.getFieldNames(), values)); } for (int i = 0; i < values.size(); ++i) { Object value = values.get(i); Schema.Field field = schema.getField(i); if (value == null) { if (!field.getNullable()) { throw new IllegalArgumentException( String.format("Field %s is not nullable", field.getName())); } verifiedValues.add(null); } else { verifiedValues.add(verify(value, field.getType(), field.getName())); } } return verifiedValues; }
public static Object autoCastField(Schema.Field field, Object rawObj) { if (rawObj == null) { if (!field.getNullable()) { throw new IllegalArgumentException(String.format("Field %s not nullable", field.getName()));
private static List<TableFieldSchema> toTableFieldSchema(Schema schema) { List<TableFieldSchema> fields = new ArrayList<>(schema.getFieldCount()); for (Field schemaField : schema.getFields()) { FieldType type = schemaField.getType(); TableFieldSchema field = new TableFieldSchema().setName(schemaField.getName()); if (schemaField.getDescription() != null && !"".equals(schemaField.getDescription())) { field.setDescription(schemaField.getDescription()); } if (!schemaField.getNullable()) { field.setMode(Mode.REQUIRED.toString()); } if (TypeName.ARRAY == type.getTypeName()) { type = type.getCollectionElementType(); field.setMode(Mode.REPEATED.toString()); } if (TypeName.ROW == type.getTypeName()) { Schema subType = type.getRowSchema(); field.setFields(toTableFieldSchema(subType)); } field.setType(toStandardSQLTypeName(type).toString()); fields.add(field); } return fields; }
static Schema getOutputSchema(Schema inputSchema, FieldAccessDescriptor fieldAccessDescriptor) { if (fieldAccessDescriptor.allFields()) { return inputSchema; } Schema.Builder builder = new Schema.Builder(); for (int fieldId : fieldAccessDescriptor.fieldIdsAccessed()) { builder.addField(inputSchema.getField(fieldId)); } for (Map.Entry<Integer, FieldAccessDescriptor> nested : fieldAccessDescriptor.nestedFields().entrySet()) { Field field = inputSchema.getField(nested.getKey()); FieldAccessDescriptor nestedDescriptor = nested.getValue(); FieldType nestedType = FieldType.row(getOutputSchema(field.getType().getRowSchema(), nestedDescriptor)); if (field.getNullable()) { builder.addNullableField(field.getName(), nestedType); } else { builder.addField(field.getName(), nestedType); } } return builder.build(); }
@Override public int hashCode() { return Objects.hash(getName(), getDescription(), getType(), getNullable()); } }
@Nullable @Override @SuppressWarnings({"TypeParameterUnusedInFormals", "unchecked"}) public <T> T getValue(int fieldIdx) { Field field = getSchema().getField(fieldIdx); FieldType type = field.getType(); Object fieldValue = getters.get(fieldIdx).get(getterTarget); if (fieldValue == null && !field.getNullable()) { throw new RuntimeException("Null value set on non-nullable field" + field); } return fieldValue != null ? getValue(type, fieldValue, fieldIdx) : null; }
@Test public void testNullables() { Schema schema = POJOUtils.schemaFromPojoClass(POJOWithNullables.class); assertTrue(schema.getField("str").getNullable()); assertFalse(schema.getField("anInt").getNullable()); }
private static RelDataType toRelDataType( RelDataTypeFactory dataTypeFactory, Schema schema, int fieldIndex) { Schema.Field field = schema.getField(fieldIndex); RelDataType type = toRelDataType(dataTypeFactory, field.getType()); return dataTypeFactory.createTypeWithNullability(type, field.getNullable()); }
private boolean equivalent(Field otherField, EquivalenceNullablePolicy nullablePolicy) { if (nullablePolicy == EquivalenceNullablePolicy.SAME && !otherField.getNullable().equals(getNullable())) { return false; } else if (nullablePolicy == EquivalenceNullablePolicy.WEAKEN) { if (getNullable() && !otherField.getNullable()) { return false; } } return otherField.getName().equals(getName()) && getType().equivalent(otherField.getType()); }
/** Returns true if two fields are equal, ignoring name and description. */ public boolean typesEqual(Field other) { return getType().typesEqual(other.getType()) && Objects.equals(getNullable(), other.getNullable()); }
@Override public boolean equals(Object o) { if (!(o instanceof Field)) { return false; } Field other = (Field) o; return Objects.equals(getName(), other.getName()) && Objects.equals(getDescription(), other.getDescription()) && Objects.equals(getType(), other.getType()) && Objects.equals(getNullable(), other.getNullable()); }
@Test public void testNullable() { Schema schema = JavaBeanUtils.schemaFromJavaBeanClass(NullableBean.class); assertTrue(schema.getField("str").getNullable()); assertFalse(schema.getField("anInt").getNullable()); }
private void unparseColumn(SqlWriter writer, Schema.Field column) { writer.sep(","); writer.identifier(column.getName()); writer.identifier(CalciteUtils.toSqlTypeName(column.getType()).name()); if (column.getNullable() != null && !column.getNullable()) { writer.keyword("NOT NULL"); } if (column.getDescription() != null) { writer.keyword("COMMENT"); writer.literal(column.getDescription()); } }