embulk-filter-row 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,20 @@
1
1
  package org.embulk.filter.row;
2
2
 
3
- import org.embulk.config.Task;
4
- import org.embulk.spi.Exec;
3
+ import com.google.common.base.Throwables;
4
+
5
+ import org.apache.commons.lang3.math.NumberUtils;
6
+ import org.embulk.config.ConfigException;
5
7
  import org.embulk.spi.Column;
6
- import org.embulk.spi.type.Type;
8
+ import org.embulk.spi.time.Timestamp;
9
+ import org.embulk.spi.time.TimestampParseException;
10
+ import org.embulk.spi.time.TimestampParser;
7
11
  import org.embulk.spi.type.BooleanType;
8
- import org.embulk.spi.type.LongType;
9
12
  import org.embulk.spi.type.DoubleType;
13
+ import org.embulk.spi.type.LongType;
10
14
  import org.embulk.spi.type.StringType;
11
15
  import org.embulk.spi.type.TimestampType;
12
- import org.embulk.config.ConfigException;
13
-
16
+ import org.embulk.spi.type.Type;
14
17
  import org.joda.time.DateTimeZone;
15
- import org.embulk.spi.time.Timestamp;
16
- import org.embulk.spi.time.TimestampParser;
17
- import org.embulk.spi.time.TimestampParseException;
18
- import com.google.common.base.Throwables;
19
-
20
- import org.embulk.filter.row.ConditionConfig;
21
- import org.embulk.filter.row.Condition;
22
- import org.embulk.filter.row.BooleanCondition;
23
- import org.embulk.filter.row.LongCondition;
24
- import org.embulk.filter.row.DoubleCondition;
25
- import org.embulk.filter.row.StringCondition;
26
- import org.embulk.filter.row.TimestampCondition;
27
18
  import org.jruby.embed.ScriptingContainer;
28
19
 
29
20
  public class ConditionFactory
@@ -64,7 +55,7 @@ public class ConditionFactory
64
55
  else if (columnType instanceof TimestampType) {
65
56
  return createTimestampCondition();
66
57
  }
67
- assert(false);
58
+ assert false;
68
59
  return null;
69
60
  }
70
61
 
@@ -77,7 +68,7 @@ public class ConditionFactory
77
68
  throw new ConfigException(String.format("RowFilterPlugin: Argument is missing on column: %s", columnName));
78
69
  }
79
70
  else if (conditionConfig.getArgument().get() instanceof Boolean) {
80
- Boolean argument = (Boolean)conditionConfig.getArgument().get();
71
+ Boolean argument = (Boolean) conditionConfig.getArgument().get();
81
72
  return new BooleanCondition(operator, argument, not);
82
73
  }
