Skip to content

Commit 2e45270

Browse files
authored
Support BigQuery dialect in Beam SQL (#35472)
* Support BigQuery dialect in Beam SQL * fixed the tests * switched to setCalciteConnectionProperties
1 parent 1d3b6b9 commit 2e45270

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/BeamSqlPipelineOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.beam.sdk.extensions.sql.impl;
1919

20+
import java.util.Map;
2021
import org.apache.beam.sdk.options.Default;
2122
import org.apache.beam.sdk.options.Description;
2223
import org.apache.beam.sdk.options.PipelineOptions;
@@ -30,6 +31,12 @@ public interface BeamSqlPipelineOptions extends PipelineOptions {
3031

3132
void setPlannerName(String className);
3233

34+
@Description(
35+
"The properties of calcite connection. For example <('fun','bigquery'), ('lex','big_query')>")
36+
Map<String, String> getCalciteConnectionProperties();
37+
38+
void setCalciteConnectionProperties(Map<String, String> properties);
39+
3340
@Description(
3441
"Default timezone for ZetaSQL analyzer; has no effect on CalciteSQL. Allow tz database "
3542
+ "timezone names in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones")

sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/JdbcDriver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.sql.SQLException;
2626
import java.util.ArrayList;
2727
import java.util.List;
28+
import java.util.Map;
2829
import java.util.Properties;
2930
import java.util.function.Consumer;
3031
import org.apache.beam.sdk.extensions.sql.SqlTransform;
@@ -165,6 +166,13 @@ private static JdbcConnection getConnection(PipelineOptions options) {
165166
Properties properties = new Properties();
166167
properties.setProperty(
167168
SCHEMA_FACTORY.camelName(), BeamCalciteSchemaFactory.Empty.class.getName());
169+
BeamSqlPipelineOptions sqlOptions = options.as(BeamSqlPipelineOptions.class);
170+
if (sqlOptions != null) {
171+
Map<String, String> calciteConnectionProperties = sqlOptions.getCalciteConnectionProperties();
172+
if (calciteConnectionProperties != null) {
173+
properties.putAll(calciteConnectionProperties);
174+
}
175+
}
168176
JdbcConnection connection;
169177
try {
170178
connection = (JdbcConnection) INSTANCE.connect(CONNECT_STRING_PREFIX, properties);

sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/JdbcDriverTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ private Row row(Schema schema, Object... values) {
472472
return Row.withSchema(schema).addValues(values).build();
473473
}
474474

475+
@Test
476+
public void testCalciteConnectionProperties_BigQuery() throws Exception {
477+
BeamSqlPipelineOptions options =
478+
PipelineOptionsFactory.create().as(BeamSqlPipelineOptions.class);
479+
options.setCalciteConnectionProperties(ImmutableMap.of("fun", "BIGQUERY", "lex", "BIG_QUERY"));
480+
CalciteConnection connection = JdbcDriver.connect(BOUNDED_TABLE, options);
481+
assertEquals(
482+
"BIGQUERY", connection.getProperties().getProperty("fun", "CALCITE").toUpperCase());
483+
assertEquals(
484+
"BIG_QUERY", connection.getProperties().getProperty("lex", "CALCITE").toUpperCase());
485+
}
486+
475487
@Test
476488
public void testInternalConnect_setDirectRunner() throws Exception {
477489
CalciteConnection connection =
@@ -509,4 +521,53 @@ public void testInternalConnect_driverManagerDifferentProtocol() throws Exceptio
509521

510522
DriverManager.getConnection("jdbc:baaaaaad");
511523
}
524+
525+
@Test
526+
public void testBigQueryDialect() throws Exception {
527+
ReadOnlyTableProvider tableProvider =
528+
new ReadOnlyTableProvider(
529+
"test",
530+
ImmutableMap.of(
531+
"test",
532+
TestUnboundedTable.of(
533+
Schema.FieldType.INT32, "order_id",
534+
Schema.FieldType.INT32, "site_id",
535+
Schema.FieldType.INT32, "price",
536+
Schema.FieldType.STRING, "label",
537+
Schema.FieldType.STRING, "date",
538+
Schema.FieldType.DATETIME, "order_time")
539+
.timestampColumnIndex(5)
540+
.addRows(
541+
Duration.ZERO,
542+
1,
543+
1,
544+
1,
545+
"test-1",
546+
"2025-06-30",
547+
FIRST_DATE,
548+
1,
549+
2,
550+
6,
551+
"test-2",
552+
"2025-06-30",
553+
FIRST_DATE)));
554+
BeamSqlPipelineOptions options =
555+
PipelineOptionsFactory.create().as(BeamSqlPipelineOptions.class);
556+
options.setCalciteConnectionProperties(ImmutableMap.of("fun", "bigquery"));
557+
558+
CalciteConnection connection = JdbcDriver.connect(tableProvider, options);
559+
assertEquals("bigquery", connection.getProperties().getProperty("fun", "CALCITE"));
560+
Statement statement = connection.createStatement();
561+
562+
ResultSet resultSet1 =
563+
statement.executeQuery(
564+
"SELECT SUBSTRING(`label`,2), SUBSTR(`label`, 2,4) FROM test LIMIT 1");
565+
assertTrue(resultSet1.next());
566+
assertFalse(resultSet1.next());
567+
568+
ResultSet resultSet2 = statement.executeQuery("SELECT * FROM test LIMIT 2");
569+
assertTrue(resultSet2.next());
570+
assertTrue(resultSet2.next());
571+
assertFalse(resultSet2.next());
572+
}
512573
}

0 commit comments

Comments
 (0)