Skip to content

Commit

Permalink
sql: desugar JSON to JSONB
Browse files Browse the repository at this point in the history
The distinction between the type names "JSON" and "JSONB" is
inconsequential. The types are already reported properly in
introspection using the right alias. This patch removes this
distinction in code.

Release note: None
  • Loading branch information
knz committed Aug 22, 2018
1 parent df7abd7 commit bfad209
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 273 deletions.
2 changes: 1 addition & 1 deletion pkg/ccl/partitionccl/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ func allPartitioningTests(rng *rand.Rand) []partitioningTest {
case sqlbase.ColumnType_COLLATEDSTRING:
typ.Locale = sqlbase.RandCollationLocale(rng)
colType = fmt.Sprintf(`STRING COLLATE %s`, *typ.Locale)
case sqlbase.ColumnType_JSON:
case sqlbase.ColumnType_JSONB:
// Not indexable.
continue
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/testdata/dump/inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ dump d t
----
----
CREATE TABLE t (
a JSON NULL,
b JSON NULL,
a JSONB NULL,
b JSONB NULL,
INVERTED INDEX idx (a),
INVERTED INDEX idx2 (b),
FAMILY "primary" (a, b, rowid)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/testdata/dump/row
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CREATE TABLE t (
e DECIMAL NULL,
u UUID NULL,
ip INET NULL,
j JSON NULL,
j JSONB NULL,
ary STRING[] NULL,
tz TIMESTAMP WITH TIME ZONE NULL,
e1 DECIMAL(2) NULL,
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/coltypes/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ var (
INet = &TIPAddr{}

// JSON is an immutable T instance.
JSON = &TJSON{Name: "JSON"}
// JSONB is an immutable T instance.
JSONB = &TJSON{Name: "JSONB"}
JSON = &TJSON{}

// Oid is an immutable T instance.
Oid = &TOid{Name: "OID"}
Expand Down
8 changes: 3 additions & 5 deletions pkg/sql/coltypes/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ func (node *TIPAddr) Format(buf *bytes.Buffer, f lex.EncodeFlags) {
}

// TJSON represents the JSON column type.
type TJSON struct {
Name string
}
type TJSON struct{}

// TypeName implements the ColTypeFormatter interface.
func (node *TJSON) TypeName() string { return node.Name }
func (node *TJSON) TypeName() string { return "JSONB" }

// Format implements the ColTypeFormatter interface.
func (node *TJSON) Format(buf *bytes.Buffer, _ lex.EncodeFlags) {
buf.WriteString(node.Name)
buf.WriteString(node.TypeName())
}

// TOid represents an OID type, which is the type of system object
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/distsqlrun/scrub_tablereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
var ScrubTypes = []sqlbase.ColumnType{
{SemanticType: sqlbase.ColumnType_STRING},
{SemanticType: sqlbase.ColumnType_STRING},
{SemanticType: sqlbase.ColumnType_JSON},
{SemanticType: sqlbase.ColumnType_JSONB},
}

type scrubTableReader struct {
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/logictest/testdata/logic_test/inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ query TT
SHOW CREATE TABLE c
----
c CREATE TABLE c (
id INT NOT NULL,
foo JSON NULL,
bar JSON NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INVERTED INDEX c_foo_idx (foo),
INVERTED INDEX c_bar_idx (bar),
FAMILY "primary" (id, foo, bar)
)
id INT NOT NULL,
foo JSONB NULL,
bar JSONB NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INVERTED INDEX c_foo_idx (foo),
INVERTED INDEX c_bar_idx (bar),
FAMILY "primary" (id, foo, bar)
)

statement error indexing more than one column with an inverted index is not supported
CREATE TABLE d (
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ SELECT bar FROM foo WHERE bar->'a' = '"b"'::JSON
statement error pgcode 0A000 can't order by column type jsonb
SELECT bar FROM foo ORDER BY bar

statement error pgcode 0A000 column k is of type JSON and thus is not indexable
statement error pgcode 0A000 column k is of type JSONB and thus is not indexable
CREATE TABLE pk (k JSON PRIMARY KEY)

query T rowsort
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/table
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ CREATE TABLE alltypes (
cint8 INT8,
cinteger INTEGER,
cinterval INTERVAL,
cjson JSONB,
cnumeric NUMERIC,
cnumeric1 NUMERIC(1),
cnumeric21 NUMERIC(2,1),
Expand Down Expand Up @@ -382,6 +383,7 @@ cint64 BIGINT true NULL ·
cint8 BIGINT true NULL · {} false
cinteger INTEGER true NULL · {} false
cinterval INTERVAL true NULL · {} false
cjson JSONB true NULL · {} false
cnumeric DECIMAL true NULL · {} false
cnumeric1 DECIMAL(1) true NULL · {} false
cnumeric21 DECIMAL(2,1) true NULL · {} false
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/opt/memo/private_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,7 @@ func TestInternColType(t *testing.T) {
// Miscellaneous types.
test(coltypes.UUID, &coltypes.TUUID{}, true)
test(coltypes.INet, &coltypes.TIPAddr{}, true)
test(coltypes.JSON, &coltypes.TJSON{Name: "JSON"}, true)
test(coltypes.JSONB, &coltypes.TJSON{Name: "JSONB"}, true)
test(coltypes.JSON, coltypes.JSONB, false)
test(coltypes.JSON, &coltypes.TJSON{}, true)
test(coltypes.Oid, &coltypes.TOid{Name: "OID"}, true)

// String types.
Expand Down
7 changes: 3 additions & 4 deletions pkg/sql/opt/memo/testdata/typing
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,14 @@ SELECT
FROM b
----
scalar-group-by
├── columns: array_agg:3(decimal[]) avg:4(decimal) bool_and:6(bool) bool_or:7(bool) concat_agg:8(string) count:9(int) count:10(int) max:11(string) max:12(decimal) sum_int:14(int) sum:15(decimal) sqrdiff:16(decimal) variance:17(decimal) stddev:18(decimal) xor_agg:19(int) json_agg:21(jsonb) jsonb_agg:23(jsonb)
├── columns: array_agg:3(decimal[]) avg:4(decimal) bool_and:6(bool) bool_or:7(bool) concat_agg:8(string) count:9(int) count:10(int) max:11(string) max:12(decimal) sum_int:14(int) sum:15(decimal) sqrdiff:16(decimal) variance:17(decimal) stddev:18(decimal) xor_agg:19(int) json_agg:21(jsonb) jsonb_agg:22(jsonb)
├── project
│ ├── columns: column5:5(bool) column13:13(int) column20:20(jsonb) column22:22(jsonb) x:1(string!null) z:2(decimal!null)
│ ├── columns: column5:5(bool) column13:13(int) column20:20(jsonb) x:1(string!null) z:2(decimal!null)
│ ├── scan b
│ │ └── columns: x:1(string!null) z:2(decimal!null)
│ └── projections
│ ├── z = 0 [type=bool]
│ ├── x::INT [type=int]
│ ├── x::JSON [type=jsonb]
│ └── x::JSONB [type=jsonb]
└── aggregations
├── array-agg [type=decimal[]]
Expand Down Expand Up @@ -373,7 +372,7 @@ scalar-group-by
├── json-agg [type=jsonb]
│ └── variable: column20 [type=jsonb]
└── jsonb-agg [type=jsonb]
└── variable: column22 [type=jsonb]
└── variable: column20 [type=jsonb]

# ConstAgg internal aggregate function.
opt
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/norm/testdata/rules/comp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ select
│ ├── key: (1)
│ └── fd: (1)-->(2-5)
└── filters [type=bool, outer=(4)]
└── (s::JSON - 1) = '[1]' [type=bool, outer=(4)]
└── (s::JSONB - 1) = '[1]' [type=bool, outer=(4)]

# --------------------------------------------------
# NormalizeCmpConstMinus
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/opt/norm/testdata/rules/scalar
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ SELECT
FROM a
----
project
├── columns: i:7(int) arr:8(int[]) json:9(jsonb) int:10(int) s:11(string)
├── columns: i:7(int) arr:8(int[]) jsonb:9(jsonb!null) int:10(int) s:11(string)
├── fd: ()-->(9,10)
├── scan a
│ └── columns: a.i:2(int) a.s:4(string) a.arr:6(int[])
└── projections [outer=(2,4,6)]
├── variable: a.i [type=int, outer=(2)]
├── variable: a.arr [type=int[], outer=(6)]
├── '[1, 2]'::JSONB [type=jsonb]
├── const: '[1, 2]' [type=jsonb]
├── null [type=int]
└── variable: a.s [type=string, outer=(4)]

Expand All @@ -180,7 +180,7 @@ project
└── projections [outer=(2,4,6)]
├── a.i::FLOAT8 [type=float, outer=(2)]
├── a.arr::DECIMAL[] [type=decimal[], outer=(6)]
├── a.s::JSON [type=jsonb, outer=(4)]
├── a.s::JSONB [type=jsonb, outer=(4)]
├── a.s::VARCHAR(2) [type=string, outer=(4)]
├── a.i::SMALLINT::INT8 [type=int, outer=(2)]
├── a.s::CHAR::VARCHAR [type=string, outer=(4)]
Expand Down
44 changes: 16 additions & 28 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,6 @@ func TestParse(t *testing.T) {
{`SELECT a#>>'{x}'`},
{`SELECT (a->'x')->'y'`},
{`SELECT (a->'x')->>'y'`},
{`SELECT ''::JSON`},
{`SELECT ''::JSONB`},

{`SELECT 1 FROM t`},
{`SELECT 1, 2 FROM t`},
Expand Down Expand Up @@ -583,32 +581,20 @@ func TestParse(t *testing.T) {
{`SELECT 'a' FROM t@{NO_INDEX_JOIN}`},
{`SELECT * FROM t AS "of" AS OF SYSTEM TIME '2016-01-01'`},

{`SELECT BOOL 'foo'`},
{`SELECT INT 'foo'`},
{`SELECT FLOAT4 'foo'`},
{`SELECT DECIMAL 'foo'`},
{`SELECT CHAR 'foo'`},
{`SELECT VARCHAR 'foo'`},
{`SELECT STRING 'foo'`},
{`SELECT BYTES 'foo'`},
{`SELECT DATE 'foo'`},
{`SELECT TIME 'foo'`},
{`SELECT TIMESTAMP 'foo'`},
{`SELECT TIMESTAMP WITH TIME ZONE 'foo'`},

{`SELECT '1'::INT`},
{`SELECT '1'::SERIAL`},
{`SELECT '1':::INT`},
{`SELECT '1':::SERIAL`},
{`SELECT INT '1'`},
{`SELECT SERIAL '1'`},

{`SELECT 'foo'::JSON`},
{`SELECT 'foo'::JSONB`},
{`SELECT 'foo':::JSON`},
{`SELECT 'foo':::JSONB`},
{`SELECT JSON 'foo'`},
{`SELECT JSONB 'foo'`},
{`SELECT BOOL 'foo', 'foo'::BOOL`},
{`SELECT INT 'foo', 'foo'::INT`},
{`SELECT FLOAT4 'foo', 'foo'::FLOAT4`},
{`SELECT DECIMAL 'foo', 'foo'::DECIMAL`},
{`SELECT CHAR 'foo', 'foo'::CHAR`},
{`SELECT VARCHAR 'foo', 'foo'::VARCHAR`},
{`SELECT STRING 'foo', 'foo'::STRING`},
{`SELECT BYTES 'foo', 'foo'::BYTES`},
{`SELECT DATE 'foo', 'foo'::DATE`},
{`SELECT TIME 'foo', 'foo'::TIME`},
{`SELECT TIMESTAMP 'foo', 'foo'::TIMESTAMP`},
{`SELECT TIMESTAMP WITH TIME ZONE 'foo', 'foo'::TIMESTAMP WITH TIME ZONE`},
{`SELECT JSONB 'foo', 'foo'::JSONB`},
{`SELECT SERIAL 'foo', 'foo'::SERIAL`},

{`SELECT '192.168.0.1'::INET`},
{`SELECT '192.168.0.1':::INET`},
Expand Down Expand Up @@ -1124,6 +1110,8 @@ func TestParse2(t *testing.T) {
`CREATE TABLE a (b BOOL)`},
{`CREATE TABLE a (b TEXT)`,
`CREATE TABLE a (b STRING)`},
{`CREATE TABLE a (b JSON)`,
`CREATE TABLE a (b JSONB)`},
{`CREATE TABLE a (b BYTES, c BYTEA, d BLOB)`,
`CREATE TABLE a (b BYTES, c BYTES, d BYTES)`},
{`CREATE TABLE a (b CHAR(1), c CHARACTER(1), d CHARACTER(3))`,
Expand Down
11 changes: 4 additions & 7 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ func newNameFromStr(s string) *tree.Name {
%type <coltypes.T> numeric opt_numeric_modifiers
%type <coltypes.T> opt_float
%type <coltypes.T> character_with_length character_without_length
%type <coltypes.T> const_datetime const_interval const_json
%type <coltypes.T> const_datetime const_interval
%type <coltypes.T> bit_with_length bit_without_length
%type <coltypes.T> character_base
%type <coltypes.CastTargetType> postgres_oid
Expand Down Expand Up @@ -5869,13 +5869,7 @@ opt_array_bounds:
const_json:
JSON
{
$$.val = coltypes.JSON
}
| JSONB
{
$$.val = coltypes.JSONB
}
simple_typename:
const_typename
Expand All @@ -5899,6 +5893,9 @@ const_typename:
| character_without_length
| const_datetime
| const_json
{
$$.val = coltypes.JSON
}
| BLOB
{
$$.val = coltypes.Bytes
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sem/tree/col_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestParseColumnType(t *testing.T) {
{"UUID", &coltypes.TUUID{}},
{"INET", &coltypes.TIPAddr{}},
{"DATE", &coltypes.TDate{}},
{"JSONB", &coltypes.TJSON{}},
{"TIME", &coltypes.TTime{}},
{"TIMESTAMP", &coltypes.TTimestamp{}},
{"TIMESTAMP WITH TIME ZONE", &coltypes.TTimestampTZ{}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sqlbase/column_type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func MarshalColumnValue(col ColumnDescriptor, val tree.Datum) (roachpb.Value, er
r.SetBytes(data)
return r, nil
}
case ColumnType_JSON:
case ColumnType_JSONB:
if v, ok := val.(*tree.DJSON); ok {
data, err := json.EncodeJSON(nil, v.JSON)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/sql/sqlbase/column_type_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ func (c *ColumnType) InformationSchemaVisibleType() string {
return "timestamp with time zone"
case ColumnType_BYTES:
return "bytea"
case ColumnType_JSON:
return "jsonb"
case ColumnType_NULL:
return "unknown"
case ColumnType_TUPLE:
Expand Down Expand Up @@ -491,7 +489,7 @@ func DatumTypeToColumnSemanticType(ptyp types.T) (ColumnType_SemanticType, error
case types.OidVector:
return ColumnType_OIDVECTOR, nil
case types.JSON:
return ColumnType_JSON, nil
return ColumnType_JSONB, nil
default:
if ptyp.FamilyEqual(types.FamCollatedString) {
return ColumnType_COLLATEDSTRING, nil
Expand Down Expand Up @@ -537,7 +535,7 @@ func columnSemanticTypeToDatumType(c *ColumnType, k ColumnType_SemanticType) typ
return types.UUID
case ColumnType_INET:
return types.INet
case ColumnType_JSON:
case ColumnType_JSONB:
return types.JSON
case ColumnType_TUPLE:
return types.FamTuple
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sqlbase/encoded_datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestEncDatumCompare(t *testing.T) {
for kind := range ColumnType_SemanticType_name {
kind := ColumnType_SemanticType(kind)
if kind == ColumnType_NULL || kind == ColumnType_ARRAY || kind == ColumnType_INT2VECTOR ||
kind == ColumnType_OIDVECTOR || kind == ColumnType_JSON || kind == ColumnType_TUPLE {
kind == ColumnType_OIDVECTOR || kind == ColumnType_JSONB || kind == ColumnType_TUPLE {
continue
}
typ := ColumnType{SemanticType: kind}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func DatumTypeHasCompositeKeyEncoding(typ types.T) bool {
// MustBeValueEncoded returns true if columns of the given kind can only be value
// encoded.
func MustBeValueEncoded(semanticType ColumnType_SemanticType) bool {
return semanticType == ColumnType_ARRAY || semanticType == ColumnType_JSON || semanticType == ColumnType_TUPLE
return semanticType == ColumnType_ARRAY || semanticType == ColumnType_JSONB || semanticType == ColumnType_TUPLE
}

// HasOldStoredColumns returns whether the index has stored columns in the old
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func (desc *TableDescriptor) ValidateTable(st *cluster.Settings) error {
if st != nil && st.Version.HasBeenInitialized() {
if !st.Version.IsMinSupported(cluster.Version2_0) {
for _, def := range desc.Columns {
if def.Type.SemanticType == ColumnType_JSON {
if def.Type.SemanticType == ColumnType_JSONB {
return errors.New("cluster version does not support JSONB (>= 2.0 required)")
}
if def.ComputeExpr != nil {
Expand Down Expand Up @@ -1551,7 +1551,7 @@ func columnTypeIsIndexable(t ColumnType) bool {
// columnTypeIsInvertedIndexable returns whether the type t is valid to be indexed
// using an inverted index.
func columnTypeIsInvertedIndexable(t ColumnType) bool {
return t.SemanticType == ColumnType_JSON
return t.SemanticType == ColumnType_JSONB
}

func notIndexableError(cols []ColumnDescriptor, inverted bool) error {
Expand Down
Loading

0 comments on commit bfad209

Please sign in to comment.