@Override protected RowExpression visitDereferenceExpression(DereferenceExpression node, Void context) { RowType rowType = (RowType) getType(node.getBase()); String fieldName = node.getField().getValue(); List<Field> fields = rowType.getFields(); int index = -1; for (int i = 0; i < fields.size(); i++) { Field field = fields.get(i); if (field.getName().isPresent() && field.getName().get().equalsIgnoreCase(fieldName)) { checkArgument(index < 0, "Ambiguous field %s in type %s", field, rowType.getDisplayName()); index = i; } } if (legacyRowFieldOrdinalAccess && index < 0) { OptionalInt rowIndex = parseAnonymousRowFieldOrdinalAccess(fieldName, fields); if (rowIndex.isPresent()) { index = rowIndex.getAsInt(); } } checkState(index >= 0, "could not find field name: %s", node.getField()); Type returnType = getType(node); return call(dereferenceSignature(returnType, rowType), returnType, process(node.getBase(), context), constant(index, INTEGER)); }
Field field = fields.get(i); if (field.getName().isPresent() && field.getName().get().equalsIgnoreCase(fieldName)) { checkArgument(index < 0, "Ambiguous field %s in type %s", field, rowType.getDisplayName()); index = i;
@Test public void testRowDisplayNoColumnNames() { List<Type> types = asList( BOOLEAN, DOUBLE, new ArrayType(VARCHAR), new MapType( BOOLEAN, DOUBLE, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"))); RowType row = RowType.anonymous(types); assertEquals( row.getDisplayName(), "row(boolean, double, array(varchar), map(boolean, double))"); }
@Test public void testRowDisplayName() { List<RowType.Field> fields = asList( RowType.field("bool_col", BOOLEAN), RowType.field("double_col", DOUBLE), RowType.field("array_col", new ArrayType(VARCHAR)), RowType.field("map_col", new MapType( BOOLEAN, DOUBLE, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")))); RowType row = RowType.from(fields); assertEquals( row.getDisplayName(), "row(bool_col boolean, double_col double, array_col array(varchar), map_col map(boolean, double))"); }
@Test public void testRowDisplayMixedUnnamedColumns() { List<RowType.Field> fields = asList( RowType.field(BOOLEAN), RowType.field("double_col", DOUBLE), RowType.field(new ArrayType(VARCHAR)), RowType.field("map_col", new MapType( BOOLEAN, DOUBLE, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")))); RowType row = RowType.from(fields); assertEquals( row.getDisplayName(), "row(boolean, double_col double, array(varchar), map_col map(boolean, double))"); }