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));
}
}
}
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());
}
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.");
}
}
}
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);
}
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());
}
Aggregations