pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] sijie closed pull request #3202: enable java enums to be queried in sql
Date Tue, 18 Dec 2018 01:56:32 GMT
sijie closed pull request #3202: enable java enums to be queried in sql
URL: https://github.com/apache/pulsar/pull/3202
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarMetadata.java
b/pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarMetadata.java
index ddc27a4e93..19cfe5f6ef 100644
--- a/pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarMetadata.java
+++ b/pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/PulsarMetadata.java
@@ -399,6 +399,12 @@ public void accept(PulsarInternalColumn pulsarInternalColumn) {
         } else if (fieldSchema.getType() == Schema.Type.MAP) {
 
         } else if (fieldSchema.getType() == Schema.Type.ENUM) {
+            PulsarColumnMetadata columnMetadata = new PulsarColumnMetadata(fieldName,
+                    convertType(fieldSchema.getType(), fieldSchema.getLogicalType()),
+                    null, null, false, false,
+                    fieldNames.toArray(new String[fieldNames.size()]),
+                    positionIndices.toArray(new Integer[positionIndices.size()]));
+            columnMetadataList.add(columnMetadata);
 
         } else if (fieldSchema.getType() == Schema.Type.FIXED) {
 
@@ -433,6 +439,8 @@ static Type convertType(Schema.Type avroType, LogicalType logicalType)
{
                 return VarbinaryType.VARBINARY;
             case STRING:
                 return VarcharType.VARCHAR;
+            case ENUM:
+                return VarcharType.VARCHAR;
             default:
                 log.error("Cannot convert type: %s", avroType);
                 return null;
@@ -449,5 +457,6 @@ static boolean isPrimitiveType(Schema.Type type) {
                 || Schema.Type.DOUBLE == type
                 || Schema.Type.BYTES == type
                 || Schema.Type.STRING == type;
+
     }
 }
diff --git a/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarConnector.java
b/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarConnector.java
index 53191c6583..b6eade22da 100644
--- a/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarConnector.java
+++ b/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarConnector.java
@@ -143,6 +143,12 @@
             public int field1;
         }
 
+        public enum TestEnum {
+            TEST_ENUM_1,
+            TEST_ENUM_2,
+            TEST_ENUM_3
+        }
+
         public int field1;
         public String field2;
         public float field3;
@@ -156,6 +162,7 @@
         @org.apache.avro.reflect.AvroSchema("{ \"type\": \"int\", \"logicalType\": \"date\"
}")
         public int date;
         public TestPulsarConnector.Bar bar;
+        public TestEnum field7;
     }
 
     public static class Bar {
@@ -249,6 +256,8 @@
             fooTypes.put("bar.test2.field5", BooleanType.BOOLEAN);
             fooTypes.put("bar.test2.field6", BigintType.BIGINT);
             fooTypes.put("bar.test2.foobar.field1", IntegerType.INTEGER);
+            // Enums currently map to VARCHAR
+            fooTypes.put("field7", VarcharType.VARCHAR);
 
             topicsToNumEntries = new HashMap<>();
             topicsToNumEntries.put(TOPIC_1.getSchemaName(), 1233L);
@@ -510,6 +519,19 @@
                     bar_test2_foobar_fieldNames1,
                     bar_test2_foobar_positionIndices1));
 
+            String[] fieldNames10 = {"field7"};
+            Integer[] positionIndices10 = {10};
+            fooFieldNames.put("field7", fieldNames10);
+            fooPositionIndices.put("field7", positionIndices10);
+            fooColumnHandles.add(new PulsarColumnHandle(pulsarConnectorId.toString(),
+                    "field7",
+                    fooTypes.get("field7"),
+                    false,
+                    false,
+                    fieldNames10,
+                    positionIndices10));
+
+
             fooColumnHandles.addAll(PulsarInternalColumn.getInternalFields().stream().map(
                     new Function<PulsarInternalColumn, PulsarColumnHandle>() {
                         @Override
@@ -565,7 +587,7 @@ public PulsarColumnHandle apply(PulsarInternalColumn pulsarInternalColumn)
{
             fooFunctions.put("bar.test2.field5", integer -> (integer + 1) % 32 == 0);
             fooFunctions.put("bar.test2.field6", integer -> integer + 15L);
             fooFunctions.put("bar.test2.foobar.field1", integer -> integer % 3);
-
+            fooFunctions.put("field7", integer -> Foo.TestEnum.values()[integer % Foo.TestEnum.values().length]);
 
         } catch (Throwable e) {
             System.out.println("Error: " + e);
@@ -844,6 +866,7 @@ public void run() {
                                     foo.time = (int) fooFunctions.get("time").apply(count);
                                     foo.date = (int) fooFunctions.get("date").apply(count);
                                     foo.bar = bar;
+                                    foo.field7 = (Foo.TestEnum) fooFunctions.get("field7").apply(count);
 
                                     PulsarApi.MessageMetadata messageMetadata = PulsarApi.MessageMetadata.newBuilder()
                                             .setProducerName("test-producer").setSequenceId(positions.get(topic))
diff --git a/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarRecordCursor.java
b/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarRecordCursor.java
index b0fc42a2ac..7a8b18f80c 100644
--- a/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarRecordCursor.java
+++ b/pulsar-sql/presto-pulsar/src/test/java/org/apache/pulsar/sql/presto/TestPulsarRecordCursor.java
@@ -109,6 +109,9 @@ public void testTopics() throws Exception {
                         } else if (fooColumnHandles.get(i).getName().equals("bar.test2.foobar.field1"))
{
                             Assert.assertEquals(pulsarRecordCursor.getLong(i), ((Integer)
fooFunctions.get("bar.test2.foobar.field1").apply(count)).longValue());
                             columnsSeen.add(fooColumnHandles.get(i).getName());
+                        } else if (fooColumnHandles.get(i).getName().equals("field7")) {
+                            Assert.assertEquals(pulsarRecordCursor.getSlice(i).getBytes(),
fooFunctions.get("field7").apply(count).toString().getBytes());
+                            columnsSeen.add(fooColumnHandles.get(i).getName());
                         } else {
                             if (PulsarInternalColumn.getInternalFieldsMap().containsKey(fooColumnHandles.get(i).getName()))
{
                                 columnsSeen.add(fooColumnHandles.get(i).getName());


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message