You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/10/18 23:49:29 UTC

[GitHub] [beam] htyleo commented on a change in pull request #15746: [BEAM-13068] Add a SQL API in Beam Go SDK

htyleo commented on a change in pull request #15746:
URL: https://github.com/apache/beam/pull/15746#discussion_r731391933



##########
File path: sdks/go/pkg/beam/transforms/sql/sql.go
##########
@@ -0,0 +1,121 @@
+// 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 sql contains SQL transform APIs, allowing SQL queries to be used
+// in Beam Go pipelines.
+//
+// NOTE: This is an experimental feature. It currently only works when an
+// expansion service/handler is registered for SQL transform. The APIs are
+// subject to change without backward compatibility guarantees.
+package sql
+
+import (
+	"reflect"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/sql/sqlx"
+)
+
+// Option is the base type of all the SQL transform options.
+type Option func(*Options)
+
+// Options contain all the options for a SQL transform.
+type Options struct {
+	dialect       string
+	expansionAddr string
+	inputs        map[string]beam.PCollection
+	outType       beam.FullType
+	customs       []sqlx.Option
+}
+
+// Add adds a custom option.
+func (o *Options) Add(opt sqlx.Option) {
+	o.customs = append(o.customs, opt)
+}
+
+// Input adds a named PCollection input to the transform.
+func Input(name string, in beam.PCollection) Option {
+	return func(o *Options) {
+		o.inputs[name] = in
+	}
+}
+
+// OutputType specifies the output PCollection type of the transform.
+// It must match the SQL output schema.
+//
+// There is currently no default output type, so users must set this option.
+// In the future, Row, once implemented, may become the default output type.
+func OutputType(t reflect.Type) Option {
+	return func(o *Options) {
+		o.outType = typex.New(t)
+	}
+}
+
+// Dialect specifies the SQL dialect, e.g. use 'zetasql' for ZetaSQL.
+func Dialect(dialect string) Option {
+	return func(o *Options) {
+		o.dialect = dialect
+	}
+}
+
+// ExpansionAddr is the URL of the expansion service to use.
+func ExpansionAddr(addr string) Option {
+	return func(o *Options) {
+		o.expansionAddr = addr
+	}
+}
+
+// Transform creates a SQL-based transform over zero or more PCollections
+// and/or named data sources.
+//
+// PCollection inputs can be added using the sql.Input option. SQL queries can
+// also refer to external tables that can be resolved by the expansion service.
+//
+// The output PCollection type must be specified by the sql.OutputType option.
+//
+// Example:
+//
+//  in := beam.Create(s, 1, 2, 3)
+//  out := sql.Transform(s, "SELECT COUNT(*) FROM t",
+//      sql.Input("t", in),
+//      sql.OutputType(reflect.TypeOf(int64(0))))
+//  // `out` is a PCollection<int64> with a single element 3.
+func Transform(s beam.Scope, query string, opts ...Option) beam.PCollection {
+	options := &Options{
+		inputs: make(map[string]beam.PCollection),
+	}
+	for _, opt := range opts {
+		opt(options)
+	}
+	if options.outType == nil {
+		panic("output type must be specified for sql.Transform")
+	}
+
+	payload := beam.CrossLanguagePayload(&sqlx.ExpansionPayload{
+		Query:   query,
+		Dialect: options.dialect,
+		Options: options.customs,
+	})
+
+	expansionAddr := sqlx.DefaultExpansionAddr
+	if options.expansionAddr != "" {
+		expansionAddr = options.expansionAddr

Review comment:
       That's a great point, thanks! Done.




-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org