From 48f3118b88c5d62dadc479110e2d69f243fcaec4 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 22 Nov 2022 23:30:06 +0900 Subject: [PATCH] Fix custom catalog and table --- catalog_test.go | 4 +- internal/ccall/go-zetasql/bind_darwin.go | 69 +++++++++++++++++++ internal/ccall/go-zetasql/bind_linux.go | 69 +++++++++++++++++++ internal/ccall/go-zetasql/callback_darwin.go | 15 ++-- internal/ccall/go-zetasql/callback_linux.go | 15 ++-- .../go-zetasql/public/catalog/bridge.inc | 22 ++++++ .../public/simple_catalog/bind_darwin.go | 69 +++++++++++++++++++ .../public/simple_catalog/bind_linux.go | 69 +++++++++++++++++++ .../public/simple_catalog/bridge.inc | 43 ++++++++++++ .../public/simple_catalog/bridge_extern.h | 6 ++ internal/cmd/generator/bridge.yaml | 33 +++++++++ internal/helper/bind.go | 5 ++ types/catalog.go | 45 ++++++++---- types/constant.go | 46 ++++++++++++- types/table.go | 33 ++++++--- 15 files changed, 504 insertions(+), 39 deletions(-) diff --git a/catalog_test.go b/catalog_test.go index 2871026e..b8d415f6 100644 --- a/catalog_test.go +++ b/catalog_test.go @@ -58,8 +58,8 @@ 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) FindConstant(path []string) (types.Constant, int, error) { + return nil, 0, nil } func (c *myCatalog) FindConversion(from, to types.Type) (types.Conversion, error) { diff --git a/internal/ccall/go-zetasql/bind_darwin.go b/internal/ccall/go-zetasql/bind_darwin.go index 46062c1a..f47fa38e 100644 --- a/internal/ccall/go-zetasql/bind_darwin.go +++ b/internal/ccall/go-zetasql/bind_darwin.go @@ -28355,6 +28355,20 @@ func zetasql_Catalog_FindType(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *un C.export_zetasql_Catalog_FindType(arg0, arg1, arg2, arg3) } +func Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *int, arg4 *unsafe.Pointer) { + zetasql_Catalog_FindConstant( + arg0, + arg1, + arg2, + (*C.int)(unsafe.Pointer(arg3)), + arg4, + ) +} + +func zetasql_Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *C.int, arg4 *unsafe.Pointer) { + C.export_zetasql_Catalog_FindConstant(arg0, arg1, arg2, arg3, arg4) +} + func Catalog_SuggestTable(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer) { zetasql_Catalog_SuggestTable( arg0, @@ -28899,6 +28913,61 @@ func zetasql_SimpleCatalog_AddZetaSQLFunctions(arg0 unsafe.Pointer, arg1 unsafe. C.export_zetasql_SimpleCatalog_AddZetaSQLFunctions(arg0, arg1) } +func Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_Name( + arg0, + arg1, + ) +} + +func zetasql_Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_Name(arg0, arg1) +} + +func Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_FullName( + arg0, + arg1, + ) +} + +func zetasql_Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_FullName(arg0, arg1) +} + +func Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_type( + arg0, + arg1, + ) +} + +func zetasql_Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_type(arg0, arg1) +} + +func Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_DebugString( + arg0, + arg1, + ) +} + +func zetasql_Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_DebugString(arg0, arg1) +} + +func Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_name_path( + arg0, + arg1, + ) +} + +func zetasql_Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_name_path(arg0, arg1) +} + func Model_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { zetasql_Model_Name( arg0, diff --git a/internal/ccall/go-zetasql/bind_linux.go b/internal/ccall/go-zetasql/bind_linux.go index 08e6fa7e..a36ea517 100644 --- a/internal/ccall/go-zetasql/bind_linux.go +++ b/internal/ccall/go-zetasql/bind_linux.go @@ -28358,6 +28358,20 @@ func zetasql_Catalog_FindType(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *un C.export_zetasql_Catalog_FindType(arg0, arg1, arg2, arg3) } +func Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *int, arg4 *unsafe.Pointer) { + zetasql_Catalog_FindConstant( + arg0, + arg1, + arg2, + (*C.int)(unsafe.Pointer(arg3)), + arg4, + ) +} + +func zetasql_Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *C.int, arg4 *unsafe.Pointer) { + C.export_zetasql_Catalog_FindConstant(arg0, arg1, arg2, arg3, arg4) +} + func Catalog_SuggestTable(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer) { zetasql_Catalog_SuggestTable( arg0, @@ -28902,6 +28916,61 @@ func zetasql_SimpleCatalog_AddZetaSQLFunctions(arg0 unsafe.Pointer, arg1 unsafe. C.export_zetasql_SimpleCatalog_AddZetaSQLFunctions(arg0, arg1) } +func Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_Name( + arg0, + arg1, + ) +} + +func zetasql_Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_Name(arg0, arg1) +} + +func Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_FullName( + arg0, + arg1, + ) +} + +func zetasql_Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_FullName(arg0, arg1) +} + +func Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_type( + arg0, + arg1, + ) +} + +func zetasql_Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_type(arg0, arg1) +} + +func Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_DebugString( + arg0, + arg1, + ) +} + +func zetasql_Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_DebugString(arg0, arg1) +} + +func Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + zetasql_Constant_name_path( + arg0, + arg1, + ) +} + +func zetasql_Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_Constant_name_path(arg0, arg1) +} + func Model_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { zetasql_Model_Name( arg0, diff --git a/internal/ccall/go-zetasql/callback_darwin.go b/internal/ccall/go-zetasql/callback_darwin.go index def820ee..eae72c65 100644 --- a/internal/ccall/go-zetasql/callback_darwin.go +++ b/internal/ccall/go-zetasql/callback_darwin.go @@ -48,7 +48,7 @@ type GoCatalog struct { FindTableValuedFunction func([]string) (unsafe.Pointer, error) FindProcedure func([]string) (unsafe.Pointer, error) FindType func([]string) (unsafe.Pointer, error) - FindConstant func([]string) (unsafe.Pointer, error) + FindConstant func([]string) (unsafe.Pointer, int, error) FindConversion func(unsafe.Pointer, unsafe.Pointer) (unsafe.Pointer, error) ExtendedTypeSuperTypes func(unsafe.Pointer) (unsafe.Pointer, error) SuggestTable func([]string) string @@ -202,17 +202,18 @@ func GoCatalog_FindType(v unsafe.Pointer, pathPtr unsafe.Pointer, typ *unsafe.Po } //export GoCatalog_FindConstant -func GoCatalog_FindConstant(v unsafe.Pointer, pathPtr unsafe.Pointer, constant *unsafe.Pointer, ret **C.char) { +func GoCatalog_FindConstant(v unsafe.Pointer, pathPtr unsafe.Pointer, numNamesConsumed *C.int, constant *unsafe.Pointer, ret **C.char) { h := *(*cgo.Handle)(v) cat := h.Value().(*GoCatalog) var path []string ptrToSlice(pathPtr, func(p unsafe.Pointer) { path = append(path, C.GoString((*C.char)(p))) }) - consPtr, err := cat.FindConstant(path) + consPtr, num, err := cat.FindConstant(path) if err != nil { *ret = C.CString(err.Error()) } else { + *numNamesConsumed = C.int(num) *constant = consPtr } } @@ -344,9 +345,9 @@ func GoTable_IsValueTable(v unsafe.Pointer) C.int { h := *(*cgo.Handle)(v) table := h.Value().(*GoTable) if table.IsValueTable() { - return 0 + return 1 } - return 1 + return 0 } //export GoTable_SerializationID @@ -384,9 +385,9 @@ func GoTable_SupportsAnonymization(v unsafe.Pointer) C.int { h := *(*cgo.Handle)(v) table := h.Value().(*GoTable) if table.SupportsAnonymization() { - return 0 + return 1 } - return 1 + return 0 } //export GoTable_TableTypeName diff --git a/internal/ccall/go-zetasql/callback_linux.go b/internal/ccall/go-zetasql/callback_linux.go index fe94eeec..9fc04711 100644 --- a/internal/ccall/go-zetasql/callback_linux.go +++ b/internal/ccall/go-zetasql/callback_linux.go @@ -49,7 +49,7 @@ type GoCatalog struct { FindTableValuedFunction func([]string) (unsafe.Pointer, error) FindProcedure func([]string) (unsafe.Pointer, error) FindType func([]string) (unsafe.Pointer, error) - FindConstant func([]string) (unsafe.Pointer, error) + FindConstant func([]string) (unsafe.Pointer, int, error) FindConversion func(unsafe.Pointer, unsafe.Pointer) (unsafe.Pointer, error) ExtendedTypeSuperTypes func(unsafe.Pointer) (unsafe.Pointer, error) SuggestTable func([]string) string @@ -203,17 +203,18 @@ func GoCatalog_FindType(v unsafe.Pointer, pathPtr unsafe.Pointer, typ *unsafe.Po } //export GoCatalog_FindConstant -func GoCatalog_FindConstant(v unsafe.Pointer, pathPtr unsafe.Pointer, constant *unsafe.Pointer, ret **C.char) { +func GoCatalog_FindConstant(v unsafe.Pointer, pathPtr unsafe.Pointer, numNamesConsumed *C.int, constant *unsafe.Pointer, ret **C.char) { h := *(*cgo.Handle)(v) cat := h.Value().(*GoCatalog) var path []string ptrToSlice(pathPtr, func(p unsafe.Pointer) { path = append(path, C.GoString((*C.char)(p))) }) - consPtr, err := cat.FindConstant(path) + consPtr, num, err := cat.FindConstant(path) if err != nil { *ret = C.CString(err.Error()) } else { + *numNamesConsumed = C.int(num) *constant = consPtr } } @@ -345,9 +346,9 @@ func GoTable_IsValueTable(v unsafe.Pointer) C.int { h := *(*cgo.Handle)(v) table := h.Value().(*GoTable) if table.IsValueTable() { - return 0 + return 1 } - return 1 + return 0 } //export GoTable_SerializationID @@ -385,9 +386,9 @@ func GoTable_SupportsAnonymization(v unsafe.Pointer) C.int { h := *(*cgo.Handle)(v) table := h.Value().(*GoTable) if table.SupportsAnonymization() { - return 0 + return 1 } - return 1 + return 0 } //export GoTable_TableTypeName diff --git a/internal/ccall/go-zetasql/public/catalog/bridge.inc b/internal/ccall/go-zetasql/public/catalog/bridge.inc index 37be8220..2defd4a0 100644 --- a/internal/ccall/go-zetasql/public/catalog/bridge.inc +++ b/internal/ccall/go-zetasql/public/catalog/bridge.inc @@ -194,6 +194,28 @@ public: } absl::Status FindConstantWithPathPrefix(const absl::Span path, int* num_names_consumed, const Constant** constant, const FindOptions& options = FindOptions()) { + if (path.empty()) { + return EmptyNamePathInternalError("Constant"); + } + GoSlice *path_slice = (GoSlice *)malloc(sizeof(GoSlice)); + const char **data = (const char **)malloc(sizeof(void *) * path.size()); + for (size_t i = 0; i < path.size(); i++) { + data[i] = path[i].c_str(); + } + path_slice->data = (void *)data; + path_slice->len = path.size(); + path_slice->cap = path.size(); + + void *constV = nullptr; + char *ret = nullptr; + GoCatalog_FindConstant(this->go_catalog_ptr, (void *)path_slice, num_names_consumed, &constV, &ret); + if (constV == nullptr) { + return ConstantNotFoundError(path); + } + if (ret != nullptr) { + return absl::InvalidArgumentError(ret); + } + *constant = (const Constant *)constV; return absl::OkStatus(); } diff --git a/internal/ccall/go-zetasql/public/simple_catalog/bind_darwin.go b/internal/ccall/go-zetasql/public/simple_catalog/bind_darwin.go index dec8bf89..11a66b8e 100644 --- a/internal/ccall/go-zetasql/public/simple_catalog/bind_darwin.go +++ b/internal/ccall/go-zetasql/public/simple_catalog/bind_darwin.go @@ -2204,6 +2204,20 @@ func simple_catalog_Catalog_FindType(arg0 unsafe.Pointer, arg1 unsafe.Pointer, a C.export_zetasql_public_simple_catalog_Catalog_FindType(arg0, arg1, arg2, arg3) } +func Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *int, arg4 *unsafe.Pointer) { + simple_catalog_Catalog_FindConstant( + arg0, + arg1, + arg2, + (*C.int)(unsafe.Pointer(arg3)), + arg4, + ) +} + +func simple_catalog_Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *C.int, arg4 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Catalog_FindConstant(arg0, arg1, arg2, arg3, arg4) +} + func Catalog_SuggestTable(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer) { simple_catalog_Catalog_SuggestTable( arg0, @@ -2748,6 +2762,61 @@ func simple_catalog_SimpleCatalog_AddZetaSQLFunctions(arg0 unsafe.Pointer, arg1 C.export_zetasql_public_simple_catalog_SimpleCatalog_AddZetaSQLFunctions(arg0, arg1) } +func Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_Name( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_Name(arg0, arg1) +} + +func Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_FullName( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_FullName(arg0, arg1) +} + +func Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_type( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_type(arg0, arg1) +} + +func Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_DebugString( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_DebugString(arg0, arg1) +} + +func Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_name_path( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_name_path(arg0, arg1) +} + func Model_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { simple_catalog_Model_Name( arg0, diff --git a/internal/ccall/go-zetasql/public/simple_catalog/bind_linux.go b/internal/ccall/go-zetasql/public/simple_catalog/bind_linux.go index 0b945e52..0edf470a 100644 --- a/internal/ccall/go-zetasql/public/simple_catalog/bind_linux.go +++ b/internal/ccall/go-zetasql/public/simple_catalog/bind_linux.go @@ -2207,6 +2207,20 @@ func simple_catalog_Catalog_FindType(arg0 unsafe.Pointer, arg1 unsafe.Pointer, a C.export_zetasql_public_simple_catalog_Catalog_FindType(arg0, arg1, arg2, arg3) } +func Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *int, arg4 *unsafe.Pointer) { + simple_catalog_Catalog_FindConstant( + arg0, + arg1, + arg2, + (*C.int)(unsafe.Pointer(arg3)), + arg4, + ) +} + +func simple_catalog_Catalog_FindConstant(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer, arg3 *C.int, arg4 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Catalog_FindConstant(arg0, arg1, arg2, arg3, arg4) +} + func Catalog_SuggestTable(arg0 unsafe.Pointer, arg1 unsafe.Pointer, arg2 *unsafe.Pointer) { simple_catalog_Catalog_SuggestTable( arg0, @@ -2751,6 +2765,61 @@ func simple_catalog_SimpleCatalog_AddZetaSQLFunctions(arg0 unsafe.Pointer, arg1 C.export_zetasql_public_simple_catalog_SimpleCatalog_AddZetaSQLFunctions(arg0, arg1) } +func Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_Name( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_Name(arg0, arg1) +} + +func Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_FullName( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_FullName(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_FullName(arg0, arg1) +} + +func Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_type( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_type(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_type(arg0, arg1) +} + +func Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_DebugString( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_DebugString(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_DebugString(arg0, arg1) +} + +func Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + simple_catalog_Constant_name_path( + arg0, + arg1, + ) +} + +func simple_catalog_Constant_name_path(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { + C.export_zetasql_public_simple_catalog_Constant_name_path(arg0, arg1) +} + func Model_Name(arg0 unsafe.Pointer, arg1 *unsafe.Pointer) { simple_catalog_Model_Name( arg0, diff --git a/internal/ccall/go-zetasql/public/simple_catalog/bridge.inc b/internal/ccall/go-zetasql/public/simple_catalog/bridge.inc index b819e248..da139566 100644 --- a/internal/ccall/go-zetasql/public/simple_catalog/bridge.inc +++ b/internal/ccall/go-zetasql/public/simple_catalog/bridge.inc @@ -1243,6 +1243,19 @@ void GO_EXPORT(Catalog_FindType)(void * arg0,void * arg1,void **arg2,void ** arg *arg3 = new absl::Status(ret); } +void GO_EXPORT(Catalog_FindConstant)(void * arg0,void * arg1,void ** arg2, int *arg3, void ** arg4) +{ + zetasql::Catalog *catalog = (zetasql::Catalog *)arg0; + auto pathvec = simple_catalog_slice_to_strs(arg1); + const absl::Span path(pathvec); + const zetasql::Constant *constant = nullptr; + int num = 0; + absl::Status ret = catalog->FindConstantWithPathPrefix(path, &num, &constant); + *arg2 = (void *)constant; + *arg3 = num; + *arg4 = new absl::Status(ret); +} + void GO_EXPORT(Catalog_SuggestTable)(void * arg0,void * arg1,void ** arg2) { zetasql::Catalog *catalog = (zetasql::Catalog *)arg0; @@ -1688,6 +1701,36 @@ void GO_EXPORT(SimpleCatalog_AddZetaSQLFunctions)(void * arg0,void *arg1) catalog->AddZetaSQLFunctions(*options); } +void GO_EXPORT(Constant_Name)(void * arg0,void ** arg1) +{ + zetasql::Constant *constant = (zetasql::Constant *)arg0; + *arg1 = (void *)((new std::string(constant->Name()))->c_str()); +} + +void GO_EXPORT(Constant_FullName)(void * arg0,void ** arg1) +{ + zetasql::Constant *constant = (zetasql::Constant *)arg0; + *arg1 = (void *)((new std::string(constant->FullName()))->c_str()); +} + +void GO_EXPORT(Constant_type)(void * arg0,void ** arg1) +{ + zetasql::Constant *constant = (zetasql::Constant *)arg0; + *arg1 = (void *)constant->type(); +} + +void GO_EXPORT(Constant_DebugString)(void * arg0,void ** arg1) +{ + zetasql::Constant *constant = (zetasql::Constant *)arg0; + *arg1 = (void *)((new std::string(constant->DebugString()))->c_str()); +} + +void GO_EXPORT(Constant_name_path)(void * arg0,void ** arg1) +{ + zetasql::Constant *constant = (zetasql::Constant *)arg0; + SIMPLE_CATALOG_GET_STRS(constant->name_path, arg1); +} + void GO_EXPORT(Model_Name)(void * arg0,void ** arg1) { zetasql::Model *model = (zetasql::Model *)arg0; diff --git a/internal/ccall/go-zetasql/public/simple_catalog/bridge_extern.h b/internal/ccall/go-zetasql/public/simple_catalog/bridge_extern.h index 847c40d6..9deb2b12 100644 --- a/internal/ccall/go-zetasql/public/simple_catalog/bridge_extern.h +++ b/internal/ccall/go-zetasql/public/simple_catalog/bridge_extern.h @@ -194,6 +194,7 @@ extern void GO_EXPORT(Catalog_FindFunction)(void * arg0,void * arg1,void ** arg2 extern void GO_EXPORT(Catalog_FindTableValuedFunction)(void * arg0,void * arg1,void ** arg2,void ** arg3); extern void GO_EXPORT(Catalog_FindProcedure)(void * arg0,void * arg1,void ** arg2,void ** arg3); extern void GO_EXPORT(Catalog_FindType)(void * arg0,void * arg1,void ** arg2,void ** arg3); +extern void GO_EXPORT(Catalog_FindConstant)(void * arg0,void * arg1,void ** arg2,int* arg3,void ** arg4); extern void GO_EXPORT(Catalog_SuggestTable)(void * arg0,void * arg1,void ** arg2); extern void GO_EXPORT(Catalog_SuggestModel)(void * arg0,void * arg1,void ** arg2); extern void GO_EXPORT(Catalog_SuggestFunction)(void * arg0,void * arg1,void ** arg2); @@ -240,6 +241,11 @@ extern void GO_EXPORT(SimpleCatalog_AddProcedureWithName)(void * arg0,void * arg extern void GO_EXPORT(SimpleCatalog_AddConstant)(void * arg0,void * arg1); extern void GO_EXPORT(SimpleCatalog_AddConstantWithName)(void * arg0,void * arg1,void * arg2); extern void GO_EXPORT(SimpleCatalog_AddZetaSQLFunctions)(void * arg0,void * arg1); +extern void GO_EXPORT(Constant_Name)(void * arg0,void ** arg1); +extern void GO_EXPORT(Constant_FullName)(void * arg0,void ** arg1); +extern void GO_EXPORT(Constant_type)(void * arg0,void ** arg1); +extern void GO_EXPORT(Constant_DebugString)(void * arg0,void ** arg1); +extern void GO_EXPORT(Constant_name_path)(void * arg0,void ** arg1); extern void GO_EXPORT(Model_Name)(void * arg0,void ** arg1); extern void GO_EXPORT(Model_FullName)(void * arg0,void ** arg1); extern void GO_EXPORT(Model_NumInputs)(void * arg0,int* arg1); diff --git a/internal/cmd/generator/bridge.yaml b/internal/cmd/generator/bridge.yaml index b7cba34c..9745442a 100644 --- a/internal/cmd/generator/bridge.yaml +++ b/internal/cmd/generator/bridge.yaml @@ -5697,6 +5697,14 @@ ret: - struct - struct + - name: Catalog_FindConstant + args: + - struct + - struct + ret: + - struct + - int + - struct - name: Catalog_SuggestTable args: - struct @@ -5947,6 +5955,31 @@ args: - struct - struct + - name: Constant_Name + args: + - struct + ret: + - string + - name: Constant_FullName + args: + - struct + ret: + - string + - name: Constant_type + args: + - struct + ret: + - struct + - name: Constant_DebugString + args: + - struct + ret: + - string + - name: Constant_name_path + args: + - struct + ret: + - struct - name: Model_Name args: - struct diff --git a/internal/helper/bind.go b/internal/helper/bind.go index a1c0b356..02523968 100644 --- a/internal/helper/bind.go +++ b/internal/helper/bind.go @@ -68,3 +68,8 @@ func SliceToPtr(v interface{}, cb func(int) unsafe.Pointer) unsafe.Pointer { } return unsafe.Pointer(slice) } + +type CPtrHolder struct { + Ptr unsafe.Pointer + Handler unsafe.Pointer +} diff --git a/types/catalog.go b/types/catalog.go index 74dbde37..8efda99f 100644 --- a/types/catalog.go +++ b/types/catalog.go @@ -21,7 +21,7 @@ type Catalog interface { FindTableValuedFunction(path []string) (TableValuedFunction, error) FindProcedure(path []string) (*Procedure, error) FindType(path []string) (Type, error) - FindConstant(path []string) (Constant, error) + FindConstant(path []string) (Constant, int, error) FindConversion(from, to Type) (Conversion, error) ExtendedTypeSuperTypes(typ Type) (*TypeListView, error) SuggestTable(mistypedPath []string) string @@ -135,7 +135,7 @@ func (c *BaseCatalog) FindType(path []string) (Type, error) { v unsafe.Pointer status unsafe.Pointer ) - internal.Catalog_FindType(c.raw, unsafe.Pointer(&path), &v, &status) + internal.Catalog_FindType(c.raw, helper.StringsToPtr(path), &v, &status) st := helper.NewStatus(status) if !st.OK() { return nil, st.Error() @@ -143,8 +143,18 @@ func (c *BaseCatalog) FindType(path []string) (Type, error) { return newType(v), nil } -func (c *BaseCatalog) FindConstant(path []string) (Constant, error) { - return nil, fmt.Errorf("unimplemented Catalog.FindConstant") +func (c *BaseCatalog) FindConstant(path []string) (Constant, int, error) { + var ( + constant unsafe.Pointer + num int + status unsafe.Pointer + ) + internal.Catalog_FindConstant(c.raw, helper.StringsToPtr(path), &constant, &num, &status) + st := helper.NewStatus(status) + if !st.OK() { + return nil, 0, st.Error() + } + return newConstant(constant), num, nil } func (c *BaseCatalog) FindConversion(from, to Type) (Conversion, error) { @@ -596,12 +606,16 @@ func (c *SimpleCatalog) AddZetaSQLBuiltinFunctions(opt *BuiltinFunctionOptions) } var ( - catalogMap = map[Catalog]unsafe.Pointer{} + catalogMap = map[Catalog]*helper.CPtrHolder{} catalogMapMu sync.Mutex ) func getRawCatalog(c Catalog) unsafe.Pointer { switch cat := c.(type) { + case *BaseCatalog: + return cat.raw + case *BaseEnumerableCatalog: + return cat.getRaw() case *SimpleCatalog: return cat.getRaw() } @@ -610,7 +624,7 @@ func getRawCatalog(c Catalog) unsafe.Pointer { ptr, exists := catalogMap[c] if exists { - return ptr + return ptr.Ptr } goCatalog := &internal.GoCatalog{ @@ -672,12 +686,15 @@ func getRawCatalog(c Catalog) unsafe.Pointer { } return t.getRaw(), nil }, - FindConstant: func(path []string) (unsafe.Pointer, error) { - constant, err := c.FindConstant(path) + FindConstant: func(path []string) (unsafe.Pointer, int, error) { + constant, num, err := c.FindConstant(path) if err != nil { - return nil, err + return nil, 0, err } - return constant.getRaw(), nil + if constant == nil { + return nil, 0, nil + } + return constant.getRaw(), num, nil }, FindConversion: func(from, to unsafe.Pointer) (unsafe.Pointer, error) { conv, err := c.FindConversion(newType(from), newType(to)) @@ -715,8 +732,12 @@ func getRawCatalog(c Catalog) unsafe.Pointer { } h := cgo.NewHandle(goCatalog) + handlerPtr := unsafe.Pointer(&h) var ret unsafe.Pointer - internal.GoCatalog_new(unsafe.Pointer(&h), &ret) - catalogMap[c] = ret + internal.GoCatalog_new(handlerPtr, &ret) + catalogMap[c] = &helper.CPtrHolder{ + Ptr: ret, + Handler: handlerPtr, + } return ret } diff --git a/types/constant.go b/types/constant.go index 5399b3d8..076a1125 100644 --- a/types/constant.go +++ b/types/constant.go @@ -1,8 +1,18 @@ package types -import "unsafe" +import ( + "unsafe" + + internal "github.com/goccy/go-zetasql/internal/ccall/go-zetasql" + "github.com/goccy/go-zetasql/internal/helper" +) type Constant interface { + Name() string + FullName() string + Type() Type + DebugString() string + NamePath() []string getRaw() unsafe.Pointer } @@ -10,6 +20,40 @@ type constant struct { raw unsafe.Pointer } +func (c *constant) Name() string { + var v unsafe.Pointer + internal.Constant_Name(c.raw, &v) + return helper.PtrToString(v) +} + +func (c *constant) FullName() string { + var v unsafe.Pointer + internal.Constant_FullName(c.raw, &v) + return helper.PtrToString(v) +} + +func (c *constant) Type() Type { + var v unsafe.Pointer + internal.Constant_type(c.raw, &v) + return newType(v) +} + +func (c *constant) DebugString() string { + var v unsafe.Pointer + internal.Constant_DebugString(c.raw, &v) + return helper.PtrToString(v) +} + +func (c *constant) NamePath() []string { + var v unsafe.Pointer + internal.Constant_name_path(c.raw, &v) + var ret []string + helper.PtrToSlice(v, func(p unsafe.Pointer) { + ret = append(ret, helper.PtrToString(p)) + }) + return ret +} + func (c *constant) getRaw() unsafe.Pointer { return c.raw } diff --git a/types/table.go b/types/table.go index dbd992be..5d7aa74d 100644 --- a/types/table.go +++ b/types/table.go @@ -234,30 +234,39 @@ func (t *SimpleTable) SetAllowDuplicateColumnNames(value bool) error { return nil } -func newTable(v unsafe.Pointer) Table { - return &SimpleTable{BaseTable: &BaseTable{raw: v}} -} - var ( - tableMap = map[Table]unsafe.Pointer{} + tableMap = map[Table]*helper.CPtrHolder{} tableMapMu sync.Mutex ) +func newTable(v unsafe.Pointer) Table { + tableMapMu.Lock() + defer tableMapMu.Unlock() + + for t, holder := range tableMap { + if holder.Ptr == v { + return t + } + } + return &SimpleTable{BaseTable: &BaseTable{raw: v}} +} + func getRawTable(v Table) unsafe.Pointer { if v == nil { return nil } switch t := v.(type) { + case *BaseTable: + return t.raw case *SimpleTable: return t.getRaw() } - tableMapMu.Lock() defer tableMapMu.Unlock() - ptr, exists := tableMap[v] + holder, exists := tableMap[v] if exists { - return ptr + return holder.Ptr } goTable := &internal.GoTable{ @@ -311,7 +320,11 @@ func getRawTable(v Table) unsafe.Pointer { h := cgo.NewHandle(goTable) var ret unsafe.Pointer - internal.GoTable_new(unsafe.Pointer(&h), &ret) - tableMap[v] = ret + handlerPtr := unsafe.Pointer(&h) + internal.GoTable_new(handlerPtr, &ret) + tableMap[v] = &helper.CPtrHolder{ + Ptr: ret, + Handler: handlerPtr, + } return ret }