dan-s1 commented on code in PR #8728: URL: https://github.com/apache/nifi/pull/8728#discussion_r1589300753
########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/ImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.nifi.processors.standard.db.ColumnDescription; +import org.apache.nifi.util.StringUtils; + +/** + * A generic database adapter that generates Impala compatible SQL. + */ +public class ImpalaDatabaseAdapter extends GenericDatabaseAdapter { + @Override + public String getName() { + return "Impala"; + } + + @Override + public String getDescription() { + return "Generates Impala compatible SQL"; + } + + @Override + public String unwrapIdentifier(String identifier) { + // Removes double quotes and back-ticks. + return identifier == null ? null : identifier.replaceAll("[\"`]", ""); + } + + @Override + public boolean supportsUpsert() { + return true; + } + + @Override + public boolean supportsInsertIgnore() { + return false; + } + + @Override + public String getUpsertStatement(String table, List<String> columnNames, Collection<String> uniqueKeyColumnNames) { + if (StringUtils.isEmpty(table)) { + throw new IllegalArgumentException("Table name cannot be null or blank"); + } + if (columnNames == null || columnNames.isEmpty()) { + throw new IllegalArgumentException("Column names cannot be null or empty"); + } + if (uniqueKeyColumnNames == null || uniqueKeyColumnNames.isEmpty()) { + throw new IllegalArgumentException("Key column names cannot be null or empty"); + } Review Comment: ```suggestion if (StringUtils.isEmpty(columnNames) { throw new IllegalArgumentException("Column names cannot be null or empty"); } if (StringUtils.isEmpty(uniqueKeyColumnNames) { throw new IllegalArgumentException("Key column names cannot be null or empty"); } ``` ########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/impl/TestImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestImpalaDatabaseAdapter { + + private ImpalaDatabaseAdapter testSubject; + + @BeforeEach + public void setUp() throws Exception { + testSubject = new ImpalaDatabaseAdapter(); + } + + @Test + public void testSupportsUpsert() { + assertTrue(testSubject.supportsUpsert(), testSubject.getClass().getSimpleName() + " should support upsert"); + } + + @Test + public void testGetUpsertStatementWithNullTableName() { + testGetUpsertStatement(null, Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithBlankTableName() { + testGetUpsertStatement("", Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithNullColumnNames() { + testGetUpsertStatement("notEmpty", null, Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.emptyList(), Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithNullKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), null, new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), Collections.emptyList(), new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatement() { + // GIVEN + String tableName = "table"; + List<String> columnNames = Arrays.asList("column1", "column2", "column3", "column4"); + Collection<String> uniqueKeyColumnNames = Arrays.asList("column2", "column4"); + + String expected = "UPSERT INTO" + + " table(column1, column2, column3, column4) VALUES (?, ?, ?, ?)"; + + // WHEN + // THEN Review Comment: ```suggestion ``` ########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/impl/TestImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestImpalaDatabaseAdapter { + + private ImpalaDatabaseAdapter testSubject; + + @BeforeEach + public void setUp() throws Exception { + testSubject = new ImpalaDatabaseAdapter(); + } + + @Test + public void testSupportsUpsert() { + assertTrue(testSubject.supportsUpsert(), testSubject.getClass().getSimpleName() + " should support upsert"); + } + + @Test + public void testGetUpsertStatementWithNullTableName() { + testGetUpsertStatement(null, Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithBlankTableName() { + testGetUpsertStatement("", Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithNullColumnNames() { + testGetUpsertStatement("notEmpty", null, Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.emptyList(), Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithNullKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), null, new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), Collections.emptyList(), new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatement() { + // GIVEN Review Comment: ```suggestion ``` ########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/impl/TestImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestImpalaDatabaseAdapter { + + private ImpalaDatabaseAdapter testSubject; + + @BeforeEach + public void setUp() throws Exception { + testSubject = new ImpalaDatabaseAdapter(); + } + + @Test + public void testSupportsUpsert() { + assertTrue(testSubject.supportsUpsert(), testSubject.getClass().getSimpleName() + " should support upsert"); + } + + @Test + public void testGetUpsertStatementWithNullTableName() { + testGetUpsertStatement(null, Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithBlankTableName() { + testGetUpsertStatement("", Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithNullColumnNames() { + testGetUpsertStatement("notEmpty", null, Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.emptyList(), Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithNullKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), null, new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), Collections.emptyList(), new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatement() { + // GIVEN + String tableName = "table"; + List<String> columnNames = Arrays.asList("column1", "column2", "column3", "column4"); + Collection<String> uniqueKeyColumnNames = Arrays.asList("column2", "column4"); + + String expected = "UPSERT INTO" + + " table(column1, column2, column3, column4) VALUES (?, ?, ?, ?)"; + + // WHEN + // THEN + testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, expected); + } + + private void testGetUpsertStatement(String tableName, List<String> columnNames, Collection<String> uniqueKeyColumnNames, IllegalArgumentException expected) { + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { + testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, (String) null); + }); + assertEquals(expected.getMessage(), e.getMessage()); + } + + private void testGetUpsertStatement(String tableName, List<String> columnNames, Collection<String> uniqueKeyColumnNames, String expected) { + // WHEN + String actual = testSubject.getUpsertStatement(tableName, columnNames, uniqueKeyColumnNames); + + // THEN Review Comment: ```suggestion ``` ########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/impl/TestImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TestImpalaDatabaseAdapter { + + private ImpalaDatabaseAdapter testSubject; + + @BeforeEach + public void setUp() throws Exception { + testSubject = new ImpalaDatabaseAdapter(); + } + + @Test + public void testSupportsUpsert() { + assertTrue(testSubject.supportsUpsert(), testSubject.getClass().getSimpleName() + " should support upsert"); + } + + @Test + public void testGetUpsertStatementWithNullTableName() { + testGetUpsertStatement(null, Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithBlankTableName() { + testGetUpsertStatement("", Collections.singletonList("notEmpty"), Collections.singletonList("notEmpty"), new IllegalArgumentException("Table name cannot be null or blank")); + } + + @Test + public void testGetUpsertStatementWithNullColumnNames() { + testGetUpsertStatement("notEmpty", null, Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.emptyList(), Collections.singletonList("notEmpty"), new IllegalArgumentException("Column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithNullKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), null, new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatementWithEmptyKeyColumnNames() { + testGetUpsertStatement("notEmpty", Collections.singletonList("notEmpty"), Collections.emptyList(), new IllegalArgumentException("Key column names cannot be null or empty")); + } + + @Test + public void testGetUpsertStatement() { + // GIVEN + String tableName = "table"; + List<String> columnNames = Arrays.asList("column1", "column2", "column3", "column4"); + Collection<String> uniqueKeyColumnNames = Arrays.asList("column2", "column4"); + + String expected = "UPSERT INTO" + + " table(column1, column2, column3, column4) VALUES (?, ?, ?, ?)"; + + // WHEN + // THEN + testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, expected); + } + + private void testGetUpsertStatement(String tableName, List<String> columnNames, Collection<String> uniqueKeyColumnNames, IllegalArgumentException expected) { + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { + testGetUpsertStatement(tableName, columnNames, uniqueKeyColumnNames, (String) null); + }); + assertEquals(expected.getMessage(), e.getMessage()); + } + + private void testGetUpsertStatement(String tableName, List<String> columnNames, Collection<String> uniqueKeyColumnNames, String expected) { + // WHEN Review Comment: ```suggestion ``` ########## nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/ImpalaDatabaseAdapter.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.nifi.processors.standard.db.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.nifi.processors.standard.db.ColumnDescription; +import org.apache.nifi.util.StringUtils; + +/** + * A generic database adapter that generates Impala compatible SQL. + */ +public class ImpalaDatabaseAdapter extends GenericDatabaseAdapter { + @Override + public String getName() { + return "Impala"; + } + + @Override + public String getDescription() { + return "Generates Impala compatible SQL"; + } + + @Override + public String unwrapIdentifier(String identifier) { + // Removes double quotes and back-ticks. + return identifier == null ? null : identifier.replaceAll("[\"`]", ""); + } + + @Override + public boolean supportsUpsert() { + return true; + } + + @Override + public boolean supportsInsertIgnore() { + return false; + } + + @Override + public String getUpsertStatement(String table, List<String> columnNames, Collection<String> uniqueKeyColumnNames) { + if (StringUtils.isEmpty(table)) { + throw new IllegalArgumentException("Table name cannot be null or blank"); + } + if (columnNames == null || columnNames.isEmpty()) { + throw new IllegalArgumentException("Column names cannot be null or empty"); + } + if (uniqueKeyColumnNames == null || uniqueKeyColumnNames.isEmpty()) { + throw new IllegalArgumentException("Key column names cannot be null or empty"); + } + + String columns = columnNames.stream() + .collect(Collectors.joining(", ")); + + String parameterizedInsertValues = columnNames.stream() + .map(__ -> "?") + .collect(Collectors.joining(", ")); + + List<String> updateValues = new ArrayList<>(); + for (int i = 0; i < columnNames.size(); i++) { + updateValues.add(columnNames.get(i) + " = ?"); + } Review Comment: Intellij is complaining with > Contents of collection 'updateValues' are updated, but never queried -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
