phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gr...@apache.org
Subject phoenix git commit: PHOENIX-2434 Improve booleans in CSV import
Date Mon, 14 Dec 2015 09:01:07 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master afa9dee2b -> e8a7feedb


PHOENIX-2434 Improve booleans in CSV import

Do not automatically treat any unrecognized input as being false,
but instead fail on it.

Also, recognize 1/0 and t/f as true/false.


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/e8a7feed
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/e8a7feed
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/e8a7feed

Branch: refs/heads/master
Commit: e8a7feedb260bbb4c5abc88ab5e3325f98176ee9
Parents: afa9dee
Author: Bruno Dumon <bruno@ngdata.com>
Authored: Sun Dec 13 16:04:07 2015 +0100
Committer: Gabriel Reid <gabrielr@ngdata.com>
Committed: Mon Dec 14 08:37:48 2015 +0100

----------------------------------------------------------------------
 .../phoenix/util/csv/CsvUpsertExecutor.java       | 15 +++++++++++++++
 .../phoenix/util/AbstractUpsertExecutorTest.java  | 18 +++++++++++++-----
 .../phoenix/util/csv/CsvUpsertExecutorTest.java   | 17 +++++++++++++++--
 3 files changed, 43 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/e8a7feed/phoenix-core/src/main/java/org/apache/phoenix/util/csv/CsvUpsertExecutor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/csv/CsvUpsertExecutor.java
b/phoenix-core/src/main/java/org/apache/phoenix/util/csv/CsvUpsertExecutor.java
index 4a3af21..cddafc6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/csv/CsvUpsertExecutor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/csv/CsvUpsertExecutor.java
@@ -28,6 +28,7 @@ import javax.annotation.Nullable;
 import org.apache.commons.csv.CSVRecord;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.query.QueryServicesOptions;
+import org.apache.phoenix.schema.types.PBoolean;
 import org.apache.phoenix.schema.types.PDataType;
 import org.apache.phoenix.schema.types.PTimestamp;
 import org.apache.phoenix.util.ColumnInfo;
@@ -154,6 +155,20 @@ public class CsvUpsertExecutor extends UpsertExecutor<CSVRecord, String>
{
                 byte[] byteValue = new byte[dataType.getByteSize()];
                 dataType.getCodec().encodeLong(epochTime, byteValue, 0);
                 return dataType.toObject(byteValue);
+            } else if (dataType == PBoolean.INSTANCE) {
+                switch (input.toLowerCase()) {
+                    case "true":
+                    case "t":
+                    case "1":
+                        return Boolean.TRUE;
+                    case "false":
+                    case "f":
+                    case "0":
+                        return Boolean.FALSE;
+                    default:
+                        throw new RuntimeException("Invalid boolean value: '" + input
+                                + "', must be one of ['true','t','1','false','f','0']");
+                }
             }
             return dataType.toObject(input);
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e8a7feed/phoenix-core/src/test/java/org/apache/phoenix/util/AbstractUpsertExecutorTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/AbstractUpsertExecutorTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/util/AbstractUpsertExecutorTest.java
index b614312..61b03fb 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/util/AbstractUpsertExecutorTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/AbstractUpsertExecutorTest.java
@@ -58,7 +58,8 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends BaseConnectionles
                 new ColumnInfo("ID", Types.BIGINT),
                 new ColumnInfo("NAME", Types.VARCHAR),
                 new ColumnInfo("AGE", Types.INTEGER),
-                new ColumnInfo("VALUES", PIntegerArray.INSTANCE.getSqlType()));
+                new ColumnInfo("VALUES", PIntegerArray.INSTANCE.getSqlType()),
+                new ColumnInfo("BEARD", Types.BOOLEAN));
 
         preparedStatement = mock(PreparedStatement.class);
         upsertListener = mock(UpsertExecutor.UpsertListener.class);
@@ -72,7 +73,8 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends BaseConnectionles
 
     @Test
     public void testExecute() throws Exception {
-        getUpsertExecutor().execute(createRecord(123L, "NameValue", 42, Arrays.asList(1,
2, 3)));
+        getUpsertExecutor().execute(createRecord(123L, "NameValue", 42,
+                Arrays.asList(1, 2, 3), true));
 
         verify(upsertListener).upsertDone(1L);
         verifyNoMoreInteractions(upsertListener);
@@ -81,6 +83,7 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends BaseConnectionles
         verify(preparedStatement).setObject(2, "NameValue");
         verify(preparedStatement).setObject(3, Integer.valueOf(42));
         verify(preparedStatement).setObject(4, PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE,
new Object[]{1,2,3}));
+        verify(preparedStatement).setObject(5, Boolean.TRUE);
         verify(preparedStatement).execute();
         verifyNoMoreInteractions(preparedStatement);
     }
@@ -96,7 +99,8 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends BaseConnectionles
 
     @Test
     public void testExecute_TooManyFields() throws Exception {
-        R recordWithTooManyFields = createRecord(123L, "NameValue", 42, Arrays.asList(1,
2, 3), "Garbage");
+        R recordWithTooManyFields = createRecord(123L, "NameValue", 42, Arrays.asList(1,
2, 3),
+                true, "Garbage");
         getUpsertExecutor().execute(recordWithTooManyFields);
 
         verify(upsertListener).upsertDone(1L);
@@ -106,13 +110,15 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends
BaseConnectionles
         verify(preparedStatement).setObject(2, "NameValue");
         verify(preparedStatement).setObject(3, Integer.valueOf(42));
         verify(preparedStatement).setObject(4, PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE,
new Object[]{1,2,3}));
+        verify(preparedStatement).setObject(5, Boolean.TRUE);
         verify(preparedStatement).execute();
         verifyNoMoreInteractions(preparedStatement);
     }
 
     @Test
     public void testExecute_NullField() throws Exception {
-        getUpsertExecutor().execute(createRecord(123L, "NameValue", null, Arrays.asList(1,
2, 3)));
+        getUpsertExecutor().execute(createRecord(123L, "NameValue", null,
+                Arrays.asList(1, 2, 3), false));
 
         verify(upsertListener).upsertDone(1L);
         verifyNoMoreInteractions(upsertListener);
@@ -121,13 +127,15 @@ public abstract class AbstractUpsertExecutorTest<R, F> extends
BaseConnectionles
         verify(preparedStatement).setObject(2, "NameValue");
         verify(preparedStatement).setNull(3, columnInfoList.get(2).getSqlType());
         verify(preparedStatement).setObject(4, PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE,
new Object[]{1,2,3}));
+        verify(preparedStatement).setObject(5, Boolean.FALSE);
         verify(preparedStatement).execute();
         verifyNoMoreInteractions(preparedStatement);
     }
 
     @Test
     public void testExecute_InvalidType() throws Exception {
-        R recordWithInvalidType = createRecord(123L, "NameValue", "ThisIsNotANumber", Arrays.asList(1,
2, 3));
+        R recordWithInvalidType = createRecord(123L, "NameValue", "ThisIsNotANumber",
+                Arrays.asList(1, 2, 3), true);
         getUpsertExecutor().execute(recordWithInvalidType);
 
         verify(upsertListener).errorOnRecord(eq(recordWithInvalidType), any(Throwable.class));

http://git-wip-us.apache.org/repos/asf/phoenix/blob/e8a7feed/phoenix-core/src/test/java/org/apache/phoenix/util/csv/CsvUpsertExecutorTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/csv/CsvUpsertExecutorTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/util/csv/CsvUpsertExecutorTest.java
index 7a09bee..c887ff7 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/util/csv/CsvUpsertExecutorTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/csv/CsvUpsertExecutorTest.java
@@ -21,15 +21,20 @@ import java.io.IOException;
 import java.sql.SQLException;
 import java.util.List;
 
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVRecord;
 import org.apache.phoenix.util.AbstractUpsertExecutorTest;
 import org.apache.phoenix.util.UpsertExecutor;
 import org.junit.Before;
+import org.junit.Test;
 
-import com.google.common.base.Joiner;
-import com.google.common.collect.Iterables;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
 
 public class CsvUpsertExecutorTest extends AbstractUpsertExecutorTest<CSVRecord, String>
{
 
@@ -63,4 +68,12 @@ public class CsvUpsertExecutorTest extends AbstractUpsertExecutorTest<CSVRecord,
         upsertExecutor = new CsvUpsertExecutor(conn, columnInfoList, preparedStatement,
                 upsertListener, ARRAY_SEP);
     }
+
+    @Test
+    public void testExecute_InvalidBoolean() throws Exception {
+        CSVRecord csvRecordWithInvalidType = createRecord("123,NameValue,42,1:2:3,NotABoolean");
+        upsertExecutor.execute(ImmutableList.of(csvRecordWithInvalidType));
+
+        verify(upsertListener).errorOnRecord(eq(csvRecordWithInvalidType), any(Throwable.class));
+    }
 }


Mime
View raw message