diff --git a/internal/formatter.go b/internal/formatter.go index b5e776d..f553de1 100644 --- a/internal/formatter.go +++ b/internal/formatter.go @@ -658,6 +658,16 @@ func (n *ArrayScanNode) FormatSQL(ctx context.Context) (string, error) { if err != nil { return "", err } + var onClause string + if n.node.JoinExpr() == nil { + onClause = "" + } else { + joinExpr, err := newNode(n.node.JoinExpr()).FormatSQL(ctx) + if err != nil { + return "", err + } + onClause = " ON " + joinExpr + } colName := uniqueColumnName(ctx, n.node.ElementColumn()) if n.node.InputScan() != nil { input, err := newNode(n.node.InputScan()).FormatSQL(ctx) @@ -669,16 +679,18 @@ func (n *ArrayScanNode) FormatSQL(ctx context.Context) (string, error) { return "", err } return fmt.Sprintf( - "SELECT *, json_each.value AS `%s` %s, json_each(zetasqlite_decode_array(%s))", + "SELECT *, json_each.value AS `%s` %s, json_each(zetasqlite_decode_array(%s))%s", colName, formattedInput, arrayExpr, + onClause, ), nil } return fmt.Sprintf( - "SELECT json_each.value AS `%s` FROM json_each(zetasqlite_decode_array(%s))", + "SELECT json_each.value AS `%s` FROM json_each(zetasqlite_decode_array(%s))%s", colName, arrayExpr, + onClause, ), nil } diff --git a/internal/function_register.go b/internal/function_register.go index 2dd46a1..33e8e21 100644 --- a/internal/function_register.go +++ b/internal/function_register.go @@ -427,6 +427,9 @@ func RegisterFunctions(conn *sqlite3.SQLiteConn) error { if err != nil { return "", err } + if decoded == nil { + return "null", nil + } array, err := decoded.ToArray() if err != nil { return "", err