Search in sources :

Example 6 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class ClickHouseClientTest method testRenameColumn.

/**
 * testRenameColumn
 */
@Test
public void testRenameColumn() throws NoSuchFieldException, IllegalAccessException {
    String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
    // get actual table table_with_float_col_*
    String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("table_with_float_col"));
    List<String> expectedColumns = Arrays.asList("col1", "col2", "col3", "col4");
    List<String> actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
    JdbcIdentity identity = JdbcIdentity.from(SESSION);
    SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
    JdbcTableHandle tableHandle = clickHouseClient.getTableHandle(identity, schemaTableName).get();
    List<JdbcColumnHandle> columns = clickHouseClient.getColumns(SESSION, tableHandle);
    JdbcColumnHandle columnHandle = null;
    for (JdbcColumnHandle column : columns) {
        if ("col4".equalsIgnoreCase(column.getColumnName())) {
            columnHandle = column;
            break;
        }
    }
    Field field = JdbcTableHandle.class.getDeclaredField("catalogName");
    field.setAccessible(true);
    field.set(tableHandle, null);
    clickHouseClient.renameColumn(identity, tableHandle, columnHandle, "newcol4");
    expectedColumns = Arrays.asList("col1", "col2", "col3", "newcol4");
    actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
}
Also used : Field(java.lang.reflect.Field) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) JdbcIdentity(io.prestosql.plugin.jdbc.JdbcIdentity) Test(org.testng.annotations.Test)

Example 7 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class ClickHouseClient method getColumns.

@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
        PreparedStatement statement = connection.prepareStatement(sql)) {
        ResultSetMetaData metadata = statement.getMetaData();
        ImmutableMap.Builder<String, ColumnHandle> builder = new ImmutableMap.Builder<>();
        for (int i = 1; i <= metadata.getColumnCount(); i++) {
            String columnName = metadata.getColumnLabel(i);
            String typeName = metadata.getColumnTypeName(i);
            int precision = metadata.getPrecision(i);
            int dataType = metadata.getColumnType(i);
            int scale = metadata.getScale(i);
            boolean isNullAble = metadata.isNullable(i) != ResultSetMetaData.columnNoNulls;
            if (dataType == Types.DECIMAL) {
                String loweredColumnName = columnName.toLowerCase(ENGLISH);
                Type type = types.get(loweredColumnName);
                if (type instanceof DecimalType) {
                    DecimalType decimalType = (DecimalType) type;
                    precision = decimalType.getPrecision();
                    scale = decimalType.getScale();
                }
            }
            JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
            Optional<ColumnMapping> columnMapping;
            try {
                columnMapping = toPrestoType(session, connection, typeHandle);
            } catch (UnsupportedOperationException ex) {
                // User configured to fail the query if the data type is not supported
                return Collections.emptyMap();
            }
            // skip unsupported column types
            if (columnMapping.isPresent()) {
                Type type = columnMapping.get().getType();
                JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullAble);
                builder.put(columnName.toLowerCase(ENGLISH), handle);
            } else {
                return Collections.emptyMap();
            }
        }
        return builder.build();
    } catch (SQLException | PrestoException e) {
        logger.error("in clickhouse push down, clickhouse data source error msg[%s] rewrite sql[%s]", e.getMessage(), sql);
        return Collections.emptyMap();
    }
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) PreparedStatement(java.sql.PreparedStatement) PrestoException(io.prestosql.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) ResultSetMetaData(java.sql.ResultSetMetaData) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) CharType(io.prestosql.spi.type.CharType) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) DecimalType(io.prestosql.spi.type.DecimalType) StandardColumnMappings.tinyintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping) StandardColumnMappings.varcharColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.varcharColumnMapping) StandardColumnMappings.smallintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.smallintColumnMapping) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping) StandardColumnMappings.integerColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.integerColumnMapping) StandardColumnMappings.bigintColumnMapping(io.prestosql.plugin.jdbc.StandardColumnMappings.bigintColumnMapping) SuppressFBWarnings(io.prestosql.spi.SuppressFBWarnings)

Example 8 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class TestBaseJdbcPushDownBase method tableScan.

