Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: Adding support for int2, int8, float4 and float8 to be in line with postgres support of numerical types. #16720

Merged
merged 1 commit into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -1134,3 +1134,13 @@ query I
SELECT COUNT(*) FROM pg_catalog.pg_index WHERE indkey[0] IS NULL;
----
0

## TODO(masha): #16769
#statement ok
#CREATE TABLE types(a int8, b int2);

#query I
#SELECT attname, atttypid, typname FROM pg_attribute a JOIN pg_type t ON a.atttypid=t.oid WHERE attrelid = 'types'::REGCLASS;
#attname atttypid typname
#a 20 int8
#b 20 int2
63 changes: 61 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/scale
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,74 @@ INSERT INTO tb VALUES (3)
statement ok
INSERT INTO tb VALUES (7)

statement error bit string too long for type BIT\(3\) \(column "b"\)
statement error integer out of range for type BIT \(column "b"\)
INSERT INTO tb VALUES (15)

statement ok
UPDATE tb SET b = 2 WHERE b = 7

statement error bit string too long for type BIT\(3\) \(column "b"\)
statement error integer out of range for type BIT \(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)

# Note that neither of these value are INT2, but we only check
# on insert and update, not mathematical operations
statement ok
INSERT INTO tc VALUES (60000-59999)

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

statement error integer out of range for type SMALLINT \(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 SMALLINT \(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 INTEGER \(column "b"\)
INSERT INTO tc1 VALUES (-2147483649)

statement error integer out of range for type INTEGER \(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 INTEGER \(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 REAL true NULL {}
cfloat8 DOUBLE PRECISION true NULL {}
cint INT true NULL {}
cint2 SMALLINT true NULL {}
cint4 INTEGER true NULL {}
cint64 BIGINT true NULL {}
cint8 BIGINT true NULL {}
cinteger INTEGER true NULL {}
cinterval INTERVAL true NULL {}
cnumeric DECIMAL true NULL {}
cnumeric1 DECIMAL(1) true NULL {}
cnumeric21 DECIMAL(2,1) true NULL {}
creal REAL 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
29 changes: 17 additions & 12 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 @@ -139,15 +141,18 @@ func (node *IntColType) IsSerial() bool {

// Pre-allocated immutable float column types.
var (
floatColTypeReal = &FloatColType{Name: "REAL"}
floatColTypeFloat = &FloatColType{Name: "FLOAT"}
floatColTypeDouble = &FloatColType{Name: "DOUBLE PRECISION"}
floatColTypeReal = &FloatColType{Name: "REAL", Width: 32}
floatColTypeFloat = &FloatColType{Name: "FLOAT", Width: 64}
floatColTypeFloat4 = &FloatColType{Name: "FLOAT4", Width: 32}
floatColTypeFloat8 = &FloatColType{Name: "FLOAT8", Width: 64}
floatColTypeDouble = &FloatColType{Name: "DOUBLE PRECISION", Width: 64}
)

// 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 @@ -157,7 +162,7 @@ func NewFloatColType(prec int, precSpecified bool) *FloatColType {
if prec == 0 && !precSpecified {
return floatColTypeFloat
}
return &FloatColType{Name: "FLOAT", Prec: prec, PrecSpecified: precSpecified}
return &FloatColType{Name: "FLOAT", Width: 64, Prec: prec, PrecSpecified: precSpecified}
}

// Format implements the NodeFormatter interface.
Expand Down
18 changes: 11 additions & 7 deletions pkg/sql/parser/col_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ 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"}},
{"FLOAT(4)", &FloatColType{Name: "FLOAT", Prec: 4, PrecSpecified: true}},
{"REAL", &FloatColType{Name: "REAL", Width: 32}},
{"DOUBLE PRECISION", &FloatColType{Name: "DOUBLE PRECISION", Width: 64}},
{"FLOAT", &FloatColType{Name: "FLOAT", Width: 64}},
{"FLOAT4", &FloatColType{Name: "FLOAT4", Width: 32}},
{"FLOAT8", &FloatColType{Name: "FLOAT8", Width: 64}},
{"FLOAT(4)", &FloatColType{Name: "FLOAT", Width: 64, Prec: 4, PrecSpecified: true}},
{"DEC", &DecimalColType{Name: "DEC"}},
{"DECIMAL", &DecimalColType{Name: "DECIMAL"}},
{"NUMERIC", &DecimalColType{Name: "NUMERIC"}},
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.

Loading