-
Notifications
You must be signed in to change notification settings - Fork 640
feat(core): add DECIMAL (BigDecimal) property data type #3209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a28554e
9d5eaab
bae56ca
834ef89
0146849
47aa818
0bb7724
d7eb80c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.api.graph; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
|
|
||
| /** | ||
| * Reads the "properties" object of a vertex or edge body so that a JSON | ||
| * fraction keeps every digit: it becomes a BigDecimal instead of a double, | ||
| * which is what a DECIMAL property key needs and what every numeric key | ||
| * narrows through DataType.valueToNumber as before. Only property values | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, thanks. Done in the first of your forms: |
||
| * are read this way; the rest of the request body (job parameters, schema | ||
| * userdata, query options) keeps Jackson's default number types. | ||
| */ | ||
| public class PropertiesDeserializer extends JsonDeserializer<Map<String, Object>> { | ||
|
|
||
| @Override | ||
| public Map<String, Object> deserialize(JsonParser parser, | ||
| DeserializationContext ctxt) | ||
| throws IOException { | ||
| JsonToken token = parser.currentToken(); | ||
| if (token == JsonToken.VALUE_NULL) { | ||
| return null; | ||
| } | ||
| if (token != JsonToken.START_OBJECT) { | ||
| throw JsonMappingException.from(parser, | ||
| "Expected an object for 'properties', but got " + token); | ||
| } | ||
| return readObject(parser); | ||
| } | ||
|
|
||
| private static Map<String, Object> readObject(JsonParser parser) | ||
| throws IOException { | ||
| Map<String, Object> object = new LinkedHashMap<>(); | ||
| while (parser.nextToken() != JsonToken.END_OBJECT) { | ||
| String name = parser.currentName(); | ||
| parser.nextToken(); | ||
| object.put(name, readValue(parser)); | ||
| } | ||
| return object; | ||
| } | ||
|
|
||
| private static List<Object> readArray(JsonParser parser) | ||
| throws IOException { | ||
| List<Object> array = new ArrayList<>(); | ||
| while (parser.nextToken() != JsonToken.END_ARRAY) { | ||
| array.add(readValue(parser)); | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| /** One value at the parser's current token: exact fractions, nested objects and arrays. */ | ||
| public static Object readValue(JsonParser parser) throws IOException { | ||
| JsonToken token = parser.currentToken(); | ||
| switch (token) { | ||
| case START_OBJECT: | ||
| return readObject(parser); | ||
| case START_ARRAY: | ||
| return readArray(parser); | ||
| case VALUE_STRING: | ||
| return parser.getText(); | ||
| case VALUE_NUMBER_INT: | ||
| return parser.getNumberValue(); | ||
| case VALUE_NUMBER_FLOAT: | ||
| // Exact: the literal's digits, not the nearest double | ||
| return parser.getDecimalValue(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. |
||
| case VALUE_TRUE: | ||
| return Boolean.TRUE; | ||
| case VALUE_FALSE: | ||
| return Boolean.FALSE; | ||
| case VALUE_NULL: | ||
| return null; | ||
| default: | ||
| throw JsonMappingException.from(parser, | ||
| "Unexpected token in 'properties': " + token); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.api.schema; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.hugegraph.api.graph.PropertiesDeserializer; | ||
| import org.apache.hugegraph.schema.Userdata; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
| import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
|
||
| /** | ||
| * The userdata of a property key: {@code ~default_value} is read like an | ||
| * element property (a JSON fraction becomes a BigDecimal with every digit, | ||
| * so a DECIMAL default reaches the key exactly), every other entry keeps | ||
| * Jackson's default types, so custom metadata such as {@code {"rate":0.85}} | ||
| * stays a double and round-trips as a JSON number. | ||
| */ | ||
| public class UserdataDeserializer extends StdDeserializer<Userdata> { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| public UserdataDeserializer() { | ||
| super(Userdata.class); | ||
| } | ||
|
|
||
| @Override | ||
| public Userdata deserialize(JsonParser parser, DeserializationContext context) | ||
| throws IOException { | ||
| JsonToken token = parser.currentToken(); | ||
| if (token == JsonToken.VALUE_NULL) { | ||
| return null; | ||
| } | ||
| if (token != JsonToken.START_OBJECT) { | ||
| throw JsonMappingException.from(parser, | ||
| "Expected an object for 'user_data', but got " + token); | ||
| } | ||
| Map<String, Object> map = new LinkedHashMap<>(); | ||
| while (parser.nextToken() != JsonToken.END_OBJECT) { | ||
| String name = parser.currentName(); | ||
| parser.nextToken(); | ||
| if (Userdata.DEFAULT_VALUE.equals(name)) { | ||
| map.put(name, PropertiesDeserializer.readValue(parser)); | ||
| } else { | ||
| map.put(name, context.readValue(parser, Object.class)); | ||
| } | ||
| } | ||
| return new Userdata(map); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: The new method was inserted between
@SuppressWarnings("unchecked")andparseProperties(), so the annotation now applies tonormalizeProperties()andparseProperties()loses it. The annotation also sits above the Javadoc, so the Javadoc tool no longer attaches that comment tonormalizeProperties().PropertyKey.javahas the same problem at lines 123-133: the block that describesnormalizeDefaultValue()sits directly above the separate Javadoc ofundoExact(), so neither method gets the right documentation.Requested change: put
@SuppressWarnings("unchecked")back directly aboveparseProperties(), and move thenormalizeDefaultValue()Javadoc inPropertyKeyto that method.