use of io.prestosql.spi.connector.ColumnMetadata in project hetu-core by openlookeng.
the class TpchMetadata method getTableMetadata.
private static ConnectorTableMetadata getTableMetadata(String schemaName, TpchTable<?> tpchTable, ColumnNaming columnNaming) {
ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
for (TpchColumn<? extends TpchEntity> column : tpchTable.getColumns()) {
columns.add(new ColumnMetadata(columnNaming.getName(column), getPrestoType(column), false, null, null, false, emptyMap()));
}
columns.add(new ColumnMetadata(ROW_NUMBER_COLUMN_NAME, BIGINT, null, true));
SchemaTableName tableName = new SchemaTableName(schemaName, tpchTable.getTableName());
return new ConnectorTableMetadata(tableName, columns.build());
}
use of io.prestosql.spi.connector.ColumnMetadata in project hetu-core by openlookeng.
the class H2QueryRunner method insertRows.
private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data) {
List<ColumnMetadata> columns = tableMetadata.getColumns().stream().filter(columnMetadata -> !columnMetadata.isHidden()).collect(toImmutableList());
String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);
RecordCursor cursor = data.cursor();
while (true) {
// insert 1000 rows at a time
PreparedBatch batch = handle.prepareBatch(sql);
for (int row = 0; row < 1000; row++) {
if (!cursor.advanceNextPosition()) {
if (batch.size() > 0) {
batch.execute();
}
return;
}
for (int column = 0; column < columns.size(); column++) {
Type type = columns.get(column).getType();
if (BOOLEAN.equals(type)) {
batch.bind(column, cursor.getBoolean(column));
} else if (BIGINT.equals(type)) {
batch.bind(column, cursor.getLong(column));
} else if (INTEGER.equals(type)) {
batch.bind(column, (int) cursor.getLong(column));
} else if (DOUBLE.equals(type)) {
batch.bind(column, cursor.getDouble(column));
} else if (type instanceof VarcharType) {
batch.bind(column, cursor.getSlice(column).toStringUtf8());
} else if (DATE.equals(type)) {
long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
// H2 expects dates in to be millis at midnight in the JVM timezone
long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millisUtc);
batch.bind(column, new Date(localMillis));
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
batch.add();
}
batch.execute();
}
}
use of io.prestosql.spi.connector.ColumnMetadata in project hetu-core by openlookeng.
the class CarbondataMetadata method doGetTableMetadata.
@Override
protected ConnectorTableMetadata doGetTableMetadata(ConnectorSession session, SchemaTableName tableName) {
Optional<Table> finalTable = metastore.getTable(new HiveIdentity(session), tableName.getSchemaName(), tableName.getTableName());
if (!finalTable.isPresent() || finalTable.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
throw new TableNotFoundException(tableName);
}
Function<HiveColumnHandle, ColumnMetadata> metadataGetter = columnMetadataGetter(finalTable.get(), typeManager);
ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
for (HiveColumnHandle columnHandle : hiveColumnHandles(finalTable.get())) {
columns.add(metadataGetter.apply(columnHandle));
}
// External location property
ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
properties.put(LOCATION_PROPERTY, finalTable.get().getStorage().getLocation());
// Storage format property
properties.put(HiveTableProperties.STORAGE_FORMAT_PROPERTY, CarbondataStorageFormat.CARBON);
// Partitioning property
List<String> partitionedBy = finalTable.get().getPartitionColumns().stream().map(Column::getName).collect(toList());
if (!partitionedBy.isEmpty()) {
properties.put(HiveTableProperties.PARTITIONED_BY_PROPERTY, partitionedBy);
}
Optional<String> comment = Optional.ofNullable(finalTable.get().getParameters().get(TABLE_COMMENT));
// add partitioned columns into immutableColumns
ImmutableList.Builder<ColumnMetadata> immutableColumns = ImmutableList.builder();
for (HiveColumnHandle columnHandle : hiveColumnHandles(finalTable.get())) {
if (columnHandle.getColumnType().equals(HiveColumnHandle.ColumnType.PARTITION_KEY)) {
immutableColumns.add(metadataGetter.apply(columnHandle));
}
}
return new ConnectorTableMetadata(tableName, columns.build(), properties.build(), comment, Optional.of(immutableColumns.build()), Optional.of(NON_INHERITABLE_PROPERTIES));
}
use of io.prestosql.spi.connector.ColumnMetadata in project hetu-core by openlookeng.
the class LocalFileMetadata method getColumnHandles.
private Map<String, ColumnHandle> getColumnHandles(LocalFileTableHandle tableHandle) {
ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
int index = 0;
for (ColumnMetadata column : localFileTables.getColumns(tableHandle)) {
int ordinalPosition;
if (column.getName().equals(LocalFileColumnHandle.SERVER_ADDRESS_COLUMN_NAME)) {
ordinalPosition = LocalFileColumnHandle.SERVER_ADDRESS_ORDINAL_POSITION;
} else {
ordinalPosition = index;
index++;
}
columnHandles.put(column.getName(), new LocalFileColumnHandle(column.getName(), column.getType(), ordinalPosition));
}
return columnHandles.build();
}
use of io.prestosql.spi.connector.ColumnMetadata in project hetu-core by openlookeng.
the class TestHBaseConnector method testAddColumnException.
/**
* testAddColumnException
*/
@Test
public void testAddColumnException() throws Exception {
HBaseTableHandle tableHandle = TestUtils.createHBaseTableHandle("hbase", "test_table");
// ColumnExist
try {
ColumnMetadata column = new ColumnMetadata("name", VARCHAR);
hcm.addColumn(session, tableHandle, column);
throw new PrestoException(HBaseErrorCode.HBASE_TABLE_DNE, "testAddColumn : failed");
} catch (PrestoException e) {
assertEquals(e.getMessage(), format("addColumn fail, cause the column[name] already exists in table[test_table] ."));
}
// TableFamilySizeIsZero
try {
Map<String, Object> properties = TestUtils.createProperties();
properties.put("hbase_table_name", "hbase:table");
properties.put("family", "f");
properties.put("qualifier", "qualifier");
ColumnMetadata column = new ColumnMetadata("newColumn", VARCHAR, true, "test", "false", false, properties);
hcm.addColumn(session, tableHandle, column);
throw new PrestoException(HBaseErrorCode.HBASE_TABLE_DNE, "testAddColumn : failed");
} catch (PrestoException e) {
assertEquals(e.getMessage(), format("Table hbase:test_table does not exist any more"));
}
}
Aggregations