Skip to content

Commit

Permalink
sql: Adding support for int2, int8, float4 and float8 to be in line
Browse files Browse the repository at this point in the history
with postgres support of numerical types.

Before this change we didn't support any of the types mentioned above.

After this change we'll support those types as alias and we will check
for overflow on INSERT and UPDATE. We currently won't support type checking
on mathematical operations, inline with how decimals are currently implemented.
Support for those operations would require a change to Datums, which is outside
of the scope for this change.

Closes #12481
Closes #14493
  • Loading branch information
Masha Schneider committed Jun 27, 2017
1 parent ae0872a commit d6adb98
Show file tree
Hide file tree
Showing 12 changed files with 5,209 additions and 5,047 deletions.
54 changes: 54 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/scale
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,60 @@ UPDATE tb SET b = 2 WHERE b = 7
statement error bit string too long for type BIT\(3\) \(column "b"\)
UPDATE tb SET b = 16 WHERE b = 2

statement ok
CREATE TABLE tc (
b INT2,
UNIQUE INDEX a (b)
)

statement ok
INSERT INTO tc VALUES (50)

statement ok
INSERT INTO tc VALUES (-32768)

statement ok
INSERT INTO tc VALUES (32767)

statement error integer out of range for type INT2 \(column "b"\)
INSERT INTO tc VALUES (-32769)

statement error integer out of range for type INT2 \(column "b"\)
INSERT INTO tc VALUES (32768)

statement ok
UPDATE tc SET b = 80 WHERE b = 50

statement error integer out of range for type INT2 \(column "b"\)
UPDATE tc SET b = 32768 WHERE b = 32767

statement ok
CREATE TABLE tc1 (
b INT4,
UNIQUE INDEX a (b)
)

statement ok
INSERT INTO tc1 VALUES (50)

statement ok
INSERT INTO tc1 VALUES (-2147483648)

statement ok
INSERT INTO tc1 VALUES (2147483647)

statement error integer out of range for type INT4 \(column "b"\)
INSERT INTO tc1 VALUES (-2147483649)

statement error integer out of range for type INT4 \(column "b"\)
INSERT INTO tc1 VALUES (2147483648)

statement ok
UPDATE tc1 SET b = 80 WHERE b = 50

statement error integer out of range for type INT4 \(column "b"\)
UPDATE tc1 SET b = 2147483648 WHERE b = 2147483647

