Search in sources :

Example 41 with ConnectorTableMetadata

use of com.facebook.presto.spi.ConnectorTableMetadata in project presto by prestodb.

the class MetadataManager method getTableMetadata.

@Override
public TableMetadata getTableMetadata(Session session, TableHandle tableHandle) {
    ConnectorId connectorId = tableHandle.getConnectorId();
    ConnectorMetadata metadata = getMetadata(session, connectorId);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(session.toConnectorSession(connectorId), tableHandle.getConnectorHandle());
    return new TableMetadata(connectorId, tableMetadata);
}
Also used : ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 42 with ConnectorTableMetadata

use of com.facebook.presto.spi.ConnectorTableMetadata in project presto by prestodb.

the class LogicalPlanner method createTableMetadata.

private ConnectorTableMetadata createTableMetadata(QualifiedObjectName table, List<ColumnMetadata> columns, Map<String, Expression> propertyExpressions, List<Expression> parameters) {
    ConnectorId connectorId = metadata.getCatalogHandle(session, table.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + table.getCatalogName()));
    Map<String, Object> properties = metadata.getTablePropertyManager().getProperties(connectorId, table.getCatalogName(), propertyExpressions, session, metadata, parameters);
    return new ConnectorTableMetadata(table.asSchemaTableName(), columns, properties);
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 43 with ConnectorTableMetadata

use of com.facebook.presto.spi.ConnectorTableMetadata in project presto by prestodb.

the class TestMemoryMetadata method testActiveTableIds.

@Test
public void testActiveTableIds() {
    assertThatNoTableIsCreated();
    SchemaTableName firstTableName = new SchemaTableName("default", "first_table");
    metadata.createTable(SESSION, new ConnectorTableMetadata(firstTableName, ImmutableList.of(), ImmutableMap.of()));
    MemoryTableHandle firstTableHandle = (MemoryTableHandle) metadata.getTableHandle(SESSION, firstTableName);
    Long firstTableId = firstTableHandle.getTableId();
    assertTrue(metadata.beginInsert(SESSION, firstTableHandle).getActiveTableIds().contains(firstTableId));
    SchemaTableName secondTableName = new SchemaTableName("default", "second_table");
    metadata.createTable(SESSION, new ConnectorTableMetadata(secondTableName, ImmutableList.of(), ImmutableMap.of()));
    MemoryTableHandle secondTableHandle = (MemoryTableHandle) metadata.getTableHandle(SESSION, secondTableName);
    Long secondTableId = secondTableHandle.getTableId();
    assertNotEquals(firstTableId, secondTableId);
    assertTrue(metadata.beginInsert(SESSION, secondTableHandle).getActiveTableIds().contains(firstTableId));
    assertTrue(metadata.beginInsert(SESSION, secondTableHandle).getActiveTableIds().contains(secondTableId));
}
Also used : SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Example 44 with ConnectorTableMetadata

use of com.facebook.presto.spi.ConnectorTableMetadata in project presto by prestodb.

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()) {
                batch.execute();
                return;
            }
            PreparedBatchPart part = batch.add();
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    part.bind(column, cursor.getBoolean(column));
                } else if (BIGINT.equals(type)) {
                    part.bind(column, cursor.getLong(column));
                } else if (INTEGER.equals(type)) {
                    part.bind(column, (int) cursor.getLong(column));
                } else if (DOUBLE.equals(type)) {
                    part.bind(column, cursor.getDouble(column));
                } else if (type instanceof VarcharType) {
                    part.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);
                    part.bind(column, new Date(localMillis));
                } else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
        }
        batch.execute();
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) Time(java.sql.Time) TpchRecordSet.createTpchRecordSet(com.facebook.presto.tpch.TpchRecordSet.createTpchRecordSet) LINE_ITEM(io.airlift.tpch.TpchTable.LINE_ITEM) StatementContext(org.skife.jdbi.v2.StatementContext) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) DecimalType(com.facebook.presto.spi.type.DecimalType) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) ResultSet(java.sql.ResultSet) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) UNKNOWN(com.facebook.presto.type.UnknownType.UNKNOWN) DateTimeZoneIndex.getDateTimeZone(com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) REGION(io.airlift.tpch.TpchTable.REGION) TINYINT(com.facebook.presto.spi.type.TinyintType.TINYINT) Collections.nCopies(java.util.Collections.nCopies) Timestamp(java.sql.Timestamp) TpchMetadata(com.facebook.presto.tpch.TpchMetadata) TINY_SCHEMA_NAME(com.facebook.presto.tpch.TpchMetadata.TINY_SCHEMA_NAME) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) RecordCursor(com.facebook.presto.spi.RecordCursor) TpchTableHandle(com.facebook.presto.tpch.TpchTableHandle) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) PreparedBatchPart(org.skife.jdbi.v2.PreparedBatchPart) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) Joiner(com.google.common.base.Joiner) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) Strings.padEnd(com.google.common.base.Strings.padEnd) NATION(io.airlift.tpch.TpchTable.NATION) TIMESTAMP_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Type(com.facebook.presto.spi.type.Type) DBI(org.skife.jdbi.v2.DBI) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Language(org.intellij.lang.annotations.Language) Session(com.facebook.presto.Session) TIME(com.facebook.presto.spi.type.TimeType.TIME) SMALLINT(com.facebook.presto.spi.type.SmallintType.SMALLINT) TIME_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) RecordSet(com.facebook.presto.spi.RecordSet) Date(java.sql.Date) CharType(com.facebook.presto.spi.type.CharType) TimeUnit(java.util.concurrent.TimeUnit) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Handle(org.skife.jdbi.v2.Handle) DATE(com.facebook.presto.spi.type.DateType.DATE) REAL(com.facebook.presto.spi.type.RealType.REAL) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Closeable(java.io.Closeable) VarcharType(com.facebook.presto.spi.type.VarcharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) Type(com.facebook.presto.spi.type.Type) CharType(com.facebook.presto.spi.type.CharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) RecordCursor(com.facebook.presto.spi.RecordCursor) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) PreparedBatchPart(org.skife.jdbi.v2.PreparedBatchPart) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) Date(java.sql.Date)

Example 45 with ConnectorTableMetadata

use of com.facebook.presto.spi.ConnectorTableMetadata in project presto by prestodb.

the class TestingMetadata method renameColumn.

@Override
public void renameColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle source, String target) {
    ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
    SchemaTableName tableName = getTableName(tableHandle);
    ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, source);
    List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
    columns.set(columns.indexOf(columnMetadata), new ColumnMetadata(target, columnMetadata.getType(), columnMetadata.getComment(), columnMetadata.isHidden()));
    tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties()));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ArrayList(java.util.ArrayList) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

Aggregations

ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)60 SchemaTableName (com.facebook.presto.spi.SchemaTableName)28 Test (org.testng.annotations.Test)28 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)26 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)23 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)17 ImmutableList (com.google.common.collect.ImmutableList)14 ConnectorSession (com.facebook.presto.spi.ConnectorSession)13 ColumnHandle (com.facebook.presto.spi.ColumnHandle)12 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 List (java.util.List)11 ConnectorOutputTableHandle (com.facebook.presto.spi.ConnectorOutputTableHandle)10 RaptorTableHandle (com.facebook.presto.raptor.RaptorTableHandle)8 PrestoException (com.facebook.presto.spi.PrestoException)7 ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)6 Constraint (com.facebook.presto.spi.Constraint)6 Path (org.apache.hadoop.fs.Path)6 Table (com.facebook.presto.hive.metastore.Table)5 ConnectorPageSink (com.facebook.presto.spi.ConnectorPageSink)5