83
74
  else {
@@ -93,8 +84,8 @@ public class ConditionFactory
93
84
  else if (!conditionConfig.getArgument().isPresent()) {
94
85
  throw new ConfigException(String.format("RowFilterPlugin: Argument is missing on column: %s", columnName));
95
86
  }
96
- else if (conditionConfig.getArgument().get() instanceof Number) {
97
- Long argument = new Long(conditionConfig.getArgument().get().toString()).longValue();
87
+ else if (NumberUtils.isNumber(conditionConfig.getArgument().get().toString())) {
88
+ Double argument = new Double(conditionConfig.getArgument().get().toString());
98
89
  return new LongCondition(operator, argument, not);
99
90
  }
100
91
  else {
@@ -110,7 +101,7 @@ public class ConditionFactory
110
101
  else if (!conditionConfig.getArgument().isPresent()) {
111
102
  throw new ConfigException(String.format("RowFilterPlugin: Argument is missing on column: %s", columnName));
112
103
  }
113
- else if (conditionConfig.getArgument().get() instanceof Number) {
104
+ else if (NumberUtils.isNumber(conditionConfig.getArgument().get().toString())) {
114
105
  Double argument = new Double(conditionConfig.getArgument().get().toString());
115
106
  return new DoubleCondition(operator, argument, not);
116
107
  }
@@ -128,7 +119,7 @@ public class ConditionFactory
128
119
  throw new ConfigException(String.format("RowFilterPlugin: Argument is missing on column: %s", columnName));
129
120
  }
130
121
  else if (conditionConfig.getArgument().get() instanceof String) {
131
- String argument = (String)conditionConfig.getArgument().get();
122
+ String argument = (String) conditionConfig.getArgument().get();
132
123
  return new StringCondition(operator, argument, not);
133
124
  }
134
125
  else {
@@ -145,15 +136,16 @@ public class ConditionFactory
145
136
  throw new ConfigException(String.format("RowFilterPlugin: Argument is missing on column: %s", columnName));
146
137
  }
147
138
  else if (conditionConfig.getArgument().get() instanceof String) {
148
- String argument = (String)conditionConfig.getArgument().get();
149
- String format = (String)conditionConfig.getFormat().get();
150
- DateTimeZone timezone = DateTimeZone.forID((String)conditionConfig.getTimezone().get());
139
+ String argument = (String) conditionConfig.getArgument().get();
140
+ String format = (String) conditionConfig.getFormat().get();
141
+ DateTimeZone timezone = DateTimeZone.forID((String) conditionConfig.getTimezone().get());
151
142
 
152
143
  TimestampParser parser = new TimestampParser(jruby, format, timezone);
153
144
  try {
154
145
  Timestamp timestamp = parser.parse(argument);
155
146
  return new TimestampCondition(operator, timestamp, not);
156
- } catch(TimestampParseException ex) {
147
+ }
148
+ catch (TimestampParseException ex) {
157
149
  throw Throwables.propagate(ex);
158
150
  }
159
151
  }
@@ -5,65 +5,75 @@ public class DoubleCondition implements Condition
5
5
  private DoubleComparator comparator;
6
6
 
7
7
  // @FunctionalInterface
8
- interface DoubleComparator {
8
+ interface DoubleComparator
9
+ {
9
10
  boolean compare(Double subject);
10
11
  }
11
12
 
12
- public DoubleCondition(final String operator, final Double argument, final boolean not) {
13
+ public DoubleCondition(final String operator, final Double argument, final boolean not)
14
+ {
13
15
  final DoubleComparator comparator;
14
16
  switch (operator.toUpperCase()) {
15
17
  case "IS NULL":
16
18
  comparator = new DoubleComparator() {
17
- public boolean compare(Double subject) {
19
+ public boolean compare(Double subject)
20
+ {
18
21
  return subject == null;
19
22
  }
20
23
  };
21
24
  break;
22
25
  case "IS NOT NULL":
23
26
  comparator = new DoubleComparator() {
24
- public boolean compare(Double subject) {
27
+ public boolean compare(Double subject)
28
+ {
25
29
  return subject != null;
26
30
  }
27
31
  };
28
32
  break;
29
33
  case ">":
30
34
  comparator = new DoubleComparator() {
31
- public boolean compare(Double subject) {
35
+ public boolean compare(Double subject)
36
+ {
32
37
  return subject == null ? false : subject.compareTo(argument) > 0;
33
38
  }
34
39
  };
35
40
  break;
36
41
  case ">=":
37
42
  comparator = new DoubleComparator() {
38
- public boolean compare(Double subject) {
43
+ public boolean compare(Double subject)
44
+ {
39
45
  return subject == null ? false : subject.compareTo(argument) >= 0;
40
46
  }
41
47
  };
42
48
  break;
43
49
  case "<":
44
50
  comparator = new DoubleComparator() {
45
- public boolean compare(Double subject) {
51
+ public boolean compare(Double subject)
52
+ {
46
53
  return subject == null ? false : subject.compareTo(argument) < 0;
47
54
  }
48
55
  };
49
56
  break;
50
57
  case "<=":
51
58
  comparator = new DoubleComparator() {
52
- public boolean compare(Double subject) {
59
+ public boolean compare(Double subject)
60
+ {
53
61
  return subject == null ? false : subject.compareTo(argument) <= 0;
54
62
  }
55
63
  };
56
64
  break;
57
65
  case "!=":
58
66
  comparator = new DoubleComparator() {
59
- public boolean compare(Double subject) {
67
+ public boolean compare(Double subject)
68
+ {
60
69
  return subject == null ? true : !subject.equals(argument);
61
70
  }
62
71
  };
63
72
  break;
64
73
  default: // case "==":
65
74
  comparator = new DoubleComparator() {
66
- public boolean compare(Double subject) {
75
+ public boolean compare(Double subject)
76
+ {
67
77
  return subject == null ? false : subject.equals(argument);
68
78
  }
69
79
  };
@@ -72,14 +82,16 @@ public class DoubleCondition implements Condition
72
82
  this.comparator = comparator;
73
83
  if (not) {
74
84
  this.comparator = new DoubleComparator() {
75
- public boolean compare(Double subject) {
85
+ public boolean compare(Double subject)
86
+ {
76
87
  return !comparator.compare(subject);
77
88
  }
78
89
  };
79
90
  }
80
91
  }
81
92
 
82
- public boolean compare(Double subject) {
93
+ public boolean compare(Double subject)
94
+ {
83
95
  return this.comparator.compare(subject);
84
96
  }
85
97
  }
@@ -1,70 +1,81 @@
1
1
  package org.embulk.filter.row;
2
2
 
3
+
3
4
  public class LongCondition implements Condition
4
5
  {
5
6
  private LongComparator comparator;
6
7
 
7
8
  // @FunctionalInterface
8
- interface LongComparator {
9
+ interface LongComparator
10
+ {
9
11
  boolean compare(Long subject);
10
12
  }
11
13
 
12
- public LongCondition(final String operator, final Long argument, final boolean not) {
14
+ public LongCondition(final String operator, final Double argument, final boolean not)
15
+ {
13
16
  final LongComparator comparator;
14
17
  switch (operator.toUpperCase()) {
15
18
  case "IS NULL":
16
19
  comparator = new LongComparator() {
17
- public boolean compare(Long subject) {
20
+ public boolean compare(Long subject)
21
+ {
18
22
  return subject == null;
19
23
  }
20
24
  };
21
25
  break;
22
26
  case "IS NOT NULL":
23
27
  comparator = new LongComparator() {
24
- public boolean compare(Long subject) {
28
+ public boolean compare(Long subject)
29
+ {
25
30
  return subject != null;
26
31
  }
27
32
  };
28
33
  break;
29
34
  case ">":
30
35
  comparator = new LongComparator() {
31
- public boolean compare(Long subject) {
32
- return subject == null ? false : subject.compareTo(argument) > 0;
36
+ public boolean compare(Long subject)
37
+ {
38
+ return subject == null ? false : new Double(subject).compareTo(argument) > 0;
33
39
  }
34
40
  };
35
41
  break;
36
42
  case ">=":
37
43
  comparator = new LongComparator() {
38
- public boolean compare(Long subject) {
39
- return subject == null ? false : subject.compareTo(argument) >= 0;
44
+ public boolean compare(Long subject)
45
+ {
46
+ return subject == null ? false : new Double(subject).compareTo(argument) >= 0;
40
47
  }
41
48
  };
42
49
  break;
43
50
  case "<":
44
51
  comparator = new LongComparator() {
45
- public boolean compare(Long subject) {
46
- return subject == null ? false : subject.compareTo(argument) < 0;
52
+ public boolean compare(Long subject)
53
+ {
54
+ return subject == null ? false : new Double(subject).compareTo(argument) < 0;
47
55
  }
48
56
  };
49
57
  break;
50
58
  case "<=":
51
59
  comparator = new LongComparator() {
52
- public boolean compare(Long subject) {
53
- return subject == null ? false : subject.compareTo(argument) <= 0;
60
+ public boolean compare(Long subject)
61
+ {
62
+ return subject == null ? false : new Double(subject).compareTo(argument) <= 0;
54
63
  }
55
64
  };
56
65
  break;
57
66
  case "!=":
58
67
  comparator = new LongComparator() {
59
- public boolean compare(Long subject) {
60
- return subject == null ? true : !subject.equals(argument);
68
+ public boolean compare(Long subject)
69
+ {
70
+ return subject == null ? true : !new Double(subject).equals(argument);
61
71
  }
62
72
  };
63
73
  break;
64
74
  default: // case "==":
65
75
  comparator = new LongComparator() {
66
- public boolean compare(Long subject) {
67
- return subject == null ? false : subject.equals(argument);
76
+ public boolean compare(Long subject)
77
+ {
78
+ return subject == null ? false : new Double(subject).equals(argument);
68
79
  }
69
80
  };
70
81
  break;
@@ -72,14 +83,16 @@ public class LongCondition implements Condition
72
83
  this.comparator = comparator;
73
84
  if (not) {
74
85
  this.comparator = new LongComparator() {
75
- public boolean compare(Long subject) {
86
+ public boolean compare(Long subject)
87
+ {
76
88
  return !comparator.compare(subject);
77
89
  }
78
90
  };
79
91
  }
80
92
  }
81
93
 
82
- public boolean compare(Long subject) {
94
+ public boolean compare(Long subject)
95
+ {
83
96
  return this.comparator.compare(subject);
84
97
  }
85
98
  }
@@ -5,17 +5,20 @@ public class StringCondition implements Condition
5
5
  private StringComparator comparator;
6
6
 
7
7
  // @FunctionalInterface
8
- interface StringComparator {
8
+ interface StringComparator
9
+ {
9
10
  boolean compare(String subject);
10
11
  }
11
12
 
12
- public StringCondition(final String operator, final String argument, final boolean not) {
13
+ public StringCondition(final String operator, final String argument, final boolean not)
14
+ {
13
15
  final StringComparator comparator;
14
16
  switch (operator.toUpperCase()) {
15
17
  case "START_WITH":
16
18
  case "STARTSWITH":
17
19
  comparator = new StringComparator() {
18
- public boolean compare(String subject) {
20
+ public boolean compare(String subject)
21
+ {
19
22
  return subject == null ? false : subject.startsWith(argument);
20
23
  }
21
24
  };
@@ -23,7 +26,8 @@ public class StringCondition implements Condition
23
26
  case "END_WITH":
24
27
  case "ENDSWITH":
25
28
  comparator = new StringComparator() {
26
- public boolean compare(String subject) {
29
+ public boolean compare(String subject)
30
+ {
27
31
  return subject == null ? false : subject.endsWith(argument);
28
32
  }
29
33
  };
@@ -31,35 +35,40 @@ public class StringCondition implements Condition
31
35
  case "INCLUDE":
32
36
  case "CONTAINS":
33
37
  comparator = new StringComparator() {
34
- public boolean compare(String subject) {
38
+ public boolean compare(String subject)
39
+ {
35
40
  return subject == null ? false : subject.contains(argument);
36
41
  }
37
42
  };
38
43
  break;
39
44
  case "IS NULL":
40
45
  comparator = new StringComparator() {
41
- public boolean compare(String subject) {
46
+ public boolean compare(String subject)
47
+ {
42
48
  return subject == null;
43
49
  }
44
50
  };
45
51
  break;
46
52
  case "IS NOT NULL":
47
53
  comparator = new StringComparator() {
48
- public boolean compare(String subject) {
54
+ public boolean compare(String subject)
55
+ {
49
56
  return subject != null;
50
57
  }
51
58
  };
52
59
  break;
53
60
  case "!=":
54
61
  comparator = new StringComparator() {
55
- public boolean compare(String subject) {
62
+ public boolean compare(String subject)
63
+ {
56
64
  return subject == null ? true : !subject.equals(argument);
57
65
  }
58
66
  };
59
67
  break;
60
68
  default: // case "==":
61
69
  comparator = new StringComparator() {
62
- public boolean compare(String subject) {
70
+ public boolean compare(String subject)
71
+ {
63
72
  return subject == null ? false : subject.equals(argument);
64
73
  }
65
74
  };
@@ -68,14 +77,16 @@ public class StringCondition implements Condition
68
77
  this.comparator = comparator;
69
78
  if (not) {
70
79
  this.comparator = new StringComparator() {
71
- public boolean compare(String subject) {
80
+ public boolean compare(String subject)
81
+ {
72
82
  return !comparator.compare(subject);
73
83
  }
74
84
  };
75
85
  }
76
86
  }
77
87
 
78
- public boolean compare(String subject) {
88
+ public boolean compare(String subject)
89
+ {
79
90
  return this.comparator.compare(subject);
80
91
  }
81
92
  }
@@ -6,65 +6,75 @@ public class TimestampCondition implements Condition
6
6
  private TimestampComparator comparator;
7
7
 
8
8
  // @FunctionalInterface
9
- interface TimestampComparator {
9
+ interface TimestampComparator
10
+ {
10
11
  boolean compare(Timestamp subject);
11
12
  }
12
13
 
13
- public TimestampCondition(final String operator, final Timestamp argument, final boolean not) {
14
+ public TimestampCondition(final String operator, final Timestamp argument, final boolean not)
15
+ {
14
16
  final TimestampComparator comparator;
15
17
  switch (operator.toUpperCase()) {
16
18
  case ">":
17
19
  comparator = new TimestampComparator() {
18
- public boolean compare(Timestamp subject) {
20
+ public boolean compare(Timestamp subject)
21
+ {
19
22
  return subject == null ? false : subject.compareTo(argument) > 0;
20
23
  }
21
24
  };
22
25
  break;
23
26
  case ">=":
24
27
  comparator = new TimestampComparator() {
25
- public boolean compare(Timestamp subject) {
28
+ public boolean compare(Timestamp subject)
29
+ {
26
30
  return subject == null ? false : subject.compareTo(argument) >= 0;
27
31
  }
28
32
  };
29
33
  break;
30
34
  case "<":
31
35
  comparator = new TimestampComparator() {
32
- public boolean compare(Timestamp subject) {
36
+ public boolean compare(Timestamp subject)
37
+ {
33
38
  return subject == null ? false : subject.compareTo(argument) < 0;
34
39
  }
35
40
  };
36
41
  break;
37
42
  case "<=":
38
43
  comparator = new TimestampComparator() {
39
- public boolean compare(Timestamp subject) {
44
+ public boolean compare(Timestamp subject)
45
+ {
40
46
  return subject == null ? false : subject.compareTo(argument) <= 0;
41
47
  }
42
48
  };
43
49
  break;
44
50
  case "IS NULL":
45
51
  comparator = new TimestampComparator() {
46
- public boolean compare(Timestamp subject) {
52
+ public boolean compare(Timestamp subject)
53
+ {
47
54
  return subject == null;
48
55
  }
49
56
  };
50
57
  break;
51
58
  case "IS NOT NULL":
52
59
  comparator = new TimestampComparator() {
53
- public boolean compare(Timestamp subject) {
60
+ public boolean compare(Timestamp subject)
61
+ {
54
62
  return subject != null;
55
63
  }
56
64
  };
57
65
  break;
58
66
  case "!=":
59
67
  comparator = new TimestampComparator() {
60
- public boolean compare(Timestamp subject) {
68
+ public boolean compare(Timestamp subject)
69
+ {
61
70
  return subject == null ? true : !subject.equals(argument);
62
71
  }
63
72
  };
64
73
  break;
65
74
  default: // case "==":
66
75
  comparator = new TimestampComparator() {
67
- public boolean compare(Timestamp subject) {
76
+ public boolean compare(Timestamp subject)
77
+ {
68
78
  return subject == null ? false : subject.equals(argument);
69
79
  }
70
80
  };
@@ -73,14 +83,16 @@ public class TimestampCondition implements Condition
73
83
  this.comparator = comparator;
74
84
  if (not) {
75
85
  this.comparator = new TimestampComparator() {
76
- public boolean compare(Timestamp subject) {
86
+ public boolean compare(Timestamp subject)
87
+ {
77
88
  return !comparator.compare(subject);
78
89
  }
79
90
  };
80
91
  }
81
92
  }
82
93
 
83
- public boolean compare(Timestamp subject) {
94
+ public boolean compare(Timestamp subject)
95
+ {
84
96
  return this.comparator.compare(subject);
85
97
  }
86
98
  }
@@ -1,13 +1,15 @@
1
1
  package org.embulk.filter.row;
2
2
 
3
- import org.embulk.filter.row.BooleanCondition;
4
3
  import org.junit.Test;
5
- import static org.junit.Assert.*;
4
+
5
+ import static org.junit.Assert.assertFalse;
6
+ import static org.junit.Assert.assertTrue;
6
7
 
7
8
  public class TestBooleanCondition
8
9
  {
9
10
  @Test
10
- public void testIsNull() {
11
+ public void testIsNull()
12
+ {
11
13
  BooleanCondition condition = new BooleanCondition("IS NULL", null, false);
12
14
  assertTrue(condition.compare(null));
13
15
  assertFalse(condition.compare(new Boolean(true)));
@@ -15,7 +17,8 @@ public class TestBooleanCondition
15
17
  }
16
18
 
17
19
  @Test
18
- public void testIsNotNull() {
20
+ public void testIsNotNull()
21
+ {
19
22
  BooleanCondition condition = new BooleanCondition("IS NOT NULL", null, false);
20
23
  assertFalse(condition.compare(null));
21
24
  assertTrue(condition.compare(new Boolean(true)));
@@ -23,7 +26,8 @@ public class TestBooleanCondition
23
26
  }
24
27
 
25
28
  @Test
26
- public void testNot() {
29
+ public void testNot()
30
+ {
27
31
  BooleanCondition condition = new BooleanCondition("IS NOT NULL", null, true);
28
32
  assertTrue(condition.compare(null));
29
33
  assertFalse(condition.compare(new Boolean(true)));
@@ -31,7 +35,8 @@ public class TestBooleanCondition
31
35
  }
32
36
 
33
37
  @Test
34
- public void testEquals() {
38
+ public void testEquals()
39
+ {
35
40
  BooleanCondition condition = new BooleanCondition("==", new Boolean(true), false);
36
41
  assertTrue(condition.compare(new Boolean(true)));
37
42
  assertFalse(condition.compare(new Boolean(false)));
@@ -47,7 +52,8 @@ public class TestBooleanCondition
47
52
  }
48
53
 
49
54
  @Test
50
- public void testNotEquals() {
55
+ public void testNotEquals()
56
+ {
51
57
  BooleanCondition condition = new BooleanCondition("!=", new Boolean(true), false);
52
58
  assertFalse(condition.compare(new Boolean(true)));
53
59
  assertTrue(condition.compare(new Boolean(false)));