-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ExpressionMethods.java
226 lines (211 loc) · 7.65 KB
/
ExpressionMethods.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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 Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.cypher.internal.runtime.compiled.expressions;
import org.opencypher.v9_0.util.CypherTypeException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.neo4j.internal.kernel.api.CursorFactory;
import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.internal.kernel.api.PropertyCursor;
import org.neo4j.internal.kernel.api.Transaction;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.ArrayValue;
import org.neo4j.values.storable.DoubleValue;
import org.neo4j.values.storable.DurationValue;
import org.neo4j.values.storable.NumberValue;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.StringValue;
import org.neo4j.values.storable.TemporalValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.VirtualValues;
import static org.neo4j.values.storable.Values.NO_VALUE;
import static org.neo4j.values.storable.Values.doubleValue;
import static org.neo4j.values.storable.Values.stringValue;
/**
* This class contains static helper methods used by the compiled expressions
*/
public final class ExpressionMethods
{
private ExpressionMethods()
{
throw new UnsupportedOperationException( "Do not instantiate" );
}
public static DoubleValue sin( AnyValue in )
{
if ( in instanceof NumberValue )
{
return doubleValue( Math.sin( ((NumberValue) in).doubleValue() ) );
}
else
{
throw new CypherTypeException( "sin requires numbers", null );
}
}
public static DoubleValue round( AnyValue in )
{
if ( in instanceof NumberValue )
{
return doubleValue( Math.round( ((NumberValue) in).doubleValue() ) );
}
else
{
throw new CypherTypeException( "round requires numbers", null );
}
}
public static DoubleValue rand()
{
return doubleValue( ThreadLocalRandom.current().nextDouble() );
}
//TODO this is horrible spaghetti code, we should push most of this down to AnyValue
public static AnyValue add( AnyValue lhs, AnyValue rhs )
{
if ( lhs == NO_VALUE || rhs == NO_VALUE )
{
return NO_VALUE;
}
if ( lhs instanceof NumberValue && rhs instanceof NumberValue )
{
return ((NumberValue) lhs).plus( (NumberValue) rhs );
}
//List addition
boolean lhsIsListValue = lhs instanceof ListValue;
if ( lhsIsListValue && rhs instanceof ListValue )
{
return VirtualValues.concat( (ListValue) lhs, (ListValue) rhs );
}
else if ( lhsIsListValue )
{
return VirtualValues.appendToList( (ListValue) lhs, rhs );
}
else if ( rhs instanceof ListValue )
{
return VirtualValues.prependToList( (ListValue) rhs, lhs );
}
// String addition
if ( lhs instanceof TextValue )
{
if ( rhs instanceof Value )
{
// Unfortunately string concatenation is not defined for temporal and spatial types, so we need to
// exclude them
if ( !(rhs instanceof TemporalValue || rhs instanceof DurationValue || rhs instanceof PointValue) )
{
return stringValue( ((TextValue) lhs).stringValue() + ((Value) rhs).prettyPrint());
}
else
{
//TODO this seems wrong but it is what we currently do in compiled runtime
return stringValue(((TextValue) lhs).stringValue() + String.valueOf( rhs ));
}
}
}
if ( rhs instanceof TextValue )
{
if ( lhs instanceof Value )
{
// Unfortunately string concatenation is not defined for temporal and spatial types, so we need to
// exclude them
if ( !(lhs instanceof TemporalValue || lhs instanceof DurationValue || lhs instanceof PointValue) )
{
return stringValue( ((Value) lhs).prettyPrint() + ((TextValue) rhs).stringValue());
}
else
{
//TODO this seems wrong but it is what we currently do in compiled runtime
return stringValue(String.valueOf( lhs ) + ((TextValue) rhs).stringValue() );
}
}
}
// Temporal values
if ( lhs instanceof TemporalValue )
{
if ( rhs instanceof DurationValue )
{
return ((TemporalValue) lhs).plus( (DurationValue) rhs );
}
}
if ( lhs instanceof DurationValue )
{
if ( rhs instanceof TemporalValue )
{
return ((TemporalValue) rhs).plus( (DurationValue) lhs );
}
if ( rhs instanceof DurationValue )
{
return ((DurationValue) lhs).add( (DurationValue) rhs );
}
}
throw new CypherTypeException(
String.format( "Don't know how to add `%s` and `%s`", lhs, rhs), null );
}
public static AnyValue subtract( AnyValue lhs, AnyValue rhs )
{
if ( lhs instanceof NumberValue && rhs instanceof NumberValue )
{
return ((NumberValue) lhs).minus( (NumberValue) rhs );
}
else
{
//todo
throw new CypherTypeException( "can only subtract numbers", null );
}
}
public static AnyValue multiply( AnyValue lhs, AnyValue rhs )
{
if ( lhs instanceof NumberValue && rhs instanceof NumberValue )
{
return ((NumberValue) lhs).times( (NumberValue) rhs );
}
else
{
//todo
throw new CypherTypeException( "can only multiply numbers", null );
}
}
public static Value nodeProperty( Transaction tx, long node, int property )
{
CursorFactory cursors = tx.cursors();
try ( NodeCursor nodes = cursors.allocateNodeCursor();
PropertyCursor properties = cursors.allocatePropertyCursor() )
{
tx.dataRead().singleNode( node, nodes );
if ( nodes.next() )
{
nodes.properties( properties );
while ( properties.next() )
{
if ( properties.propertyKey() == property )
{
return properties.propertyValue();
}
}
}
return Values.NO_VALUE;
}
}
}