protected TableScanNode tableScan(PlanBuilder planBuilder, JdbcTableHandle connectorTableHandle, JdbcColumnHandle... columnHandles) {
    List<Symbol> symbols = Arrays.stream(columnHandles).map(column -> new Symbol(column.getColumnName().toLowerCase(Locale.ENGLISH))).collect(toImmutableList());
    ImmutableMap.Builder<Symbol, ColumnHandle> assignments = ImmutableMap.builder();
    for (int i = 0; i < symbols.size(); i++) {
        assignments.put(symbols.get(i), columnHandles[i]);
    }
    TableHandle tableHandle = new TableHandle(catalogName, connectorTableHandle, TestingTransactionHandle.create(), Optional.empty());
    return planBuilder.tableScan(tableHandle, symbols, assignments.build());
}
Also used : Arrays(java.util.Arrays) TestingSession(io.prestosql.testing.TestingSession) SystemSessionProperties(io.prestosql.SystemSessionProperties) TypeProvider(io.prestosql.sql.planner.TypeProvider) DecimalType(io.prestosql.spi.type.DecimalType) SqlParser(io.prestosql.sql.parser.SqlParser) DeterminismEvaluator(io.prestosql.spi.relation.DeterminismEvaluator) ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer) HetuConfig(io.prestosql.utils.HetuConfig) WarningCollector(io.prestosql.execution.warnings.WarningCollector) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) FilterNode(io.prestosql.spi.plan.FilterNode) Collectors.toMap(java.util.stream.Collectors.toMap) ParametricType(io.prestosql.spi.type.ParametricType) Locale(java.util.Locale) JDBC_BIGINT(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_BIGINT) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) FunctionMetadataManager(io.prestosql.spi.function.FunctionMetadataManager) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Type(io.prestosql.spi.type.Type) SqlToRowExpressionTranslator(io.prestosql.sql.relational.SqlToRowExpressionTranslator) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) ImmutableMap(com.google.common.collect.ImmutableMap) RealType(io.prestosql.spi.type.RealType) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) CatalogName(io.prestosql.spi.connector.CatalogName) DriverConnectionFactory(io.prestosql.plugin.jdbc.DriverConnectionFactory) TableScanNode(io.prestosql.spi.plan.TableScanNode) PlanNode(io.prestosql.spi.plan.PlanNode) Driver(org.h2.Driver) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) MetadataManager(io.prestosql.metadata.MetadataManager) String.format(java.lang.String.format) NodeRef(io.prestosql.sql.tree.NodeRef) TimestampType(io.prestosql.spi.type.TimestampType) RowExpressionService(io.prestosql.spi.relation.RowExpressionService) List(java.util.List) HYPER_LOG_LOG(io.prestosql.spi.type.HyperLogLogType.HYPER_LOG_LOG) ExpressionUtils(io.prestosql.sql.ExpressionUtils) TestingTransactionHandle(io.prestosql.testing.TestingTransactionHandle) SessionPropertyManager(io.prestosql.metadata.SessionPropertyManager) FULL_PUSHDOWN(io.prestosql.plugin.jdbc.optimization.JdbcPushDownModule.FULL_PUSHDOWN) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) PlanBuilder(io.prestosql.sql.planner.iterative.rule.test.PlanBuilder) TypeSignature(io.prestosql.spi.type.TypeSignature) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) Types(java.sql.Types) BigintType(io.prestosql.spi.type.BigintType) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) MethodHandle(java.lang.invoke.MethodHandle) FunctionKind(io.prestosql.spi.function.FunctionKind) SESSION(io.prestosql.testing.TestingConnectorSession.SESSION) TypeSignatureParameter(io.prestosql.spi.type.TypeSignatureParameter) HashMap(java.util.HashMap) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) ID(io.prestosql.spi.type.TestingIdType.ID) TypeNotFoundException(io.prestosql.spi.type.TypeNotFoundException) TableHandle(io.prestosql.spi.metadata.TableHandle) JDBC_VARCHAR(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_VARCHAR) QueryGenerator(io.prestosql.spi.sql.QueryGenerator) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) DoubleType(io.prestosql.spi.type.DoubleType) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) DATE(io.prestosql.spi.type.DateType.DATE) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) Symbol(io.prestosql.spi.plan.Symbol) JDBC_INTEGER(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_INTEGER) Properties(java.util.Properties) Assignments(io.prestosql.spi.plan.Assignments) IntegerType(io.prestosql.spi.type.IntegerType) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) TypeManager(io.prestosql.spi.type.TypeManager) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) BaseJdbcClient(io.prestosql.plugin.jdbc.BaseJdbcClient) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) JDBC_REAL(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_REAL) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) LimitNode(io.prestosql.spi.plan.LimitNode) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) RowExpression(io.prestosql.spi.relation.RowExpression) JdbcClient(io.prestosql.plugin.jdbc.JdbcClient) JDBC_BOOLEAN(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_BOOLEAN) JDBC_DOUBLE(io.prestosql.plugin.jdbc.TestingJdbcTypeHandle.JDBC_DOUBLE) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) Expression(io.prestosql.sql.tree.Expression) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Symbol(io.prestosql.spi.plan.Symbol) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) TableHandle(io.prestosql.spi.metadata.TableHandle) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class ClickHouseQueryBuilder method toConjuncts.

