-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support float type in data frame (#129)
Signed-off-by: Yaliang Wu <ylwu@amazon.com>
- Loading branch information
Showing
9 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
common/src/main/java/org/opensearch/ml/common/dataframe/FloatValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
* | ||
*/ | ||
|
||
package org.opensearch.ml.common.dataframe; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
import lombok.experimental.FieldDefaults; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) | ||
@RequiredArgsConstructor | ||
@ToString | ||
public class FloatValue implements ColumnValue { | ||
float value; | ||
|
||
@Override | ||
public ColumnType columnType() { | ||
return ColumnType.FLOAT; | ||
} | ||
|
||
@Override | ||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public double doubleValue() { | ||
return Float.valueOf(value).doubleValue(); | ||
} | ||
|
||
@Override | ||
public float floatValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeEnum(columnType()); | ||
out.writeFloat(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
common/src/test/java/org/opensearch/ml/common/dataframe/FloatValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
* | ||
*/ | ||
|
||
package org.opensearch.ml.common.dataframe; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.common.xcontent.XContentType; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class FloatValueTest { | ||
|
||
@Test | ||
public void floatValue() { | ||
FloatValue floatValue = new FloatValue(2.1f); | ||
assertEquals(ColumnType.FLOAT, floatValue.columnType()); | ||
assertEquals(2.1f, floatValue.getValue()); | ||
assertEquals(2.1f, floatValue.floatValue(), 1e-5); | ||
assertEquals(2.1d, floatValue.doubleValue(), 1e-5); | ||
} | ||
|
||
@Test | ||
public void testToXContent() throws IOException { | ||
FloatValue floatValue = new FloatValue(2.1f); | ||
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); | ||
floatValue.toXContent(builder); | ||
|
||
assertNotNull(builder); | ||
String jsonStr = Strings.toString(builder); | ||
assertEquals("{\"column_type\":\"FLOAT\",\"value\":2.1}", jsonStr); | ||
} | ||
} |