Skip to content
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

support short and long type in data frame #131

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@

public enum ColumnType {
STRING,
SHORT,
INTEGER,
LONG,
DOUBLE,
BOOLEAN,
FLOAT,
NULL;

public static ColumnType from(Object object) {
if(object instanceof Short) {
return SHORT;
}

if(object instanceof Integer) {
return INTEGER;
}

if(object instanceof Long) {
return LONG;
}

if(object instanceof String) {
return STRING;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public interface ColumnValue extends Writeable, ToXContentObject {

Object getValue();

default short shortValue() {
throw new RuntimeException("the value isn't Short type");
}

default int intValue() {
throw new RuntimeException("the value isn't Integer type");
}

default long longValue() {
throw new RuntimeException("the value isn't Long type");
}

default String stringValue() {
throw new RuntimeException("the value isn't String type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ public ColumnValue build(Object object) {
return new NullValue();
}

if(object instanceof Short) {
return new ShortValue((Short)object);
}

if(object instanceof Integer) {
return new IntValue((Integer)object);
}

if(object instanceof Long) {
return new LongValue((Long)object);
}

if(object instanceof String) {
return new StringValue((String)object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ public class ColumnValueReader implements Writeable.Reader<ColumnValue> {
public ColumnValue read(StreamInput in) throws IOException {
ColumnType columnType = in.readEnum(ColumnType.class);
switch (columnType){
case SHORT:
return new ShortValue(in.readShort());
case INTEGER:
return new IntValue(in.readInt());
case LONG:
return new LongValue(in.readLong());
case DOUBLE:
return new DoubleValue(in.readDouble());
case STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public int intValue() {
return value;
}

@Override
public double doubleValue() {
return Integer.valueOf(value).doubleValue();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeEnum(columnType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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 LongValue implements ColumnValue {
long value;

@Override
public ColumnType columnType() {
return ColumnType.LONG;
}

@Override
public Object getValue() {
return value;
}

@Override
public long longValue() {
return value;
}

@Override
public double doubleValue() {
return Long.valueOf(value).doubleValue();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeEnum(columnType());
out.writeLong(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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 ShortValue implements ColumnValue {
short value;

@Override
public ColumnType columnType() {
return ColumnType.SHORT;
}

@Override
public Object getValue() {
return value;
}

@Override
public short shortValue() {
return value;
}

@Override
public double doubleValue() {
return Short.valueOf(value).doubleValue();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeEnum(columnType());
out.writeShort(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void build() {
value = ColumnValueBuilder.build(2);
assertEquals(ColumnType.INTEGER, value.columnType());
assertEquals(2, value.intValue());
assertEquals(2.0d, value.doubleValue(), 1e-5);

value = ColumnValueBuilder.build("string");
assertEquals(ColumnType.STRING, value.columnType());
Expand All @@ -49,6 +50,17 @@ public void build() {
value = ColumnValueBuilder.build(2.1f);
assertEquals(ColumnType.FLOAT, value.columnType());
assertEquals(2.1f, value.floatValue(), 1e-5);
assertEquals(2.1d, value.doubleValue(), 1e-5);

value = ColumnValueBuilder.build((short)2);
assertEquals(ColumnType.SHORT, value.columnType());
assertEquals(2, value.shortValue());
assertEquals(2.0d, value.doubleValue(), 1e-5);

value = ColumnValueBuilder.build((long)2);
assertEquals(ColumnType.LONG, value.columnType());
assertEquals(2, value.longValue());
assertEquals(2.0d, value.doubleValue(), 1e-5);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,24 @@ public void read_FloatValue() throws IOException {
assertEquals(ColumnType.FLOAT, value.columnType());
assertEquals(2.1f, value.floatValue(), 1e-5);
}
}

@Test
public void read_ShortValue() throws IOException {
ColumnValue value = new ShortValue((short)2);
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
value.writeTo(bytesStreamOutput);
value = reader.read(bytesStreamOutput.bytes().streamInput());
assertEquals(ColumnType.SHORT, value.columnType());
assertEquals(2, value.shortValue());
}

@Test
public void read_LongValue() throws IOException {
ColumnValue value = new LongValue((long)2);
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
value.writeTo(bytesStreamOutput);
value = reader.read(bytesStreamOutput.bytes().streamInput());
assertEquals(ColumnType.LONG, value.columnType());
assertEquals(2, value.longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void wrongStringValue() {
public void wrongDoubleValue() {
exceptionRule.expect(RuntimeException.class);
exceptionRule.expectMessage("the value isn't Double type");
ColumnValue value = new IntValue(1);
ColumnValue value = new BooleanValue(true);
value.doubleValue();
}

Expand All @@ -68,4 +68,20 @@ public void wrongFloatValue() {
ColumnValue value = new IntValue(1);
value.floatValue();
}

@Test
public void wrongShortValue() {
exceptionRule.expect(RuntimeException.class);
exceptionRule.expectMessage("the value isn't Short type");
ColumnValue value = new IntValue(1);
value.shortValue();
}

@Test
public void wrongLongValue() {
exceptionRule.expect(RuntimeException.class);
exceptionRule.expectMessage("the value isn't Long type");
ColumnValue value = new IntValue(1);
value.longValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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 LongValueTest {

@Test
public void longValue() {
LongValue longValue = new LongValue((long)2);
assertEquals(ColumnType.LONG, longValue.columnType());
assertEquals(2L, longValue.getValue());
assertEquals(2.0d, longValue.doubleValue(), 1e-5);
}

@Test
public void testToXContent() throws IOException {
LongValue longValue = new LongValue((long)2);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
longValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
assertEquals("{\"column_type\":\"LONG\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright OpenSearch Contributors
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use new copyright header for new files based on this doc: https://github.com/opensearch-project/.github/blob/main/CONTRIBUTING.md#license-headers

* SPDX-License-Identifier: Apache-2.0
*/

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 ShortValueTest {

@Test
public void shortValue() {
ShortValue shortValue = new ShortValue((short)2);
assertEquals(ColumnType.SHORT, shortValue.columnType());
assertEquals((short)2, shortValue.getValue());
assertEquals(2.0d, shortValue.doubleValue(), 1e-5);
}

@Test
public void testToXContent() throws IOException {
ShortValue shortValue = new ShortValue((short)2);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
shortValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
assertEquals("{\"column_type\":\"SHORT\",\"value\":2}", jsonStr);
}
}