private List<String> toConjuncts(JdbcClient client, ConnectorSession session, Connection connection, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain, List<ClickHouseQueryBuilder.TypeAndValue> accumulator) {
    if (tupleDomain.isNone()) {
        return ImmutableList.of(ALWAYS_FALSE);
    }
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (JdbcColumnHandle column : columns) {
        Domain domain = tupleDomain.getDomains().get().get(column);
        if (domain != null) {
            domain = pushDownDomain(client, session, connection, column, domain);
            builder.add(toPredicate(column.getColumnName(), domain, column, accumulator));
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) Domain(io.prestosql.spi.predicate.Domain) TupleDomain(io.prestosql.spi.predicate.TupleDomain)

Example 10 with JdbcColumnHandle

use of io.prestosql.plugin.jdbc.JdbcColumnHandle in project hetu-core by openlookeng.

the class ClickHouseClientTest method testDropColumn.

/**
 * testDropColumn
 */
@Test
public void testDropColumn() throws NoSuchFieldException, IllegalAccessException {
    String schemaName = getNameByUpperCaseIdentifiers(database.getSchema());
    // get actual table example_*
    String tableName = getNameByUpperCaseIdentifiers(database.getActualTable("example"));
    List<String> expectedColumns = Arrays.asList("text", "text_short", "value");
    List<String> actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
    JdbcIdentity identity = JdbcIdentity.from(SESSION);
    SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName);
    JdbcTableHandle tableHandle = clickHouseClient.getTableHandle(identity, schemaTableName).get();
    List<JdbcColumnHandle> columns = clickHouseClient.getColumns(SESSION, tableHandle);
    JdbcColumnHandle columnHandle = null;
    for (JdbcColumnHandle column : columns) {
        if ("value".equalsIgnoreCase(column.getColumnName())) {
            columnHandle = column;
            break;
        }
    }
    Field field = JdbcTableHandle.class.getDeclaredField("catalogName");
    field.setAccessible(true);
    field.set(tableHandle, null);
    clickHouseClient.dropColumn(identity, tableHandle, columnHandle);
    expectedColumns = Arrays.asList("text", "text_short");
    actualColumns = getActualColumns(schemaName, tableName);
    assertEquals(actualColumns, expectedColumns);
}
Also used : Field(java.lang.reflect.Field) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) JdbcTableHandle(io.prestosql.plugin.jdbc.JdbcTableHandle) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) JdbcIdentity(io.prestosql.plugin.jdbc.JdbcIdentity) Test(org.testng.annotations.Test)

Aggregations

JdbcColumnHandle (io.prestosql.plugin.jdbc.JdbcColumnHandle)17 JdbcTypeHandle (io.prestosql.plugin.jdbc.JdbcTypeHandle)8 PrestoException (io.prestosql.spi.PrestoException)8 Type (io.prestosql.spi.type.Type)8 SQLException (java.sql.SQLException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 ColumnMapping (io.prestosql.plugin.jdbc.ColumnMapping)7 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)7 Connection (java.sql.Connection)7 VarcharType (io.prestosql.spi.type.VarcharType)6 PreparedStatement (java.sql.PreparedStatement)6 JdbcTableHandle (io.prestosql.plugin.jdbc.JdbcTableHandle)5 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)5 DecimalType (io.prestosql.spi.type.DecimalType)5 JdbcIdentity (io.prestosql.plugin.jdbc.JdbcIdentity)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 Test (org.testng.annotations.Test)4 SuppressFBWarnings (io.prestosql.spi.SuppressFBWarnings)3 BigintType (io.prestosql.spi.type.BigintType)3 CharType (io.prestosql.spi.type.CharType)3