Skip to content

Commit

Permalink
[BEAM-7832] Add ZetaSQL as a dialect in BeamSQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
amaliujia committed Aug 12, 2019
1 parent 6c65527 commit e2fd047
Show file tree
Hide file tree
Showing 54 changed files with 9,922 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .test-infra/jenkins/job_PreCommit_BeamSQL_ZetaSQL.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/

import PrecommitJobBuilder

PrecommitJobBuilder builder = new PrecommitJobBuilder(
scope: this,
nameBase: 'JavaBeamZetaSQL',
gradleTask: ':javaPreCommitBeamZetaSQL',
gradleSwitches: ['-PdisableSpotlessCheck=true'], // spotless checked in separate pre-commit
triggerPathPatterns: [
'^sdks/java/extensions/sql/.*$',
]
)
builder.build {
publishers {
archiveJunit('**/build/test-results/**/*.xml')
}
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ task javaPreCommit() {
dependsOn ":runners:direct-java:needsRunnerTests"
}

task javaPreCommitBeamZetaSQL() {
dependsOn ":sdks:java:extensions:sql:runZetaSQLTest"
}

task javaPreCommitPortabilityApi() {
dependsOn ":runners:google-cloud-dataflow-java:worker:build"
dependsOn ":runners:google-cloud-dataflow-java:examples:verifyPortabilityApi"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@
<suppress id="ForbidNonVendoredGuava" files=".*kinesis.*KinesisIO\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*kinesis.*KinesisProducerMock\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*bigtable.*VendoredListenableFutureAdapter\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*bigtable.*VendoredListenableFutureAdapter\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*bigtable.*BigtableServiceImplTest\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*sql.*BeamValuesRel\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*sql.*BeamEnumerableConverterTest\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*zetasql.*TableScanConverter\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*zetasql.*ExpressionConverter\.java" />
<suppress id="ForbidNonVendoredGuava" files=".*zetasql.*ZetaSQLPlannerImpl\.java" />

<!-- Flink -->
<!-- Checkstyle does not correctly detect package files across multiple source directories. -->
Expand Down
33 changes: 33 additions & 0 deletions sdks/java/extensions/sql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ applyJavaNature(
include(dependency("org.apache.calcite:.*"))
include(dependency("org.apache.calcite.avatica:.*"))
include(dependency("org.codehaus.janino:.*"))
include(dependency("com.google.api.grpc:.*"))
include(dependency("com.google.zetasql:.*"))
}
// guava uses the com.google.common and com.google.thirdparty package namespaces
relocate("com.google.common", project.getJavaRelocatedPath("com.google.common")) {
Expand All @@ -44,9 +46,17 @@ applyJavaNature(
exclude "com.google.common.testing.**"
exclude "com.google.common.util.concurrent.testing.**"
}
relocate "com.google.api", getJavaRelocatedPath("com.google.api")
relocate "com.google.cloud", getJavaRelocatedPath("com.google.cloud")
relocate "com.google.logging", getJavaRelocatedPath("com.google.logging")
relocate "com.google.longrunning", getJavaRelocatedPath("com.google.longrunning")
relocate "com.google.rpc", getJavaRelocatedPath("com.google.rpc")
relocate "com.google.type", getJavaRelocatedPath("com.google.type")

relocate "com.google.thirdparty", project.getJavaRelocatedPath("com.google.thirdparty")

relocate "com.google.protobuf", getJavaRelocatedPath("com.google.protobuf")
relocate "com.google.zetasql", getJavaRelocatedPath("com.google.zetasql")
relocate "org.apache.calcite", getJavaRelocatedPath("org.apache.calcite")

// Looking up the compiler factory in Calcite depends on having a properties
Expand Down Expand Up @@ -82,6 +92,10 @@ dependencies {
compile "org.apache.calcite:calcite-core:$calcite_version"
compile "org.apache.calcite:calcite-linq4j:$calcite_version"
compile "org.apache.calcite.avatica:avatica-core:$avatica_version"
compile "com.google.api.grpc:proto-google-common-protos:1.12.0" // Interfaces with ZetaSQL use this
compile "com.google.zetasql:zetasql-jni-channel:2019.07.1"
compile "com.google.zetasql:zetasql-client:2019.07.1"
compile "com.google.zetasql:zetasql-types:2019.07.1"
shadow project(path: ":sdks:java:core", configuration: "shadow")
shadow project(":sdks:java:extensions:join-library")
shadow library.java.slf4j_api
Expand All @@ -91,6 +105,8 @@ dependencies {
shadow library.java.jackson_databind
shadow library.java.jackson_dataformat_yaml
shadow library.java.joda_time
shadow library.java.protobuf_java
shadow library.java.protobuf_java_util
shadow "com.alibaba:fastjson:1.2.49"
shadow "com.jayway.jsonpath:json-path:2.4.0"
shadow project(path: ":runners:direct-java", configuration: "shadow")
Expand All @@ -110,6 +126,10 @@ dependencies {
permitUnusedDeclared "net.jcip:jcip-annotations:1.0"
permitUnusedDeclared library.java.jackson_dataformat_yaml

permitUnusedDeclared "com.google.api.grpc:proto-google-common-protos:1.12.0"
permitUnusedDeclared "com.google.zetasql:zetasql-jni-channel:2019.07.1"
permitUnusedDeclared "com.google.protobuf:protobuf-java-util:3.6.0"

// Dependencies that are bundled in when we bundle Calcite
permitUsedUndeclared "org.codehaus.janino:janino:3.0.11"
permitUsedUndeclared "org.codehaus.janino:commons-compiler:3.0.11"
Expand Down Expand Up @@ -187,6 +207,19 @@ task runPojoExample(type: JavaExec) {
args = ["--runner=DirectRunner"]
}

task runZetaSQLTest(type: Test) {
// Disable Gradle cache (it should not be used because the IT's won't run).
outputs.upToDateWhen { false }

include '**/*ZetaSQL.class'
classpath = project(":sdks:java:extensions:sql")
.sourceSets
.test
.runtimeClasspath
testClassesDirs = files(project(":sdks:java:extensions:sql").sourceSets.test.output.classesDirs)
useJUnit { }
}

task integrationTest(type: Test) {
group = "Verification"
def gcpProject = project.findProperty('gcpProject') ?: 'apache-beam-testing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public String explain(String sqlString) throws ParseException {
public static class BeamSqlEnvBuilder {
private static final String CALCITE_PLANNER =
"org.apache.beam.sdk.extensions.sql.impl.CalciteQueryPlanner";
private static final String ZETASQL_PLANNER =
"org.apache.beam.sdk.extensions.sql.zetasql.ZetaSQLQueryPlanner";
private String queryPlannerClassName;
private TableProvider defaultTableProvider;
private String currentSchemaName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.beam.sdk.extensions.sql.zetasql;

import java.lang.reflect.Method;
import org.apache.calcite.linq4j.tree.Types;

/** BeamBuiltinMethods. */
public class BeamBuiltinMethods {
public static final Method STARTS_WITH_METHOD =
Types.lookupMethod(StringFunctions.class, "startsWith", String.class, String.class);

public static final Method ENDS_WITH_METHOD =
Types.lookupMethod(StringFunctions.class, "endsWith", String.class, String.class);

public static final Method LIKE_METHOD =
Types.lookupMethod(StringFunctions.class, "like", String.class, String.class);

public static final Method CONCAT_METHOD =
Types.lookupMethod(
StringFunctions.class,
"concat",
String.class,
String.class,
String.class,
String.class,
String.class);

public static final Method REPLACE_METHOD =
Types.lookupMethod(
StringFunctions.class, "replace", String.class, String.class, String.class);

public static final Method TRIM_METHOD =
Types.lookupMethod(StringFunctions.class, "trim", String.class, String.class);

public static final Method LTRIM_METHOD =
Types.lookupMethod(StringFunctions.class, "ltrim", String.class, String.class);

public static final Method RTRIM_METHOD =
Types.lookupMethod(StringFunctions.class, "rtrim", String.class, String.class);

public static final Method SUBSTR_METHOD =
Types.lookupMethod(StringFunctions.class, "substr", String.class, long.class, long.class);

public static final Method REVERSE_METHOD =
Types.lookupMethod(StringFunctions.class, "reverse", String.class);

public static final Method CHAR_LENGTH_METHOD =
Types.lookupMethod(StringFunctions.class, "charLength", String.class);

public static final Method TIMESTAMP_METHOD =
Types.lookupMethod(TimestampFunctions.class, "timestamp", String.class, String.class);

public static final Method DATE_METHOD =
Types.lookupMethod(DateFunctions.class, "date", Integer.class, Integer.class, Integer.class);
}
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.beam.sdk.extensions.sql.zetasql;

import java.io.UnsupportedEncodingException;
import org.joda.time.DateTime;

/** BeamCodegenUtils. */
public class BeamCodegenUtils {
// convert bytes to String in UTF8 encoding.
public static String toStringUTF8(byte[] bytes) {
try {
return new String(bytes, "UTF8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

public static String toStringTimestamp(long timestamp) {
return DateTimeUtils.formatTimestampWithTimeZone(new DateTime(timestamp));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.beam.sdk.extensions.sql.zetasql;

import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

/** DateFunctions. */
public class DateFunctions {
public DateTime date(Integer year, Integer month, Integer day) {
return DateTimeUtils.parseDate(
String.join("-", year.toString(), month.toString(), day.toString()));
}

public DateTime date(DateTime ts) {
return date(ts, "UTC");
}

public DateTime date(DateTime ts, String timezone) {
return ts.withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));
}
}
Loading

0 comments on commit e2fd047

Please sign in to comment.