forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SQL] Support complex identifiers in DataCatalog
- Loading branch information
Showing
10 changed files
with
538 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ain/java/org/apache/beam/sdk/extensions/sql/meta/provider/datacatalog/ZetaSqlIdUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.meta.provider.datacatalog; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap; | ||
|
||
/** Utils to work with ZetaSQL-compatible IDs. */ | ||
class ZetaSqlIdUtils { | ||
|
||
/** | ||
* Some special characters we explicitly handle. | ||
* | ||
* <p>Everything else is ignored, e.g. tabs, newlines, etc. | ||
*/ | ||
private static final Pattern SPECIAL_CHARS_ESCAPE = | ||
Pattern.compile( | ||
"(?<SpecialChar>[" | ||
+ "\\\\" // slash | ||
+ "`" // backtick | ||
+ "'" // single quote | ||
+ "\"" // double quote | ||
+ "?" // question mark | ||
+ "])"); | ||
|
||
private static final ImmutableMap<String, String> WHITESPACES = | ||
ImmutableMap.of( | ||
"\n", "\\\\n", | ||
"\t", "\\\\t", | ||
"\r", "\\\\r", | ||
"\f", "\\\\f"); | ||
|
||
private static final Pattern SIMPLE_ID = Pattern.compile("[A-Za-z_][A-Za-z_0-9]*"); | ||
|
||
/** | ||
* Joins parts into a single compound ZetaSQL identifier. | ||
* | ||
* <p>Escapes backticks, slashes, double and single quotes, doesn't handle other special | ||
* characters for now. | ||
*/ | ||
static String escapeAndJoin(List<String> parts) { | ||
return parts.stream() | ||
.map(ZetaSqlIdUtils::escapeSpecialChars) | ||
.map(ZetaSqlIdUtils::replaceWhitespaces) | ||
.map(ZetaSqlIdUtils::backtickIfNeeded) | ||
.collect(joining(".")); | ||
} | ||
|
||
private static String escapeSpecialChars(String str) { | ||
return SPECIAL_CHARS_ESCAPE.matcher(str).replaceAll("\\\\${SpecialChar}"); | ||
} | ||
|
||
private static String replaceWhitespaces(String s) { | ||
return WHITESPACES.keySet().stream() | ||
.reduce(s, (str, whitespace) -> str.replaceAll(whitespace, WHITESPACES.get(whitespace))); | ||
} | ||
|
||
private static String backtickIfNeeded(String s) { | ||
return SIMPLE_ID.matcher(s).matches() ? s : ("`" + s + "`"); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...a/org/apache/beam/sdk/extensions/sql/meta/provider/datacatalog/DataCatalogBigQueryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.meta.provider.datacatalog; | ||
|
||
import static org.apache.beam.sdk.schemas.Schema.FieldType.INT64; | ||
import static org.apache.beam.sdk.schemas.Schema.FieldType.STRING; | ||
|
||
import com.google.api.services.bigquery.model.TableFieldSchema; | ||
import com.google.api.services.bigquery.model.TableReference; | ||
import com.google.api.services.bigquery.model.TableRow; | ||
import com.google.api.services.bigquery.model.TableSchema; | ||
import org.apache.beam.sdk.extensions.sql.SqlTransform; | ||
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO; | ||
import org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder; | ||
import org.apache.beam.sdk.io.gcp.bigquery.TestBigQuery; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
import org.apache.beam.sdk.testing.PAssert; | ||
import org.apache.beam.sdk.testing.TestPipeline; | ||
import org.apache.beam.sdk.transforms.Create; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.Row; | ||
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList; | ||
import org.joda.time.Duration; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Integration tests for DataCatalog+BigQuery. */ | ||
@RunWith(JUnit4.class) | ||
public class DataCatalogBigQueryIT { | ||
|
||
private static final Schema ID_NAME_SCHEMA = | ||
Schema.builder().addNullableField("id", INT64).addNullableField("name", STRING).build(); | ||
|
||
@Rule public transient TestPipeline writeToBQPipeline = TestPipeline.create(); | ||
@Rule public transient TestPipeline readPipeline = TestPipeline.create(); | ||
@Rule public transient TestBigQuery bigQuery = TestBigQuery.create(ID_NAME_SCHEMA); | ||
|
||
@Test | ||
public void testReadWrite() throws Exception { | ||
createBQTableWith( | ||
new TableRow().set("id", 1).set("name", "name1"), | ||
new TableRow().set("id", 2).set("name", "name2"), | ||
new TableRow().set("id", 3).set("name", "name3")); | ||
|
||
TableReference bqTable = bigQuery.tableReference(); | ||
String tableId = | ||
String.format( | ||
"bigquery.`table`.`%s`.`%s`.`%s`", | ||
bqTable.getProjectId(), bqTable.getDatasetId(), bqTable.getTableId()); | ||
|
||
PCollection<Row> result = | ||
readPipeline.apply( | ||
"query", | ||
SqlTransform.query("SELECT id, name FROM " + tableId) | ||
.withDefaultTableProvider( | ||
"datacatalog", DataCatalogTableProvider.create(readPipeline.getOptions()))); | ||
|
||
PAssert.that(result).containsInAnyOrder(row(1, "name1"), row(2, "name2"), row(3, "name3")); | ||
readPipeline.run().waitUntilFinish(Duration.standardMinutes(2)); | ||
} | ||
|
||
private Row row(long id, String name) { | ||
return Row.withSchema(ID_NAME_SCHEMA).addValues(id, name).build(); | ||
} | ||
|
||
private void createBQTableWith(TableRow r1, TableRow r2, TableRow r3) { | ||
writeToBQPipeline | ||
.apply(Create.of(r1, r2, r3).withCoder(TableRowJsonCoder.of())) | ||
.apply( | ||
BigQueryIO.writeTableRows() | ||
.to(bigQuery.tableSpec()) | ||
.withSchema( | ||
new TableSchema() | ||
.setFields( | ||
ImmutableList.of( | ||
new TableFieldSchema().setName("id").setType("INTEGER"), | ||
new TableFieldSchema().setName("name").setType("STRING")))) | ||
.withoutValidation()); | ||
writeToBQPipeline.run().waitUntilFinish(Duration.standardMinutes(2)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...java/org/apache/beam/sdk/extensions/sql/meta/provider/datacatalog/ZetaSqlIdUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.meta.provider.datacatalog; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
/** Unit tests for {@link ZetaSqlIdUtils}. */ | ||
public class ZetaSqlIdUtilsTest { | ||
|
||
@Test | ||
public void testHandlesSimpleIds() { | ||
List<String> id = Arrays.asList("aaa", "BbB", "zAzzz00"); | ||
assertEquals("aaa.BbB.zAzzz00", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
|
||
@Test | ||
public void testHandlesMixedIds() { | ||
List<String> id = Arrays.asList("aaa", "Bb---B", "zAzzz00"); | ||
assertEquals("aaa.`Bb---B`.zAzzz00", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
|
||
@Test | ||
public void testHandlesSpecialChars() { | ||
List<String> id = Arrays.asList("a\\a", "b`b", "c'c", "d\"d", "e?e"); | ||
assertEquals("`a\\\\a`.`b\\`b`.`c\\'c`.`d\\\"d`.`e\\?e`", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
|
||
@Test | ||
public void testHandlesSpecialCharsInOnePart() { | ||
List<String> id = Arrays.asList("a\\ab`bc'cd\"de?e"); | ||
assertEquals("`a\\\\ab\\`bc\\'cd\\\"de\\?e`", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
|
||
@Test | ||
public void testHandlesWhiteSpaces() { | ||
List<String> id = Arrays.asList("a\na", "b\tb", "c\rc", "d\fd"); | ||
assertEquals("`a\\na`.`b\\tb`.`c\\rc`.`d\\fd`", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
|
||
@Test | ||
public void testHandlesWhiteSpacesInOnePart() { | ||
List<String> id = Arrays.asList("a\nab\tbc\rcd\fd"); | ||
assertEquals("`a\\nab\\tbc\\rcd\\fd`", ZetaSqlIdUtils.escapeAndJoin(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.