-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CompiledConversionUtils.java
705 lines (664 loc) · 24.6 KB
/
CompiledConversionUtils.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
* Copyright (c) 2002-2018 "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.codegen;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.neo4j.cypher.internal.util.v3_4.CypherTypeException;
import org.neo4j.cypher.internal.util.v3_4.IncomparableValuesException;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.kernel.impl.core.EmbeddedProxySPI;
import org.neo4j.kernel.impl.util.NodeProxyWrappingNodeValue;
import org.neo4j.kernel.impl.util.RelationshipProxyWrappingValue;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.values.AnyValue;
import org.neo4j.values.SequenceValue;
import org.neo4j.values.storable.ArrayValue;
import org.neo4j.values.storable.BooleanValue;
import org.neo4j.values.storable.DurationValue;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.NodeValue;
import org.neo4j.values.virtual.RelationshipValue;
import org.neo4j.values.virtual.VirtualNodeValue;
import org.neo4j.values.virtual.VirtualRelationshipValue;
import org.neo4j.values.virtual.VirtualValues;
import static java.lang.String.format;
import static org.neo4j.values.SequenceValue.IterationPreference.RANDOM_ACCESS;
import static org.neo4j.values.storable.Values.NO_VALUE;
// Class with static methods used by compiled execution plans
@SuppressWarnings( "unused" )
public abstract class CompiledConversionUtils
{
public static boolean coerceToPredicate( Object value )
{
if ( value == null || value == Values.NO_VALUE )
{
return false;
}
if ( value instanceof BooleanValue )
{
return ((BooleanValue) value).booleanValue();
}
if ( value instanceof Boolean )
{
return (boolean) value;
}
if ( value instanceof ArrayValue )
{
return ((ArrayValue) value).length() > 0;
}
if ( value.getClass().isArray() )
{
return Array.getLength( value ) > 0;
}
throw new CypherTypeException( "Don't know how to treat that as a predicate: " + value.toString(), null );
}
public static Set<?> toSet( Object value )
{
if ( value == null || value == NO_VALUE )
{
return Collections.emptySet();
}
else if ( value instanceof SequenceValue )
{
SequenceValue sequenceValue = (SequenceValue) value;
Iterator<AnyValue> iterator = sequenceValue.iterator();
Set<AnyValue> set;
if ( sequenceValue.iterationPreference() == RANDOM_ACCESS )
{
// If we have a random access sequence value length() should be cheap and we can optimize the initial capacity
int length = sequenceValue.length();
set = new HashSet<>( length );
for ( int i = 0; i < length; i++ )
{
set.add( sequenceValue.value( i ) );
}
}
else
{
set = new HashSet<>();
while ( iterator.hasNext() )
{
AnyValue element = iterator.next();
set.add( element );
}
}
return set;
}
else if ( value instanceof Collection<?> )
{
return new HashSet<>( (Collection<?>) value );
}
else if ( value instanceof LongStream )
{
LongStream stream = (LongStream) value;
return stream.boxed().collect( Collectors.toSet() );
}
else if ( value instanceof IntStream )
{
IntStream stream = (IntStream) value;
return stream.boxed().collect( Collectors.toSet() );
}
else if ( value instanceof DoubleStream )
{
DoubleStream stream = (DoubleStream) value;
return stream.boxed().collect( Collectors.toSet() );
}
else if ( value.getClass().isArray() )
{
int len = Array.getLength( value );
HashSet<Object> collection = new HashSet<>( len );
for ( int i = 0; i < len; i++ )
{
collection.add( Array.get( value, i ) );
}
return collection;
}
throw new CypherTypeException(
"Don't know how to create a set out of " + value.getClass().getSimpleName(), null );
}
public static CompositeKey compositeKey( long... keys )
{
return new CompositeKey( keys );
}
public static class CompositeKey
{
private final long[] key;
private CompositeKey( long[] key )
{
this.key = key;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
CompositeKey that = (CompositeKey) o;
return Arrays.equals( key, that.key );
}
@Override
public int hashCode()
{
return Arrays.hashCode( key );
}
}
/**
* Checks equality according to OpenCypher
* @return true if equal, false if not equal and null if incomparable
*/
public static Boolean equals( Object lhs, Object rhs )
{
if ( lhs == null || rhs == null || lhs == NO_VALUE || rhs == NO_VALUE )
{
return null;
}
boolean lhsVirtualNodeValue = lhs instanceof VirtualNodeValue;
if ( lhsVirtualNodeValue || rhs instanceof VirtualNodeValue || lhs instanceof VirtualRelationshipValue ||
rhs instanceof VirtualRelationshipValue )
{
if ( (lhsVirtualNodeValue && !(rhs instanceof VirtualNodeValue)) ||
(rhs instanceof VirtualNodeValue && !lhsVirtualNodeValue) ||
(lhs instanceof VirtualRelationshipValue && !(rhs instanceof VirtualRelationshipValue)) ||
(rhs instanceof VirtualRelationshipValue && !(lhs instanceof VirtualRelationshipValue)) )
{
throw new IncomparableValuesException( lhs.getClass().getSimpleName(), rhs.getClass().getSimpleName() );
}
return lhs.equals( rhs );
}
AnyValue lhsValue = lhs instanceof AnyValue ? (AnyValue) lhs : ValueUtils.of( lhs );
AnyValue rhsValue = rhs instanceof AnyValue ? (AnyValue) rhs : ValueUtils.of( rhs );
return lhsValue.ternaryEquals( rhsValue );
}
// Ternary OR
public static Boolean or( Object lhs, Object rhs )
{
Boolean l = toBooleanOrNull( lhs );
Boolean r = toBooleanOrNull( rhs );
if ( l == null && r == null )
{
return null;
}
else if ( l == null )
{
return r ? true : null;
}
else if ( r == null )
{
return l ? true : null;
}
return l || r;
}
// Ternary NOT
public static Boolean not( Object predicate )
{
Boolean b = toBooleanOrNull( predicate );
if ( b == null )
{
return null;
}
return !b;
}
private static Boolean toBooleanOrNull( Object o )
{
if ( o == null || o == NO_VALUE )
{
return null;
}
else if ( o instanceof Boolean )
{
return (Boolean) o;
}
else if ( o instanceof BooleanValue )
{
return ((BooleanValue) o).booleanValue();
}
throw new CypherTypeException( "Don't know how to treat that as a boolean: " + o.toString(), null );
}
@SuppressWarnings( {"unchecked", "WeakerAccess"} )
public static AnyValue materializeAnyResult( EmbeddedProxySPI proxySpi, Object anyValue )
{
if ( anyValue == null || anyValue == NO_VALUE )
{
return NO_VALUE;
}
else if ( anyValue instanceof AnyValue )
{
return materializeAnyValueResult( proxySpi, anyValue );
}
else if ( anyValue instanceof List )
{
return VirtualValues.fromList( (List<AnyValue>) (((List) anyValue).stream()
.map( v -> materializeAnyResult( proxySpi, v ) ).collect( Collectors.toList() ) ) );
}
else if ( anyValue instanceof Map )
{
Map<String,?> incoming = (Map<String,?>) anyValue;
HashMap<String,AnyValue> outgoing = new HashMap<>( incoming.size() );
for ( Map.Entry<String,?> entry : incoming.entrySet() )
{
outgoing.put( entry.getKey(), materializeAnyResult( proxySpi, entry.getValue() ) );
}
return VirtualValues.map( outgoing );
}
else if ( anyValue instanceof PrimitiveNodeStream )
{
return VirtualValues.fromList( ((PrimitiveNodeStream) anyValue).longStream()
.mapToObj( id -> (AnyValue) ValueUtils.fromNodeProxy( proxySpi.newNodeProxy( id ) ) )
.collect( Collectors.toList() ) );
}
else if ( anyValue instanceof PrimitiveRelationshipStream )
{
return VirtualValues.fromList( ((PrimitiveRelationshipStream) anyValue).longStream()
.mapToObj( id -> (AnyValue) ValueUtils.fromRelationshipProxy( proxySpi.newRelationshipProxy( id ) ) )
.collect( Collectors.toList() ) );
}
else if ( anyValue instanceof LongStream )
{
long[] array = ((LongStream) anyValue).toArray();
return Values.longArray( array );
}
else if ( anyValue instanceof DoubleStream )
{
double[] array = ((DoubleStream) anyValue).toArray();
return Values.doubleArray( array );
}
else if ( anyValue instanceof IntStream )
{
// IntStream is only used for list of primitive booleans
return VirtualValues.fromList( ((IntStream) anyValue).mapToObj( i -> Values.booleanValue( i != 0 ) ).collect( Collectors.toList() ) );
}
else if ( anyValue.getClass().isArray() )
{
Class<?> componentType = anyValue.getClass().getComponentType();
int length = Array.getLength( anyValue );
if ( componentType.isPrimitive() )
{
Object copy = Array.newInstance( componentType, length );
//noinspection SuspiciousSystemArraycopy
System.arraycopy( anyValue, 0, copy, 0, length );
return ValueUtils.of( copy );
}
else if ( anyValue instanceof String[] )
{
return Values.stringArray( (String[]) anyValue );
}
else
{
AnyValue[] copy = new AnyValue[length];
for ( int i = 0; i < length; i++ )
{
copy[i] = materializeAnyResult( proxySpi, Array.get( anyValue, i ) );
}
return VirtualValues.list( copy );
}
}
else
{
return ValueUtils.of( anyValue );
}
}
// NOTE: This assumes anyValue is an instance of AnyValue
public static AnyValue materializeAnyValueResult( EmbeddedProxySPI proxySpi, Object anyValue )
{
if ( anyValue instanceof VirtualNodeValue )
{
if ( anyValue instanceof NodeValue )
{
return (AnyValue) anyValue;
}
return ValueUtils.fromNodeProxy( proxySpi.newNodeProxy( ((VirtualNodeValue) anyValue).id() ) );
}
if ( anyValue instanceof VirtualRelationshipValue )
{
if ( anyValue instanceof RelationshipValue )
{
return (AnyValue) anyValue;
}
return ValueUtils.fromRelationshipProxy( proxySpi.newRelationshipProxy( ((VirtualRelationshipValue) anyValue).id() ) );
}
// If it is a list or map, run it through a ValueMapper that will create proxy objects for entities if needed.
// This will first do a dry run and return as it is if no conversion is needed.
// If in the future we will always create proxy objects directly whenever we create values we can skip this
// Doing this conversion lazily instead, by wrapping with TransformedListValue or TransformedMapValue is probably not a
// good idea because of the complexities involved (see TOMBSTONE in VirtualValues about why TransformedListValue was killed).
// NOTE: There is also a case where a ListValue can be storable (ArrayValueListValue) where no conversion is needed
if ( (anyValue instanceof ListValue && !((ListValue) anyValue).storable()) || anyValue instanceof MapValue )
{
return CompiledMaterializeValueMapper.mapAnyValue( proxySpi, (AnyValue) anyValue );
}
return (AnyValue) anyValue;
}
public static NodeValue materializeNodeValue( EmbeddedProxySPI proxySpi, Object anyValue )
{
// Null check has to be done outside by the generated code
if ( anyValue instanceof NodeValue )
{
return (NodeValue) anyValue;
}
else if ( anyValue instanceof VirtualNodeValue )
{
return ValueUtils.fromNodeProxy( proxySpi.newNodeProxy( ((VirtualNodeValue) anyValue).id() ) );
}
else if ( anyValue instanceof Node )
{
return ValueUtils.fromNodeProxy( (Node) anyValue );
}
throw new IllegalArgumentException( "Do not know how to materialize node value from type " + anyValue.getClass().getName() );
}
public static RelationshipValue materializeRelationshipValue( EmbeddedProxySPI proxySpi, Object anyValue )
{
// Null check has to be done outside by the generated code
if ( anyValue instanceof RelationshipValue )
{
return (RelationshipValue) anyValue;
}
else if ( anyValue instanceof VirtualRelationshipValue )
{
return ValueUtils.fromRelationshipProxy( proxySpi.newRelationshipProxy( ((VirtualRelationshipValue) anyValue).id() ) );
}
else if ( anyValue instanceof Relationship )
{
return ValueUtils.fromRelationshipProxy( (Relationship) anyValue );
}
throw new IllegalArgumentException( "Do not know how to materialize relationship value from type " + anyValue.getClass().getName() );
}
public static Iterator iteratorFrom( Object iterable )
{
if ( iterable instanceof Iterable )
{
return ((Iterable) iterable).iterator();
}
else if ( iterable instanceof PrimitiveEntityStream )
{
return ((PrimitiveEntityStream) iterable).iterator();
}
else if ( iterable instanceof LongStream )
{
return ((LongStream) iterable).iterator();
}
else if ( iterable instanceof DoubleStream )
{
return ((DoubleStream) iterable).iterator();
}
else if ( iterable instanceof IntStream )
{
return ((IntStream) iterable).iterator();
}
else if ( iterable == null || iterable == NO_VALUE )
{
return Collections.emptyIterator();
}
else if ( iterable.getClass().isArray() )
{
return new ArrayIterator( iterable );
}
else
{
return Stream.of(iterable).iterator();
}
}
@SuppressWarnings( "unchecked" )
public static LongStream toLongStream( Object list )
{
if ( list == null )
{
return LongStream.empty();
}
else if ( list instanceof SequenceValue )
{
throw new IllegalArgumentException( "Need to implement support for SequenceValue in CompiledConversionUtils.toLongStream" );
}
else if ( list instanceof List )
{
return ((List) list).stream().mapToLong( n -> ((Number) n).longValue() );
}
else if ( Object[].class.isAssignableFrom( list.getClass() ) )
{
return Arrays.stream( (Object[]) list ).mapToLong( n -> ((Number) n).longValue() );
}
else if ( list instanceof byte[] )
{
byte[] array = (byte[]) list;
return IntStream.range( 0, array.length ).mapToLong( i -> array[i] );
}
else if ( list instanceof short[] )
{
short[] array = (short[]) list;
return IntStream.range( 0, array.length ).mapToLong( i -> array[i] );
}
else if ( list instanceof int[] )
{
return IntStream.of( (int[]) list ).mapToLong( i -> i );
}
else if ( list instanceof long[] )
{
return LongStream.of( (long[]) list );
}
throw new IllegalArgumentException( format( "Can not be converted to stream: %s", list.getClass().getName() ) );
}
@SuppressWarnings( "unchecked" )
public static DoubleStream toDoubleStream( Object list )
{
if ( list == null )
{
return DoubleStream.empty();
}
else if ( list instanceof SequenceValue )
{
throw new IllegalArgumentException( "Need to implement support for SequenceValue in CompiledConversionUtils.toDoubleStream" );
}
else if ( list instanceof List )
{
return ((List) list).stream().mapToDouble( n -> ((Number) n).doubleValue() );
}
else if ( Object[].class.isAssignableFrom( list.getClass() ) )
{
return Arrays.stream( (Object[]) list ).mapToDouble( n -> ((Number) n).doubleValue() );
}
else if ( list instanceof float[] )
{
float[] array = (float[]) list;
return IntStream.range( 0, array.length ).mapToDouble( i -> array[i] );
}
else if ( list instanceof double[] )
{
return DoubleStream.of( (double[]) list );
}
throw new IllegalArgumentException( format( "Can not be converted to stream: %s", list.getClass().getName() ) );
}
@SuppressWarnings( "unchecked" )
public static IntStream toBooleanStream( Object list )
{
if ( list == null )
{
return IntStream.empty();
}
else if ( list instanceof SequenceValue )
{
throw new IllegalArgumentException( "Need to implement support for SequenceValue in CompiledConversionUtils.toBooleanStream" );
}
else if ( list instanceof List )
{
return ((List) list).stream().mapToInt( n -> ((Number) n).intValue() );
}
else if ( Object[].class.isAssignableFrom( list.getClass() ) )
{
return Arrays.stream( (Object[]) list ).mapToInt( n -> ((Number) n).intValue() );
}
else if ( list instanceof boolean[] )
{
boolean[] array = (boolean[]) list;
return IntStream.range( 0, array.length ).map( i -> (array[i]) ? 1 : 0 );
}
throw new IllegalArgumentException( format( "Can not be converted to stream: %s", list.getClass().getName() ) );
}
@SuppressWarnings( "unused" ) // called from compiled code
public static long unboxNodeOrNull( VirtualNodeValue value )
{
if ( value == null )
{
return -1L;
}
return value.id();
}
@SuppressWarnings( "unused" ) // called from compiled code
public static long unboxRelationshipOrNull( VirtualRelationshipValue value )
{
if ( value == null )
{
return -1L;
}
return value.id();
}
@SuppressWarnings( "unused" ) // called from compiled code
public static long extractLong( Object obj )
{
if ( obj == null || obj == NO_VALUE )
{
return -1L;
}
else if ( obj instanceof VirtualNodeValue )
{
return ((VirtualNodeValue) obj).id();
}
else if ( obj instanceof VirtualRelationshipValue )
{
return ((VirtualRelationshipValue) obj).id();
}
else if ( obj instanceof Long )
{
return (Long) obj;
}
else
{
throw new IllegalArgumentException(
format( "Can not be converted to long: %s", obj.getClass().getName() ) );
}
}
//In the store we only support storable Value types and arrays thereof.
//In cypher we must make an effort to transform Cypher lists to appropriate arrays whenever
//we are using sending values down to the store or to an index.
public static Object makeValueNeoSafe( Object object )
{
AnyValue value = object instanceof AnyValue ? ((AnyValue) object) : ValueUtils.of( object );
return org.neo4j.cypher.internal.runtime.interpreted.makeValueNeoSafe.apply( value );
}
@SuppressWarnings( "unchecked" )
public static Object mapGetProperty( Object object, String key )
{
if ( object instanceof MapValue )
{
MapValue map = (MapValue) object;
return map.get( key );
}
if ( object instanceof NodeProxyWrappingNodeValue )
{
return Values.of( ((NodeProxyWrappingNodeValue) object).nodeProxy().getProperty( key ) );
}
if ( object instanceof RelationshipProxyWrappingValue )
{
return Values.of( ((RelationshipProxyWrappingValue) object).relationshipProxy().getProperty( key ) );
}
if ( object instanceof PropertyContainer ) // Entity that is not wrapped by an AnyValue
{
return Values.of( ((PropertyContainer) object).getProperty( key ) );
}
if ( object instanceof NodeValue )
{
return ((NodeValue) object).properties().get( key );
}
if ( object instanceof RelationshipValue )
{
return ((RelationshipValue) object).properties().get( key );
}
if ( object instanceof Map<?,?> )
{
Map<String,Object> map = (Map<String,Object>) object;
return map.get( key );
}
if ( object instanceof TemporalValue<?,?> )
{
return ((TemporalValue<?,?>) object).get( key );
}
if ( object instanceof DurationValue )
{
return ((DurationValue) object).get( key );
}
if ( object instanceof PointValue )
{
return ((PointValue) object).get( key );
}
// NOTE: VirtualNodeValue and VirtualRelationshipValue will fall through to here
// To handle these we would need specialized cursor code
throw new CypherTypeException( String.format( "Type mismatch: expected a map but was %s", object ), null );
}
static class ArrayIterator implements Iterator
{
private int position;
private final int len;
private final Object array;
private ArrayIterator( Object array )
{
this.position = 0;
this.len = Array.getLength( array );
this.array = array;
}
@Override
public boolean hasNext()
{
return position < len;
}
@Override
public Object next()
{
if ( position >= len )
{
throw new NoSuchElementException();
}
int offset = position++;
return Array.get( array, offset );
}
}
}