statement ok
CREATE TABLE td (
d DECIMAL(3, 2),
Expand Down
162 changes: 85 additions & 77 deletions pkg/sql/logictest/testdata/logic_test/table
Original file line number Diff line number Diff line change
Expand Up @@ -306,88 +306,96 @@ CREATE TABLE test.dupe_named_constraints (

statement ok
CREATE TABLE test.alltypes (
a BOOL,
b INT,
c INT8,
d INT64,
e INTEGER,
f SMALLINT,
g BIGINT,
h SERIAL,
i SMALLSERIAL,
j BIGSERIAL,
k BIT,
l BIT(12),
m CHAR,
n CHAR(12),
o VARCHAR,
p VARCHAR(12),
q REAL,
r FLOAT,
s DOUBLE PRECISION,
t DEC,
u DEC(1),
v DEC(2,1),
w DECIMAL,
x DECIMAL(1),
y DECIMAL(2,1),
z NUMERIC,
aa NUMERIC(1),
ab NUMERIC(2,1),
ac DATE,
ad TIMESTAMP,
ae TIMESTAMP WITH TIME ZONE,
af STRING,
ag STRING(12),
ah TEXT,
ai BLOB,
aj BYTES,
ak BYTEA,
al INTERVAL
cbigint BIGINT,
cbigserial BIGSERIAL,
cbit BIT,
cbit12 BIT(12),
cblob BLOB,
cbool BOOL,
cbytea BYTEA,
cbytes BYTES,
cchar CHAR,
cchar12 CHAR(12),
cdate DATE,
cdec DEC,
cdec1 DEC(1),
cdec21 DEC(2,1),
cdecimal DECIMAL,
cdecimal1 DECIMAL(1),
cdecimal21 DECIMAL(2,1),
cdoubleprecision DOUBLE PRECISION,
cfloat FLOAT,
cfloat4 FLOAT4,
cfloat8 FLOAT8,
cint INT,
cint2 INT2,
cint4 INT4,
cint64 INT64,
cint8 INT8,
cinteger INTEGER,
cinterval INTERVAL,
cnumeric NUMERIC,
cnumeric1 NUMERIC(1),
cnumeric21 NUMERIC(2,1),
creal REAL,
cserial SERIAL,
csmallint SMALLINT,
csmallserial SMALLSERIAL,
cstring STRING,
cstring12 STRING(12),
ctext TEXT,
ctimestamp TIMESTAMP,
ctimestampwtz TIMESTAMP WITH TIME ZONE,
cvarchar VARCHAR,
cvarchar12 VARCHAR(12)
)

query TTBTT colnames
SHOW COLUMNS FROM test.alltypes
----
Field Type Null Default Indices
a BOOL true NULL {}
b INT true NULL {}
c INT8 true NULL {}
d INT64 true NULL {}
e INT true NULL {}
f INT true NULL {}
g INT true NULL {}
h INT true unique_rowid() {}
i INT true unique_rowid() {}
j INT true unique_rowid() {}
k BIT(1) true NULL {}
l BIT(12) true NULL {}
m STRING true NULL {}
n STRING(12) true NULL {}
o STRING true NULL {}
p STRING(12) true NULL {}
q FLOAT true NULL {}
r FLOAT true NULL {}
s FLOAT true NULL {}
t DECIMAL true NULL {}
u DECIMAL(1) true NULL {}
v DECIMAL(2,1) true NULL {}
w DECIMAL true NULL {}
x DECIMAL(1) true NULL {}
y DECIMAL(2,1) true NULL {}
z DECIMAL true NULL {}
aa DECIMAL(1) true NULL {}
ab DECIMAL(2,1) true NULL {}
ac DATE true NULL {}
ad TIMESTAMP true NULL {}
ae TIMESTAMP WITH TIME ZONE true NULL {}
af STRING true NULL {}
ag STRING(12) true NULL {}
ah STRING true NULL {}
ai BYTES true NULL {}
aj BYTES true NULL {}
ak BYTES true NULL {}
al INTERVAL true NULL {}
Field Type Null Default Indices
cbigint INT true NULL {}
cbigserial INT true unique_rowid() {}
cbit BIT(1) true NULL {}
cbit12 BIT(12) true NULL {}
cblob BYTES true NULL {}
cbool BOOL true NULL {}
cbytea BYTES true NULL {}
cbytes BYTES true NULL {}
cchar STRING true NULL {}
cchar12 STRING(12) true NULL {}
cdate DATE true NULL {}
cdec DECIMAL true NULL {}
cdec1 DECIMAL(1) true NULL {}
cdec21 DECIMAL(2,1) true NULL {}
cdecimal DECIMAL true NULL {}
cdecimal1 DECIMAL(1) true NULL {}
cdecimal21 DECIMAL(2,1) true NULL {}
cdoubleprecision FLOAT true NULL {}
cfloat FLOAT true NULL {}
cfloat4 FLOAT4 true NULL {}
cfloat8 FLOAT8 true NULL {}
cint INT true NULL {}
cint2 INT2 true NULL {}
cint4 INT4 true NULL {}
cint64 INT64 true NULL {}
cint8 INT8 true NULL {}
cinteger INT true NULL {}
cinterval INTERVAL true NULL {}
cnumeric DECIMAL true NULL {}
cnumeric1 DECIMAL(1) true NULL {}
cnumeric21 DECIMAL(2,1) true NULL {}
creal FLOAT true NULL {}
cserial INT true unique_rowid() {}
csmallint SMALLINT true NULL {}
csmallserial INT true unique_rowid() {}
cstring STRING true NULL {}
cstring12 STRING(12) true NULL {}
ctext STRING true NULL {}
ctimestamp TIMESTAMP true NULL {}
ctimestampwtz TIMESTAMP WITH TIME ZONE true NULL {}
cvarchar STRING true NULL {}
cvarchar12 STRING(12) true NULL {}

statement ok
CREATE DATABASE IF NOT EXISTS smtng
Expand Down
23 changes: 14 additions & 9 deletions pkg/sql/parser/col_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ func (node *BoolColType) Format(buf *bytes.Buffer, f FmtFlags) {

// Pre-allocated immutable integer column types.
var (
intColTypeBit = &IntColType{Name: "BIT", N: 1, ImplicitWidth: true}
intColTypeBit = &IntColType{Name: "BIT", Width: 1, ImplicitWidth: true}
intColTypeInt = &IntColType{Name: "INT"}
intColTypeInt2 = &IntColType{Name: "INT2", Width: 16, ImplicitWidth: true}
intColTypeInt4 = &IntColType{Name: "INT4", Width: 32, ImplicitWidth: true}
intColTypeInt8 = &IntColType{Name: "INT8"}
intColTypeInt64 = &IntColType{Name: "INT64"}
intColTypeInteger = &IntColType{Name: "INTEGER"}
intColTypeSmallInt = &IntColType{Name: "SMALLINT"}
intColTypeSmallInt = &IntColType{Name: "SMALLINT", Width: 16, ImplicitWidth: true}
intColTypeBigInt = &IntColType{Name: "BIGINT"}
intColTypeSerial = &IntColType{Name: "SERIAL"}
intColTypeSmallSerial = &IntColType{Name: "SMALLSERIAL"}
Expand All @@ -108,25 +110,25 @@ var (

var errBitLengthNotPositive = errors.New("length for type bit must be at least 1")

func newIntBitType(n int) (*IntColType, error) {
if n < 1 {
func newIntBitType(width int) (*IntColType, error) {
if width < 1 {
return nil, errBitLengthNotPositive
}
return &IntColType{Name: "BIT", N: n}, nil
return &IntColType{Name: "BIT", Width: width}, nil
}

// IntColType represents an INT, INTEGER, SMALLINT or BIGINT type.
type IntColType struct {
Name string
N int
Width int
ImplicitWidth bool
}

// Format implements the NodeFormatter interface.
func (node *IntColType) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(node.Name)
if node.N > 0 && !node.ImplicitWidth {
fmt.Fprintf(buf, "(%d)", node.N)
if node.Width > 0 && !node.ImplicitWidth {
fmt.Fprintf(buf, "(%d)", node.Width)
}
}

Expand All @@ -141,13 +143,16 @@ func (node *IntColType) IsSerial() bool {
var (
floatColTypeReal = &FloatColType{Name: "REAL"}
floatColTypeFloat = &FloatColType{Name: "FLOAT"}
floatColTypeFloat4 = &FloatColType{Name: "FLOAT4", Prec: 24, Width: 16, PrecSpecified: true}
floatColTypeFloat8 = &FloatColType{Name: "FLOAT8", Prec: 24, Width: 32, PrecSpecified: true}
floatColTypeDouble = &FloatColType{Name: "DOUBLE PRECISION"}
)

// FloatColType represents a REAL, DOUBLE or FLOAT type.
type FloatColType struct {
Name string
Prec int
Width int
PrecSpecified bool // true if the value of Prec is not the default
}

Expand All @@ -163,7 +168,7 @@ func NewFloatColType(prec int, precSpecified bool) *FloatColType {
// Format implements the NodeFormatter interface.
func (node *FloatColType) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(node.Name)
if node.Prec > 0 {
if node.Prec > 0 && node.Width == 0 {
fmt.Fprintf(buf, "(%d)", node.Prec)
}
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/sql/parser/col_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ func TestParseColumnType(t *testing.T) {
str string
expectedType ColumnType
}{
{"BIT", &IntColType{Name: "BIT", N: 1, ImplicitWidth: true}},
{"BIT(2)", &IntColType{Name: "BIT", N: 2}},
{"BIT", &IntColType{Name: "BIT", Width: 1, ImplicitWidth: true}},
{"BIT(2)", &IntColType{Name: "BIT", Width: 2}},
{"BOOL", &BoolColType{Name: "BOOL"}},
{"BOOLEAN", &BoolColType{Name: "BOOLEAN"}},
{"SMALLINT", &IntColType{Name: "SMALLINT"}},
{"SMALLINT", &IntColType{Name: "SMALLINT", Width: 16, ImplicitWidth: true}},
{"BIGINT", &IntColType{Name: "BIGINT"}},
{"INTEGER", &IntColType{Name: "INTEGER"}},
{"INT", &IntColType{Name: "INT"}},
{"INT2", &IntColType{Name: "INT2", Width: 16, ImplicitWidth: true}},
{"INT4", &IntColType{Name: "INT4", Width: 32, ImplicitWidth: true}},
{"INT8", &IntColType{Name: "INT8"}},
{"INT64", &IntColType{Name: "INT64"}},
{"REAL", &FloatColType{Name: "REAL"}},
{"DOUBLE PRECISION", &FloatColType{Name: "DOUBLE PRECISION"}},
{"FLOAT", &FloatColType{Name: "FLOAT"}},
{"FLOAT4", &FloatColType{Name: "FLOAT4", Prec: 24, Width: 16, PrecSpecified: true}},
{"FLOAT8", &FloatColType{Name: "FLOAT8", Prec: 24, Width: 32, PrecSpecified: true}},
{"FLOAT(4)", &FloatColType{Name: "FLOAT", Prec: 4, PrecSpecified: true}},
{"DEC", &DecimalColType{Name: "DEC"}},
{"DECIMAL", &DecimalColType{Name: "DECIMAL"}},
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/parser/keywords.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/sql/parser/reserved_keywords.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d6adb98

Please sign in to comment.