darshan-sj commented on code in PR #32008: URL: https://github.com/apache/beam/pull/32008#discussion_r1729065381
########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerQuerySourceDef.java: ########## @@ -0,0 +1,56 @@ +/* + * 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.io.gcp.spanner; + +import static org.apache.beam.sdk.io.gcp.spanner.StructUtils.structTypeToBeamRowSchema; + +import com.google.cloud.spanner.ReadContext; +import com.google.cloud.spanner.ResultSet; +import com.google.cloud.spanner.Statement; +import org.apache.beam.sdk.schemas.Schema; + +class SpannerQuerySourceDef implements SpannerSourceDef { + + private final SpannerConfig config; + private final Statement query; + + static SpannerQuerySourceDef create(SpannerConfig config, Statement query) { + return new SpannerQuerySourceDef(config, query); + } + + private SpannerQuerySourceDef(SpannerConfig config, Statement query) { + this.config = config; + this.query = query; + } + + /** {@inheritDoc} */ + @Override + public Schema getBeamSchema() { + Schema beamSchema; + try (SpannerAccessor spannerAccessor = SpannerAccessor.getOrCreate(config)) { + try (ReadContext readContext = spannerAccessor.getDatabaseClient().singleUse()) { + ResultSet result = readContext.analyzeQuery(query, ReadContext.QueryAnalyzeMode.PLAN); Review Comment: Please test this before merging. Actual execution without mocks. ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/StructUtils.java: ########## @@ -52,6 +67,58 @@ public static Row structToBeamRow(Struct struct, Schema schema) { return Row.withSchema(schema).withFieldValues(structValues).build(); } + public static Schema structTypeToBeamRowSchema(StructType structType, boolean isRead) { + Schema.Builder beamSchema = Schema.builder(); + structType + .getFieldsList() + .forEach( + field -> { + Schema.FieldType fieldType; + try { + fieldType = convertSpannerTypeToBeamFieldType(field.getType()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException( + "Error processing struct to row: " + e.getMessage()); + } + // Treat reads from Spanner as Nullable and leave Null handling to Spanner + if (isRead) { + beamSchema.addNullableField(field.getName(), fieldType); + } else { + beamSchema.addField(field.getName(), fieldType); + } + }); + return beamSchema.build(); + } + + public static Schema.FieldType convertSpannerTypeToBeamFieldType( + com.google.spanner.v1.Type spannerType) { + switch (spannerType.getCode()) { + case BOOL: + return Schema.FieldType.BOOLEAN; + case BYTES: + return Schema.FieldType.BYTES; + case TIMESTAMP: + case DATE: + return Schema.FieldType.DATETIME; + case INT64: + return Schema.FieldType.INT64; + case FLOAT32: + return Schema.FieldType.FLOAT; + case FLOAT64: + return Schema.FieldType.DOUBLE; + case NUMERIC: + return Schema.FieldType.DECIMAL; Review Comment: Is it expected that the datatype of the corresponding values in the rowStruct to be of certain datatype? Like what is the datatype expected for DECIMAL? Does Spanner client library give the data in the expected datatype for all these types? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
