Hive13DdlGenerator extends DdlGenerator { @Override public String generateReplaceColumnsStatement(BusinessObjectFormatDdlRequest request, BusinessObjectFormatEntity businessObjectFormatEntity) { BusinessObjectFormat businessObjectFormat = businessObjectFormatHelper.createBusinessObjectFormatFromEntity(businessObjectFormatEntity); assertSchemaColumnsNotEmpty(businessObjectFormat, businessObjectFormatEntity); StringBuilder builder = new StringBuilder(34); builder.append("ALTER TABLE `"); builder.append(request.getTableName()); builder.append("` REPLACE COLUMNS (\n"); builder.append(generateDdlColumns(businessObjectFormatEntity, businessObjectFormat)); return builder.toString().trim() + ';'; }  String escapeSingleQuotes(String string); @Override String generateCreateTableDdl(BusinessObjectDataDdlRequest request, BusinessObjectFormatEntity businessObjectFormatEntity,
        CustomDdlEntity customDdlEntity, List<String> storageNames, List<StorageEntity> requestedStorageEntities,
        Map<String, StorageEntity> cachedStorageEntities, Map<String, String> cachedS3BucketNames); @Override String generateCreateTableDdl(BusinessObjectFormatDdlRequest request, BusinessObjectFormatEntity businessObjectFormatEntity,
        CustomDdlEntity customDdlEntity); @Override String generateReplaceColumnsStatement(BusinessObjectFormatDdlRequest request, BusinessObjectFormatEntity businessObjectFormatEntity); String getDdlCharacterValue(String string); String getDdlCharacterValue(String string, boolean escapeSingleBackslash); @Override BusinessObjectDataDdlOutputFormatEnum getDdlOutputFormat(); List<HivePartitionDto> getHivePartitions(BusinessObjectDataKey businessObjectDataKey, List<SchemaColumn> autoDiscoverableSubPartitionColumns,
        String s3KeyPrefix, Collection<String> storageFiles, String storageName); Pattern getHivePathPattern(List<SchemaColumn> partitionColumns); String getHivePathRegex(List<SchemaColumn> partitionColumns); static final String NO_PARTITIONING_PARTITION_KEY; static final String NO_PARTITIONING_PARTITION_VALUE; static final String ORC_HIVE_FILE_FORMAT; static final String PARQUET_HIVE_FILE_FORMAT; static final String TEXT_HIVE_FILE_FORMAT; static final String JSON_HIVE_FILE_FORMAT; static final String REGEX_S3_EMPTY_PARTITION; }
