16
16
17
17
package com .alibaba .graphscope .gremlin .result ;
18
18
19
- import com .alibaba .graphscope .common .jna .type .FfiKeyType ;
20
- import com .alibaba .graphscope .gaia .proto .Common ;
21
19
import com .alibaba .graphscope .gaia .proto .IrResult ;
22
20
import com .alibaba .graphscope .gremlin .exception .GremlinResultParserException ;
23
- import com .alibaba .graphscope .gremlin .transform .alias .AliasManager ;
24
21
25
22
import org .apache .tinkerpop .gremlin .structure .Element ;
26
23
import org .slf4j .Logger ;
27
24
import org .slf4j .LoggerFactory ;
28
25
29
- import java .util .Collections ;
30
- import java .util .HashMap ;
31
26
import java .util .List ;
32
- import java .util .Map ;
33
27
34
28
public enum GremlinResultParserFactory implements GremlinResultParser {
35
29
GRAPH_ELEMENT {
@@ -44,158 +38,15 @@ public Object parseFrom(IrResult.Results results) {
44
38
return graphElement ;
45
39
}
46
40
},
41
+
47
42
SINGLE_VALUE {
48
43
@ Override
49
44
public Object parseFrom (IrResult .Results results ) {
50
45
IrResult .Entry entry = ParserUtils .getHeadEntry (results );
51
46
return ParserUtils .parseEntry (entry );
52
47
}
53
48
},
54
- PROJECT_VALUE {
55
- // values("name") -> key: head, value: "marko"
56
- // valueMap("name") -> key: head, value: {name, "marko"}
57
- // select("a").by("name") -> key: head, value: "marko"
58
- // select("a", "b").by("name") -> key: a, value: "marko"; key: b, value: "josh"
59
- // select("a", "b").by(valueMap("name")) -> key: a, value: {name, "marko"}; key: b, value:
60
- // {name, "josh"}
61
- @ Override
62
- public Object parseFrom (IrResult .Results results ) {
63
- logger .debug ("{}" , results );
64
- IrResult .Record record = results .getRecord ();
65
- logger .debug ("{}" , record );
66
- Map <String , Object > projectResult = new HashMap <>();
67
- record .getColumnsList ()
68
- .forEach (
69
- column -> {
70
- String tag = getColumnKeyAsResultKey (column .getNameOrId ());
71
- Object parseEntry = ParserUtils .parseEntry (column .getEntry ());
72
- if (parseEntry instanceof Map ) {
73
- Map projectTags = (Map ) parseEntry ;
74
- // return empty Map if none properties
75
- Map tagEntry =
76
- (Map )
77
- projectResult .computeIfAbsent (
78
- tag , k1 -> new HashMap <>());
79
- projectTags .forEach (
80
- (k , v ) -> {
81
- if (!(v instanceof EmptyValue )) {
82
- String nameOrId = null ;
83
- if (k
84
- instanceof
85
- List ) { // valueMap("name") -> Map<["",
86
- // "name"], value>
87
- nameOrId = (String ) ((List ) k ).get (1 );
88
- } else if (k
89
- instanceof
90
- String ) { // valueMap() -> Map<"name",
91
- // value>
92
- nameOrId = (String ) k ;
93
- } else if (k
94
- instanceof
95
- Number ) { // valueMap() -> Map<1, value>
96
- nameOrId = String .valueOf (k );
97
- }
98
- if (nameOrId == null || nameOrId .isEmpty ()) {
99
- throw new GremlinResultParserException (
100
- "map value should have property"
101
- + " key" );
102
- }
103
- String property = getPropertyName (nameOrId );
104
- tagEntry .put (
105
- property , Collections .singletonList (v ));
106
- }
107
- });
108
- } else {
109
- if (!(parseEntry instanceof EmptyValue )) {
110
- projectResult .put (tag , parseEntry );
111
- }
112
- }
113
- });
114
- if (projectResult .isEmpty ()) {
115
- return EmptyValue .INSTANCE ;
116
- } else if (projectResult .size () == 1 ) {
117
- return projectResult .entrySet ().iterator ().next ().getValue ();
118
- } else {
119
- return projectResult ;
120
- }
121
- }
122
49
123
- // a_1 -> a, i.e. g.V().as("a").select("a")
124
- // name_1 -> name, i.e. g.V().values("name")
125
- // a_name_1 -> a, i.e. g.V().as("a").select("a").by("name")
126
- private String getColumnKeyAsResultKey (Common .NameOrId columnKey ) {
127
- if (columnKey .getItemCase () == Common .NameOrId .ItemCase .ITEM_NOT_SET ) {
128
- return "" ;
129
- }
130
- switch (columnKey .getItemCase ()) {
131
- case ITEM_NOT_SET :
132
- return "" ;
133
- case NAME :
134
- String key = columnKey .getName ();
135
- return AliasManager .getPrefix (key );
136
- case ID :
137
- return String .valueOf (columnKey .getId ());
138
- default :
139
- throw new GremlinResultParserException (columnKey .getItemCase () + " is invalid" );
140
- }
141
- }
142
-
143
- // propertyId is in String format, i.e. "1"
144
- private String getPropertyName (String nameOrId ) {
145
- Common .NameOrId .Builder builder = Common .NameOrId .newBuilder ();
146
- if (nameOrId .matches ("^[0-9]+$" )) {
147
- builder .setId (Integer .valueOf (nameOrId ));
148
- } else {
149
- builder .setName (nameOrId );
150
- }
151
- return ParserUtils .getKeyName (builder .build (), FfiKeyType .Column );
152
- }
153
- },
154
- UNION {
155
- @ Override
156
- public Object parseFrom (IrResult .Results results ) {
157
- GremlinResultParser resultParser = inferFromIrResults (results );
158
- return resultParser .parseFrom (results );
159
- }
160
-
161
- // try to infer from the results
162
- private GremlinResultParser inferFromIrResults (IrResult .Results results ) {
163
- int columns = results .getRecord ().getColumnsList ().size ();
164
- logger .debug ("result is {}" , results );
165
- if (columns == 1 ) {
166
- IrResult .Entry entry = ParserUtils .getHeadEntry (results );
167
- switch (entry .getInnerCase ()) {
168
- case ELEMENT :
169
- IrResult .Element element = entry .getElement ();
170
- if (element .getInnerCase () == IrResult .Element .InnerCase .VERTEX
171
- || element .getInnerCase () == IrResult .Element .InnerCase .EDGE
172
- || element .getInnerCase ()
173
- == IrResult .Element .InnerCase .GRAPH_PATH ) {
174
- return GRAPH_ELEMENT ;
175
- } else if (element .getInnerCase () == IrResult .Element .InnerCase .OBJECT ) {
176
- Common .Value value = element .getObject ();
177
- if (value .getItemCase ()
178
- == Common .Value .ItemCase .PAIR_ARRAY ) { // project
179
- return PROJECT_VALUE ;
180
- } else { // simple type
181
- return SINGLE_VALUE ;
182
- }
183
- } else {
184
- throw new GremlinResultParserException (
185
- element .getInnerCase () + " is invalid" );
186
- }
187
- case COLLECTION : // path()
188
- default :
189
- throw new GremlinResultParserException (
190
- entry .getInnerCase () + " is unsupported yet" );
191
- }
192
- } else if (columns > 1 ) { // project or group
193
- return PROJECT_VALUE ;
194
- } else {
195
- throw new GremlinResultParserException ("columns should not be empty" );
196
- }
197
- }
198
- },
199
50
SUBGRAPH {
200
51
@ Override
201
52
public Object parseFrom (IrResult .Results results ) {
0 commit comments