eolivelli commented on a change in pull request #9841:
URL: https://github.com/apache/pulsar/pull/9841#discussion_r591302771



##########
File path: 
pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/decoder/protobufnative/PulsarProtobufNativeColumnDecoder.java
##########
@@ -0,0 +1,386 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.sql.presto.decoder.protobufnative;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static io.airlift.slice.Slices.utf8Slice;
+import static 
io.prestosql.decoder.DecoderErrorCode.DECODER_CONVERSION_NOT_SUPPORTED;
+import static io.prestosql.spi.StandardErrorCode.GENERIC_USER_ERROR;
+import static io.prestosql.spi.type.Varchars.truncateToLength;
+import static java.lang.Float.floatToIntBits;
+import static java.lang.String.format;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.DynamicMessage;
+import com.google.protobuf.EnumValue;
+import io.airlift.slice.Slice;
+import io.airlift.slice.Slices;
+import io.prestosql.decoder.DecoderColumnHandle;
+import io.prestosql.decoder.FieldValueProvider;
+import io.prestosql.spi.PrestoException;
+import io.prestosql.spi.block.Block;
+import io.prestosql.spi.block.BlockBuilder;
+import io.prestosql.spi.type.ArrayType;
+import io.prestosql.spi.type.BigintType;
+import io.prestosql.spi.type.BooleanType;
+import io.prestosql.spi.type.DoubleType;
+import io.prestosql.spi.type.IntegerType;
+import io.prestosql.spi.type.MapType;
+import io.prestosql.spi.type.RealType;
+import io.prestosql.spi.type.RowType;
+import io.prestosql.spi.type.RowType.Field;
+import io.prestosql.spi.type.SmallintType;
+import io.prestosql.spi.type.TimestampType;
+import io.prestosql.spi.type.TinyintType;
+import io.prestosql.spi.type.Type;
+import io.prestosql.spi.type.VarbinaryType;
+import io.prestosql.spi.type.VarcharType;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Pulsar {@link org.apache.pulsar.common.schema.SchemaType#PROTOBUF_NATIVE} 
ColumnDecoder.
+ */
+public class PulsarProtobufNativeColumnDecoder {
+    private static final Set<Type> SUPPORTED_PRIMITIVE_TYPES = ImmutableSet.of(
+            BooleanType.BOOLEAN,
+            IntegerType.INTEGER,
+            BigintType.BIGINT,
+            RealType.REAL,
+            DoubleType.DOUBLE,
+            VarbinaryType.VARBINARY);
+
+    private final Type columnType;
+    private final String columnMapping;
+    private final String columnName;
+
+    public PulsarProtobufNativeColumnDecoder(DecoderColumnHandle columnHandle) 
{
+        try {
+            requireNonNull(columnHandle, "columnHandle is null");
+            this.columnType = columnHandle.getType();
+            this.columnMapping = columnHandle.getMapping();
+            this.columnName = columnHandle.getName();
+            checkArgument(!columnHandle.isInternal(),
+                    "unexpected internal column '%s'", columnName);
+            checkArgument(columnHandle.getFormatHint() == null,
+                    "unexpected format hint '%s' defined for column '%s'", 
columnHandle.getFormatHint(), columnName);
+            checkArgument(columnHandle.getDataFormat() == null,
+                    "unexpected data format '%s' defined for column '%s'", 
columnHandle.getDataFormat(), columnName);
+            checkArgument(columnHandle.getMapping() != null,
+                    "mapping not defined for column '%s'", columnName);
+            checkArgument(isSupportedType(columnType),
+                    "Unsupported column type '%s' for column '%s'", 
columnType, columnName);
+        } catch (IllegalArgumentException e) {
+            throw new PrestoException(GENERIC_USER_ERROR, e);
+        }
+    }
+
+    private boolean isSupportedType(Type type) {

Review comment:
       static?

##########
File path: 
pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/decoder/protobufnative/PulsarProtobufNativeColumnDecoder.java
##########
@@ -0,0 +1,386 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.sql.presto.decoder.protobufnative;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static io.airlift.slice.Slices.utf8Slice;
+import static 
io.prestosql.decoder.DecoderErrorCode.DECODER_CONVERSION_NOT_SUPPORTED;
+import static io.prestosql.spi.StandardErrorCode.GENERIC_USER_ERROR;
+import static io.prestosql.spi.type.Varchars.truncateToLength;
+import static java.lang.Float.floatToIntBits;
+import static java.lang.String.format;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.DynamicMessage;
+import com.google.protobuf.EnumValue;
+import io.airlift.slice.Slice;
+import io.airlift.slice.Slices;
+import io.prestosql.decoder.DecoderColumnHandle;
+import io.prestosql.decoder.FieldValueProvider;
+import io.prestosql.spi.PrestoException;
+import io.prestosql.spi.block.Block;
+import io.prestosql.spi.block.BlockBuilder;
+import io.prestosql.spi.type.ArrayType;
+import io.prestosql.spi.type.BigintType;
+import io.prestosql.spi.type.BooleanType;
+import io.prestosql.spi.type.DoubleType;
+import io.prestosql.spi.type.IntegerType;
+import io.prestosql.spi.type.MapType;
+import io.prestosql.spi.type.RealType;
+import io.prestosql.spi.type.RowType;
+import io.prestosql.spi.type.RowType.Field;
+import io.prestosql.spi.type.SmallintType;
+import io.prestosql.spi.type.TimestampType;
+import io.prestosql.spi.type.TinyintType;
+import io.prestosql.spi.type.Type;
+import io.prestosql.spi.type.VarbinaryType;
+import io.prestosql.spi.type.VarcharType;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Pulsar {@link org.apache.pulsar.common.schema.SchemaType#PROTOBUF_NATIVE} 
ColumnDecoder.
+ */
+public class PulsarProtobufNativeColumnDecoder {
+    private static final Set<Type> SUPPORTED_PRIMITIVE_TYPES = ImmutableSet.of(
+            BooleanType.BOOLEAN,
+            IntegerType.INTEGER,
+            BigintType.BIGINT,
+            RealType.REAL,
+            DoubleType.DOUBLE,
+            VarbinaryType.VARBINARY);
+
+    private final Type columnType;
+    private final String columnMapping;
+    private final String columnName;
+
+    public PulsarProtobufNativeColumnDecoder(DecoderColumnHandle columnHandle) 
{
+        try {
+            requireNonNull(columnHandle, "columnHandle is null");
+            this.columnType = columnHandle.getType();
+            this.columnMapping = columnHandle.getMapping();
+            this.columnName = columnHandle.getName();
+            checkArgument(!columnHandle.isInternal(),
+                    "unexpected internal column '%s'", columnName);
+            checkArgument(columnHandle.getFormatHint() == null,
+                    "unexpected format hint '%s' defined for column '%s'", 
columnHandle.getFormatHint(), columnName);
+            checkArgument(columnHandle.getDataFormat() == null,
+                    "unexpected data format '%s' defined for column '%s'", 
columnHandle.getDataFormat(), columnName);
+            checkArgument(columnHandle.getMapping() != null,
+                    "mapping not defined for column '%s'", columnName);
+            checkArgument(isSupportedType(columnType),
+                    "Unsupported column type '%s' for column '%s'", 
columnType, columnName);
+        } catch (IllegalArgumentException e) {
+            throw new PrestoException(GENERIC_USER_ERROR, e);
+        }
+    }
+
+    private boolean isSupportedType(Type type) {
+        if (isSupportedPrimitive(type)) {
+            return true;
+        }
+
+        if (type instanceof ArrayType) {
+            checkArgument(type.getTypeParameters().size() == 1,
+                    "expecting exactly one type parameter for array");
+            return isSupportedType(type.getTypeParameters().get(0));
+        }
+
+        if (type instanceof MapType) {
+            List<Type> typeParameters = type.getTypeParameters();
+            checkArgument(typeParameters.size() == 2,
+                    "expecting exactly two type parameters for map");
+            return isSupportedType(typeParameters.get(1)) && 
isSupportedType(typeParameters.get(0));
+        }
+
+        if (type instanceof RowType) {
+            for (Type fieldType : type.getTypeParameters()) {
+                if (!isSupportedType(fieldType)) {
+                    return false;
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
+    private boolean isSupportedPrimitive(Type type) {

Review comment:
       static ?

##########
File path: 
pulsar-sql/presto-pulsar/src/main/java/org/apache/pulsar/sql/presto/decoder/protobufnative/PulsarProtobufNativeRowDecoderFactory.java
##########
@@ -0,0 +1,167 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.sql.presto.decoder.protobufnative;
+
+import static com.google.common.collect.ImmutableList.toImmutableList;
+import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;
+import static io.prestosql.spi.type.VarcharType.createUnboundedVarcharType;
+import static java.util.stream.Collectors.toList;
+
+import com.google.common.collect.ImmutableList;
+import com.google.protobuf.Descriptors;
+import io.airlift.log.Logger;
+import io.prestosql.decoder.DecoderColumnHandle;
+import io.prestosql.spi.PrestoException;
+import io.prestosql.spi.connector.ColumnMetadata;
+import io.prestosql.spi.type.ArrayType;
+import io.prestosql.spi.type.BigintType;
+import io.prestosql.spi.type.BooleanType;
+import io.prestosql.spi.type.DoubleType;
+import io.prestosql.spi.type.IntegerType;
+import io.prestosql.spi.type.RealType;
+import io.prestosql.spi.type.RowType;
+import io.prestosql.spi.type.StandardTypes;
+import io.prestosql.spi.type.Type;
+import io.prestosql.spi.type.TypeManager;
+import io.prestosql.spi.type.TypeSignature;
+import io.prestosql.spi.type.TypeSignatureParameter;
+import io.prestosql.spi.type.VarbinaryType;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
+import 
org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeSchema;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.schema.SchemaInfo;
+import org.apache.pulsar.sql.presto.PulsarColumnHandle;
+import org.apache.pulsar.sql.presto.PulsarColumnMetadata;
+import org.apache.pulsar.sql.presto.PulsarRowDecoder;
+import org.apache.pulsar.sql.presto.PulsarRowDecoderFactory;
+
+/**
+ * PulsarRowDecoderFactory for {@link 
org.apache.pulsar.common.schema.SchemaType#PROTOBUF_NATIVE}.
+ */
+public class PulsarProtobufNativeRowDecoderFactory implements 
PulsarRowDecoderFactory {
+
+    private TypeManager typeManager;
+
+    public PulsarProtobufNativeRowDecoderFactory(TypeManager typeManager) {
+        this.typeManager = typeManager;
+    }
+
+    @Override
+    public PulsarRowDecoder createRowDecoder(TopicName topicName, SchemaInfo 
schemaInfo,
+                                             Set<DecoderColumnHandle> columns) 
{
+        return new 
PulsarProtobufNativeRowDecoder((GenericProtobufNativeSchema) 
GenericProtobufNativeSchema.of(schemaInfo), columns);
+    }
+
+    @Override
+    public List<ColumnMetadata> extractColumnMetadata(TopicName topicName, 
SchemaInfo schemaInfo,
+                                                      
PulsarColumnHandle.HandleKeyValueType handleKeyValueType) {
+        List<ColumnMetadata> columnMetadata;
+        String schemaJson = new String(schemaInfo.getSchema());
+        if (StringUtils.isBlank(schemaJson)) {
+            throw new PrestoException(NOT_SUPPORTED, "Topic "
+                    + topicName.toString() + " does not have a valid schema");
+        }
+        Descriptors.Descriptor schema;
+        try {
+            schema =
+                    ((GenericProtobufNativeSchema) 
GenericProtobufNativeSchema.of(schemaInfo)).getProtobufNativeSchema();
+        } catch (Exception ex) {
+            throw new PrestoException(NOT_SUPPORTED, "Topic "
+                    + topicName.toString() + " does not have a valid schema");
+        }
+
+        try {
+            columnMetadata = schema.getFields().stream()
+                    .map(field ->
+                            new 
PulsarColumnMetadata(PulsarColumnMetadata.getColumnName(handleKeyValueType, 
field.getName()),
+                                    parseProtobufPrestoType(field), 
field.getType().toString(), null, false, false,
+                                    handleKeyValueType, new 
PulsarColumnMetadata.DecoderExtraInfo(field.getName(),
+                                    null, null))
+
+                    ).collect(toList());
+        } catch (StackOverflowError e) {

Review comment:
       Why should we catch this exception ?
   IMHO this is a code smell, if we fall into a StackOverflowError then there 
is a bad problem somewhere else




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to