use of java.sql.ResultSetMetaData in project hive by apache.
the class TestJdbcDriver2 method testMetaDataGetCatalogs.
@Test
public void testMetaDataGetCatalogs() throws SQLException {
ResultSet rs = con.getMetaData().getCatalogs();
ResultSetMetaData resMeta = rs.getMetaData();
assertEquals(1, resMeta.getColumnCount());
assertEquals("TABLE_CAT", resMeta.getColumnName(1));
assertFalse(rs.next());
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class TestJdbcDriver2 method testBuiltInUDFCol.
/**
* Verify selecting using builtin UDFs
* @throws SQLException
*/
@Test
public void testBuiltInUDFCol() throws SQLException {
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select c12, bin(c12) from " + dataTypeTableName + " where c1=1");
ResultSetMetaData md = res.getMetaData();
// only one result column
assertEquals(md.getColumnCount(), 2);
// verify the system generated column name
assertEquals(md.getColumnLabel(2), "_c1");
assertTrue(res.next());
assertEquals(res.getLong(1), 1);
assertEquals(res.getString(2), "1");
res.close();
stmt.close();
}
use of java.sql.ResultSetMetaData in project hive by apache.
the class TestJdbcDriver2 method testIntervalTypes.
@Test
public void testIntervalTypes() throws Exception {
Statement stmt = con.createStatement();
// Since interval types not currently supported as table columns, need to create them
// as expressions.
ResultSet res = stmt.executeQuery("select case when c17 is null then null else interval '1' year end as col1," + " c17 - c17 as col2 from " + dataTypeTableName);
ResultSetMetaData meta = res.getMetaData();
assertEquals("col1", meta.getColumnLabel(1));
assertEquals(java.sql.Types.OTHER, meta.getColumnType(1));
assertEquals("interval_year_month", meta.getColumnTypeName(1));
assertEquals(11, meta.getColumnDisplaySize(1));
assertEquals(11, meta.getPrecision(1));
assertEquals(0, meta.getScale(1));
assertEquals(HiveIntervalYearMonth.class.getName(), meta.getColumnClassName(1));
assertEquals("col2", meta.getColumnLabel(2));
assertEquals(java.sql.Types.OTHER, meta.getColumnType(2));
assertEquals("interval_day_time", meta.getColumnTypeName(2));
assertEquals(29, meta.getColumnDisplaySize(2));
assertEquals(29, meta.getPrecision(2));
assertEquals(0, meta.getScale(2));
assertEquals(HiveIntervalDayTime.class.getName(), meta.getColumnClassName(2));
// row 1 - results should be null
assertTrue(res.next());
// skip the last (partitioning) column since it is always non-null
for (int i = 1; i < meta.getColumnCount(); i++) {
assertNull("Column " + i + " should be null", res.getObject(i));
}
// row 2 - results should be null
assertTrue(res.next());
for (int i = 1; i < meta.getColumnCount(); i++) {
assertNull("Column " + i + " should be null", res.getObject(i));
}
// row 3
assertTrue(res.next());
assertEquals("1-0", res.getString(1));
assertEquals(1, ((HiveIntervalYearMonth) res.getObject(1)).getYears());
assertEquals("0 00:00:00.000000000", res.getString(2));
assertEquals(0, ((HiveIntervalDayTime) res.getObject(2)).getDays());
stmt.close();
}
use of java.sql.ResultSetMetaData in project zeppelin by apache.
the class JDBCInterpreter method getResults.
private String getResults(ResultSet resultSet, boolean isTableType) throws SQLException {
ResultSetMetaData md = resultSet.getMetaData();
StringBuilder msg;
if (isTableType) {
msg = new StringBuilder(TABLE_MAGIC_TAG);
} else {
msg = new StringBuilder();
}
for (int i = 1; i < md.getColumnCount() + 1; i++) {
if (i > 1) {
msg.append(TAB);
}
msg.append(replaceReservedChars(md.getColumnName(i)));
}
msg.append(NEWLINE);
int displayRowCount = 0;
while (resultSet.next() && displayRowCount < getMaxResult()) {
for (int i = 1; i < md.getColumnCount() + 1; i++) {
Object resultObject;
String resultValue;
resultObject = resultSet.getObject(i);
if (resultObject == null) {
resultValue = "null";
} else {
resultValue = resultSet.getString(i);
}
msg.append(replaceReservedChars(resultValue));
if (i != md.getColumnCount()) {
msg.append(TAB);
}
}
msg.append(NEWLINE);
displayRowCount++;
}
return msg.toString();
}
use of java.sql.ResultSetMetaData in project zeppelin by apache.
the class IgniteSqlInterpreter method interpret.
@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
if (connEx != null) {
return new InterpreterResult(Code.ERROR, connEx.getMessage());
}
StringBuilder msg = new StringBuilder("%table ");
try (Statement stmt = conn.createStatement()) {
curStmt = stmt;
try (ResultSet res = stmt.executeQuery(st)) {
ResultSetMetaData md = res.getMetaData();
for (int i = 1; i <= md.getColumnCount(); i++) {
if (i > 1) {
msg.append('\t');
}
msg.append(md.getColumnName(i));
}
msg.append('\n');
while (res.next()) {
for (int i = 1; i <= md.getColumnCount(); i++) {
msg.append(res.getString(i));
if (i != md.getColumnCount()) {
msg.append('\t');
}
}
msg.append('\n');
}
}
} catch (Exception e) {
logger.error("Exception in IgniteSqlInterpreter while InterpreterResult interpret: ", e);
return IgniteInterpreterUtils.buildErrorResult(e);
} finally {
curStmt = null;
}
return new InterpreterResult(Code.SUCCESS, msg.toString());
}
Aggregations