11package org .embulk .output .databricks ;
22
3+ import static org .embulk .output .databricks .util .ConnectionUtil .*;
4+ import static org .junit .Assert .assertTrue ;
5+
36import java .sql .*;
47import java .util .*;
58import java .util .concurrent .Executor ;
9+ import org .embulk .output .databricks .util .ConfigUtil ;
10+ import org .embulk .output .databricks .util .ConnectionUtil ;
611import org .embulk .output .jdbc .JdbcColumn ;
712import org .embulk .output .jdbc .JdbcSchema ;
813import org .embulk .output .jdbc .MergeConfig ;
1116import org .junit .Test ;
1217
1318public class TestDatabricksOutputConnection {
19+ @ Test
20+ public void testTableExists () throws SQLException , ClassNotFoundException {
21+ ConfigUtil .TestTask t = ConfigUtil .createTestTask ();
22+ String asciiTableName = t .getTablePrefix () + "_test" ;
23+ String nonAsciiTableName = t .getTablePrefix () + "_テスト" ;
24+ testTableExists (t .getCatalogName (), t .getSchemaName (), asciiTableName );
25+ testTableExists (t .getNonAsciiCatalogName (), t .getSchemaName (), asciiTableName );
26+ testTableExists (t .getCatalogName (), t .getNonAsciiSchemaName (), asciiTableName );
27+ testTableExists (t .getCatalogName (), t .getSchemaName (), nonAsciiTableName );
28+ testTableExists (t .getNonAsciiCatalogName (), t .getNonAsciiSchemaName (), nonAsciiTableName );
29+ }
30+
31+ private void testTableExists (String catalogName , String schemaName , String tableName )
32+ throws SQLException , ClassNotFoundException {
33+ String fullTableName = String .format ("`%s`.`%s`.`%s`" , catalogName , schemaName , tableName );
34+ try (Connection conn = ConnectionUtil .connectByTestTask ()) {
35+ run (conn , "CREATE TABLE IF NOT EXISTS " + fullTableName );
36+ try (DatabricksOutputConnection outputConn =
37+ buildOutputConnection (conn , catalogName , schemaName )) {
38+ assertTrue (outputConn .tableExists (new TableIdentifier (null , null , tableName )));
39+ }
40+ } finally {
41+ run ("DROP TABLE IF EXISTS " + fullTableName );
42+ }
43+ }
1444
1545 @ Test
16- public void TestBuildCopySQL () throws SQLException {
17- try (DatabricksOutputConnection conn = buildOutputConnection ()) {
46+ public void testBuildCopySQL () throws SQLException {
47+ try (DatabricksOutputConnection conn = buildDummyOutputConnection ()) {
1848 TableIdentifier tableIdentifier = new TableIdentifier ("database" , "schemaName" , "tableName" );
1949 String actual = conn .buildCopySQL (tableIdentifier , "filePath" , buildJdbcSchema ());
2050 String expected =
21- "COPY INTO `database`.`schemaName`.`tableName` FROM ( SELECT _c0::string col0 , _c1::bigint col1 FROM \" filePath\" ) FILEFORMAT = CSV FORMAT_OPTIONS ( 'nullValue' = '\\ \\ N' , 'delimiter' = '\\ t' )" ;
51+ "COPY INTO `database`.`schemaName`.`tableName` FROM ( SELECT _c0::string `あ` , _c1::bigint ```` FROM \" filePath\" ) FILEFORMAT = CSV FORMAT_OPTIONS ( 'nullValue' = '\\ \\ N' , 'delimiter' = '\\ t' )" ;
2252 Assert .assertEquals (expected , actual );
2353 }
2454 }
2555
2656 @ Test
27- public void TestBuildAggregateSQL () throws SQLException {
28- try (DatabricksOutputConnection conn = buildOutputConnection ()) {
57+ public void testBuildAggregateSQL () throws SQLException {
58+ try (DatabricksOutputConnection conn = buildDummyOutputConnection ()) {
2959 List <TableIdentifier > fromTableIdentifiers = new ArrayList <>();
3060 fromTableIdentifiers .add (new TableIdentifier ("database" , "schemaName" , "tableName0" ));
3161 fromTableIdentifiers .add (new TableIdentifier ("database" , "schemaName" , "tableName1" ));
@@ -39,28 +69,28 @@ public void TestBuildAggregateSQL() throws SQLException {
3969 }
4070
4171 @ Test
42- public void TestMergeConfigSQLWithMergeRules () throws SQLException {
72+ public void testMergeConfigSQLWithMergeRules () throws SQLException {
4373 List <String > mergeKeys = buildMergeKeys ("col0" , "col1" );
4474 Optional <List <String >> mergeRules =
4575 buildMergeRules ("col0 = CONCAT(T.col0, 'test')" , "col1 = T.col1 + S.col1" );
4676 String actual = mergeConfigSQL (new MergeConfig (mergeKeys , mergeRules ));
4777 String expected =
48- "MERGE INTO `database`.`schemaName`.`tableName100` T USING `database`.`schemaName`.`tableName9` S ON (T.`col0` = S.`col0` AND T.`col1` = S.`col1`) WHEN MATCHED THEN UPDATE SET col0 = CONCAT(T.col0, 'test'), col1 = T.col1 + S.col1 WHEN NOT MATCHED THEN INSERT (`col0 `, `col1` ) VALUES (S.`col0 `, S.`col1 `);" ;
78+ "MERGE INTO `database`.`schemaName`.`tableName100` T USING `database`.`schemaName`.`tableName9` S ON (T.`col0` = S.`col0` AND T.`col1` = S.`col1`) WHEN MATCHED THEN UPDATE SET col0 = CONCAT(T.col0, 'test'), col1 = T.col1 + S.col1 WHEN NOT MATCHED THEN INSERT (`あ `, ```` ) VALUES (S.`あ `, S.``` `);" ;
4979 Assert .assertEquals (expected , actual );
5080 }
5181
5282 @ Test
53- public void TestMergeConfigSQLWithNoMergeRules () throws SQLException {
83+ public void testMergeConfigSQLWithNoMergeRules () throws SQLException {
5484 List <String > mergeKeys = buildMergeKeys ("col0" , "col1" );
5585 Optional <List <String >> mergeRules = Optional .empty ();
5686 String actual = mergeConfigSQL (new MergeConfig (mergeKeys , mergeRules ));
5787 String expected =
58- "MERGE INTO `database`.`schemaName`.`tableName100` T USING `database`.`schemaName`.`tableName9` S ON (T.`col0` = S.`col0` AND T.`col1` = S.`col1`) WHEN MATCHED THEN UPDATE SET `col0 ` = S.`col0 `, `col1` = S.`col1` WHEN NOT MATCHED THEN INSERT (`col0 `, `col1` ) VALUES (S.`col0 `, S.`col1 `);" ;
88+ "MERGE INTO `database`.`schemaName`.`tableName100` T USING `database`.`schemaName`.`tableName9` S ON (T.`col0` = S.`col0` AND T.`col1` = S.`col1`) WHEN MATCHED THEN UPDATE SET `あ ` = S.`あ `, ```` = S.```` WHEN NOT MATCHED THEN INSERT (`あ `, ```` ) VALUES (S.`あ `, S.``` `);" ;
5989 Assert .assertEquals (expected , actual );
6090 }
6191
6292 private String mergeConfigSQL (MergeConfig mergeConfig ) throws SQLException {
63- try (DatabricksOutputConnection conn = buildOutputConnection ()) {
93+ try (DatabricksOutputConnection conn = buildDummyOutputConnection ()) {
6494 TableIdentifier aggregateToTable =
6595 new TableIdentifier ("database" , "schemaName" , "tableName9" );
6696 TableIdentifier toTable = new TableIdentifier ("database" , "schemaName" , "tableName100" );
@@ -76,15 +106,21 @@ private Optional<List<String>> buildMergeRules(String... keys) {
76106 return keys .length > 0 ? Optional .of (Arrays .asList (keys )) : Optional .empty ();
77107 }
78108
79- private DatabricksOutputConnection buildOutputConnection () throws SQLException {
109+ private DatabricksOutputConnection buildOutputConnection (
110+ Connection conn , String catalogName , String schemaName )
111+ throws SQLException , ClassNotFoundException {
112+ return new DatabricksOutputConnection (conn , catalogName , schemaName );
113+ }
114+
115+ private DatabricksOutputConnection buildDummyOutputConnection () throws SQLException {
80116 return new DatabricksOutputConnection (
81117 buildDummyConnection (), "defaultCatalogName" , "defaultSchemaName" );
82118 }
83119
84120 private JdbcSchema buildJdbcSchema () {
85121 List <JdbcColumn > jdbcColumns = new ArrayList <>();
86- jdbcColumns .add (JdbcColumn .newTypeDeclaredColumn ("col0 " , Types .VARCHAR , "string" , true , false ));
87- jdbcColumns .add (JdbcColumn .newTypeDeclaredColumn ("col1 " , Types .BIGINT , "bigint" , true , false ));
122+ jdbcColumns .add (JdbcColumn .newTypeDeclaredColumn ("あ " , Types .VARCHAR , "string" , true , false ));
123+ jdbcColumns .add (JdbcColumn .newTypeDeclaredColumn ("` " , Types .BIGINT , "bigint" , true , false ));
88124 return new JdbcSchema (jdbcColumns );
89125 }
90126
0 commit comments