19
19
import io .trino .spi .type .Type ;
20
20
21
21
import java .util .List ;
22
+ import java .util .Optional ;
22
23
import java .util .stream .Collectors ;
23
24
24
25
import static java .util .Objects .requireNonNull ;
25
26
26
27
@ JsonSerialize
27
- public record Row (List <Expression > items )
28
+ public record Row (List <Field > fields )
28
29
implements Expression
29
30
{
30
31
public Row
31
32
{
32
- requireNonNull (items , "items is null" );
33
- items = ImmutableList .copyOf (items );
33
+ requireNonNull (fields , "fields is null" );
34
+ fields = ImmutableList .copyOf (fields );
35
+ }
36
+
37
+ public static Row anonymousRow (List <Expression > values )
38
+ {
39
+ return new Row (values .stream ()
40
+ .map (Field ::anonymousField )
41
+ .collect (Collectors .toList ()));
34
42
}
35
43
36
44
@ Override
37
45
public Type type ()
38
46
{
39
- return RowType .anonymous ( items .stream ().map (Expression :: type ).collect (Collectors .toList ()));
47
+ return RowType .from ( fields .stream ().map (Field :: asRowTypeField ).collect (Collectors .toList ()));
40
48
}
41
49
42
50
@ Override
@@ -48,16 +56,44 @@ public <R, C> R accept(IrVisitor<R, C> visitor, C context)
48
56
@ Override
49
57
public List <? extends Expression > children ()
50
58
{
51
- return items ;
59
+ return fields .stream ()
60
+ .map (Field ::value )
61
+ .collect (Collectors .toList ());
52
62
}
53
63
54
64
@ Override
55
65
public String toString ()
56
66
{
57
67
return "(" +
58
- items .stream ()
59
- .map (Expression ::toString )
68
+ fields .stream ()
69
+ .map (Field ::toString )
60
70
.collect (Collectors .joining (", " )) +
61
71
")" ;
62
72
}
73
+
74
+ @ JsonSerialize
75
+ public record Field (Optional <String > name , Expression value )
76
+ {
77
+ public Field
78
+ {
79
+ requireNonNull (name , "name is null" );
80
+ requireNonNull (value , "value is null" );
81
+ }
82
+
83
+ public static Field anonymousField (Expression value )
84
+ {
85
+ return new Field (Optional .empty (), value );
86
+ }
87
+
88
+ public RowType .Field asRowTypeField ()
89
+ {
90
+ return new RowType .Field (name , value .type ());
91
+ }
92
+
93
+ @ Override
94
+ public String toString ()
95
+ {
96
+ return name .map (n -> n + " " + value ).orElseGet (value ::toString );
97
+ }
98
+ }
63
99
}
0 commit comments