Skip to content

Commit

Permalink
Merge pull request #22 from goccy/support-custom-catalog-and-table
Browse files Browse the repository at this point in the history
Support user defined catalog and table
  • Loading branch information
goccy authored Nov 20, 2022
2 parents 50b559c + c7758f2 commit d1a8ecb
Show file tree
Hide file tree
Showing 14 changed files with 1,707 additions and 8 deletions.
190 changes: 190 additions & 0 deletions catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package zetasql_test

import (
"testing"

"github.com/goccy/go-zetasql"
"github.com/goccy/go-zetasql/resolved_ast"
"github.com/goccy/go-zetasql/types"
)

type myCatalog struct {
simpleCatalog *types.SimpleCatalog
tables []types.Table
functions []*types.Function
}

func (c *myCatalog) FullName() string {
return "catalog"
}

func (c *myCatalog) FindTable(path []string) (types.Table, error) {
tableName := path[len(path)-1]
for _, table := range c.tables {
if table.Name() == tableName {
return table, nil
}
}
return nil, nil
}

func (c *myCatalog) FindModel(path []string) (types.Model, error) {
return nil, nil
}

func (c *myCatalog) FindConnection(path []string) (types.Connection, error) {
return nil, nil
}

func (c *myCatalog) FindFunction(path []string) (*types.Function, error) {
funcName := path[len(path)-1]
for _, fn := range c.functions {
if fn.Name() == funcName {
return fn, nil
}
}
return c.simpleCatalog.FindFunction(path)
}

func (c *myCatalog) FindTableValuedFunction(path []string) (types.TableValuedFunction, error) {
return nil, nil
}

func (c *myCatalog) FindProcedure(path []string) (*types.Procedure, error) {
return nil, nil
}

func (c *myCatalog) FindType(path []string) (types.Type, error) {
return nil, nil
}

func (c *myCatalog) FindConstant(path []string) (types.Constant, error) {
return nil, nil
}

func (c *myCatalog) FindConversion(from, to types.Type) (types.Conversion, error) {
return nil, nil
}

func (c *myCatalog) ExtendedTypeSuperTypes(typ types.Type) (*types.TypeListView, error) {
return nil, nil
}

func (c *myCatalog) SuggestTable(mistypedPath []string) string {
return ""
}

func (c *myCatalog) SuggestModel(mistypedPath []string) string {
return ""
}

func (c *myCatalog) SuggestFunction(mistypedPath []string) string {
return ""
}

func (c *myCatalog) SuggestTableValuedFunction(mistypedPath []string) string {
return ""
}

func (c *myCatalog) SuggestConstant(mistypedPath []string) string {
return ""
}

type myTable struct {
name string
columns []types.Column
}

func (t *myTable) Name() string {
return t.name
}

func (t *myTable) FullName() string {
return t.name
}

func (t *myTable) NumColumns() int {
return len(t.columns)
}

func (t *myTable) Column(idx int) types.Column {
return t.columns[idx]
}

func (t *myTable) PrimaryKey() []int {
return nil
}

func (t *myTable) FindColumnByName(name string) types.Column {
return nil
}

func (t *myTable) IsValueTable() bool {
return false
}

func (t *myTable) SerializationID() int64 {
return 0
}

func (t *myTable) CreateEvaluatorTableIterator(columnIdxs []int) (*types.EvaluatorTableIterator, error) {
return nil, nil
}

func (t *myTable) AnonymizationInfo() *types.AnonymizationInfo {
return nil
}

func (t *myTable) SupportsAnonymization() bool {
return false
}

func (t *myTable) TableTypeName(mode types.ProductMode) string {
return ""
}

var (
_ types.Catalog = &myCatalog{}
_ types.Table = &myTable{}
)

