Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add sybase connector #2976

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<module>presto-postgresql</module>
<module>presto-redshift</module>
<module>presto-sqlserver</module>
<module>presto-sybase</module>
<module>presto-mongodb</module>
<module>presto-client</module>
<module>presto-parser</module>
Expand Down Expand Up @@ -436,6 +437,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-sybase</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.prestosql.hadoop</groupId>
<artifactId>hadoop-apache</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions presto-main/etc/catalog/sybase.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
connector.name=sybase
connection-url=jdbc:sybase:Tds:localhost:8000/MYSYBASE
connection-user=sa
connection-password=myPassword
case-insensitive-name-matching=true
1 change: 1 addition & 0 deletions presto-main/etc/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ plugin.bundles=\
../presto-mysql/pom.xml,\
../presto-memsql/pom.xml,\
../presto-sqlserver/pom.xml, \
../presto-sybase/pom.xml, \
../presto-postgresql/pom.xml, \
../presto-thrift/pom.xml, \
../presto-tpcds/pom.xml, \
Expand Down
Binary file added presto-sybase/jconn4-16.jar
Binary file not shown.
125 changes: 125 additions & 0 deletions presto-sybase/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.prestosql</groupId>
<artifactId>presto-root</artifactId>
<version>327</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>presto-sybase</artifactId>
<description>Presto - Sybase Connector</description>
<packaging>presto-plugin</packaging>

<properties>
<air.main.basedir>${project.parent.basedir}</air.main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-base-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>

<dependency>
<groupId>jconn4</groupId>
<artifactId>jconn4</artifactId>
<version>16.0</version>
<scope>system</scope>
<systemPath>${basedir}/jconn4-16.jar</systemPath>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>slice</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-spi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- for testing -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-main</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-tpch</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-testing-docker</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift.tpch</groupId>
<artifactId>tpch</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.jodah</groupId>
<artifactId>failsafe</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.plugin.sybase;

import com.google.common.base.Joiner;
import io.prestosql.plugin.jdbc.BaseJdbcClient;
import io.prestosql.plugin.jdbc.BaseJdbcConfig;
import io.prestosql.plugin.jdbc.ColumnMapping;
import io.prestosql.plugin.jdbc.ConnectionFactory;
import io.prestosql.plugin.jdbc.JdbcColumnHandle;
import io.prestosql.plugin.jdbc.JdbcIdentity;
import io.prestosql.plugin.jdbc.JdbcTableHandle;
import io.prestosql.plugin.jdbc.JdbcTypeHandle;
import io.prestosql.plugin.jdbc.WriteMapping;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.connector.SchemaTableName;
import io.prestosql.spi.predicate.Domain;
import io.prestosql.spi.type.CharType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.VarcharType;

import javax.inject.Inject;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

import static com.google.common.base.Preconditions.checkArgument;
import static io.prestosql.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static io.prestosql.plugin.jdbc.StandardColumnMappings.booleanWriteFunction;
import static io.prestosql.plugin.jdbc.StandardColumnMappings.charWriteFunction;
import static io.prestosql.plugin.jdbc.StandardColumnMappings.varcharWriteFunction;
import static io.prestosql.spi.type.BooleanType.BOOLEAN;
import static io.prestosql.spi.type.Varchars.isVarcharType;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;

public class SybaseClient
extends BaseJdbcClient
{
private static final Joiner DOT_JOINER = Joiner.on(".");

// Sybase supports 2100 parameters in prepared statement, let's create a space for about 4 big IN predicates
private static final int SYBASE_MAX_LIST_EXPRESSIONS = 500;

// TODO improve this by calling Domain#simplify
private static final UnaryOperator<Domain> DISABLE_UNSUPPORTED_PUSHDOWN = domain -> {
if (domain.getValues().getRanges().getRangeCount() <= SYBASE_MAX_LIST_EXPRESSIONS) {
return domain;
}
return Domain.all(domain.getType());
};

@Inject
public SybaseClient(BaseJdbcConfig config, ConnectionFactory connectionFactory)
{
super(config, "\"", connectionFactory);
}

@Override
protected void renameTable(JdbcIdentity identity, String catalogName, String schemaName, String tableName, SchemaTableName newTable)
{
String sql = format(
"sp_rename %s, %s",
singleQuote(catalogName, schemaName, tableName),
singleQuote(newTable.getTableName()));
try (Connection connection = connectionFactory.openConnection(identity)) {
execute(connection, sql);
}
catch (SQLException e) {
throw new PrestoException(JDBC_ERROR, e);
}
}

@Override
public void renameColumn(JdbcIdentity identity, JdbcTableHandle handle, JdbcColumnHandle jdbcColumn, String newColumnName)
{
try (Connection connection = connectionFactory.openConnection(identity)) {
String sql = format(
"sp_rename %s, %s, 'COLUMN'",
singleQuote(handle.getCatalogName(), handle.getSchemaName(), handle.getTableName(), jdbcColumn.getColumnName()),
singleQuote(newColumnName));
execute(connection, sql);
}
catch (SQLException e) {
throw new PrestoException(JDBC_ERROR, e);
}
}

@Override
protected void copyTableSchema(Connection connection, String catalogName, String schemaName, String tableName, String newTableName, List<String> columnNames)
throws SQLException
{
String sql = format(
"SELECT %s INTO %s FROM %s WHERE 0 = 1",
columnNames.stream()
.map(this::quoted)
.collect(joining(", ")),
quoted(catalogName, schemaName, newTableName),
quoted(catalogName, schemaName, tableName));
execute(connection, sql);
}

@Override
public Optional<ColumnMapping> toPrestoType(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle)
{
Optional<ColumnMapping> mapping = getForcedMappingToVarchar(typeHandle);
if (mapping.isPresent()) {
return mapping;
}
// TODO implement proper type mapping
return super.toPrestoType(session, connection, typeHandle)
.map(columnMapping -> new ColumnMapping(
columnMapping.getType(),
columnMapping.getReadFunction(),
columnMapping.getWriteFunction(),
columnMapping.getWriteNullFunction(),
DISABLE_UNSUPPORTED_PUSHDOWN));
}

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type)
{
if (type == BOOLEAN) {
return WriteMapping.booleanMapping("bit", booleanWriteFunction());
}

if (isVarcharType(type)) {
VarcharType varcharType = (VarcharType) type;
String dataType;
if (varcharType.isUnbounded() || varcharType.getBoundedLength() > 4000) {
dataType = "nvarchar(max)";
}
else {
dataType = "nvarchar(" + varcharType.getBoundedLength() + ")";
}
return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
}

if (type instanceof CharType) {
CharType charType = (CharType) type;
String dataType;
if (charType.getLength() > 4000) {
dataType = "nvarchar(max)";
}
else {
dataType = "nchar(" + charType.getLength() + ")";
}
return WriteMapping.sliceMapping(dataType, charWriteFunction());
}

// TODO implement proper type mapping
return super.toWriteMapping(session, type);
}

@Override
protected Optional<BiFunction<String, Long, String>> limitFunction()
{
return Optional.of((sql, limit) -> {
String start = "SELECT ";
checkArgument(sql.startsWith(start));
return "SELECT TOP " + limit + " " + sql.substring(start.length());
});
}

@Override
public boolean isLimitGuaranteed()
{
return true;
}

private static String singleQuote(String... objects)
{
return singleQuote(DOT_JOINER.join(objects));
}

private static String singleQuote(String literal)
{
return "\'" + literal + "\'";
}
}
Loading