Skip to content

Commit f384118

Browse files
committed
Fallback in functions and aggregates metadata queries for yugabyte
1 parent c4804bb commit f384118

File tree

1 file changed

+89
-19
lines changed

1 file changed

+89
-19
lines changed

metadata.go

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package gocql
77
import (
88
"encoding/hex"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"strconv"
1213
"strings"
@@ -1048,13 +1049,17 @@ func getFunctionsMetadata(session *Session, keyspaceName string) ([]FunctionMeta
10481049
if session.cfg.ProtoVersion == protoVersion1 || !session.hasAggregatesAndFunctions {
10491050
return nil, nil
10501051
}
1052+
var withoutBody bool
10511053
var tableName string
10521054
if session.useSystemSchema {
10531055
tableName = "system_schema.functions"
10541056
} else {
10551057
tableName = "system.schema_functions"
10561058
}
1057-
stmt := fmt.Sprintf(`
1059+
FuncsStmt:
1060+
var stmt string
1061+
if !withoutBody {
1062+
stmt = fmt.Sprintf(`
10581063
SELECT
10591064
function_name,
10601065
argument_types,
@@ -1065,6 +1070,18 @@ func getFunctionsMetadata(session *Session, keyspaceName string) ([]FunctionMeta
10651070
return_type
10661071
FROM %s
10671072
WHERE keyspace_name = ?`, tableName)
1073+
} else {
1074+
stmt = fmt.Sprintf(`
1075+
SELECT
1076+
function_name,
1077+
argument_types,
1078+
argument_names,
1079+
called_on_null_input,
1080+
language,
1081+
return_type
1082+
FROM %s
1083+
WHERE keyspace_name = ?`, tableName)
1084+
}
10681085

10691086
var functions []FunctionMetadata
10701087

@@ -1073,14 +1090,25 @@ func getFunctionsMetadata(session *Session, keyspaceName string) ([]FunctionMeta
10731090
function := FunctionMetadata{Keyspace: keyspaceName}
10741091
var argumentTypes []string
10751092
var returnType string
1076-
err := rows.Scan(&function.Name,
1077-
&argumentTypes,
1078-
&function.ArgumentNames,
1079-
&function.Body,
1080-
&function.CalledOnNullInput,
1081-
&function.Language,
1082-
&returnType,
1083-
)
1093+
var err error
1094+
if !withoutBody {
1095+
err = rows.Scan(&function.Name,
1096+
&argumentTypes,
1097+
&function.ArgumentNames,
1098+
&function.Body,
1099+
&function.CalledOnNullInput,
1100+
&function.Language,
1101+
&returnType,
1102+
)
1103+
} else {
1104+
err = rows.Scan(&function.Name,
1105+
&argumentTypes,
1106+
&function.ArgumentNames,
1107+
&function.CalledOnNullInput,
1108+
&function.Language,
1109+
&returnType,
1110+
)
1111+
}
10841112
if err != nil {
10851113
return nil, err
10861114
}
@@ -1093,6 +1121,14 @@ func getFunctionsMetadata(session *Session, keyspaceName string) ([]FunctionMeta
10931121
}
10941122

10951123
if err := rows.Err(); err != nil {
1124+
if !withoutBody {
1125+
// Yugabyte doesn't support the body column in the functions table
1126+
var rerr RequestError
1127+
if errors.As(err, &rerr) && rerr.Code() == ErrCodeInvalid {
1128+
withoutBody = true
1129+
goto FuncsStmt
1130+
}
1131+
}
10961132
return nil, err
10971133
}
10981134

@@ -1103,14 +1139,17 @@ func getAggregatesMetadata(session *Session, keyspaceName string) ([]AggregateMe
11031139
if session.cfg.ProtoVersion == protoVersion1 || !session.hasAggregatesAndFunctions {
11041140
return nil, nil
11051141
}
1142+
var withoutReturnType bool
11061143
var tableName string
11071144
if session.useSystemSchema {
11081145
tableName = "system_schema.aggregates"
11091146
} else {
11101147
tableName = "system.schema_aggregates"
11111148
}
1112-
1113-
stmt := fmt.Sprintf(`
1149+
AggsStmt:
1150+
var stmt string
1151+
if !withoutReturnType {
1152+
stmt = fmt.Sprintf(`
11141153
SELECT
11151154
aggregate_name,
11161155
argument_types,
@@ -1121,6 +1160,18 @@ func getAggregatesMetadata(session *Session, keyspaceName string) ([]AggregateMe
11211160
state_type
11221161
FROM %s
11231162
WHERE keyspace_name = ?`, tableName)
1163+
} else {
1164+
stmt = fmt.Sprintf(`
1165+
SELECT
1166+
aggregate_name,
1167+
argument_types,
1168+
final_func,
1169+
initcond,
1170+
state_func,
1171+
state_type
1172+
FROM %s
1173+
WHERE keyspace_name = ?`, tableName)
1174+
}
11241175

11251176
var aggregates []AggregateMetadata
11261177

@@ -1130,14 +1181,25 @@ func getAggregatesMetadata(session *Session, keyspaceName string) ([]AggregateMe
11301181
var argumentTypes []string
11311182
var returnType string
11321183
var stateType string
1133-
err := rows.Scan(&aggregate.Name,
1134-
&argumentTypes,
1135-
&aggregate.finalFunc,
1136-
&aggregate.InitCond,
1137-
&returnType,
1138-
&aggregate.stateFunc,
1139-
&stateType,
1140-
)
1184+
var err error
1185+
if !withoutReturnType {
1186+
err = rows.Scan(&aggregate.Name,
1187+
&argumentTypes,
1188+
&aggregate.finalFunc,
1189+
&aggregate.InitCond,
1190+
&returnType,
1191+
&aggregate.stateFunc,
1192+
&stateType,
1193+
)
1194+
} else {
1195+
err = rows.Scan(&aggregate.Name,
1196+
&argumentTypes,
1197+
&aggregate.finalFunc,
1198+
&aggregate.InitCond,
1199+
&aggregate.stateFunc,
1200+
&stateType,
1201+
)
1202+
}
11411203
if err != nil {
11421204
return nil, err
11431205
}
@@ -1151,6 +1213,14 @@ func getAggregatesMetadata(session *Session, keyspaceName string) ([]AggregateMe
11511213
}
11521214

11531215
if err := rows.Err(); err != nil {
1216+
if !withoutReturnType {
1217+
// Yugabyte doesn't support the return_type column in the aggregates table
1218+
var rerr RequestError
1219+
if errors.As(err, &rerr) && rerr.Code() == ErrCodeInvalid {
1220+
withoutReturnType = true
1221+
goto AggsStmt
1222+
}
1223+
}
11541224
return nil, err
11551225
}
11561226

0 commit comments

Comments
 (0)