Skip to content

Commit

Permalink
Unit tests for including result set metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Pigott committed Dec 8, 2018
1 parent 72d64cc commit 03091a8
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@
*/
public class JdbcToArrow {

public static final String SQL_CATALOG_NAME_KEY = "SQL_CATALOG_NAME";
public static final String SQL_TABLE_NAME_KEY = "SQL_TABLE_NAME";
public static final String SQL_COLUMN_NAME_KEY = "SQL_COLUMN_NAME";
public static final String SQL_TYPE_KEY = "SQL_TYPE";

/**
* For the given SQL query, execute and fetch the data from Relational DB and convert it to Arrow objects.
* This method uses the default Calendar instance with default TimeZone and Locale as returned by the JVM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig
final Map<String, String> metadata;
if (config.getIncludeMetadata()) {
metadata = new HashMap<String, String>();
metadata.put(JdbcToArrow.SQL_CATALOG_NAME_KEY, rsmd.getCatalogName(i));
metadata.put(JdbcToArrow.SQL_TABLE_NAME_KEY, rsmd.getTableName(i));
metadata.put(JdbcToArrow.SQL_COLUMN_NAME_KEY, columnName);
metadata.put(JdbcToArrow.SQL_TYPE_KEY, rsmd.getColumnTypeName(i));
metadata.put(Constants.SQL_CATALOG_NAME_KEY, rsmd.getCatalogName(i));
metadata.put(Constants.SQL_TABLE_NAME_KEY, rsmd.getTableName(i));
metadata.put(Constants.SQL_COLUMN_NAME_KEY, columnName);
metadata.put(Constants.SQL_TYPE_KEY, rsmd.getColumnTypeName(i));

} else {
metadata = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.arrow.adapter.jdbc;

public class Constants {

public static final String SQL_CATALOG_NAME_KEY = "SQL_CATALOG_NAME";
public static final String SQL_TABLE_NAME_KEY = "SQL_TABLE_NAME";
public static final String SQL_COLUMN_NAME_KEY = "SQL_COLUMN_NAME";
public static final String SQL_TYPE_KEY = "SQL_TYPE";

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.arrow.vector.BaseValueVector;
import org.apache.arrow.vector.BigIntVector;
Expand All @@ -41,6 +45,7 @@
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.Schema;

/**
* This is a Helper class which has functionalities to read and assert the values from the given FieldVector object.
Expand Down Expand Up @@ -179,6 +184,30 @@ public static void assertFieldMetadataIsEmpty(VectorSchemaRoot schema) {
}
}

public static void assertFieldMetadataMatchesResultSetMetadata(ResultSetMetaData rsmd, Schema schema)
throws SQLException {
assertNotNull(schema);
assertNotNull(schema.getFields());
assertNotNull(rsmd);

List<Field> fields = schema.getFields();

assertEquals(rsmd.getColumnCount(), fields.size());

// Vector columns are created in the same order as ResultSet columns.
for (int i = 0; i < rsmd.getColumnCount(); ++i) {
Map<String, String> metadata = fields.get(i).getMetadata();

assertNotNull(metadata);
assertEquals(4, metadata.size());

assertEquals(rsmd.getCatalogName(i + 1), metadata.get(Constants.SQL_CATALOG_NAME_KEY));
assertEquals(rsmd.getTableName(i + 1), metadata.get(Constants.SQL_TABLE_NAME_KEY));
assertEquals(rsmd.getColumnName(i + 1), metadata.get(Constants.SQL_COLUMN_NAME_KEY));
assertEquals(rsmd.getColumnTypeName(i + 1), metadata.get(Constants.SQL_TYPE_KEY));
}
}

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
Expand All @@ -33,10 +34,12 @@
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -127,6 +130,14 @@ public void testJdbcToArroValues() throws SQLException, IOException {
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
JdbcToArrowConfig config = new JdbcToArrowConfig(new RootAllocator(0), Calendar.getInstance(), true);
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
}

/**
* This method calls the assert methods for various DataSets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertVarcharVectorValues;

import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -42,6 +43,7 @@
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand All @@ -58,6 +60,7 @@
import org.apache.arrow.vector.VarBinaryVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -153,6 +156,14 @@ public void testJdbcToArroValues() throws SQLException, IOException {
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
JdbcToArrowConfig config = new JdbcToArrowConfig(new RootAllocator(0), Calendar.getInstance(), true);
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
}

/**
* This method calls the assert methods for various DataSets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertNullValues;

import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -29,6 +30,7 @@
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand All @@ -45,6 +47,7 @@
import org.apache.arrow.vector.VarBinaryVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -110,6 +113,13 @@ public void testJdbcToArroValues() throws SQLException, IOException {
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
JdbcToArrowConfig config = new JdbcToArrowConfig(new RootAllocator(0), Calendar.getInstance(), true);
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
}

/**
* This method calls the assert methods for various DataSets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.getLongValues;

import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -50,6 +51,7 @@
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand All @@ -66,6 +68,7 @@
import org.apache.arrow.vector.VarBinaryVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -144,6 +147,14 @@ public void testJdbcToArroValues() throws SQLException, IOException {
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
JdbcToArrowConfig config = new JdbcToArrowConfig(new RootAllocator(0), Calendar.getInstance(), true);
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
}

/**
* This method calls the assert methods for various DataSets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper.assertTimeVectorValues;

import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -32,12 +33,14 @@
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowTestHelper;
import org.apache.arrow.adapter.jdbc.JdbcToArrowUtils;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.DateMilliVector;
import org.apache.arrow.vector.TimeMilliVector;
import org.apache.arrow.vector.TimeStampVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -120,6 +123,15 @@ public void testJdbcToArroValues() throws SQLException, IOException {
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone())))));
}

@Test
public void testJdbcSchemaMetadata() throws SQLException {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()));
JdbcToArrowConfig config = new JdbcToArrowConfig(new RootAllocator(0), calendar, true);
ResultSetMetaData rsmd = conn.createStatement().executeQuery(table.getQuery()).getMetaData();
Schema schema = JdbcToArrowUtils.jdbcToArrowSchema(rsmd, config);
JdbcToArrowTestHelper.assertFieldMetadataMatchesResultSetMetadata(rsmd, schema);
}

/**
* This method calls the assert methods for various DataSets.
*
Expand Down

0 comments on commit 03091a8

Please sign in to comment.