/** * Get a {@link Boolean} value by field index, {@link ClassCastException} is thrown if schema * doesn't match. */ @Nullable public Boolean getBoolean(int idx) { return getValue(idx); }
/** * Get a {@link TypeName#INT32} value by field index, {@link ClassCastException} is thrown if * schema doesn't match. */ @Nullable public Integer getInt32(int idx) { return getValue(idx); }
/** * Get a {@link BigDecimal} value by field index, {@link ClassCastException} is thrown if schema * doesn't match. */ @Nullable public BigDecimal getDecimal(int idx) { return getValue(idx); }
/** * Get an array value by field index, {@link IllegalStateException} is thrown if schema doesn't * match. */ @Nullable public <T> List<T> getArray(int idx) { return getValue(idx); }
/** * Get a {@link TypeName#BYTE} value by field index, {@link ClassCastException} is thrown if * schema doesn't match. */ @Nullable public Byte getByte(int idx) { return getValue(idx); }
/** * Get a {@link TypeName#BYTES} value by field index, {@link ClassCastException} is thrown if * schema doesn't match. */ @Nullable public byte[] getBytes(int idx) { return getValue(idx); }
/** * Get a {@link String} value by field index, {@link ClassCastException} is thrown if schema * doesn't match. */ @Nullable public String getString(int idx) { return getValue(idx); }
/** * Get a {@link TypeName#FLOAT} value by field index, {@link ClassCastException} is thrown if * schema doesn't match. */ @Nullable public Float getFloat(int idx) { return getValue(idx); }
/** * Get a {@link TypeName#INT64} value by field index, {@link ClassCastException} is thrown if * schema doesn't match. */ @Nullable public Long getInt64(int idx) { return getValue(idx); }
/** * Get a MAP value by field index, {@link IllegalStateException} is thrown if schema doesn't * match. */ @Nullable public <T1, T2> Map<T1, T2> getMap(int idx) { return getValue(idx); }
@Override public <T> SerializableFunction<Row, T> fromRowFunction(TypeDescriptor<T> typeDescriptor) { return r -> r.getValue(0); } }
private static BitSet scanNullFields(Row row, boolean hasNullableFields) { BitSet nullFields = new BitSet(row.getFieldCount()); if (hasNullableFields) { for (int idx = 0; idx < row.getFieldCount(); ++idx) { if (row.getValue(idx) == null) { nullFields.set(idx); } } } return nullFields; } }
/** * Get a {@link TypeName#DATETIME} value by field index, {@link IllegalStateException} is thrown * if schema doesn't match. */ @Nullable public ReadableDateTime getDateTime(int idx) { ReadableInstant instant = getValue(idx); return instant == null ? null : new DateTime(instant).withZone(instant.getZone()); }
/** Get value by field name, {@link ClassCastException} is thrown if type doesn't match. */ @Nullable @SuppressWarnings("TypeParameterUnusedInFormals") public <T> T getValue(String fieldName) { return getValue(getSchema().indexOf(fieldName)); }
@SuppressWarnings("unchecked") static void encodeDelegate( Coder[] coders, Row value, OutputStream outputStream, boolean hasNullableFields) throws IOException { NULL_LIST_CODER.encode(scanNullFields(value, hasNullableFields), outputStream); for (int idx = 0; idx < value.getFieldCount(); ++idx) { Object fieldValue = value.getValue(idx); if (value.getValue(idx) != null) { coders[idx].encode(fieldValue, outputStream); } } }
@Override public CovarianceAccumulator addInput(CovarianceAccumulator currentVariance, Row rawInput) { if (rawInput == null) { return currentVariance; } checkArgument(rawInput.getFieldCount() > 1); return currentVariance.combineWith( CovarianceAccumulator.ofSingleElement( SqlFunctions.toBigDecimal((Object) rawInput.getValue(0)), SqlFunctions.toBigDecimal((Object) rawInput.getValue(1)))); }
@Override public BeamSqlPrimitive evaluate( Row inputRow, BoundedWindow window, BeamSqlExpressionEnvironment env) { Row dynamicRow = (Row) opValueEvaluated(0, inputRow, window, env); String fieldName = (String) opValueEvaluated(1, inputRow, window, env); SqlTypeName fieldType = getFieldType(dynamicRow, fieldName); return BeamSqlPrimitive.of(fieldType, dynamicRow.getValue(fieldName)); }
@Override public int hashCode() { int h = 1; for (int i = 0; i < getFieldCount(); i++) { h = 31 * h + Equals.deepHashCode(getValue(i), getSchema().getField(i).getType()); } return h; }
private static void unnestRow(Row input, Row.Builder output) { for (int i = 0; i < input.getSchema().getFieldCount(); ++i) { Field field = input.getSchema().getField(i); if (field.getType().getTypeName().isCompositeType()) { unnestRow(input.getRow(i), output); } else { output.addValue(input.getValue(i)); } } } /** A {@link PTransform} that unnests nested row. */
@Test public void testNestedNullValuesGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Row row = registry .getToRowFunction(POJOWithNestedNullable.class) .apply(new POJOWithNestedNullable(null)); assertNull(row.getValue("nested")); }