Skip to content

Commit d1a8ecb

Browse files
authored
Merge pull request #22 from goccy/support-custom-catalog-and-table
Support user defined catalog and table
2 parents 50b559c + c7758f2 commit d1a8ecb

14 files changed

+1707
-8
lines changed

Diff for: catalog_test.go

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package zetasql_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/goccy/go-zetasql"
7+
"github.com/goccy/go-zetasql/resolved_ast"
8+
"github.com/goccy/go-zetasql/types"
9+
)
10+
11+
type myCatalog struct {
12+
simpleCatalog *types.SimpleCatalog
13+
tables []types.Table
14+
functions []*types.Function
15+
}
16+
17+
func (c *myCatalog) FullName() string {
18+
return "catalog"
19+
}
20+
21+
func (c *myCatalog) FindTable(path []string) (types.Table, error) {
22+
tableName := path[len(path)-1]
23+
for _, table := range c.tables {
24+
if table.Name() == tableName {
25+
return table, nil
26+
}
27+
}
28+
return nil, nil
29+
}
30+
31+
func (c *myCatalog) FindModel(path []string) (types.Model, error) {
32+
return nil, nil
33+
}
34+
35+
func (c *myCatalog) FindConnection(path []string) (types.Connection, error) {
36+
return nil, nil
37+
}
38+
39+
func (c *myCatalog) FindFunction(path []string) (*types.Function, error) {
40+
funcName := path[len(path)-1]
41+
for _, fn := range c.functions {
42+
if fn.Name() == funcName {
43+
return fn, nil
44+
}
45+
}
46+
return c.simpleCatalog.FindFunction(path)
47+
}
48+
49+
func (c *myCatalog) FindTableValuedFunction(path []string) (types.TableValuedFunction, error) {
50+
return nil, nil
51+
}
52+
53+
func (c *myCatalog) FindProcedure(path []string) (*types.Procedure, error) {
54+
return nil, nil
55+
}
56+
57+
func (c *myCatalog) FindType(path []string) (types.Type, error) {
58+
return nil, nil
59+
}
60+
61+
func (c *myCatalog) FindConstant(path []string) (types.Constant, error) {
62+
return nil, nil
63+
}
64+
65+
func (c *myCatalog) FindConversion(from, to types.Type) (types.Conversion, error) {
66+
return nil, nil
67+
}
68+
69+
func (c *myCatalog) ExtendedTypeSuperTypes(typ types.Type) (*types.TypeListView, error) {
70+
return nil, nil
71+
}
72+
73+
func (c *myCatalog) SuggestTable(mistypedPath []string) string {
74+
return ""
75+
}
76+
77+
func (c *myCatalog) SuggestModel(mistypedPath []string) string {
78+
return ""
79+
}
80+
81+
func (c *myCatalog) SuggestFunction(mistypedPath []string) string {
82+
return ""
83+
}
84+
85+
func (c *myCatalog) SuggestTableValuedFunction(mistypedPath []string) string {
86+
return ""
87+
}
88+
89+
func (c *myCatalog) SuggestConstant(mistypedPath []string) string {
90+
return ""
91+
}
92+
93+
type myTable struct {
94+
name string
95+
columns []types.Column
96+
}
97+
98+
func (t *myTable) Name() string {
99+
return t.name
100+
}
101+
102+
func (t *myTable) FullName() string {
103+
return t.name
104+
}
105+
106+
func (t *myTable) NumColumns() int {
107+
return len(t.columns)
108+
}
109+
110+
func (t *myTable) Column(idx int) types.Column {
111+
return t.columns[idx]
112+
}
113+
114+
func (t *myTable) PrimaryKey() []int {
115+
return nil
116+
}
117+
118+
func (t *myTable) FindColumnByName(name string) types.Column {
119+
return nil
120+
}
121+
122+
func (t *myTable) IsValueTable() bool {
123+
return false
124+
}
125+
126+
func (t *myTable) SerializationID() int64 {
127+
return 0
128+
}
129+
130+
func (t *myTable) CreateEvaluatorTableIterator(columnIdxs []int) (*types.EvaluatorTableIterator, error) {
131+
return nil, nil
132+
}
133+
134+
func (t *myTable) AnonymizationInfo() *types.AnonymizationInfo {
135+
return nil
136+
}
137+
138+
func (t *myTable) SupportsAnonymization() bool {
139+
return false
140+
}
141+
142+
func (t *myTable) TableTypeName(mode types.ProductMode) string {
143+
return ""
144+
}
145+
146+
var (
147+
_ types.Catalog = &myCatalog{}
148+
_ types.Table = &myTable{}
149+
)
150+
151+
func TestCatalog(t *testing.T) {
152+
langOpt := zetasql.NewLanguageOptions()
153+
langOpt.SetNameResolutionMode(zetasql.NameResolutionDefault)
154+
langOpt.SetProductMode(types.ProductExternal)
155+
langOpt.SetSupportedStatementKinds([]resolved_ast.Kind{resolved_ast.QueryStmt})
156+
opt := zetasql.NewAnalyzerOptions()
157+
opt.SetAllowUndeclaredParameters(true)
158+
opt.SetLanguage(langOpt)
159+
160+
simpleCatalog := types.NewSimpleCatalog("simple")
161+
simpleCatalog.AddZetaSQLBuiltinFunctions(langOpt.BuiltinFunctionOptions())
162+
catalog := &myCatalog{
163+
simpleCatalog: simpleCatalog,
164+
tables: []types.Table{
165+
&myTable{
166+
name: "sample_table",
167+
columns: []types.Column{
168+
types.NewSimpleColumn("sample_table", "col1", types.Int64Type()),
169+
},
170+
},
171+
},
172+
functions: []*types.Function{
173+
types.NewFunction(
174+
[]string{"MY_FUNC"},
175+
"",
176+
types.ScalarMode,
177+
[]*types.FunctionSignature{
178+
types.NewFunctionSignature(
179+
types.NewFunctionArgumentType("", types.StringType()),
180+
nil,
181+
),
182+
},
183+
),
184+
},
185+
}
186+
query := `SELECT * FROM sample_table WHERE MY_FUNC() = 'foo'`
187+
if _, err := zetasql.AnalyzeStatement(query, catalog, opt); err != nil {
188+
t.Fatal(err)
189+
}
190+
}

