-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CompiledOrderabilityUtils.java
409 lines (383 loc) · 12.9 KB
/
CompiledOrderabilityUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.compiler.v3_2.util;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.neo4j.cypher.internal.compiler.v3_2.spi.NodeIdWrapper;
import org.neo4j.cypher.internal.compiler.v3_2.spi.RelationshipIdWrapper;
import org.neo4j.cypher.internal.frontend.v3_2.IncomparableValuesException;
import org.neo4j.cypher.internal.frontend.v3_2.UnorderableValueException;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.helpers.MathUtil;
import static java.lang.String.format;
/**
* Helper class for dealing with orderability in compiled code.
*
* <h1>
* Orderability
*
* <a href="https://github.com/opencypher/openCypher/blob/master/cip/1.accepted/CIP2016-06-14-Define-comparability-and-equality-as-well-as-orderability-and-equivalence.adoc">
* The Cypher CIP defining orderability
* </a>
*
* <p>
* Ascending global sort order of disjoint types:
*
* <ul>
* <li> MAP types
* <ul>
* <li> Regular map
*
* <li> NODE
*
* <li> RELATIONSHIP
* <ul>
*
* <li> LIST OF ANY?
*
* <li> PATH
*
* <li> STRING
*
* <li> BOOLEAN
*
* <li> NUMBER
* <ul>
* <li> NaN values are treated as the largest numbers in orderability only (i.e. they are put after positive infinity)
* </ul>
* <li> VOID (i.e. the type of null)
* </ul>
*
* TBD: POINT and GEOMETRY
*/
public class CompiledOrderabilityUtils
{
/**
* Do not instantiate this class
*/
private CompiledOrderabilityUtils()
{
throw new UnsupportedOperationException();
}
/**
* Compare with Cypher orderability semantics for disjoint types
*
* @param lhs
* @param rhs
* @return
*/
public static int compare( Object lhs, Object rhs )
{
if ( lhs == rhs )
{
return 0;
}
// null is greater than any other type
else if ( lhs == null )
{
return 1;
}
else if ( rhs == null )
{
return -1;
}
// Compare the types
// TODO: Test coverage for the Orderability CIP
SuperType leftType = SuperType.ofValue( lhs );
SuperType rightType = SuperType.ofValue( rhs );
int typeComparison = SuperType.TYPE_ID_COMPARATOR.compare( leftType, rightType );
if ( typeComparison != 0 )
{
// Types are different an decides the order
return typeComparison;
}
return leftType.comparator.compare( lhs, rhs );
}
public enum SuperType
{
MAP( 0, FALLBACK_COMPARATOR /*TODO*/ ),
NODE( 1, NODE_COMPARATOR ),
RELATIONSHIP( 2, RELATIONSHIP_COMPARATOR ),
LIST( 3, LIST_COMPARATOR ),
PATH( 4, PATH_COMPARATOR ),
STRING( 5, STRING_COMPARATOR ),
BOOLEAN( 6, BOOLEAN_COMPARATOR ),
NUMBER( 7, NUMBER_COMPARATOR ),
VOID( 8, VOID_COMPARATOR );
public final int typeId;
public final Comparator comparator;
SuperType( int typeId, Comparator comparator )
{
this.typeId = typeId;
this.comparator = comparator;
}
public boolean isSuperTypeOf( Object value )
{
return this == ofValue( value );
}
public static SuperType ofValue( Object value )
{
if ( value instanceof String || value instanceof Character )
{
return STRING;
}
else if ( value instanceof Number )
{
return NUMBER;
}
else if ( value instanceof Boolean )
{
return BOOLEAN;
}
else if ( value instanceof Map<?,?> )
{
return MAP;
}
else if ( value instanceof List<?> || value.getClass().isArray() )
{
return LIST;
}
else if ( value instanceof NodeIdWrapper )
{
if ( ((NodeIdWrapper) value).id() == -1 )
{
return VOID;
}
return NODE;
}
else if ( value instanceof RelationshipIdWrapper )
{
if ( ((RelationshipIdWrapper) value).id() == -1 )
{
return VOID;
}
return RELATIONSHIP;
}
// TODO is Path really the class that compiled runtime will be using?
else if ( value instanceof Path )
{
return PATH;
}
throw new UnorderableValueException( value.getClass().getSimpleName() );
}
public static Comparator<SuperType> TYPE_ID_COMPARATOR = new Comparator<SuperType>()
{
@Override
public int compare( SuperType left, SuperType right )
{
return left.typeId - right.typeId;
}
};
}
// NOTE: nulls are handled at the top of the public compare() method
// so the type-specific comparators should not check arguments for null
private static Comparator<Object> FALLBACK_COMPARATOR = new Comparator<Object>()
{
@Override
public int compare( Object lhs, Object rhs )
{
if ( lhs.getClass().isAssignableFrom( rhs.getClass() ) &&
lhs instanceof Comparable &&
rhs instanceof Comparable )
{
return ((Comparable) lhs).compareTo( rhs );
}
throw new IncomparableValuesException( lhs.getClass().getSimpleName(), rhs.getClass().getSimpleName() );
}
};
private static Comparator<Object> VOID_COMPARATOR = new Comparator<Object>()
{
@Override
public int compare( Object lhs, Object rhs )
{
return 0;
}
};
private static Comparator<Number> NUMBER_COMPARATOR = new Comparator<Number>()
{
@Override
public int compare( Number lhs, Number rhs )
{
// If floats, compare float values. If integer types, compare long values
if ( lhs instanceof Double && rhs instanceof Float )
{
return ((Double) lhs).compareTo( rhs.doubleValue() );
}
else if ( lhs instanceof Float && rhs instanceof Double )
{
return -((Double) rhs).compareTo( lhs.doubleValue() );
}
else if ( lhs instanceof Float && rhs instanceof Float )
{
return ((Float) lhs).compareTo( (Float) rhs );
}
else if ( lhs instanceof Double && rhs instanceof Double )
{
return ((Double) lhs).compareTo( (Double) rhs );
}
// Right hand side is neither Float nor Double
else if ( (lhs instanceof Double || lhs instanceof Float) )
{
return MathUtil.compareDoubleAgainstLong( lhs.doubleValue(), rhs.longValue() );
}
// Left hand side is neither Float nor Double
else if ( (rhs instanceof Double || rhs instanceof Float) )
{
return -MathUtil.compareDoubleAgainstLong( rhs.doubleValue(), lhs.longValue() );
}
// Everything else is a long from Cypher's point-of-view
return Long.compare( lhs.longValue(), rhs.longValue() );
}
};
private static Comparator<Object> STRING_COMPARATOR = new Comparator<Object>()
{
@Override
public int compare( Object lhs, Object rhs )
{
if ( lhs instanceof Character && rhs instanceof String )
{
return lhs.toString().compareTo( (String) rhs );
}
else if ( lhs instanceof String && rhs instanceof Character )
{
return ((String) lhs).compareTo( rhs.toString() );
}
else
{ return ((Comparable) lhs).compareTo( rhs ); }
}
};
private static Comparator<Boolean> BOOLEAN_COMPARATOR = new Comparator<Boolean>()
{
@Override
public int compare( Boolean lhs, Boolean rhs )
{
return lhs.compareTo( rhs );
}
};
private static Comparator<NodeIdWrapper> NODE_COMPARATOR = new Comparator<NodeIdWrapper>()
{
@Override
public int compare( NodeIdWrapper lhs, NodeIdWrapper rhs )
{
return Long.compare( lhs.id(), rhs.id() );
}
};
private static Comparator<RelationshipIdWrapper> RELATIONSHIP_COMPARATOR = new Comparator<RelationshipIdWrapper>()
{
@Override
public int compare( RelationshipIdWrapper lhs, RelationshipIdWrapper rhs )
{
return Long.compare( lhs.id(), rhs.id() );
}
};
// TODO test
private static Comparator<Path> PATH_COMPARATOR = new Comparator<Path>()
{
@Override
public int compare( Path lhs, Path rhs )
{
Iterator<PropertyContainer> lhsIter = lhs.iterator();
Iterator<PropertyContainer> rhsIter = lhs.iterator();
while ( lhsIter.hasNext() && rhsIter.hasNext() )
{
int result = CompiledOrderabilityUtils.compare( lhsIter.next(), rhsIter.next() );
if ( 0 != result )
{
return result;
}
}
return (lhsIter.hasNext()) ? 1
: (rhsIter.hasNext()) ? -1
: 0;
}
};
private static Comparator<Object> LIST_COMPARATOR = new Comparator<Object>()
{
@Override
public int compare( Object lhs, Object rhs )
{
Iterator lhsIter = toIterator( lhs );
Iterator rhsIter = toIterator( rhs );
while ( lhsIter.hasNext() && rhsIter.hasNext() )
{
int result = CompiledOrderabilityUtils.compare( lhsIter.next(), rhsIter.next() );
if ( 0 != result )
{
return result;
}
}
return (lhsIter.hasNext()) ? 1
: (rhsIter.hasNext()) ? -1
: 0;
}
private Iterator toIterator( Object o )
{
Class clazz = o.getClass();
if ( Iterable.class.isAssignableFrom( clazz) )
{
return ((Iterable) o).iterator();
}
else if ( Object[].class.isAssignableFrom( clazz) )
{
return Arrays.stream( (Object[]) o ).iterator();
}
else if ( clazz.equals( int[].class ) )
{
return IntStream.of( (int[]) o ).iterator();
}
else if ( clazz.equals( long[].class ) )
{
return LongStream.of( (long[]) o ).iterator();
}
else if ( clazz.equals( float[].class ) )
{
return IntStream.range( 0, ((float[]) o).length ).mapToObj( i -> ((float[]) o)[i] ).iterator();
}
else if ( clazz.equals( double[].class ) )
{
return DoubleStream.of( (double[]) o ).iterator();
}
else if ( clazz.equals( String[].class ) )
{
return Arrays.stream( (String[]) o ).iterator();
}
else if ( clazz.equals( boolean[].class ) )
{
// TODO Is there a better way to covert boolean[] to Iterator?
return IntStream.range( 0, ((boolean[]) o).length ).mapToObj( i -> ((boolean[]) o)[i] ).iterator();
}
else if ( clazz.equals( Boolean[].class ) )
{
return Arrays.stream( (Boolean[]) o ).iterator();
}
else
{
throw new UnsupportedOperationException( format( "Can not convert to iterator: %s", clazz.getName() ) );
}
}
};
}