@Test public void testGenerateReplaceColumnsStatement() { BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest(); businessObjectFormatDdlRequest.setTableName(TABLE_NAME); BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper .createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, FORMAT_DOCUMENT_SCHEMA, FORMAT_DOCUMENT_SCHEMA_URL, true, PARTITION_KEY); { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(0); schemaColumnEntity.setName("col1"); schemaColumnEntity.setType("varchar"); schemaColumnEntity.setSize("255"); schemaColumnEntity.setDescription("lorem ipsum"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(1); schemaColumnEntity.setName("col2"); schemaColumnEntity.setType("date"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(2); schemaColumnEntity.setName("col3"); schemaColumnEntity.setType("map <double, array<bigint(5)>>"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(3); schemaColumnEntity.setName("col4"); schemaColumnEntity.setType("uniontype<int,double,array<string>,struct<a:int,b:string>>"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(3); schemaColumnEntity.setName("col4"); schemaColumnEntity.setType("struct<s:string,f:float,m:map<double,array<bigint>>>"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } String actual = hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity); String expected = "ALTER TABLE `" + businessObjectFormatDdlRequest.getTableName() + "` REPLACE COLUMNS (\n" + " `col1` VARCHAR(255) COMMENT 'lorem ipsum',\n" + " `col2` DATE,\n" + " `col3` map<double,array<bigint(5)>>,\n" + " `col4` uniontype<int,double,array<string>,struct<a:int,b:string>>,\n" + " `col4` struct<s:string,f:float,m:map<double,array<bigint>>>);"; Assert.assertEquals("generated DDL", expected, actual); }
@Test public void testGenerateReplaceColumnsStatementErrorNotComplexType() { BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest(); businessObjectFormatDdlRequest.setTableName(TABLE_NAME); BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper .createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, FORMAT_DOCUMENT_SCHEMA, FORMAT_DOCUMENT_SCHEMA_URL, true, PARTITION_KEY); { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(0); schemaColumnEntity.setName("col1"); schemaColumnEntity.setType("MAP<DOUBLE,"); schemaColumnEntity.setSize("255"); schemaColumnEntity.setDescription("lorem ipsum"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } try { String actual = hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity); Assert.fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { Assert.assertEquals("thrown exception type", IllegalArgumentException.class, e.getClass()); Assert.assertEquals("thrown exception message", "Column \"col1\" has an unsupported data type \"MAP<DOUBLE,\" in the schema for business object format {namespace: \"" + NAMESPACE + "\", businessObjectDefinitionName: \"" + BDEF_NAME + "\", businessObjectFormatUsage: \"" + FORMAT_USAGE_CODE + "\", businessObjectFormatFileType: \"" + FORMAT_FILE_TYPE_CODE + "\", businessObjectFormatVersion: " + FORMAT_VERSION + "}. Exception : \"Error: type expected at the end of 'map<double,'\"", e.getMessage()); } }
@Test public void testGenerateReplaceColumnsStatementErrorNotValidSchemaColumnDataType() { BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest(); businessObjectFormatDdlRequest.setTableName(TABLE_NAME); BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper .createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, FORMAT_DOCUMENT_SCHEMA, FORMAT_DOCUMENT_SCHEMA_URL, true, PARTITION_KEY); { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(0); schemaColumnEntity.setName("col1"); schemaColumnEntity.setType("fooobaar"); schemaColumnEntity.setSize("255"); schemaColumnEntity.setDescription("lorem ipsum"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } try { String actual = hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity); Assert.fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { Assert.assertEquals("thrown exception type", IllegalArgumentException.class, e.getClass()); Assert.assertEquals("thrown exception message", "Column \"col1\" has an unsupported data type \"fooobaar\" in the schema for business object format {namespace: \"" + NAMESPACE + "\", businessObjectDefinitionName: \"" + BDEF_NAME + "\", businessObjectFormatUsage: \"" + FORMAT_USAGE_CODE + "\", businessObjectFormatFileType: \"" + FORMAT_FILE_TYPE_CODE + "\", businessObjectFormatVersion: " + FORMAT_VERSION + "}. Exception : \"Error: type expected at the position 0 of 'fooobaar' but 'fooobaar' is found.\"", e.getMessage()); } }
@Test public void testGenerateReplaceColumnsStatementErrorValidSchemaColumnPrimitiveDataType() { BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest(); businessObjectFormatDdlRequest.setTableName(TABLE_NAME); BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper .createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, FORMAT_DOCUMENT_SCHEMA, FORMAT_DOCUMENT_SCHEMA_URL, true, PARTITION_KEY); { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPosition(0); schemaColumnEntity.setName("col1"); schemaColumnEntity.setType("int(25)"); schemaColumnEntity.setDescription("lorem ipsum"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } try { String actual = hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity); Assert.fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { Assert.assertEquals("thrown exception type", IllegalArgumentException.class, e.getClass()); Assert.assertEquals("thrown exception message", "Column \"col1\" has an unsupported data type \"int(25)\" in the schema for business object format {namespace: \"" + NAMESPACE + "\", businessObjectDefinitionName: \"" + BDEF_NAME + "\", businessObjectFormatUsage: \"" + FORMAT_USAGE_CODE + "\", businessObjectFormatFileType: \"" + FORMAT_FILE_TYPE_CODE + "\", businessObjectFormatVersion: " + FORMAT_VERSION + "}. Exception : \"null\"", e.getMessage()); } }
@Test public void testGenerateReplaceColumnsStatementAssertionErrorIfColumnsEmpty() { BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest(); businessObjectFormatDdlRequest.setTableName(TABLE_NAME); BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper .createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, FORMAT_DOCUMENT_SCHEMA, FORMAT_DOCUMENT_SCHEMA_URL, true, PARTITION_KEY); { SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity(); schemaColumnEntity.setPartitionLevel(0); schemaColumnEntity.setName("col1"); schemaColumnEntity.setType("date"); businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity); } try { hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity); Assert.fail("expected IllegalArgumentException, but no exception was thrown"); } catch (Exception e) { Assert.assertEquals("thrown exception type", IllegalArgumentException.class, e.getClass()); Assert.assertEquals("thrown exception message", "No schema columns specified for business object format {namespace: \"" + NAMESPACE + "\", businessObjectDefinitionName: \"" + BDEF_NAME + "\", businessObjectFormatUsage: \"" + FORMAT_USAGE_CODE + "\", businessObjectFormatFileType: \"" + FORMAT_FILE_TYPE_CODE + "\", businessObjectFormatVersion: " + FORMAT_VERSION + "}.", e.getMessage()); } }