embulk-parser-jsonl 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +17 -14
- data/bench/gen_dummy.rb +5 -0
- data/bench/typecast.yml +17 -0
- data/bench/without_typecast.yml +17 -0
- data/build.gradle +1 -1
- data/embulk-parser-jsonl.gemspec +1 -1
- data/example/compat.yml +21 -0
- data/example/example.yml +5 -10
- data/example/example_without_typecast.yml +18 -0
- data/example/sample.json +2 -2
- data/src/main/java/org/embulk/parser/jsonl/ColumnCaster.java +97 -0
- data/src/main/java/org/embulk/parser/jsonl/ColumnVisitorImpl.java +164 -0
- data/src/main/java/org/embulk/parser/jsonl/JsonRecordValidateException.java +11 -3
- data/src/main/java/org/embulk/parser/jsonl/JsonlParserPlugin.java +39 -14
- data/src/main/java/org/embulk/parser/jsonl/cast/BooleanCast.java +39 -0
- data/src/main/java/org/embulk/parser/jsonl/cast/DoubleCast.java +41 -0
- data/src/main/java/org/embulk/parser/jsonl/cast/JsonCast.java +40 -0
- data/src/main/java/org/embulk/parser/jsonl/cast/LongCast.java +47 -0
- data/src/main/java/org/embulk/parser/jsonl/cast/StringCast.java +82 -0
- data/src/test/java/org/embulk/parser/jsonl/TestColumnCaster.java +256 -0
- data/src/test/java/org/embulk/parser/jsonl/cast/TestBooleanCast.java +56 -0
- data/src/test/java/org/embulk/parser/jsonl/cast/TestDoubleCast.java +50 -0
- data/src/test/java/org/embulk/parser/jsonl/cast/TestJsonCast.java +80 -0
- data/src/test/java/org/embulk/parser/jsonl/cast/TestLongCast.java +42 -0
- data/src/test/java/org/embulk/parser/jsonl/cast/TestStringCast.java +103 -0
- metadata +21 -7
- data/src/main/java/org/embulk/parser/jsonl/JsonlColumnOption.java +0 -16
- data/src/main/java/org/embulk/parser/jsonl/getter/ColumnGetterFactory.java +0 -24
- data/src/main/java/org/embulk/parser/jsonl/getter/CommonColumnGetter.java +0 -131
- data/src/main/java/org/embulk/parser/jsonl/getter/StringColumnGetter.java +0 -68
@@ -3,12 +3,20 @@ package org.embulk.parser.jsonl;
|
|
3
3
|
import org.embulk.spi.DataException;
|
4
4
|
|
5
5
|
public class JsonRecordValidateException
|
6
|
-
extends DataException
|
7
|
-
|
6
|
+
extends DataException
|
7
|
+
{
|
8
|
+
public JsonRecordValidateException(String message)
|
9
|
+
{
|
8
10
|
super(message);
|
9
11
|
}
|
10
12
|
|
11
|
-
public JsonRecordValidateException(Throwable cause)
|
13
|
+
public JsonRecordValidateException(String message, Throwable cause)
|
14
|
+
{
|
15
|
+
super(message, cause);
|
16
|
+
}
|
17
|
+
|
18
|
+
public JsonRecordValidateException(Throwable cause)
|
19
|
+
{
|
12
20
|
super(cause);
|
13
21
|
}
|
14
22
|
}
|
@@ -10,8 +10,6 @@ import org.embulk.config.ConfigException;
|
|
10
10
|
import org.embulk.config.ConfigSource;
|
11
11
|
import org.embulk.config.Task;
|
12
12
|
import org.embulk.config.TaskSource;
|
13
|
-
import org.embulk.parser.jsonl.getter.CommonColumnGetter;
|
14
|
-
import org.embulk.parser.jsonl.getter.ColumnGetterFactory;
|
15
13
|
import org.embulk.spi.Column;
|
16
14
|
import org.embulk.spi.ColumnConfig;
|
17
15
|
import org.embulk.spi.DataException;
|
@@ -25,6 +23,7 @@ import org.embulk.spi.SchemaConfig;
|
|
25
23
|
import org.embulk.spi.json.JsonParseException;
|
26
24
|
import org.embulk.spi.json.JsonParser;
|
27
25
|
import org.embulk.spi.time.TimestampParser;
|
26
|
+
import org.embulk.spi.type.Type;
|
28
27
|
import org.embulk.spi.util.LineDecoder;
|
29
28
|
import org.embulk.spi.util.Timestamps;
|
30
29
|
import org.msgpack.value.Value;
|
@@ -37,6 +36,23 @@ import static org.msgpack.value.ValueFactory.newString;
|
|
37
36
|
public class JsonlParserPlugin
|
38
37
|
implements ParserPlugin
|
39
38
|
{
|
39
|
+
@Deprecated
|
40
|
+
public interface JsonlColumnOption
|
41
|
+
extends Task
|
42
|
+
{
|
43
|
+
@Config("type")
|
44
|
+
@ConfigDefault("null")
|
45
|
+
Optional<Type> getType();
|
46
|
+
}
|
47
|
+
|
48
|
+
public interface TypecastColumnOption
|
49
|
+
extends Task
|
50
|
+
{
|
51
|
+
@Config("typecast")
|
52
|
+
@ConfigDefault("null")
|
53
|
+
public Optional<Boolean> getTypecast();
|
54
|
+
}
|
55
|
+
|
40
56
|
public interface PluginTask
|
41
57
|
extends Task, LineDecoder.DecoderTask, TimestampParser.Task
|
42
58
|
{
|
@@ -53,8 +69,13 @@ public class JsonlParserPlugin
|
|
53
69
|
@ConfigDefault("false")
|
54
70
|
boolean getStopOnInvalidRecord();
|
55
71
|
|
72
|
+
@Config("default_typecast")
|
73
|
+
@ConfigDefault("true")
|
74
|
+
Boolean getDefaultTypecast();
|
75
|
+
|
56
76
|
@Config("column_options")
|
57
77
|
@ConfigDefault("{}")
|
78
|
+
@Deprecated
|
58
79
|
Map<String, JsonlColumnOption> getColumnOptions();
|
59
80
|
}
|
60
81
|
|
@@ -73,16 +94,27 @@ public class JsonlParserPlugin
|
|
73
94
|
public void transaction(ConfigSource configSource, Control control)
|
74
95
|
{
|
75
96
|
PluginTask task = configSource.loadConfig(PluginTask.class);
|
97
|
+
|
98
|
+
if (! task.getColumnOptions().isEmpty()) {
|
99
|
+
log.warn("embulk-parser-jsonl: \"column_options\" option is deprecated, specify type directly to \"columns\" option with typecast: true (default: true).");
|
100
|
+
}
|
101
|
+
|
76
102
|
SchemaConfig schemaConfig = getSchemaConfig(task);
|
77
103
|
ImmutableList.Builder<Column> columns = ImmutableList.builder();
|
78
104
|
for (int i = 0; i < schemaConfig.getColumnCount(); i++) {
|
79
105
|
ColumnConfig columnConfig = schemaConfig.getColumn(i);
|
80
|
-
|
81
|
-
columns.add(new Column(i, columnConfig.getName(),
|
106
|
+
Type type = getType(task, columnConfig);
|
107
|
+
columns.add(new Column(i, columnConfig.getName(), type));
|
82
108
|
}
|
83
109
|
control.run(task.dump(), new Schema(columns.build()));
|
84
110
|
}
|
85
111
|
|
112
|
+
private static Type getType(PluginTask task, ColumnConfig columnConfig)
|
113
|
+
{
|
114
|
+
JsonlColumnOption columnOption = columnOptionOf(task.getColumnOptions(), columnConfig.getName());
|
115
|
+
return columnOption.getType().or(columnConfig.getType());
|
116
|
+
}
|
117
|
+
|
86
118
|
// this method is to keep the backward compatibility of 'schema' option.
|
87
119
|
private SchemaConfig getSchemaConfig(PluginTask task)
|
88
120
|
{
|
@@ -115,13 +147,7 @@ public class JsonlParserPlugin
|
|
115
147
|
final boolean stopOnInvalidRecord = task.getStopOnInvalidRecord();
|
116
148
|
|
117
149
|
try (final PageBuilder pageBuilder = new PageBuilder(Exec.getBufferAllocator(), schema, output)) {
|
118
|
-
|
119
|
-
ImmutableMap.Builder<String, CommonColumnGetter> columnGettersBuilder = ImmutableMap.builder();
|
120
|
-
for (ColumnConfig columnConfig : schemaConfig.getColumns()) {
|
121
|
-
CommonColumnGetter columnGetter = factory.newColumnGetter(columnConfig);
|
122
|
-
columnGettersBuilder.put(columnConfig.getName(), columnGetter);
|
123
|
-
}
|
124
|
-
ImmutableMap<String, CommonColumnGetter> columnGetters = columnGettersBuilder.build();
|
150
|
+
ColumnVisitorImpl visitor = new ColumnVisitorImpl(task, schema, pageBuilder, timestampParsers);
|
125
151
|
|
126
152
|
while (decoder.nextFile()) { // TODO this implementation should be improved with new JsonParser API on Embulk v0.8.3
|
127
153
|
lineNumber = 0;
|
@@ -139,9 +165,8 @@ public class JsonlParserPlugin
|
|
139
165
|
final Map<Value, Value> record = value.asMapValue().map();
|
140
166
|
for (Column column : schema.getColumns()) {
|
141
167
|
Value v = record.get(getColumnNameValue(column));
|
142
|
-
|
143
|
-
|
144
|
-
column.visit(columnGetter);
|
168
|
+
visitor.setValue(v);
|
169
|
+
column.visit(visitor);
|
145
170
|
}
|
146
171
|
|
147
172
|
pageBuilder.addRecord();
|
@@ -0,0 +1,39 @@
|
|
1
|
+
package org.embulk.parser.jsonl.cast;
|
2
|
+
|
3
|
+
import org.embulk.spi.DataException;
|
4
|
+
import org.embulk.spi.time.Timestamp;
|
5
|
+
|
6
|
+
public class BooleanCast
|
7
|
+
{
|
8
|
+
private BooleanCast() {}
|
9
|
+
|
10
|
+
private static String buildErrorMessage(String as, boolean value)
|
11
|
+
{
|
12
|
+
return String.format("cannot cast boolean to %s: \"%s\"", as, value);
|
13
|
+
}
|
14
|
+
|
15
|
+
public static boolean asBoolean(boolean value) throws DataException
|
16
|
+
{
|
17
|
+
return value;
|
18
|
+
}
|
19
|
+
|
20
|
+
public static long asLong(boolean value) throws DataException
|
21
|
+
{
|
22
|
+
return value ? 1 : 0;
|
23
|
+
}
|
24
|
+
|
25
|
+
public static double asDouble(boolean value) throws DataException
|
26
|
+
{
|
27
|
+
throw new DataException(buildErrorMessage("double", value));
|
28
|
+
}
|
29
|
+
|
30
|
+
public static String asString(boolean value) throws DataException
|
31
|
+
{
|
32
|
+
return value ? "true" : "false";
|
33
|
+
}
|
34
|
+
|
35
|
+
public static Timestamp asTimestamp(boolean value) throws DataException
|
36
|
+
{
|
37
|
+
throw new DataException(buildErrorMessage("timestamp", value));
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
package org.embulk.parser.jsonl.cast;
|
2
|
+
|
3
|
+
import org.embulk.spi.DataException;
|
4
|
+
import org.embulk.spi.time.Timestamp;
|
5
|
+
|
6
|
+
public class DoubleCast
|
7
|
+
{
|
8
|
+
private DoubleCast() {}
|
9
|
+
|
10
|
+
private static String buildErrorMessage(String as, double value)
|
11
|
+
{
|
12
|
+
return String.format("cannot cast double to %s: \"%s\"", as, value);
|
13
|
+
}
|
14
|
+
|
15
|
+
public static boolean asBoolean(double value) throws DataException
|
16
|
+
{
|
17
|
+
throw new DataException(buildErrorMessage("boolean", value));
|
18
|
+
}
|
19
|
+
|
20
|
+
public static long asLong(double value) throws DataException
|
21
|
+
{
|
22
|
+
return (long) value;
|
23
|
+
}
|
24
|
+
|
25
|
+
public static double asDouble(double value) throws DataException
|
26
|
+
{
|
27
|
+
return value;
|
28
|
+
}
|
29
|
+
|
30
|
+
public static String asString(double value) throws DataException
|
31
|
+
{
|
32
|
+
return String.valueOf(value);
|
33
|
+
}
|
34
|
+
|
35
|
+
public static Timestamp asTimestamp(double value) throws DataException
|
36
|
+
{
|
37
|
+
long epochSecond = (long) value;
|
38
|
+
long nanoAdjustMent = (long) ((value - epochSecond) * 1000000000);
|
39
|
+
return Timestamp.ofEpochSecond(epochSecond, nanoAdjustMent);
|
40
|
+
}
|
41
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
package org.embulk.parser.jsonl.cast;
|
2
|
+
|
3
|
+
import org.embulk.spi.DataException;
|
4
|
+
import org.embulk.spi.time.Timestamp;
|
5
|
+
import org.msgpack.value.Value;
|
6
|
+
|
7
|
+
public class JsonCast
|
8
|
+
{
|
9
|
+
private JsonCast() {}
|
10
|
+
|
11
|
+
private static String buildErrorMessage(String as, Value value)
|
12
|
+
{
|
13
|
+
return String.format("cannot cast Json to %s: \"%s\"", as, value);
|
14
|
+
}
|
15
|
+
|
16
|
+
public static boolean asBoolean(Value value) throws DataException
|
17
|
+
{
|
18
|
+
throw new DataException(buildErrorMessage("boolean", value));
|
19
|
+
}
|
20
|
+
|
21
|
+
public static long asLong(Value value) throws DataException
|
22
|
+
{
|
23
|
+
throw new DataException(buildErrorMessage("long", value));
|
24
|
+
}
|
25
|
+
|
26
|
+
public static double asDouble(Value value) throws DataException
|
27
|
+
{
|
28
|
+
throw new DataException(buildErrorMessage("double", value));
|
29
|
+
}
|
30
|
+
|
31
|
+
public static String asString(Value value) throws DataException
|
32
|
+
{
|
33
|
+
return value.toString();
|
34
|
+
}
|
35
|
+
|
36
|
+
public static Timestamp asTimestamp(Value value) throws DataException
|
37
|
+
{
|
38
|
+
throw new DataException(buildErrorMessage("timestamp", value));
|
39
|
+
}
|
40
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
package org.embulk.parser.jsonl.cast;
|
2
|
+
|
3
|
+
import org.embulk.spi.DataException;
|
4
|
+
import org.embulk.spi.time.Timestamp;
|
5
|
+
|
6
|
+
public class LongCast
|
7
|
+
{
|
8
|
+
private LongCast() {}
|
9
|
+
|
10
|
+
private static String buildErrorMessage(String as, long value)
|
11
|
+
{
|
12
|
+
return String.format("cannot cast long to %s: \"%s\"", as, value);
|
13
|
+
}
|
14
|
+
|
15
|
+
public static boolean asBoolean(long value) throws DataException
|
16
|
+
{
|
17
|
+
if (value == 1) {
|
18
|
+
return true;
|
19
|
+
}
|
20
|
+
else if (value == 0) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
else {
|
24
|
+
throw new DataException(buildErrorMessage("boolean", value));
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
public static long asLong(long value) throws DataException
|
29
|
+
{
|
30
|
+
return value;
|
31
|
+
}
|
32
|
+
|
33
|
+
public static double asDouble(long value) throws DataException
|
34
|
+
{
|
35
|
+
return (double) value;
|
36
|
+
}
|
37
|
+
|
38
|
+
public static String asString(long value) throws DataException
|
39
|
+
{
|
40
|
+
return String.valueOf(value);
|
41
|
+
}
|
42
|
+
|
43
|
+
public static Timestamp asTimestamp(long value) throws DataException
|
44
|
+
{
|
45
|
+
return Timestamp.ofEpochSecond(value);
|
46
|
+
}
|
47
|
+
}
|
@@ -0,0 +1,82 @@
|
|
1
|
+
package org.embulk.parser.jsonl.cast;
|
2
|
+
|
3
|
+
import com.google.common.collect.ImmutableSet;
|
4
|
+
import org.embulk.spi.DataException;
|
5
|
+
import org.embulk.spi.time.Timestamp;
|
6
|
+
import org.embulk.spi.time.TimestampParseException;
|
7
|
+
import org.embulk.spi.time.TimestampParser;
|
8
|
+
|
9
|
+
public class StringCast
|
10
|
+
{
|
11
|
+
// copy from csv plugin
|
12
|
+
public static final ImmutableSet<String> TRUE_STRINGS =
|
13
|
+
ImmutableSet.of(
|
14
|
+
"true", "True", "TRUE",
|
15
|
+
"yes", "Yes", "YES",
|
16
|
+
"t", "T", "y", "Y",
|
17
|
+
"on", "On", "ON",
|
18
|
+
"1");
|
19
|
+
|
20
|
+
public static final ImmutableSet<String> FALSE_STRINGS =
|
21
|
+
ImmutableSet.of(
|
22
|
+
"false", "False", "FALSE",
|
23
|
+
"no", "No", "NO",
|
24
|
+
"f", "F", "n", "N",
|
25
|
+
"off", "Off", "OFF",
|
26
|
+
"0");
|
27
|
+
|
28
|
+
private StringCast() {}
|
29
|
+
|
30
|
+
private static String buildErrorMessage(String as, String value)
|
31
|
+
{
|
32
|
+
return String.format("cannot cast String to %s: \"%s\"", as, value);
|
33
|
+
}
|
34
|
+
|
35
|
+
public static boolean asBoolean(String value) throws DataException
|
36
|
+
{
|
37
|
+
if (TRUE_STRINGS.contains(value)) {
|
38
|
+
return true;
|
39
|
+
}
|
40
|
+
else if (FALSE_STRINGS.contains(value)) {
|
41
|
+
return false;
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
throw new DataException(buildErrorMessage("boolean", value));
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
public static long asLong(String value) throws DataException
|
49
|
+
{
|
50
|
+
try {
|
51
|
+
return Long.parseLong(value);
|
52
|
+
}
|
53
|
+
catch (NumberFormatException ex) {
|
54
|
+
throw new DataException(buildErrorMessage("long", value), ex);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
public static double asDouble(String value) throws DataException
|
59
|
+
{
|
60
|
+
try {
|
61
|
+
return Double.parseDouble(value);
|
62
|
+
}
|
63
|
+
catch (NumberFormatException ex) {
|
64
|
+
throw new DataException(buildErrorMessage("double", value), ex);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
public static String asString(String value) throws DataException
|
69
|
+
{
|
70
|
+
return value;
|
71
|
+
}
|
72
|
+
|
73
|
+
public static Timestamp asTimestamp(String value, TimestampParser parser) throws DataException
|
74
|
+
{
|
75
|
+
try {
|
76
|
+
return parser.parse(value);
|
77
|
+
}
|
78
|
+
catch (TimestampParseException ex) {
|
79
|
+
throw new DataException(buildErrorMessage("timestamp", value), ex);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
@@ -0,0 +1,256 @@
|
|
1
|
+
package org.embulk.parser.jsonl;
|
2
|
+
|
3
|
+
import org.embulk.EmbulkTestRuntime;
|
4
|
+
import org.embulk.spi.DataException;
|
5
|
+
import org.embulk.spi.time.Timestamp;
|
6
|
+
import org.embulk.spi.time.TimestampParser;
|
7
|
+
import org.joda.time.DateTimeZone;
|
8
|
+
import org.jruby.embed.ScriptingContainer;
|
9
|
+
import org.junit.Before;
|
10
|
+
import org.junit.Rule;
|
11
|
+
import org.junit.Test;
|
12
|
+
import org.msgpack.value.MapValue;
|
13
|
+
import org.msgpack.value.Value;
|
14
|
+
import org.msgpack.value.ValueFactory;
|
15
|
+
|
16
|
+
import static org.junit.Assert.assertEquals;
|
17
|
+
import static org.junit.Assert.assertTrue;
|
18
|
+
import static org.junit.Assert.fail;
|
19
|
+
|
20
|
+
public class TestColumnCaster
|
21
|
+
{
|
22
|
+
@Rule
|
23
|
+
public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
|
24
|
+
public MapValue mapValue;
|
25
|
+
public DataException thrown;
|
26
|
+
public ScriptingContainer jruby;
|
27
|
+
public TimestampParser parser;
|
28
|
+
|
29
|
+
@Before
|
30
|
+
public void createResource()
|
31
|
+
{
|
32
|
+
jruby = new ScriptingContainer();
|
33
|
+
thrown = new DataException("any");
|
34
|
+
Value[] kvs = new Value[2];
|
35
|
+
kvs[0] = ValueFactory.newString("k");
|
36
|
+
kvs[1] = ValueFactory.newString("v");
|
37
|
+
mapValue = ValueFactory.newMap(kvs);
|
38
|
+
parser = new TimestampParser(jruby, "%Y-%m-%d %H:%M:%S.%N", DateTimeZone.UTC);
|
39
|
+
}
|
40
|
+
|
41
|
+
@Test
|
42
|
+
public void asBooleanFromBoolean()
|
43
|
+
{
|
44
|
+
assertEquals(true, ColumnCaster.asBoolean(ValueFactory.newBoolean(true)));
|
45
|
+
}
|
46
|
+
|
47
|
+
@Test
|
48
|
+
public void asBooleanFromInteger()
|
49
|
+
{
|
50
|
+
assertEquals(true, ColumnCaster.asBoolean(ValueFactory.newInteger(1)));
|
51
|
+
try {
|
52
|
+
ColumnCaster.asBoolean(ValueFactory.newInteger(2));
|
53
|
+
fail();
|
54
|
+
}
|
55
|
+
catch (Throwable t) {
|
56
|
+
assertTrue(t instanceof DataException);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
@Test
|
61
|
+
public void asBooleanFromFloat()
|
62
|
+
{
|
63
|
+
try {
|
64
|
+
ColumnCaster.asBoolean(ValueFactory.newFloat(1.1));
|
65
|
+
fail();
|
66
|
+
}
|
67
|
+
catch (Throwable t) {
|
68
|
+
assertTrue(t instanceof DataException);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
@Test
|
73
|
+
public void asBooleanFromString()
|
74
|
+
{
|
75
|
+
assertEquals(true, ColumnCaster.asBoolean(ValueFactory.newString("true")));
|
76
|
+
try {
|
77
|
+
ColumnCaster.asBoolean(ValueFactory.newString("foo"));
|
78
|
+
fail();
|
79
|
+
}
|
80
|
+
catch (Throwable t) {
|
81
|
+
assertTrue(t instanceof DataException);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
@Test
|
86
|
+
public void asBooleanFromJson()
|
87
|
+
{
|
88
|
+
try {
|
89
|
+
ColumnCaster.asBoolean(mapValue);
|
90
|
+
fail();
|
91
|
+
}
|
92
|
+
catch (Throwable t) {
|
93
|
+
assertTrue(t instanceof DataException);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
@Test
|
98
|
+
public void asLongFromBoolean()
|
99
|
+
{
|
100
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newBoolean(true)));
|
101
|
+
}
|
102
|
+
|
103
|
+
@Test
|
104
|
+
public void asLongFromInteger()
|
105
|
+
{
|
106
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newInteger(1)));
|
107
|
+
}
|
108
|
+
|
109
|
+
@Test
|
110
|
+
public void asLongFromFloat()
|
111
|
+
{
|
112
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newFloat(1.5)));
|
113
|
+
}
|
114
|
+
|
115
|
+
@Test
|
116
|
+
public void asLongFromString()
|
117
|
+
{
|
118
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newString("1")));
|
119
|
+
try {
|
120
|
+
ColumnCaster.asLong(ValueFactory.newString("foo"));
|
121
|
+
fail();
|
122
|
+
}
|
123
|
+
catch (Throwable t) {
|
124
|
+
assertTrue(t instanceof DataException);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
@Test
|
129
|
+
public void asLongFromJson()
|
130
|
+
{
|
131
|
+
try {
|
132
|
+
ColumnCaster.asLong(mapValue);
|
133
|
+
fail();
|
134
|
+
}
|
135
|
+
catch (Throwable t) {
|
136
|
+
assertTrue(t instanceof DataException);
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
@Test
|
141
|
+
public void asDoubleFromBoolean()
|
142
|
+
{
|
143
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newBoolean(true)));
|
144
|
+
}
|
145
|
+
|
146
|
+
@Test
|
147
|
+
public void asDoubleFromInteger()
|
148
|
+
{
|
149
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newInteger(1)));
|
150
|
+
}
|
151
|
+
|
152
|
+
@Test
|
153
|
+
public void asDoubleFromFloat()
|
154
|
+
{
|
155
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newFloat(1.5)));
|
156
|
+
}
|
157
|
+
|
158
|
+
@Test
|
159
|
+
public void asDoubleFromString()
|
160
|
+
{
|
161
|
+
assertEquals(1, ColumnCaster.asLong(ValueFactory.newString("1")));
|
162
|
+
try {
|
163
|
+
ColumnCaster.asLong(ValueFactory.newString("foo"));
|
164
|
+
fail();
|
165
|
+
}
|
166
|
+
catch (Throwable t) {
|
167
|
+
assertTrue(t instanceof DataException);
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
@Test
|
172
|
+
public void asDoubleFromJson()
|
173
|
+
{
|
174
|
+
try {
|
175
|
+
ColumnCaster.asLong(mapValue);
|
176
|
+
fail();
|
177
|
+
}
|
178
|
+
catch (Throwable t) {
|
179
|
+
assertTrue(t instanceof DataException);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
@Test
|
184
|
+
public void asStringFromBoolean()
|
185
|
+
{
|
186
|
+
assertEquals("true", ColumnCaster.asString(ValueFactory.newBoolean(true)));
|
187
|
+
}
|
188
|
+
|
189
|
+
@Test
|
190
|
+
public void asStringFromInteger()
|
191
|
+
{
|
192
|
+
assertEquals("1", ColumnCaster.asString(ValueFactory.newInteger(1)));
|
193
|
+
}
|
194
|
+
|
195
|
+
@Test
|
196
|
+
public void asStringFromFloat()
|
197
|
+
{
|
198
|
+
assertEquals("1.5", ColumnCaster.asString(ValueFactory.newFloat(1.5)));
|
199
|
+
}
|
200
|
+
|
201
|
+
@Test
|
202
|
+
public void asStringFromString()
|
203
|
+
{
|
204
|
+
assertEquals("1", ColumnCaster.asString(ValueFactory.newString("1")));
|
205
|
+
}
|
206
|
+
|
207
|
+
@Test
|
208
|
+
public void asStringFromJson()
|
209
|
+
{
|
210
|
+
assertEquals("{\"k\":\"v\"}", ColumnCaster.asString(mapValue));
|
211
|
+
}
|
212
|
+
|
213
|
+
@Test
|
214
|
+
public void asTimestampFromBoolean()
|
215
|
+
{
|
216
|
+
try {
|
217
|
+
ColumnCaster.asTimestamp(ValueFactory.newBoolean(true), parser);
|
218
|
+
fail();
|
219
|
+
}
|
220
|
+
catch (Throwable t) {
|
221
|
+
assertTrue(t instanceof DataException);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
@Test
|
226
|
+
public void asTimestampFromInteger()
|
227
|
+
{
|
228
|
+
assertEquals(1, ColumnCaster.asTimestamp(ValueFactory.newInteger(1), parser).getEpochSecond());
|
229
|
+
}
|
230
|
+
|
231
|
+
@Test
|
232
|
+
public void asTimestampFromFloat()
|
233
|
+
{
|
234
|
+
Timestamp expected = Timestamp.ofEpochSecond(1463084053, 500000000);
|
235
|
+
assertEquals(expected, ColumnCaster.asTimestamp(ValueFactory.newFloat(1463084053.5), parser));
|
236
|
+
}
|
237
|
+
|
238
|
+
@Test
|
239
|
+
public void asTimestampFromString()
|
240
|
+
{
|
241
|
+
Timestamp expected = Timestamp.ofEpochSecond(1463084053, 500000000);
|
242
|
+
assertEquals(expected, ColumnCaster.asTimestamp(ValueFactory.newString("2016-05-12 20:14:13.5"), parser));
|
243
|
+
}
|
244
|
+
|
245
|
+
@Test
|
246
|
+
public void asTimestampFromJson()
|
247
|
+
{
|
248
|
+
try {
|
249
|
+
ColumnCaster.asTimestamp(mapValue, parser);
|
250
|
+
fail();
|
251
|
+
}
|
252
|
+
catch (Throwable t) {
|
253
|
+
assertTrue(t instanceof DataException);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
}
|