12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778 |
- # Copyright Materialize, Inc. and contributors. All rights reserved.
- #
- # Use of this software is governed by the Business Source License
- # included in the LICENSE file at the root of this repository.
- #
- # As of the Change Date specified in that file, in accordance with
- # the Business Source License, use of this software will be governed
- # by the Apache License, Version 2.0.
- mode cockroach
- statement ok
- CREATE TABLE t (a float, b int)
- statement ok
- INSERT INTO t VALUES (4.7, 2)
- # Test that all arithmetic and comparison operators coalesce their arguments.
- # The goal is not to test every possible combination of arguments, but just a
- # basic sanity check. An old version of the code forgot to include modulus in
- # list of operators that should coalesce their inputs.
- query RRRRRBBBBBB
- SELECT
- a + b,
- a - b,
- a * b,
- a / b,
- a % b,
- a < b,
- a <= b,
- a > b,
- a >= b,
- a = b,
- a <> b
- FROM t
- ----
- 6.7 2.7 9.4 2.35 0.7000000000000002 false false true true false true
- # Do not allow int4 text comparisons
- query error db error: ERROR: operator does not exist: text < integer
- SELECT 'foo'::text < 5::int;
- query error db error: ERROR: operator does not exist: integer < text
- SELECT 1 < ALL(VALUES(NULL))
- # But string *literals* can coerce to anything.
- query T
- SELECT '1' < 2
- ----
- true
- query T
- SELECT 'true' OR 'false'
- ----
- true
- # Int literals <=32::MAX/>= MIN are int4
- query T
- SELECT pg_typeof(100)
- ----
- integer
- query T
- SELECT pg_typeof(-100)
- ----
- integer
- query T
- SELECT pg_typeof(2147483647)
- ----
- integer
- query T
- SELECT pg_typeof(-2147483648)
- ----
- integer
- # Int literals that exceed i32 are int8
- query T
- SELECT pg_typeof(2147483648)
- ----
- bigint
- query T
- SELECT pg_typeof(-2147483649)
- ----
- bigint
- query T
- SELECT pg_typeof(9223372036854775807)
- ----
- bigint
- query T
- SELECT pg_typeof(-9223372036854775808)
- ----
- bigint
- # Int literals that exceed i64 are numeric
- query T
- SELECT pg_typeof(9223372036854775808)
- ----
- numeric
- query T
- SELECT pg_typeof(-9223372036854775809)
- ----
- numeric
- # Use comparison ops to check for type promotion
- # int2 promotes to int4
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1::smallint > 1;
- ----
- Map ((smallint_to_integer(integer_to_smallint(1)) > 1))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check int2 promotes to numeric
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1::smallint > 1.1;
- ----
- Map ((smallint_to_numeric(integer_to_smallint(1)) > 1.1))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check int8 promotes to numeric
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1::bigint > 1.11111
- ----
- Map ((bigint_to_numeric(integer_to_bigint(1)) > 1.11111))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check int8 promotes to double precision
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1::bigint > 1.11111::float
- ----
- Map ((bigint_to_double(integer_to_bigint(1)) > numeric_to_double(1.11111)))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check numeric promotes to double precision
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1.1 > 1::float;
- ----
- Map ((numeric_to_double(1.1) > integer_to_double(1)))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check numerics do not get promoted
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1.1 > 1.1
- ----
- Map ((1.1 > 1.1))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check floats do not get promoted
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 1::float > 1::float
- ----
- Map ((integer_to_double(1) > integer_to_double(1)))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Checks that float8 is preferred type for int4
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT floor(1);
- ----
- Map (floorf64(integer_to_double(1)))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Cannot implicitly cast int4 to string
- query error db error: ERROR: function char_length\(integer\) does not exist
- SELECT char_length(321);
- # Cannot implicitly cast double precision to numeric
- query error db error: ERROR: function round\(double precision, integer\) does not exist
- SELECT round(1.23::float, 1);
- # Check that float8 is the most common type
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT coalesce(1::int, 1::numeric, 1::float);
- ----
- Map (coalesce(integer_to_double(1), numeric_to_double(integer_to_numeric(1)), integer_to_double(1)))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Test bool->string explicit cast
- query T
- SELECT true::string;
- ----
- true
- # Test bool->string implicit cast
- query T
- SELECT concat(true);
- ----
- t
- # Check that date promotes to tstz
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT TIMESTAMPTZ '2001 03-04' - DATE '2002 03-04';
- ----
- Map ((text_to_timestamp_with_time_zone("2001 03-04") - date_to_timestamp_with_timezone(text_to_date("2002 03-04"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that ts promotes to tstz
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT TIMESTAMPTZ '2001 03-04' - TIMESTAMP '2002 03-04';
- ----
- Map ((text_to_timestamp_with_time_zone("2001 03-04") - timestamp_to_timestamp_with_time_zone(text_to_timestamp("2002 03-04"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that date promotes to ts
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT TIMESTAMP '2001 03-04' - DATE '2002 03-04';
- ----
- Map ((text_to_timestamp("2001 03-04") - date_to_timestamp(text_to_date("2002 03-04"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that tstz is most-common type
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT coalesce(DATE '2001', TIMESTAMP '2002', TIMESTAMPTZ '2003');
- ----
- Map (coalesce(date_to_timestamp_with_timezone(text_to_date("2001")), timestamp_to_timestamp_with_time_zone(text_to_timestamp("2002")), text_to_timestamp_with_time_zone("2003")))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that char casts to text for string functions
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT lower('a'::char);
- ----
- Map (lower(char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that "char" vs varchar promotes to text
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 'a'::"char" < 'a'::varchar;
- ----
- Map (("char"_to_text(text_to_"char"("a")) < varchar_to_text(text_to_varchar[len=unbounded]("a"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that "char" vs char promotes to text
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 'a'::"char" < 'a'::char;
- ----
- Map (("char"_to_text(text_to_"char"("a")) < char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that varchar promotes to char
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 'a'::char < 'a'::varchar;
- ----
- Map ((text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")) < text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that char promotes to text
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 'a'::char < 'a'::text;
- ----
- Map ((char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a"))) < "a"))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT 'a'::varchar < 'a'::text;
- ----
- Map ((varchar_to_text(text_to_varchar[len=unbounded]("a")) < "a"))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # Check that text is most-common type
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT coalesce('a'::char, 'a'::varchar, 'a'::text);
- ----
- Map (coalesce(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")), text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a")), text_to_char[len=unbounded]("a")))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT coalesce('a'::char, 'a'::text, 'a'::varchar);
- ----
- Map (coalesce(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")), text_to_char[len=unbounded]("a"), text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a"))))
- Constant
- - ()
- Target cluster: quickstart
- EOF
- query T
- SELECT 1::smallint::smallint;
- ----
- 1
- query T
- SELECT 1::smallint::bigint;
- ----
- 1
- query error CAST does not support casting from boolean to smallint
- SELECT TRUE::boolean::smallint
- query error CAST does not support casting from date to smallint
- SELECT '2001 02-03'::date::smallint
- query T
- SELECT 1.1::numeric::smallint;
- ----
- 1
- query T
- SELECT 1.2::double::smallint;
- ----
- 1
- query T
- SELECT 1.3::real::smallint;
- ----
- 1
- query T
- SELECT 2::int::smallint;
- ----
- 2
- query error CAST does not support casting from interval to smallint
- SELECT '1'::interval::smallint
- query error cannot cast jsonb object to type smallint
- SELECT '{}'::jsonb::smallint;
- query R
- SELECT '1'::jsonb::smallint;
- ----
- 1
- query R
- SELECT '1'::text::smallint
- ----
- 1
- query R
- SELECT '1'::char::smallint
- ----
- 1
- query R
- SELECT '1'::varchar::smallint
- ----
- 1
- query error CAST does not support casting from time to smallint
- SELECT '01:02:03'::time::smallint
- query error CAST does not support casting from timestamp without time zone to smallint
- SELECT '2002 03-04'::timestamp::smallint
- query error CAST does not support casting from timestamp with time zone to smallint
- SELECT '2003 04-05'::timestamptz::smallint
- query error AST does not support casting from smallint to bool
- SELECT 1::smallint::boolean;
- query T
- SELECT 1::bigint::smallint;
- ----
- 1
- query T
- SELECT 1::bigint::bigint;
- ----
- 1
- query T
- SELECT TRUE::boolean::bigint
- ----
- 1
- query error CAST does not support casting from date to bigint
- SELECT '2001 02-03'::date::bigint
- query T
- SELECT 1.1::numeric::bigint;
- ----
- 1
- query T
- SELECT 1.2::double::bigint;
- ----
- 1
- query T
- SELECT 1.3::real::bigint;
- ----
- 1
- query T
- SELECT 2::int::bigint;
- ----
- 2
- query error CAST does not support casting from interval to bigint
- SELECT '1'::interval::bigint
- query error cannot cast jsonb object to type bigint
- SELECT '{}'::jsonb::bigint;
- query R
- SELECT '1'::jsonb::bigint;
- ----
- 1
- query R
- SELECT '1'::text::bigint
- ----
- 1
- query R
- SELECT '1'::char::bigint
- ----
- 1
- query R
- SELECT '1'::varchar::bigint
- ----
- 1
- query error CAST does not support casting from time to bigint
- SELECT '01:02:03'::time::bigint
- query error CAST does not support casting from timestamp without time zone to bigint
- SELECT '2002 03-04'::timestamp::bigint
- query error CAST does not support casting from timestamp with time zone to bigint
- SELECT '2003 04-05'::timestamptz::bigint
- query T
- SELECT 1::bigint::boolean;
- ----
- true
- query T
- SELECT TRUE::boolean::boolean;
- ----
- true
- query error CAST does not support casting from date to boolean
- SELECT '2001 02-03'::date::boolean
- query error CAST does not support casting from numeric to boolean
- SELECT 1.1::numeric::boolean
- query error CAST does not support casting from double precision to boolean
- SELECT 1.2::double::boolean
- query error CAST does not support casting from real to boolean
- SELECT 1.3::real::boolean
- query T
- SELECT 2::int::boolean;
- ----
- true
- query error CAST does not support casting from interval to boolean
- SELECT '1'::interval::boolean
- query error cannot cast jsonb object to type boolean
- SELECT '{}'::jsonb::boolean;
- query error cannot cast jsonb number to type boolean
- SELECT '1'::jsonb::boolean;
- query T
- SELECT 'true'::text::boolean
- ----
- true
- query T
- SELECT 'true'::char(4)::boolean
- ----
- true
- query T
- SELECT 'true'::varchar::boolean
- ----
- true
- query error CAST does not support casting from time to boolean
- SELECT '01:02:03'::time::boolean
- query error CAST does not support casting from timestamp without time zone to boolean
- SELECT '2002 03-04'::timestamp::boolean
- query error CAST does not support casting from timestamp with time zone to boolean
- SELECT '2003 04-05'::timestamptz::boolean
- query error CAST does not support casting from smallint to date
- SELECT 1::smallint::date
- query error CAST does not support casting from bigint to date
- SELECT 1::bigint::date
- query error CAST does not support casting from boolean to date
- SELECT TRUE::boolean::date
- query T
- SELECT '2001 02-03'::date::date;
- ----
- 2001-02-03
- query error CAST does not support casting from numeric to date
- SELECT 1.1::numeric::date
- query error CAST does not support casting from double precision to date
- SELECT 1.2::double::date
- query error CAST does not support casting from real to date
- SELECT 1.3::real::date
- query error CAST does not support casting from integer to date
- SELECT 2::int::date
- query error CAST does not support casting from interval to date
- SELECT '1'::interval::date
- query error CAST does not support casting from jsonb to date
- SELECT '{}'::jsonb::date
- query T
- SELECT '2001 02-03'::text::date
- ----
- 2001-02-03
- query T
- SELECT '2001 02-03'::char(11)::date
- ----
- 2001-02-03
- query T
- SELECT '2001 02-03'::varchar::date
- ----
- 2001-02-03
- query error CAST does not support casting from time to date
- SELECT '01:02:03'::time::date
- query T
- SELECT '2002 03-04'::timestamp::date;
- ----
- 2002-03-04
- query T
- SELECT '2003 04-05'::timestamptz::date;
- ----
- 2003-04-05
- query R
- SELECT 1::smallint::numeric;
- ----
- 1
- query R
- SELECT 1::bigint::numeric;
- ----
- 1
- query error CAST does not support casting from boolean to numeric
- SELECT TRUE::boolean::numeric
- query error CAST does not support casting from date to numeric
- SELECT '2001 02-03'::date::numeric
- query R
- SELECT 1.1::numeric::numeric;
- ----
- 1.1
- query R
- SELECT 1.2::double::numeric;
- ----
- 1.2
- query R
- SELECT 1.3::real::numeric;
- ----
- 1.3
- query R
- SELECT 2::int::numeric;
- ----
- 2
- query error CAST does not support casting from interval to numeric
- SELECT '1'::interval::numeric
- query error cannot cast jsonb object to type numeric
- SELECT '{}'::jsonb::numeric;
- query R
- SELECT '1'::jsonb::numeric;
- ----
- 1
- query R
- SELECT '1'::text::numeric
- ----
- 1
- query R
- SELECT '1'::char::numeric
- ----
- 1
- query R
- SELECT '1'::varchar::numeric
- ----
- 1
- query error CAST does not support casting from time to numeric
- SELECT '01:02:03'::time::numeric
- query error CAST does not support casting from timestamp without time zone to numeric
- SELECT '2002 03-04'::timestamp::numeric
- query error CAST does not support casting from timestamp with time zone to numeric
- SELECT '2003 04-05'::timestamptz::numeric
- query T
- SELECT 1::smallint::double;
- ----
- 1.000
- query T
- SELECT 1::bigint::double;
- ----
- 1.000
- query error CAST does not support casting from boolean to double precision
- SELECT TRUE::boolean::double
- query error CAST does not support casting from date to double precision
- SELECT '2001 02-03'::date::double
- query T
- SELECT 1.1::numeric::double;
- ----
- 1.100
- query T
- SELECT 1.2::double::double;
- ----
- 1.200
- query T
- SELECT 1.3::real::double;
- ----
- 1.300
- query T
- SELECT 2::int::double;
- ----
- 2.000
- query error CAST does not support casting from interval to double precision
- SELECT '1'::interval::double
- query error cannot cast jsonb object to type double precision
- SELECT '{}'::jsonb::double;
- query T
- SELECT '1'::jsonb::double;
- ----
- 1.000
- query R
- SELECT '1'::text::double
- ----
- 1
- query R
- SELECT '1'::char::double
- ----
- 1
- query R
- SELECT '1'::varchar::double
- ----
- 1
- query error CAST does not support casting from time to double precision
- SELECT '01:02:03'::time::double
- query error CAST does not support casting from timestamp without time zone to double precision
- SELECT '2002 03-04'::timestamp::double
- query error CAST does not support casting from timestamp with time zone to double precision
- SELECT '2003 04-05'::timestamptz::double
- query T
- SELECT 1::smallint::real;
- ----
- 1.000
- query T
- SELECT 1::bigint::real;
- ----
- 1.000
- query error CAST does not support casting from boolean to real
- SELECT TRUE::boolean::real
- query error CAST does not support casting from date to real
- SELECT '2001 02-03'::date::real
- query T
- SELECT 1.1::numeric::real;
- ----
- 1.100
- query T
- SELECT 1.2::double::real;
- ----
- 1.200
- query T
- SELECT 1.3::real::real;
- ----
- 1.300
- query T
- SELECT 2::int::real;
- ----
- 2.000
- query error CAST does not support casting from interval to real
- SELECT '1'::interval::real
- query error cannot cast jsonb object to type real
- SELECT '{}'::jsonb::real;
- query R
- SELECT '2'::jsonb::real;
- ----
- 2
- query R
- SELECT '1'::text::real
- ----
- 1
- query R
- SELECT '1'::char::real
- ----
- 1
- query R
- SELECT '1'::varchar::real
- ----
- 1
- query error CAST does not support casting from time to real
- SELECT '01:02:03'::time::real
- query error CAST does not support casting from timestamp without time zone to real
- SELECT '2002 03-04'::timestamp::real
- query error CAST does not support casting from timestamp with time zone to real
- SELECT '2003 04-05'::timestamptz::real
- query T
- SELECT 1::smallint::integer;
- ----
- 1
- query T
- SELECT 1::bigint::integer;
- ----
- 1
- query error CAST does not support casting from date to integer
- SELECT '2001 02-03'::date::integer
- query T
- SELECT 1.1::numeric::integer;
- ----
- 1
- query T
- SELECT 1.2::double::integer;
- ----
- 1
- query T
- SELECT 1.3::real::integer;
- ----
- 1
- query T
- SELECT 2::int::integer;
- ----
- 2
- query error CAST does not support casting from interval to integer
- SELECT '1'::interval::integer
- query error cannot cast jsonb object to type integer
- SELECT '{}'::jsonb::integer;
- query R
- SELECT '1'::jsonb::integer;
- ----
- 1
- query R
- SELECT '1'::text::integer
- ----
- 1
- query R
- SELECT '1'::char::integer
- ----
- 1
- query R
- SELECT '1'::varchar::integer
- ----
- 1
- query error CAST does not support casting from time to integer
- SELECT '01:02:03'::time::integer
- query error CAST does not support casting from timestamp without time zone to integer
- SELECT '2002 03-04'::timestamp::integer
- query error CAST does not support casting from timestamp with time zone to integer
- SELECT '2003 04-05'::timestamptz::integer
- query error CAST does not support casting from smallint to interval
- SELECT 1::smallint::interval
- query error CAST does not support casting from bigint to interval
- SELECT 1::bigint::interval
- query error CAST does not support casting from boolean to interval
- SELECT TRUE::boolean::interval
- query error CAST does not support casting from date to interval
- SELECT '2001 02-03'::date::interval
- query error CAST does not support casting from numeric to interval
- SELECT 1.1::numeric::interval
- query error CAST does not support casting from double precision to interval
- SELECT 1.2::double::interval
- query error CAST does not support casting from real to interval
- SELECT 1.3::real::interval
- query error CAST does not support casting from integer to interval
- SELECT 2::int::interval
- query T
- SELECT '1'::interval::interval;
- ----
- 00:00:01
- query error CAST does not support casting from jsonb to interval
- SELECT '{}'::jsonb::interval
- query T
- SELECT '1'::text::interval
- ----
- 00:00:01
- query T
- SELECT '1'::char::interval
- ----
- 00:00:01
- query T
- SELECT '1'::varchar::interval
- ----
- 00:00:01
- query T
- SELECT '01:02:03'::time::interval;
- ----
- 01:02:03
- query error CAST does not support casting from timestamp without time zone to interval
- SELECT '2002 03-04'::timestamp::interval
- query error CAST does not support casting from timestamp with time zone to interval
- SELECT '2003 04-05'::timestamptz::interval
- query error CAST does not support casting from smallint to jsonb
- SELECT 1::smallint::jsonb
- query error CAST does not support casting from bigint to jsonb
- SELECT 1::bigint::jsonb
- #pginvalid
- query error CAST does not support casting from boolean to jsonb
- SELECT TRUE::boolean::jsonb;
- query error CAST does not support casting from date to jsonb
- SELECT '2001 02-03'::date::jsonb
- query error CAST does not support casting from numeric to jsonb
- SELECT 1.1::numeric::jsonb
- query error CAST does not support casting from double precision to jsonb
- SELECT 1.2::double::jsonb;
- query error CAST does not support casting from integer to jsonb
- SELECT 2::int::jsonb;
- query error CAST does not support casting from interval to jsonb
- SELECT '1'::interval::jsonb
- query T
- SELECT '{}'::jsonb::jsonb;
- ----
- {}
- query T
- SELECT '1'::jsonb::jsonb;
- ----
- 1
- query error CAST does not support casting from time to jsonb
- SELECT '01:02:03'::time::jsonb
- query error CAST does not support casting from timestamp without time zone to jsonb
- SELECT '2002 03-04'::timestamp::jsonb
- query error CAST does not support casting from timestamp with time zone to jsonb
- SELECT '2003 04-05'::timestamptz::jsonb
- query T
- SELECT 1::smallint::text;
- ----
- 1
- query T
- SELECT 1::bigint::text;
- ----
- 1
- query T
- SELECT TRUE::boolean::text;
- ----
- true
- query T
- SELECT '2001 02-03'::date::text;
- ----
- 2001-02-03
- query T
- SELECT 1.1::numeric::text;
- ----
- 1.1
- query T
- SELECT 1.2::double::text;
- ----
- 1.2
- query T
- SELECT 1.3::real::text;
- ----
- 1.3
- query T
- SELECT 2::int::text;
- ----
- 2
- query T
- SELECT '1'::interval::text;
- ----
- 00:00:01
- query T
- SELECT '{}'::jsonb::text;
- ----
- {}
- query T
- SELECT '1'::jsonb::text;
- ----
- 1
- query T
- SELECT 'dog'::text::text;
- ----
- dog
- query T
- SELECT 'dog'::char(3)::text;
- ----
- dog
- query T
- SELECT 'dog'::varchar::text;
- ----
- dog
- query T
- SELECT '01:02:03'::time::text;
- ----
- 01:02:03
- query T
- SELECT '2002 03-04'::timestamp::text;
- ----
- 2002-03-04 00:00:00
- query T
- SELECT '2003 04-05'::timestamptz::text;
- ----
- 2003-04-05 00:00:00+00
- query T
- SELECT trim(trailing from 1::bigint::char(20));
- ----
- 1
- query T
- SELECT trim(trailing from TRUE::boolean::char(20));
- ----
- true
- query T
- SELECT trim(trailing from '2001 02-03'::date::char(20));
- ----
- 2001-02-03
- query T
- SELECT trim(trailing from 1.1::numeric::char(20));
- ----
- 1.1
- query T
- SELECT trim(trailing from 1.2::double::char(20));
- ----
- 1.2
- query T
- SELECT trim(trailing from 1.3::real::char(20));
- ----
- 1.3
- query T
- SELECT trim(trailing from 2::int::char(20));
- ----
- 2
- query T
- SELECT trim(trailing from '1'::interval::char(20));
- ----
- 00:00:01
- query T
- SELECT trim(trailing from '{}'::jsonb::char(20));
- ----
- {}
- query T
- SELECT trim(trailing from '1'::jsonb::char(20));
- ----
- 1
- query T
- SELECT trim(trailing from 'dog'::text::char(20));
- ----
- dog
- query T
- SELECT trim(trailing from 'dog'::char(3)::char(20));
- ----
- dog
- query T
- SELECT trim(trailing from 'dog'::varchar::char(20));
- ----
- dog
- query T
- SELECT trim(trailing from '01:02:03'::time::char(20));
- ----
- 01:02:03
- query T
- SELECT trim(trailing from '2002 03-04'::timestamp::char(20));
- ----
- 2002-03-04 00:00:00
- query T
- SELECT trim(trailing from '2003 04-05'::timestamptz::char(20));
- ----
- 2003-04-05 00:00:00+
- query T
- SELECT 1::smallint::varchar;
- ----
- 1
- query T
- SELECT 1::bigint::varchar;
- ----
- 1
- query T
- SELECT TRUE::boolean::varchar;
- ----
- true
- query T
- SELECT '2001 02-03'::date::varchar;
- ----
- 2001-02-03
- query T
- SELECT 1.1::numeric::varchar;
- ----
- 1.1
- query T
- SELECT 1.2::double::varchar;
- ----
- 1.2
- query T
- SELECT 1.3::real::varchar;
- ----
- 1.3
- query T
- SELECT 2::int::varchar;
- ----
- 2
- query T
- SELECT '1'::interval::varchar;
- ----
- 00:00:01
- query T
- SELECT '{}'::jsonb::varchar;
- ----
- {}
- query T
- SELECT '1'::jsonb::varchar;
- ----
- 1
- query T
- SELECT 'dog'::text::varchar;
- ----
- dog
- query T
- SELECT 'dog'::char(3)::varchar;
- ----
- dog
- query T
- SELECT 'dog'::varchar::varchar;
- ----
- dog
- query T
- SELECT '01:02:03'::time::varchar;
- ----
- 01:02:03
- query T
- SELECT '2002 03-04'::timestamp::varchar;
- ----
- 2002-03-04 00:00:00
- query T
- SELECT '2003 04-05'::timestamptz::varchar;
- ----
- 2003-04-05 00:00:00+00
- query error CAST does not support casting from smallint to time
- SELECT 1::smallint::time
- query error CAST does not support casting from bigint to time
- SELECT 1::bigint::time
- query error CAST does not support casting from boolean to time
- SELECT TRUE::boolean::time
- query error CAST does not support casting from date to time
- SELECT '2001 02-03'::date::time
- query error CAST does not support casting from numeric to time
- SELECT 1.1::numeric::time
- query error CAST does not support casting from double precision to time
- SELECT 1.2::double::time
- query error CAST does not support casting from real to time
- SELECT 1.3::real::time
- query error CAST does not support casting from integer to time
- SELECT 2::int::time
- query T
- SELECT '1'::interval::time;
- ----
- 00:00:01
- query error CAST does not support casting from jsonb to time
- SELECT '{}'::jsonb::time
- query T
- SELECT '01:02:03'::text::time
- ----
- 01:02:03
- query T
- SELECT '01:02:03'::char(9)::time
- ----
- 01:02:03
- query T
- SELECT '01:02:03'::varchar::time
- ----
- 01:02:03
- query T
- SELECT '01:02:03'::time::time;
- ----
- 01:02:03
- query T
- SELECT '2002 03-04'::timestamp::time
- ----
- 00:00:00
- query T
- SELECT '2002 03-04 05:06:07'::timestamp::time
- ----
- 05:06:07
- query T
- SELECT '2003 04-05'::timestamptz::time
- ----
- 00:00:00
- query T
- SELECT '2003 04-05 06:07:08+00'::timestamptz::time
- ----
- 06:07:08
- query error CAST does not support casting from smallint to timestamp
- SELECT 1::smallint::timestamp
- query error CAST does not support casting from bigint to timestamp
- SELECT 1::bigint::timestamp
- query error CAST does not support casting from boolean to timestamp
- SELECT TRUE::boolean::timestamp
- query T
- SELECT '2001 02-03'::date::timestamp;
- ----
- 2001-02-03 00:00:00
- query error CAST does not support casting from numeric to timestamp
- SELECT 1.1::numeric::timestamp
- query error CAST does not support casting from double precision to timestamp
- SELECT 1.2::double::timestamp
- query error CAST does not support casting from real to timestamp
- SELECT 1.3::real::timestamp
- query error CAST does not support casting from integer to timestamp
- SELECT 2::int::timestamp
- query error CAST does not support casting from interval to timestamp
- SELECT '1'::interval::timestamp
- query error CAST does not support casting from jsonb to timestamp
- SELECT '{}'::jsonb::timestamp
- query T
- SELECT '2001 02-03'::text::timestamp
- ----
- 2001-02-03 00:00:00
- query T
- SELECT '2001 02-03'::char(10)::timestamp
- ----
- 2001-02-03 00:00:00
- query T
- SELECT '2001 02-03'::varchar::timestamp
- ----
- 2001-02-03 00:00:00
- query error CAST does not support casting from time to timestamp
- SELECT '01:02:03'::time::timestamp
- query T
- SELECT '2002 03-04'::timestamp::timestamp;
- ----
- 2002-03-04 00:00:00
- query T
- SELECT '2003 04-05'::timestamptz::timestamp;
- ----
- 2003-04-05 00:00:00
- query error CAST does not support casting from smallint to timestamp with time zone
- SELECT 1::smallint::timestamptz
- query error CAST does not support casting from bigint to timestamp with time zone
- SELECT 1::bigint::timestamptz
- query error CAST does not support casting from boolean to timestamp with time zone
- SELECT TRUE::boolean::timestamptz
- query T
- SELECT '2001 02-03'::date::timestamptz;
- ----
- 2001-02-03 00:00:00+00
- query error CAST does not support casting from numeric to timestamp with time zone
- SELECT 1.1::numeric::timestamptz
- query error CAST does not support casting from double precision to timestamp with time zone
- SELECT 1.2::double::timestamptz
- query error CAST does not support casting from real to timestamp with time zone
- SELECT 1.3::real::timestamptz
- query error CAST does not support casting from integer to timestamp with time zone
- SELECT 2::int::timestamptz
- query error CAST does not support casting from interval to timestamp with time zone
- SELECT '1'::interval::timestamptz
- query error CAST does not support casting from jsonb to timestamp with time zone
- SELECT '{}'::jsonb::timestamptz
- query T
- SELECT '2001 02-03'::text::timestamptz
- ----
- 2001-02-03 00:00:00+00
- query T
- SELECT '2001 02-03'::char(10)::timestamptz
- ----
- 2001-02-03 00:00:00+00
- query T
- SELECT '2001 02-03'::varchar::timestamptz
- ----
- 2001-02-03 00:00:00+00
- query error CAST does not support casting from time to timestamp with time zone
- SELECT '01:02:03'::time::timestamptz
- query T
- SELECT '2002 03-04'::timestamp::timestamptz;
- ----
- 2002-03-04 00:00:00+00
- query T
- SELECT '2003 04-05'::timestamptz::timestamptz;
- ----
- 2003-04-05 00:00:00+00
- query T
- SELECT 14::smallint::oid;
- ----
- 14
- query T
- SELECT 14::bigint::oid;
- ----
- 14
- query T
- SELECT 14::oid::bigint;
- ----
- 14
- query T
- SELECT 14::oid = 14::bigint;
- ----
- true
- query T
- SELECT 14::oid = 14::smallint;
- ----
- true
- query error "120129019392" OID out of range
- SELECT 120129019392::bigint::oid;
- # Check that our non-linear type promotion structure works as intended.
- # Whenever you have a uint and an int, you unify their types by selecting the
- # narrowest integer-like that the uint can be implicitly cast to. In the case
- # of 64-bit uints, this means casting the values to numeric.
- query TT
- SELECT pg_typeof(1::smallint + 1::uint2), 1::smallint + 1::uint2;
- ----
- integer 2
- query TT
- SELECT pg_typeof(1::smallint + 1::uint4), 1::smallint + 1::uint4;
- ----
- bigint 2
- query TT
- SELECT pg_typeof(1::smallint + 1::uint8), 1::smallint + 1::uint8;
- ----
- numeric 2
- query TT
- SELECT pg_typeof(1::integer + 1::uint2), 1::integer + 1::uint2;
- ----
- integer 2
- query TT
- SELECT pg_typeof(1::integer + 1::uint4), 1::integer + 1::uint4;
- ----
- bigint 2
- query TT
- SELECT pg_typeof(1::integer + 1::uint8), 1::integer + 1::uint8;
- ----
- numeric 2
- query TT
- SELECT pg_typeof(1::bigint + 1::uint2), 1::bigint + 1::uint2;
- ----
- bigint 2
- query TT
- SELECT pg_typeof(1::bigint + 1::uint4), 1::bigint + 1::uint4;
- ----
- bigint 2
- query TT
- SELECT pg_typeof(1::bigint + 1::uint8), 1::bigint + 1::uint8;
- ----
- numeric 2
- query T
- SELECT 9223372036854775807::bigint / 18446744073709551615::uint8;
- ----
- 0.499999999999999999972894945687862389148
- query T
- SELECT 18446744073709551615::uint8 / 9223372036854775807::bigint;
- ----
- 2.00000000000000000010842021724855044341
- query T
- select pg_typeof(coalesce(1::uint4, 1::int));
- ----
- bigint
- query T
- select pg_typeof(coalesce(1::uint4, 1::int, 1::numeric));
- ----
- numeric
- query T
- select pg_typeof(coalesce(1::uint8, 1::int));
- ----
- numeric
- query T
- SELECT pg_typeof(coalesce(1::uint4, 1::int, 1::numeric, 1::double));
- ----
- double precision
- # These types were not converted because the function's parameters are
- # polymorphic without enfocing a relationship between the arguments, and we only
- # aggressively cast ints and uints to a wider type in cases where it exactly
- # selects a function.
- query T multiline
- EXPLAIN RAW PLAN FOR
- SELECT jsonb_object_agg(1::uint8, 2::int);
- ----
- Reduce aggregates=[jsonb_object_agg[order_by=[]](row(row(uint8_to_text(integer_to_uint8(1)), coalesce(jsonbable_to_jsonb(integer_to_numeric(2)), json_null))))]
- Constant
- - ()
- Target cluster: quickstart
- EOF
- # This would fail to plan if we blithely unified all args into the best common
- # int/uint; the second argument to lag does not support numeric types.
- query T
- WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
- SELECT lag(f1, 1::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
- FROM t
- ----
- NULL
- query T
- WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
- SELECT pg_typeof(lag(f1, 1::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1))
- FROM t
- ----
- uint8
- # However, polymorphic type resolution can still appropriately upcast values,
- # while leaving other types alone.
- query T
- WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
- SELECT lag(f1, 1::int, 0::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
- FROM t
- ----
- 0
- query T
- WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
- SELECT pg_typeof(lag(f1, 1::int, 0::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1))
- FROM t
- ----
- numeric
- # implicit timestamp_to_timestamptz cast
- query T
- values ('2023-01-01T00:00:00.666'::timestamp(6)) union all values ('2023-01-01T00:00:00.666666'::timestamptz(3));
- ----
- 2023-01-01 00:00:00.666+00
- 2023-01-01 00:00:00.667+00
- query error numeric field overflow
- SELECT '1e-307'::float8::numeric
- statement ok
- CREATE TABLE t1(a varchar(3));
- statement ok
- CREATE TABLE t2(b varchar(4));
- statement error db error: ERROR: value too long for type character varying\(3\)
- INSERT INTO t1 VALUES ('123456');
- # From the Postgres docs: https://www.postgresql.org/docs/current/datatype-character.html
- # "An attempt to store a longer string into a column of these types will result in an error, unless the excess
- # characters are all spaces, in which case the string will be truncated to the maximum length. (This somewhat bizarre
- # exception is required by the SQL standard.)"
- statement ok
- INSERT INTO t1 VALUES ('111 ');
- statement ok
- INSERT INTO t1 VALUES ('123');
- statement ok
- INSERT INTO t2 VALUES ('1234');
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
- (SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
- ----
- Explained Query:
- Union // { types: "(varchar?)" }
- ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
- ReadStorage materialize.public.t2 // { types: "(varchar(4)?)" }
- Source materialize.public.t1
- Source materialize.public.t2
- Target cluster: quickstart
- EOF
- query T
- (SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
- ----
- 111
- 123
- 1234
- query T
- (SELECT * FROM t2) UNION ALL (SELECT * FROM t1);
- ----
- 111
- 123
- 1234
- statement ok
- CREATE TABLE t3(b char(2));
- statement ok
- INSERT INTO t3 VALUES ('ab');
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
- (SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
- ----
- Explained Query:
- Union // { types: "(varchar?)" }
- ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
- Project (#1) // { types: "(varchar?)" }
- Map (text_to_varchar[len=unbounded](#0)) // { types: "(char(2)?, varchar?)" }
- ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
- Source materialize.public.t1
- Source materialize.public.t3
- Target cluster: quickstart
- EOF
- query T rowsort
- (SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
- ----
- ab
- 111
- 123
- statement ok
- CREATE TABLE t4(b char(3));
- statement ok
- INSERT INTO t4 VALUES ('ab ');
- query T multiline
- EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
- (SELECT * FROM t3) UNION (SELECT * FROM t4);
- ----
- Explained Query:
- Distinct project=[#0] // { types: "(char?)" }
- Union // { types: "(char?)" }
- ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
- ReadStorage materialize.public.t4 // { types: "(char(3)?)" }
- Source materialize.public.t3
- Source materialize.public.t4
- Target cluster: quickstart
- EOF
- # Trailing spaces are treated as semantically insignificant in both Postgres and Materialize, so the above
- # 'ab' and 'ab ' end up deduplicated.
- # See also in char-varchar-distinct.td
- query T
- (SELECT * FROM t3) UNION (SELECT * FROM t4);
- ----
- ab
|