func TestCatalog(t *testing.T) {
langOpt := zetasql.NewLanguageOptions()
langOpt.SetNameResolutionMode(zetasql.NameResolutionDefault)
langOpt.SetProductMode(types.ProductExternal)
langOpt.SetSupportedStatementKinds([]resolved_ast.Kind{resolved_ast.QueryStmt})
opt := zetasql.NewAnalyzerOptions()
opt.SetAllowUndeclaredParameters(true)
opt.SetLanguage(langOpt)

simpleCatalog := types.NewSimpleCatalog("simple")
simpleCatalog.AddZetaSQLBuiltinFunctions(langOpt.BuiltinFunctionOptions())
catalog := &myCatalog{
simpleCatalog: simpleCatalog,
tables: []types.Table{
&myTable{
name: "sample_table",
columns: []types.Column{
types.NewSimpleColumn("sample_table", "col1", types.Int64Type()),
},
},
},
functions: []*types.Function{
types.NewFunction(
[]string{"MY_FUNC"},
"",
types.ScalarMode,
[]*types.FunctionSignature{
types.NewFunctionSignature(
types.NewFunctionArgumentType("", types.StringType()),
nil,
),
},
),
},
}
query := `SELECT * FROM sample_table WHERE MY_FUNC() = 'foo'`
if _, err := zetasql.AnalyzeStatement(query, catalog, opt); err != nil {
t.Fatal(err)
}
}
4 changes: 4 additions & 0 deletions internal/ccall/go-zetasql/bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#define U_ICU_ENTRY_POINT_RENAME(x) GO_EXPORT(x)
#include "go-zetasql/parser/parser/export.inc"
#include "go-zetasql/public/analyzer/export.inc"
#include "go-zetasql/public/catalog/export.inc"
#include "go-zetasql/public/simple_catalog/export.inc"
#include "go-zetasql/public/sql_formatter/export.inc"
#include "go-zetasql/parser/parser/bridge.h"
#include "go-zetasql/public/analyzer/bridge.h"
#include "go-zetasql/public/catalog/bridge.h"
#include "go-zetasql/public/simple_catalog/bridge.h"
#include "go-zetasql/public/sql_formatter/bridge.h"
#include "go-zetasql/parser/parser/bridge_cc.inc"
#include "go-zetasql/public/analyzer/bridge_cc.inc"
#include "go-zetasql/public/catalog/bridge_cc.inc"
#include "go-zetasql/public/simple_catalog/bridge_cc.inc"
#include "go-zetasql/public/sql_formatter/bridge_cc.inc"

Expand All @@ -18,6 +21,7 @@ extern "C" {
#endif /* __cplusplus */
#include "go-zetasql/parser/parser/bridge.inc"
#include "go-zetasql/public/analyzer/bridge.inc"
#include "go-zetasql/public/catalog/bridge.inc"
#include "go-zetasql/public/simple_catalog/bridge.inc"
#include "go-zetasql/public/sql_formatter/bridge.inc"

Expand Down
22 changes: 22 additions & 0 deletions internal/ccall/go-zetasql/bind_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26165,6 +26165,28 @@ func zetasql_ResolvedFunctionCallInfo_DebugString(arg0 unsafe.Pointer, arg1 *uns
C.export_zetasql_ResolvedFunctionCallInfo_DebugString(arg0, arg1)
}

func GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
zetasql_GoCatalog_new(
arg0,
arg1,
)
}

func zetasql_GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
C.export_zetasql_GoCatalog_new(arg0, arg1)
}

func GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
zetasql_GoTable_new(
arg0,
arg1,
)
}

func zetasql_GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
C.export_zetasql_GoTable_new(arg0, arg1)
}

func Type_Kind(arg0 unsafe.Pointer, arg1 *int) {
zetasql_Type_Kind(
arg0,
Expand Down
22 changes: 22 additions & 0 deletions internal/ccall/go-zetasql/bind_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26168,6 +26168,28 @@ func zetasql_ResolvedFunctionCallInfo_DebugString(arg0 unsafe.Pointer, arg1 *uns
C.export_zetasql_ResolvedFunctionCallInfo_DebugString(arg0, arg1)
}

func GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
zetasql_GoCatalog_new(
arg0,
arg1,
)
}

func zetasql_GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
C.export_zetasql_GoCatalog_new(arg0, arg1)
}

func GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
zetasql_GoTable_new(
arg0,
arg1,
)
}

func zetasql_GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
C.export_zetasql_GoTable_new(arg0, arg1)
}

func Type_Kind(arg0 unsafe.Pointer, arg1 *int) {
zetasql_Type_Kind(
arg0,
Expand Down
1 change: 1 addition & 0 deletions internal/ccall/go-zetasql/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
#include <stdint.h>
#include "../go-zetasql/parser/parser/bridge_extern.h"
#include "../go-zetasql/public/analyzer/bridge_extern.h"
#include "../go-zetasql/public/catalog/bridge_extern.h"
#include "../go-zetasql/public/simple_catalog/bridge_extern.h"
#include "../go-zetasql/public/sql_formatter/bridge_extern.h"

Expand Down
Loading

0 comments on commit d1a8ecb

Please sign in to comment.