-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add Spanner connector #16724
base: master
Are you sure you want to change the base?
Add Spanner connector #16724
Changes from 1 commit
a34eb43
5deb3f2
8b1045e
14cf842
dbe61fc
c8e5f5f
6555aa8
7097ff0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…y keys,not null fields, timestamp field options etc.
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.trino.plugin.spanner; | ||
|
||
import com.google.cloud.spanner.SpannerApiFutures; | ||
import com.google.spanner.v1.SpannerGrpc; | ||
import org.joda.time.DateTime; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
public class Test | ||
{ | ||
public static void main(String[] args) | ||
throws SQLException | ||
{ | ||
Connection connection = DriverManager.getConnection("jdbc:cloudspanner://0.0.0.0:9010/projects/spanner-project/instances/spanner-instance/databases/spanner-database;autoConfigEmulator=true"); | ||
Statement statement = connection.createStatement(); | ||
statement.execute("drop table t1"); | ||
//1679963300421000 | ||
//1591142320347 | ||
//2020-06-02T23:58:40.347847393Z | ||
Instant parse = Instant.parse("2020-06-02T23:58:40.347847393Z"); | ||
//2023-03-28T00:42:31.756+00:00 | ||
System.out.println(parse.toEpochMilli()); | ||
System.out.println(Instant.ofEpochMilli(1679963300421L)); | ||
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(1679963300421000L, 0, ZoneOffset.UTC); | ||
System.out.println(localDateTime); | ||
statement.execute("create table t1 (id int64,ts TIMESTAMP NOT NULL OPTIONS(allow_commit_timestamp=true))PRIMARY KEY (id)"); | ||
statement.execute("select * from "); | ||
/*PreparedStatement preparedStatement = connection.prepareStatement("insert into t1 values(?,?)"); | ||
preparedStatement.setInt(1,1); | ||
preparedStatement.setTimestamp(1,1);*/ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
version: '3' | ||
services: | ||
spanner: | ||
image: gcr.io/cloud-spanner-emulator/emulator:latest | ||
ports: | ||
- "9010:9010" | ||
- "9020:9020" | ||
|
||
gcloud-spanner-init: | ||
image: gcr.io/google.com/cloudsdktool/cloud-sdk:latest | ||
environment: | ||
PROJECT_ID: "spanner-project" | ||
SPANNER_EMULATOR_URL: "http://localhost:9020/" | ||
INSTANCE_NAME: "spanner-instance" | ||
DATABASE_NAME: "spanner-database" | ||
command: > | ||
bash -c 'gcloud config configurations create emulator && | ||
gcloud config set auth/disable_credentials true && | ||
gcloud config set project $${PROJECT_ID} && | ||
gcloud config set api_endpoint_overrides/spanner $${SPANNER_EMULATOR_URL} && | ||
gcloud config set auth/disable_credentials true && | ||
gcloud spanner instances create $${INSTANCE_NAME} --config=emulator-config --description=Emulator --nodes=1 | ||
gcloud spanner databases create $${DATABASE_NAME} --instance=$${INSTANCE_NAME}' | ||
spanner-cli: | ||
image: sjdaws/spanner-cli:latest | ||
environment: | ||
SPANNER_EMULATOR_HOST: "spanner:9010" | ||
depends_on: | ||
- "gcloud-spanner-init" | ||
command: ['sh', '-c', 'echo spanner client.... && tail -f /dev/null'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import io.trino.testing.MaterializedResult; | ||
import io.trino.testing.QueryRunner; | ||
import io.trino.tpch.TpchTable; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
@@ -37,7 +38,8 @@ | |
public final class SpannerSqlQueryRunner | ||
{ | ||
private static final String TPCH_SCHEMA = "tpch"; | ||
private static final String JDBC_URL = ""; | ||
//url for emulated spanner host | ||
private static final String JDBC_URL = "jdbc:cloudspanner://0.0.0.0:9010/projects/spanner-project/instances/spanner-instance/databases/spanner-database;autoConfigEmulator=true"; | ||
private static final String USER = ""; | ||
private static final String PASSWORD = ""; | ||
private static final String SCHEMA = ""; | ||
|
@@ -127,26 +129,31 @@ public static DistributedQueryRunner createSpannerSqlQueryRunner() | |
public static void main(String[] args) | ||
throws Exception | ||
{ | ||
DistributedQueryRunner queryRunner = createSpannerSqlQueryRunner( | ||
ImmutableMap.of("http-server.http.port", "8080"), | ||
ImmutableMap.of(), | ||
ImmutableMap.of(), | ||
TpchTable.getTables() | ||
, xr -> {}); | ||
DistributedQueryRunner queryRunner = getQueryRunner(); | ||
|
||
queryRunner.installPlugin(new JmxPlugin()); | ||
queryRunner.createCatalog("jmx", "jmx"); | ||
MaterializedResult schemas = queryRunner.execute("SHOW SCHEMAS FROM spanner"); | ||
System.out.println(schemas); | ||
MaterializedResult execute = queryRunner.execute("SHOW TABLES FROM spanner.public"); | ||
MaterializedResult execute = queryRunner.execute("SHOW TABLES FROM spanner.default"); | ||
System.out.println(execute); | ||
MaterializedResult create = queryRunner.execute("create table spanner.public.dept" + | ||
MaterializedResult emp = queryRunner.execute("create table if not exists spanner.default.emp" + | ||
"( id int,name varchar" + | ||
")WITH (primary_key = 'id'," + | ||
") WITH(primary_keys = ARRAY['id'])"); | ||
System.out.println(emp); | ||
MaterializedResult create = queryRunner.execute("create table if not exists spanner.default.dept" + | ||
"( id int,name varchar" + | ||
")WITH (primary_keys = ARRAY['id']," + | ||
"interleave_in_parent='emp'," + | ||
"on_delete_cascade=true)"); | ||
"on_delete_cascade=true" + | ||
")"); | ||
System.out.println(create); | ||
MaterializedResult drop = queryRunner.execute("DROP TABLE spanner.public.dept"); | ||
MaterializedResult insert = queryRunner.execute("insert into spanner.default.emp values(1,'Tom')"); | ||
System.out.println(insert); | ||
MaterializedResult count = queryRunner.execute("select count(*) from spanner.default.emp"); | ||
System.out.println(count); | ||
|
||
MaterializedResult drop = queryRunner.execute("DROP TABLE spanner.default.dept"); | ||
System.out.println(drop); | ||
|
||
System.out.println("DONE"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
|
@@ -155,4 +162,18 @@ public static void main(String[] args) | |
log.info("======== SERVER STARTED ========"); | ||
log.info("\n====\n%s\n====", queryRunner.getCoordinator().getBaseUrl()); | ||
} | ||
|
||
static DistributedQueryRunner getQueryRunner() | ||
throws Exception | ||
{ | ||
DistributedQueryRunner queryRunner = createSpannerSqlQueryRunner( | ||
ImmutableMap.of("http-server.http.port", "8080"), | ||
ImmutableMap.of(), | ||
ImmutableMap.of(), | ||
TpchTable.getTables() | ||
, xr -> {}); | ||
return queryRunner; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import io.trino.testing.DistributedQueryRunner; | ||
import io.trino.testing.MaterializedResult; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestSpannerDataTypes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
{ | ||
@Test | ||
public void testDataTypes() | ||
throws Exception | ||
{ | ||
DistributedQueryRunner queryRunner = SpannerSqlQueryRunner.getQueryRunner(); | ||
queryRunner.execute("DROP TABLE IF EXISTS spanner.default.dTest"); | ||
queryRunner.execute("create table spanner.default.dTest" + | ||
"( id int,name varchar,is_active boolean)" + | ||
"WITH (primary_keys = ARRAY['id']," + | ||
"not_null_fields=ARRAY['name','is_active'])"); | ||
queryRunner.execute("insert into spanner.default.dtest values(1,'Tom',true)"); | ||
MaterializedResult execute = queryRunner.execute("select \"id\" from spanner.default.dtest"); | ||
System.out.println(execute); | ||
} | ||
@Test | ||
public void testTimestamp() | ||
throws Exception | ||
{ | ||
DistributedQueryRunner queryRunner = SpannerSqlQueryRunner.getQueryRunner(); | ||
queryRunner.execute("DROP TABLE IF EXISTS spanner.default.dTest"); | ||
queryRunner.execute("create table spanner.default.dTest" + | ||
"( id int,name varchar,is_active boolean,ts timestamp," + | ||
"LastContactDate DATE,PopularityScore DOUBLE)" + | ||
"WITH (primary_keys = ARRAY['id']," + | ||
"not_null_fields=ARRAY['name','is_active','ts'])"); | ||
|
||
queryRunner.execute("insert into spanner.default.dtest values(1,'Tom',true," + | ||
"CURRENT_TIMESTAMP,CURRENT_DATE,1.111)"); | ||
queryRunner.execute("insert into spanner.default.dtest values(2,'Tom cat',true," + | ||
"NULL,CURRENT_DATE,1.111)"); | ||
|
||
MaterializedResult execute = queryRunner.execute("select * from spanner.default.dtest"); | ||
System.out.println(execute); | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please cover all types in this test. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove.