Search in sources :

Example 1 with ColumnMetadata

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());
}
Also used : ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata)

Example 2 with ColumnMetadata

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();
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LINE_ITEM(io.airlift.tpch.TpchTable.LINE_ITEM) DecimalType(io.prestosql.spi.type.DecimalType) RecordSet(io.prestosql.spi.connector.RecordSet) MaterializedResult(io.prestosql.testing.MaterializedResult) Array(java.sql.Array) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ParsedSql(org.jdbi.v3.core.statement.ParsedSql) ResultSet(java.sql.ResultSet) Handle(org.jdbi.v3.core.Handle) LocalTime(java.time.LocalTime) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) CUSTOMER(io.airlift.tpch.TpchTable.CUSTOMER) REGION(io.airlift.tpch.TpchTable.REGION) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) JsonFunctions.jsonParse(io.prestosql.operator.scalar.JsonFunctions.jsonParse) MathContext(java.math.MathContext) Collections.nCopies(java.util.Collections.nCopies) ArrayType(io.prestosql.spi.type.ArrayType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) String.format(java.lang.String.format) StatementContext(org.jdbi.v3.core.statement.StatementContext) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) LocalDate(java.time.LocalDate) Optional(java.util.Optional) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) TpchRecordSet.createTpchRecordSet(io.prestosql.plugin.tpch.TpchRecordSet.createTpchRecordSet) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) Joiner(com.google.common.base.Joiner) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) Strings.padEnd(com.google.common.base.Strings.padEnd) NATION(io.airlift.tpch.TpchTable.NATION) TIME_WITH_TIME_ZONE(io.prestosql.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) LocalDateTime(java.time.LocalDateTime) CharType(io.prestosql.spi.type.CharType) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) PART(io.airlift.tpch.TpchTable.PART) ArrayList(java.util.ArrayList) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) SQLException(java.sql.SQLException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Session(io.prestosql.Session) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) RecordCursor(io.prestosql.spi.connector.RecordCursor) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Jdbi(org.jdbi.v3.core.Jdbi) Language(org.intellij.lang.annotations.Language) TINY_SCHEMA_NAME(io.prestosql.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) MaterializedRow(io.prestosql.testing.MaterializedRow) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) TpchMetadata(io.prestosql.plugin.tpch.TpchMetadata) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Closeable(java.io.Closeable) SqlParser(org.jdbi.v3.core.statement.SqlParser) VarcharType(io.prestosql.spi.type.VarcharType) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) RecordCursor(io.prestosql.spi.connector.RecordCursor) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) VarcharType(io.prestosql.spi.type.VarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LocalDate(java.time.LocalDate) Date(java.sql.Date)

Example 3 with ColumnMetadata

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));
}
Also used : ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Table(io.prestosql.plugin.hive.metastore.Table) CarbonTable(org.apache.carbondata.core.metadata.schema.table.CarbonTable) ImmutableList(com.google.common.collect.ImmutableList) HiveIdentity(io.prestosql.plugin.hive.authentication.HiveIdentity) ImmutableMap(com.google.common.collect.ImmutableMap) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) HiveColumnHandle(io.prestosql.plugin.hive.HiveColumnHandle) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata)

Example 4 with ColumnMetadata

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();
}
Also used : ColumnHandle(io.prestosql.spi.connector.ColumnHandle) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(io.prestosql.spi.connector.Constraint)

Example 5 with ColumnMetadata

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"));
    }
}
Also used : ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) PrestoException(io.prestosql.spi.PrestoException) Test(org.testng.annotations.Test)

Aggregations

ColumnMetadata (io.prestosql.spi.connector.ColumnMetadata)124 ConnectorTableMetadata (io.prestosql.spi.connector.ConnectorTableMetadata)54 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)47 ImmutableList (com.google.common.collect.ImmutableList)45 Test (org.testng.annotations.Test)45 PrestoException (io.prestosql.spi.PrestoException)42 ImmutableMap (com.google.common.collect.ImmutableMap)37 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)35 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)35 HashMap (java.util.HashMap)32 List (java.util.List)32 Map (java.util.Map)31 Type (io.prestosql.spi.type.Type)29 TupleDomain (io.prestosql.spi.predicate.TupleDomain)27 ArrayList (java.util.ArrayList)27 Optional (java.util.Optional)27 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)26 ConnectorMetadata (io.prestosql.spi.connector.ConnectorMetadata)26 TestingConnectorSession (io.prestosql.testing.TestingConnectorSession)25 Set (java.util.Set)23