Skip to content

[FLINK-33761][Connector/JDBC] Add Snowflake JDBC Dialect #118

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 51 additions & 13 deletions docs/content/docs/connectors/table/jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ See how to link with it for cluster execution [here]({{< ref "docs/dev/configura

A driver dependency is also required to connect to a specified database. Here are drivers currently supported:

| Driver | Group Id | Artifact Id | JAR |
|:-----------|:---------------------------|:-----------------------|:----------------------------------------------------------------------------------------------------------------------------------|
| MySQL | `mysql` | `mysql-connector-java` | [Download](https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/) |
| Oracle | `com.oracle.database.jdbc` | `ojdbc8` | [Download](https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8) |
| PostgreSQL | `org.postgresql` | `postgresql` | [Download](https://jdbc.postgresql.org/download/) |
| Derby | `org.apache.derby` | `derby` | [Download](http://db.apache.org/derby/derby_downloads.html) |
| SQL Server | `com.microsoft.sqlserver` | `mssql-jdbc` | [Download](https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver16) |
| CrateDB | `io.crate` | `crate-jdbc` | [Download](https://repo1.maven.org/maven2/io/crate/crate-jdbc/) |
| Db2 | `com.ibm.db2.jcc` | `db2jcc` | [Download](https://www.ibm.com/support/pages/download-db2-fix-packs-version-db2-linux-unix-and-windows) |
| Trino | `io.trino` | `trino-jdbc` | [Download](https://repo1.maven.org/maven2/io/trino/trino-jdbc/) |
| OceanBase | `com.oceanbase` | `oceanbase-client` | [Download](https://repo1.maven.org/maven2/com/oceanbase/oceanbase-client/) |

| Driver | Group Id | Artifact Id | JAR |
|:-----------|:----------------------------|:------------------------|:----------------------------------------------------------------------------------------------------------------------------------|
| MySQL | `mysql` | `mysql-connector-java` | [Download](https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/) |
| Oracle | `com.oracle.database.jdbc` | `ojdbc8` | [Download](https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8) |
| PostgreSQL | `org.postgresql` | `postgresql` | [Download](https://jdbc.postgresql.org/download/) |
| Derby | `org.apache.derby` | `derby` | [Download](http://db.apache.org/derby/derby_downloads.html) |
| SQL Server | `com.microsoft.sqlserver` | `mssql-jdbc` | [Download](https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver16) |
| CrateDB | `io.crate` | `crate-jdbc` | [Download](https://repo1.maven.org/maven2/io/crate/crate-jdbc/) |
| Db2 | `com.ibm.db2.jcc` | `db2jcc` | [Download](https://www.ibm.com/support/pages/download-db2-fix-packs-version-db2-linux-unix-and-windows) |
| Trino | `io.trino` | `trino-jdbc` | [Download](https://repo1.maven.org/maven2/io/trino/trino-jdbc/) |
| OceanBase | `com.oceanbase` | `oceanbase-client` | [Download](https://repo1.maven.org/maven2/com/oceanbase/oceanbase-client/) |
| Snowflake | `net.snowflake` | `snowflake-jdbc` | [Download](https://repo1.maven.org/maven2/net/snowflake/snowflake-jdbc/)

JDBC connector and drivers are not part of Flink's binary distribution. See how to link with them for cluster execution [here]({{< ref "docs/dev/configuration/overview" >}}).

Expand Down Expand Up @@ -433,6 +433,13 @@ As there is no standard syntax for upsert, the following table describes the dat
WHEN NOT MATCHED THEN INSERT (..) <br>
VALUES (..)</td>
</tr>
<tr>
<td>Snowflake</td>
<td>MERGE INTO .. USING (..) ON (..) <br>
WHEN MATCHED THEN UPDATE SET (..) <br>
WHEN NOT MATCHED THEN INSERT (..) <br>
VALUES (..)</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -670,6 +677,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<th class="text-left"><a href="https://trino.io/docs/current/language/types.html">Trino type</a></th>
<th class="text-left"><a href="https://en.oceanbase.com/docs/common-oceanbase-database-10000000001106898">OceanBase MySQL mode type</a></th>
<th class="text-left"><a href="https://en.oceanbase.com/docs/common-oceanbase-database-10000000001107076">OceanBase Oracle mode type</a></th>
<th class="text-left"><a href="https://docs.snowflake.com/en/sql-reference-data-types">Snowflake</a></th>
<th class="text-left"><a href="{{< ref "docs/dev/table/types" >}}">Flink SQL type</a></th>
</tr>
</thead>
Expand All @@ -684,6 +692,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>TINYINT</code></td>
<td><code>TINYINT</code></td>
<td></td>
<td></td>
<td><code>TINYINT</code></td>
</tr>
<tr>
Expand All @@ -706,6 +715,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>SMALLINT</code><br>
<code>TINYINT UNSIGNED</code></td>
<td></td>
<td>SMALLINT</td>
<td><code>SMALLINT</code></td>
</tr>
<tr>
Expand All @@ -728,6 +738,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>MEDIUMINT</code><br>
<code>SMALLINT UNSIGNED</code></td>
<td></td>
<td>INT</td>
<td><code>INT</code></td>
</tr>
<tr>
Expand All @@ -748,6 +759,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>BIGINT</code><br>
<code>INT UNSIGNED</code></td>
<td></td>
<td>BIGINT</td>
<td><code>BIGINT</code></td>
</tr>
<tr>
Expand All @@ -760,6 +772,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td></td>
<td><code>BIGINT UNSIGNED</code></td>
<td></td>
<td></td>
<td><code>DECIMAL(20, 0)</code></td>
</tr>
<tr>
Expand All @@ -778,6 +791,9 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>FLOAT</code></td>
<td>
<code>BINARY_FLOAT</code></td>
<td>
<code>REAL</code><br>
<code>FLOAT</code></td>
<td><code>FLOAT</code></td>
</tr>
<tr>
Expand All @@ -796,6 +812,9 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>DOUBLE</code></td>
<td><code>DOUBLE</code></td>
<td><code>BINARY_DOUBLE</code></td>
<td>
<code>DOUBLE</code><br>
<code>DOUBLE PRECISION</code></td>
<td><code>DOUBLE</code></td>
</tr>
<tr>
Expand Down Expand Up @@ -824,6 +843,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td>
<code>FLOAT(s)</code><br>
<code>NUMBER(p, s)</code></td>
<td><code>NUMERIC(p, s)</code></td>
<td><code>DECIMAL(p, s)</code></td>
</tr>
<tr>
Expand All @@ -841,6 +861,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>TINYINT(1)</code></td>
<td></td>
<td><code>BOOLEAN</code></td>
<td><code>BOOLEAN</code></td>
</tr>
<tr>
<td><code>DATE</code></td>
Expand All @@ -853,6 +874,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>DATE</code></td>
<td><code>DATE</code></td>
<td><code>DATE</code></td>
<td><code>DATE</code></td>
</tr>
<tr>
<td><code>TIME [(p)]</code></td>
Expand All @@ -864,6 +886,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>TIME_WITHOUT_TIME_ZONE</code></td>
<td><code>TIME [(p)]</code></td>
<td><code>DATE</code></td>
<td><code>DATE</code></td>
<td><code>TIME [(p)] [WITHOUT TIMEZONE]</code></td>
</tr>
<tr>
Expand All @@ -880,6 +903,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td><code>DATETIME [(p)]</code></td>
<td><code>TIMESTAMP [(p)] [WITHOUT TIMEZONE]</code></td>
<td><code>TIMESTAMP [(p)] [WITHOUT TIMEZONE]</code></td>
<td><code>TIMESTAMP [(p)] [WITHOUT TIMEZONE]</code></td>
</tr>
<tr>
<td>
Expand Down Expand Up @@ -927,6 +951,14 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>NCHAR(n)</code><br>
<code>VARCHAR2(n)</code><br>
<code>CLOB</code></td>
<td>
<code>CHAR(n)</code><br>
<code>CHARACTER(n)</code><br>
<code>VARCHAR(n)</code><br>
<code>CHARACTER VARYING(n)</code><br>
<code>TEXT</code>
<code>STRING</code>
</td>
<td><code>STRING</code></td>
</tr>
<tr>
Expand All @@ -951,7 +983,12 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<code>BLOB</code></td>
<td>
<code>RAW(s)</code><br>
<code>BLOB</code></td>
<code>BLOB</code>
</td>
<td>
<code>BINARY(n)</code><br>
<code>VARBINARY(n)</code><br>
</td>
<td><code>BYTES</code></td>
</tr>
<tr>
Expand All @@ -965,6 +1002,7 @@ Flink supports connect to several databases which uses dialect like MySQL, Oracl
<td></td>
<td></td>
<td><code>ARRAY</code></td>
<td><code>ARRAY</code></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.connector.jdbc.databases.snowflake.dialect;

import org.apache.flink.annotation.Internal;
import org.apache.flink.connector.jdbc.converter.JdbcRowConverter;
import org.apache.flink.connector.jdbc.dialect.AbstractPostgresCompatibleDialect;
import org.apache.flink.table.types.logical.RowType;

import java.util.Optional;

/** JDBC dialect for Snowflake. */
@Internal
public class SnowflakeDialect extends AbstractPostgresCompatibleDialect {
private static final long serialVersionUID = 1L;

@Override
public JdbcRowConverter getRowConverter(RowType rowType) {
return new SnowflakeRowConverter(rowType);
}

@Override
public Optional<String> defaultDriverName() {
return Optional.of("net.snowflake.client.jdbc.SnowflakeDriver");
}

@Override
public String dialectName() {
return "Snowflake";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.connector.jdbc.databases.snowflake.dialect;

import org.apache.flink.annotation.Internal;
import org.apache.flink.connector.jdbc.dialect.JdbcDialect;
import org.apache.flink.connector.jdbc.dialect.JdbcDialectFactory;

/** Factory for {@link SnowflakeDialect}. */
@Internal
public class SnowflakeDialectFactory implements JdbcDialectFactory {
@Override
public boolean acceptsURL(String url) {
return url.startsWith("jdbc:snowflake:");
}

@Override
public JdbcDialect create() {
return new SnowflakeDialect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.connector.jdbc.databases.snowflake.dialect;

import org.apache.flink.annotation.Internal;
import org.apache.flink.connector.jdbc.converter.AbstractPostgresCompatibleRowConverter;
import org.apache.flink.table.types.logical.RowType;

import org.postgresql.jdbc.PgArray;

/**
* Runtime converter that responsible to convert between JDBC object and Flink internal object for
* Snowflake.
*/
@Internal
public class SnowflakeRowConverter extends AbstractPostgresCompatibleRowConverter<PgArray> {

private static final long serialVersionUID = 1L;

@Override
public String converterName() {
return "Snowflake";
}

// https://docs.snowflake.com/en/sql-reference/intro-summary-data-types
public SnowflakeRowConverter(RowType rowType) {
super(rowType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ org.apache.flink.connector.jdbc.databases.sqlserver.dialect.SqlServerDialectFact
org.apache.flink.connector.jdbc.databases.cratedb.dialect.CrateDBDialectFactory
org.apache.flink.connector.jdbc.databases.db2.dialect.Db2DialectFactory
org.apache.flink.connector.jdbc.databases.trino.dialect.TrinoDialectFactory
org.apache.flink.connector.jdbc.databases.snowflake.dialect.SnowflakeDialectFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.flink.connector.jdbc.databases.snowflake.dialect;

import org.apache.flink.connector.jdbc.dialect.JdbcDialectTypeTest;

import java.util.Arrays;
import java.util.List;

/** Tests for all DataTypes and Dialects of JDBC Snowflake connector. */
public class SnowflakeDialectTypeTest extends JdbcDialectTypeTest {
@Override
protected String testDialect() {
return "snowflake";
}

@Override
protected List<JdbcDialectTypeTest.TestItem> testData() {
return Arrays.asList(
createTestItem("BOOLEAN"),

createTestItem("TINYINT"),
createTestItem("SMALLINT"),
createTestItem("BIGINT"),
createTestItem("INT"),
createTestItem("INTEGER"),

createTestItem("DECIMAL"),
createTestItem("NUMERIC"),

createTestItem("DOUBLE"),
createTestItem("FLOAT"),

createTestItem("DECIMAL(10, 4)"),
createTestItem("DECIMAL(38, 18)"),
createTestItem("VARCHAR"),
createTestItem("CHAR"),
createTestItem("VARBINARY"),
createTestItem("DATE"),
createTestItem("TIME"),
createTestItem("TIMESTAMP(3)"),
createTestItem("TIMESTAMP WITHOUT TIME ZONE"),
createTestItem("TIMESTAMP(1) WITHOUT TIME ZONE"),
// Not valid data
createTestItem("TIMESTAMP_LTZ(3)", "Unsupported type:TIMESTAMP_LTZ(3)"));
}
}