Search in sources :

Example 1 with PxfRuntimeException

use of org.greenplum.pxf.api.error.PxfRuntimeException in project pxf by greenplum-db.

the class AvroUtilities method decodeString.

/**
 * Parse a Postgres external format into a given Avro schema
 *
 * Re-used from GPHDFS
 * https://github.com/greenplum-db/gpdb/blob/3b0bfdc169fab7f686276be7eccb024a5e29543c/gpAux/extensions/gphdfs/src/java/1.2/com/emc/greenplum/gpdb/hadoop/formathandler/util/FormatHandlerUtil.java
 * @param schema target Avro schema
 * @param value Postgres external format (the output of function named by typoutput in pg_type) or `null` if null value
 * @param isTopLevel
 * @return
 */
public Object decodeString(Schema schema, String value, boolean isTopLevel, boolean hasUserProvidedSchema) {
    LOG.debug("schema={}, value={}, isTopLevel={}", schema, value, isTopLevel);
    Schema.Type fieldType = schema.getType();
    if (fieldType == Schema.Type.ARRAY) {
        if (value == null) {
            return null;
        }
        List<Object> list = new ArrayList<>();
        String[] splits = pgUtilities.splitArray(value);
        Schema elementType = schema.getElementType();
        for (String split : splits) {
            try {
                list.add(decodeString(elementType, split, false, hasUserProvidedSchema));
            } catch (NumberFormatException | PxfRuntimeException e) {
                String hint = "";
                if (StringUtils.startsWith(split, "{")) {
                    hint = hasUserProvidedSchema ? "Value is a multi-dimensional array, please check that the provided AVRO schema has the correct dimensions." : "Value is a multi-dimensional array, user is required to provide an AVRO schema with matching dimensions.";
                } else {
                    hint = hasUserProvidedSchema ? "Check that the AVRO and GPDB schemas are correct." : "Unexpected state since PXF generated the AVRO schema.";
                }
                throw new PxfRuntimeException(String.format("Error parsing array element: %s was not of expected type %s", split, elementType), hint, e);
            }
        }
        return list;
    } else {
        if (fieldType == Schema.Type.UNION) {
            schema = firstNotNullSchema(schema.getTypes());
            fieldType = schema.getType();
            if (fieldType == Schema.Type.ARRAY) {
                return decodeString(schema, value, isTopLevel, hasUserProvidedSchema);
            }
        }
        if (value == null && !isTopLevel) {
            return null;
        }
        switch(fieldType) {
            case INT:
                return Integer.parseInt(value);
            case DOUBLE:
                return Double.parseDouble(value);
            case STRING:
            case RECORD:
            case ENUM:
            case MAP:
                return value;
            case FLOAT:
                return Float.parseFloat(value);
            case LONG:
                return Long.parseLong(value);
            case BYTES:
                return pgUtilities.parseByteaLiteral(value);
            case BOOLEAN:
                return pgUtilities.parseBoolLiteral(value);
            default:
                throw new PxfRuntimeException(String.format("type: %s is not supported", fieldType));
        }
    }
}
Also used : PxfRuntimeException(org.greenplum.pxf.api.error.PxfRuntimeException) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Example 2 with PxfRuntimeException

use of org.greenplum.pxf.api.error.PxfRuntimeException in project pxf by greenplum-db.

the class HcfsTypeTest method testFailureOnFileWhenBasePathIsNotConfigured.

@Test
public void testFailureOnFileWhenBasePathIsNotConfigured() {
    PxfRuntimeException e = assertThrows(PxfRuntimeException.class, () -> HcfsType.getHcfsType(context));
    assertEquals("invalid configuration for server 'awesome_server'", e.getMessage());
    assertEquals("Configure a valid value for 'pxf.fs.basePath' property for server 'awesome_server' to access the filesystem.", e.getHint());
}
Also used : PxfRuntimeException(org.greenplum.pxf.api.error.PxfRuntimeException) Test(org.junit.jupiter.api.Test)

Example 3 with PxfRuntimeException

use of org.greenplum.pxf.api.error.PxfRuntimeException in project pxf by greenplum-db.

the class HiveDataFragmenter method verifySchema.

/**
 * Verifies that all the Greenplum defined columns are present in the Hive
 * table schema.
 *
 * @param tbl the hive table
 */
void verifySchema(Table tbl) {
    List<FieldSchema> hiveColumns = tbl.getSd().getCols();
    List<FieldSchema> hivePartitions = tbl.getPartitionKeys();
    Set<String> columnAndPartitionNames = Stream.concat(hiveColumns.stream(), hivePartitions.stream()).map(FieldSchema::getName).collect(Collectors.toSet());
    for (ColumnDescriptor cd : context.getTupleDescription()) {
        if (!columnAndPartitionNames.contains(cd.columnName()) && !columnAndPartitionNames.contains(cd.columnName().toLowerCase())) {
            throw new PxfRuntimeException(String.format("column '%s' does not exist in the Hive schema", cd.columnName()), "Ensure the column exists and check the column name spelling and case.");
        }
    }
}
Also used : PxfRuntimeException(org.greenplum.pxf.api.error.PxfRuntimeException) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) ColumnDescriptor(org.greenplum.pxf.api.utilities.ColumnDescriptor)

Example 4 with PxfRuntimeException

use of org.greenplum.pxf.api.error.PxfRuntimeException in project pxf by greenplum-db.

the class PxfExceptionHandlerTest method testHandlePxfRuntimeException.

@Test
public void testHandlePxfRuntimeException() throws IOException {
    handler.handlePxfRuntimeException(new PxfRuntimeException("foo"), mockResponse);
    verify(mockResponse).sendError(500);
    verifyNoMoreInteractions(mockResponse);
}
Also used : PxfRuntimeException(org.greenplum.pxf.api.error.PxfRuntimeException) Test(org.junit.jupiter.api.Test)

Example 5 with PxfRuntimeException

use of org.greenplum.pxf.api.error.PxfRuntimeException in project pxf by greenplum-db.

the class HttpRequestParserTest method testMisingPxfApiVersion.

@Test
public void testMisingPxfApiVersion() {
    parameters.remove("X-GP-PXF-API-VERSION");
    PxfRuntimeException e = assertThrows(PxfRuntimeException.class, () -> parser.parseRequest(parameters, RequestType.READ_BRIDGE));
    assertEquals("Property PXF-API-VERSION has no value in the current request", e.getMessage());
    assertEquals("upgrade PXF extension (run 'pxf [cluster] register' and then 'ALTER EXTENSION pxf UPDATE')", e.getHint());
}
Also used : PxfRuntimeException(org.greenplum.pxf.api.error.PxfRuntimeException) Test(org.junit.jupiter.api.Test)

Aggregations

PxfRuntimeException (org.greenplum.pxf.api.error.PxfRuntimeException)18 Test (org.junit.jupiter.api.Test)11 Schema (org.apache.avro.Schema)5 RequestContext (org.greenplum.pxf.api.model.RequestContext)4 Charsets (com.google.common.base.Charsets)3 IOException (java.io.IOException)3 HttpHeaderDecoder (org.greenplum.pxf.service.HttpHeaderDecoder)3 RequestParser (org.greenplum.pxf.service.RequestParser)3 ReadService (org.greenplum.pxf.service.controller.ReadService)3 WriteService (org.greenplum.pxf.service.controller.WriteService)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)3 ArgumentMatchers.same (org.mockito.ArgumentMatchers.same)3 Mock (org.mockito.Mock)3 Mockito.when (org.mockito.Mockito.when)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)3 TestConfiguration (org.springframework.boot.test.context.TestConfiguration)3