Diff for: internal/ccall/go-zetasql/bind.cc

+4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
#define U_ICU_ENTRY_POINT_RENAME(x) GO_EXPORT(x)
33
#include "go-zetasql/parser/parser/export.inc"
44
#include "go-zetasql/public/analyzer/export.inc"
5+
#include "go-zetasql/public/catalog/export.inc"
56
#include "go-zetasql/public/simple_catalog/export.inc"
67
#include "go-zetasql/public/sql_formatter/export.inc"
78
#include "go-zetasql/parser/parser/bridge.h"
89
#include "go-zetasql/public/analyzer/bridge.h"
10+
#include "go-zetasql/public/catalog/bridge.h"
911
#include "go-zetasql/public/simple_catalog/bridge.h"
1012
#include "go-zetasql/public/sql_formatter/bridge.h"
1113
#include "go-zetasql/parser/parser/bridge_cc.inc"
1214
#include "go-zetasql/public/analyzer/bridge_cc.inc"
15+
#include "go-zetasql/public/catalog/bridge_cc.inc"
1316
#include "go-zetasql/public/simple_catalog/bridge_cc.inc"
1417
#include "go-zetasql/public/sql_formatter/bridge_cc.inc"
1518

@@ -18,6 +21,7 @@ extern "C" {
1821
#endif /* __cplusplus */
1922
#include "go-zetasql/parser/parser/bridge.inc"
2023
#include "go-zetasql/public/analyzer/bridge.inc"
24+
#include "go-zetasql/public/catalog/bridge.inc"
2125
#include "go-zetasql/public/simple_catalog/bridge.inc"
2226
#include "go-zetasql/public/sql_formatter/bridge.inc"
2327

Diff for: internal/ccall/go-zetasql/bind_darwin.go

+22
Original file line numberDiff line numberDiff line change
@@ -26165,6 +26165,28 @@ func zetasql_ResolvedFunctionCallInfo_DebugString(arg0 unsafe.Pointer, arg1 *uns
2616526165
C.export_zetasql_ResolvedFunctionCallInfo_DebugString(arg0, arg1)
2616626166
}
2616726167

26168+
func GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26169+
zetasql_GoCatalog_new(
26170+
arg0,
26171+
arg1,
26172+
)
26173+
}
26174+
26175+
func zetasql_GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26176+
C.export_zetasql_GoCatalog_new(arg0, arg1)
26177+
}
26178+
26179+
func GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26180+
zetasql_GoTable_new(
26181+
arg0,
26182+
arg1,
26183+
)
26184+
}
26185+
26186+
func zetasql_GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26187+
C.export_zetasql_GoTable_new(arg0, arg1)
26188+
}
26189+
2616826190
func Type_Kind(arg0 unsafe.Pointer, arg1 *int) {
2616926191
zetasql_Type_Kind(
2617026192
arg0,

Diff for: internal/ccall/go-zetasql/bind_linux.go

+22
Original file line numberDiff line numberDiff line change
@@ -26168,6 +26168,28 @@ func zetasql_ResolvedFunctionCallInfo_DebugString(arg0 unsafe.Pointer, arg1 *uns
2616826168
C.export_zetasql_ResolvedFunctionCallInfo_DebugString(arg0, arg1)
2616926169
}
2617026170

26171+
func GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26172+
zetasql_GoCatalog_new(
26173+
arg0,
26174+
arg1,
26175+
)
26176+
}
26177+
26178+
func zetasql_GoCatalog_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26179+
C.export_zetasql_GoCatalog_new(arg0, arg1)
26180+
}
26181+
26182+
func GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26183+
zetasql_GoTable_new(
26184+
arg0,
26185+
arg1,
26186+
)
26187+
}
26188+
26189+
func zetasql_GoTable_new(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) {
26190+
C.export_zetasql_GoTable_new(arg0, arg1)
26191+
}
26192+
2617126193
func Type_Kind(arg0 unsafe.Pointer, arg1 *int) {
2617226194
zetasql_Type_Kind(
2617326195
arg0,

Diff for: internal/ccall/go-zetasql/bridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
#include <stdint.h>
99
#include "../go-zetasql/parser/parser/bridge_extern.h"
1010
#include "../go-zetasql/public/analyzer/bridge_extern.h"
11+
#include "../go-zetasql/public/catalog/bridge_extern.h"
1112
#include "../go-zetasql/public/simple_catalog/bridge_extern.h"
1213
#include "../go-zetasql/public/sql_formatter/bridge_extern.h"
1314

0 commit comments

Comments
 (0)