SlideShare a Scribd company logo
www.firebase.com.br 1 © 2014 – Carlos H. Cantu 
Understanding Numbers in Firebird 
Carlos H. Cantuwww.firebase.com.br 
www.firebirdnews.org
www.firebase.com.br 2 © 2014 – Carlos H. Cantu 
Who am I? 
• 
Maintainer of www.firebase.com.br and www.firebirdnews.org 
• 
Author of 2 Firebird books published in Brazil 
• 
Software developer for about 30 years 
• 
Organizer of the Firebird Developers Day conference 
• 
Firebird consultant
www.firebase.com.br 3 © 2014 – Carlos H. Cantu 
Do you wannago crazy?!
www.firebase.com.br 4 © 2014 – Carlos H. Cantu 
Warnings! 
1. 
Internal storage type depends on database dialect 
2. 
The dialect has influence in the precision of some types and in the results of calculations 
3. 
Depending on the datatypeused, the retrieved value can be different from the original value!! 
4. 
Decimal separator is always the dot “.”
www.firebase.com.br 5 © 2014 – Carlos H. Cantu 
INTEGER types 
• 
SMALLINT 
– 
16 bits 
– 
between -32.768 and32.767 
• 
INTEGER 
– 
32 bits 
– 
between -2.147.483.648 and 2.147.483.647 
• 
BIGINT 
– 
64 bits 
– 
between -9.223.372.036.854.775.808 and 9.223.372.036.854.775.807 
– 
Only available in dialect 3
www.firebase.com.br 6 © 2014 – Carlos H. Cantu 
FLOATING POINT types 
• 
FLOAT 
– 
32 bits: 1 for signal, 8 for exponent and 23 for the mantissa. 
– 
7 digits of precision 
– 
Between -3.4 x 1038and 3.4 x 1038 
• 
DOUBLE PRECISION 
– 
64 bits: 1 for signal, 11 for exponent and 52 for the mantissa. 
– 
15 digits of precision 
– 
Between -1.7 x 10308and 1.7 x 10308
www.firebase.com.br 7 © 2014 – Carlos H. Cantu 
Pros and cons of floating types 
• 
Stored following the standard defined by the IEEE (Institute of Electrical and Electronics Engineers), with an approximated representation of the real number. 
• 
Calculations uses the math co-processor (faster). 
• 
Not recommended due to lack of precision.
www.firebase.com.br 8 © 2014 – Carlos H. Cantu 
Accuracy in FLOAT/DOUBLE (1/2) 
SQL> select cast(1234567.1234 as float) from rdb$database; 
CAST 
============== 
1234567.1 
Result displayed by IBExpert: 
1234567.125
www.firebase.com.br 9 © 2014 – Carlos H. Cantu 
Imprecision in FLOAT/DOUBLE (2/2) 
SQL> select cast(1234567.4321 as float) from rdb$database; 
CAST 
============== 
1234567.4 
Result displayed by IBExpert: 
1234567.375
www.firebase.com.br 10 © 2014 – Carlos H. Cantu 
Fixed point 
• 
NUMERIC (p,s) / DECIMAL (p,s) 
• 
Is stored occupying either 16, 32 or 64 bits 
• 
p= precision (total digits) [1 <= p <= 18] s= scale (number of digits after the “comma”) 
• 
smust be always lower or equal to p 
• 
If pand sis not informed, the internal type will be INTEGER 
• 
In FB, palways determinates the minimum number of stored digits (not follow the standard) 
• 
The retrieved value is always exactly equal to the original value!
www.firebase.com.br 11 © 2014 – Carlos H. Cantu 
Internal storage of NUMERIC and DECIMAL 
PRECISION 
INTERNAL TYPE 
DIALECT 3 
DIALECT 1 
1..4 
NUMERIC 
SMALLINT (*) 
SMALLINT 
1..4 
DECIMAL 
INTEGER (*) 
INTEGER 
5..9 
NUMERIC e DECIMAL 
INTEGER 
INTEGER 
10..18 
NUMERIC e DECIMAL 
BIGINT 
DOUBLE PRECISION(!) 
In Firebird, DECIMAL andNUMERICsare thesamething, ifp < 10. 
(*) In this case, the range of supported values are different compared to NUMERIC and DECIMAL
www.firebase.com.br 12 © 2014 – Carlos H. Cantu 
Determining the capacity of chosen numeric/decimals 
1. 
Check the internal type used depending on the precision (p) of the field. 
2. 
Check the range of values supported by the internal type. 
3. 
Divide the min and max values by 10s to know the effective range of accepted values for the field.
www.firebase.com.br 13 © 2014 – Carlos H. Cantu 
Determining the capacity of chosen numeric/decimals 
Example: 
1. 
NUMERIC (9,2) or DECIMAL (9,2) 
2. 
Internally stored as INTEGER 
3. 
Integer = -2.147.483.648 to 2.147.483.647 
4. 
As s = 2, divide by 102 
5. 
Accepted range for a field declared as NUMERIC/DECIMAL (9,2) = -21.474.836,48 to 21.474.836,47
www.firebase.com.br 14 © 2014 – Carlos H. Cantu 
Testing the limits of numeric/decimal 
SQL> select cast(-21474836.48 as numeric (9,2)), cast(-21474836.48 as decimal (9,2)) from rdb$database; 
CAST CAST 
============ ============ 
-21474836.48 -21474836.48 
SQL> select cast(-21474836.49as numeric (9,2)), cast(-21474836.49as decimal (9,2)) from rdb$database; 
CAST CAST 
============ ============ 
Statement failed, SQLSTATE = 22003 
arithmetic exception, numeric overflow, or string truncation 
-numeric value is out of range
www.firebase.com.br 15 © 2014 – Carlos H. Cantu 
Testing the limits of numeric/decimal 
SQL> select cast(32768 as decimal(4,0)) from rdb$database; --integer 
CAST 
============ 
32768 
SQL> select cast(32768 as numeric(4,0)) from rdb$database; --smallint 
CAST 
======= 
Statement failed, SQLSTATE = 22003 
arithmetic exception, numeric overflow, or string truncation 
-numeric value is out of range
www.firebase.com.br 16 © 2014 – Carlos H. Cantu 
Moving from dialect 1 to 3 
• 
Is there any field declared as NUMERIC or DECIMALwith p > 9? 
– 
No: there will be no problem at all 
– 
Yes: you may have problems! 
• 
NUMERIC and DECIMAL with p > 9 are stored as double precision in dialect 1 and the existing fields will stay like this if the DB is “migrated” to dialect 3 using gfix-sql_dialect3. 
• 
New fields declared as NUM/DEC with p > 9, created after the DB was converted to dialect 3 will use BIGINTinternally. 
• 
Recommended solution: create a new DB using a script and pump the data from old to new database.
www.firebase.com.br 17 © 2014 – Carlos H. Cantu 
Integer divisions 
• 
Dialect 1, dividing intby intresults in double precisionI.e.: 1/3 = 0,3333333333333 
• 
Dialect 3, divide intby intresults in integerI.e.: 1/3 = 0
www.firebase.com.br 18 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
• 
In dialect 1, the division will always return a double precision. 
• 
In dialect 3, the result will be a type withp= 18 and s= sum of the scales of the involved types. 
SQL> select cast(0.33 as numeric (9,2))/ cast (1 as numeric(9,2)) from rdb$database; 
DIVIDE 
===================== 
0.3300
www.firebase.com.br 19 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
SQL> select (3.00/1.00*3.5)*2.00as totalfrom rdb$database; 
TOTAL 
===================== 
21.0000000 
SQL> select (3.00/1.00/3.5)/2.00as totalfrom rdb$database; 
TOTAL 
===================== 
0.4285700
www.firebase.com.br 20 © 2014 – Carlos H. Cantu 
Division/Multiplication of fixed point numerics 
• 
There can be overflows, specially with calculations involving multiple arguments! 
select cast(1 as numeric(15,6))* cast(1 as numeric(9,8)) * 
cast(1 as numeric(15,5)) from rdb$database 
~ 1.000000* 1.00000000* 1.00000 
Integer overflow. The result of an integer operation caused the most significant bit of the result to carry.
www.firebase.com.br 21 © 2014 – Carlos H. Cantu 
Addition/Subtraction of fixed point numbers 
• 
Result will have sequal the biggest scale of the bigger member of the operation. 
• 
In dialect 1, result will always have p= 9 
• 
In dialect 3, result will always have p= 18 
SQL> select cast(1 as numeric(9,2)) + 
cast(2 as integer) from rdb$database; 
ADD 
===================== 
3.00 
SQL> select cast(0.5 as numeric(9,2)) – 
cast(1 as numeric(9,3)) from rdb$database; 
SUBTRACT 
===================== 
-0.500
www.firebase.com.br 22 © 2014 – Carlos H. Cantu 
Tips summary 
• 
Always create the database in dialect 3, and connect to it using the same dialect. 
• 
For “monetary” fields, choose numericor decimalto guarantee the accuracy. 
• 
When need to store numbers with variable scale (s), choose double precision. 
• 
To migrate a DB from dialect 1 to 3, prefers to PUMP the data instead of using gfix. 
• 
Take care with overflows in calculations involving numeric/decimal.
www.firebase.com.br 23 © 2014 – Carlos H. Cantu 
Curiosities 
INDEXES 
• 
Numbers are stored in keys as double precision (exception to the rule is BIGINT) 
• 
Pros: 
– 
For numeric/decimal, allows changing p or s without needing to reindex 
– 
For smallint/integer, allows converting between the types or to a type having a scale (s) without need to reindex 
• 
Obs: Due to lack of precision of the double precision, the search if done in an interval between the bigger previous value and the lower next value related to the searched value. 
GENERATORS 
• 
Dialect 1 = integer 
• 
Dialect 3 = bigint
www.firebase.com.br 24 © 2014 – Carlos H. Cantu 
Curiosities (do you wannago more crazy??) 
CHECK CONSTRAINTS and CLIENT DIALECTS 
The rules applied by a check constraint are based on the dialect used by the client connection in the time the constraint was created. 
Ex: check (int1 / int2) > 0.5 (rule created with dialect 1 connection) 
When connecting to the DB using dialect 3: 
Insert ... (int1, int2) values (2, 3); --Success! ~ 0.66666666 
Ex: check (int1 / int2) > 0.5 (rule created with dialect 3 connection) 
Insert ... (int1, int2) values (2, 3); --FAILURE! ~ 0
www.firebase.com.br 25 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
• 
Raising the scale means shortening the range of accepted values 
I.e.: 
numeric (9,2): range -21.474.836,48 to21.474.836,47 
numeric (9,3): range -2.147.483,648 to2.147.483,647 
This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2.
www.firebase.com.br 26 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
• 
Changing the scale of (9,2) to 3. 
• 
Solutions: 
– 
Create new field declared as (9,3) 
– 
Copy the values to the new field 
– 
Drop the old field 
– 
Rename the new field as the old one 
• 
Changing to (10,3) 
– 
Problem if there are indexes defined for that field, since the internal type changes to bigint!
www.firebase.com.br 27 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
create table test (afield numeric (9,2)); 
commit; 
insert into test values (10.12); commit; alter table test alter afield type numeric (9,3); 
This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2. 
alter table test alter afield type numeric (10,3); commit; 
update test set afield = 10.123; commit; 
select afield from test; commit; 
Result:10.123
www.firebase.com.br 28 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
alter table test 
alter afieldtype numeric (10,2); commit; 
select afield from test; 
commit; 
Result:10.12 
alter table test 
alter afield type numeric (11,3); commit; 
select afield from test; 
commit; 
Result:10.123
www.firebase.com.br 29 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
alter table test alter afield type numeric (10,2); commit; 
select afield from test; commit; 
Result:10.12 
/* “Dumb” Update */ 
update test set afield = afield; commit; 
alter table test 
alter afield type numeric (11,3); commit; 
select afield from test; commit; 
Result:10.120
www.firebase.com.br 30 © 2014 – Carlos H. Cantu 
Changing the scale of numeric/decimal fields 
“Hardcore” solution: 
update RDB$FIELDS setRDB$FIELD_SCALE = -3where RDB$FIELD_NAME = 'RDB$nnn'; 
Warning! 
• 
Be sure that the existing values “fits” in the new range, otherwise some records will be inaccessible(corruption). 
• 
Will not work in Firebird 3!
www.firebase.com.br 31 © 2014 – Carlos H. Cantu 
Additional attention! 
• 
When changing the “size” of an existing field, it can be identified with a different type by the “language/access technology” used in the client application.
www.firebase.com.br 32 © 2014 – Carlos H. Cantu 
Roudings 
• 
Firebird uses “standard rounding”: -Chose what digit will be the limit-Add 1 if the next digit is >= 5-Don’t change the digit if the next is < 5 
I.e.: 
select cast(123.45 as numeric (9,1)) from rdb$database–result: 123.5 
select cast(123.42 as numeric (9,1)) from rdb$database–result: 123.4
www.firebase.com.br 33 © 2014 – Carlos H. Cantu 
FIM 
Questions? 
www.firebase.com.br 
www.firebirdnews.org 
ThankstoallConferencesponsors:

More Related Content

Similar to Understanding Numbers in Firebird SQL

MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
Dave Stokes
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
mit23cs
 
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
DataStax Academy
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
CSUC - Consorci de Serveis Universitaris de Catalunya
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
Hemantha Kulathilake
 
Float Data Type in C.pdf
Float Data Type in C.pdfFloat Data Type in C.pdf
Float Data Type in C.pdf
SudhanshiBakre1
 
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph AnalyticsScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
Toyotaro Suzumura
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
Fwdays
 
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docxSheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
bagotjesusa
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...
Scality
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQL
Mind The Firebird
 
Aerospike Go Language Client
Aerospike Go Language ClientAerospike Go Language Client
Aerospike Go Language Client
Sayyaparaju Sunil
 
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
Lucidworks
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stack
InfluxData
 
MongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the FieldMongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the Field
MongoDB
 
OPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACKOPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACK
InfluxData
 
Benchmarking Apache Druid
Benchmarking Apache DruidBenchmarking Apache Druid
Benchmarking Apache Druid
Imply
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid
Matt Sarrel
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programming
Capuchino HuiNing
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
Rabia Khalid
 

Similar to Understanding Numbers in Firebird SQL (20)

MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
 
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
Cassandra Summit 2014: The Cassandra Experience at Orange — Season 2
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
 
Float Data Type in C.pdf
Float Data Type in C.pdfFloat Data Type in C.pdf
Float Data Type in C.pdf
 
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph AnalyticsScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
ScaleGraph - A High-Performance Library for Billion-Scale Graph Analytics
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
 
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docxSheet11-Which of the following is a nonrenewable resourceaSolar e.docx
Sheet11-Which of the following is a nonrenewable resourceaSolar e.docx
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQL
 
Aerospike Go Language Client
Aerospike Go Language ClientAerospike Go Language Client
Aerospike Go Language Client
 
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
 
Virtual training optimizing the tick stack
Virtual training  optimizing the tick stackVirtual training  optimizing the tick stack
Virtual training optimizing the tick stack
 
MongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the FieldMongoDB Days UK: Tales from the Field
MongoDB Days UK: Tales from the Field
 
OPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACKOPTIMIZING THE TICK STACK
OPTIMIZING THE TICK STACK
 
Benchmarking Apache Druid
Benchmarking Apache DruidBenchmarking Apache Druid
Benchmarking Apache Druid
 
Benchmarking Apache Druid
Benchmarking Apache Druid Benchmarking Apache Druid
Benchmarking Apache Druid
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programming
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 

More from Mind The Firebird

Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easily
Mind The Firebird
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net provider
Mind The Firebird
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions work
Mind The Firebird
 
SuperServer in Firebird 3
SuperServer in Firebird 3SuperServer in Firebird 3
SuperServer in Firebird 3
Mind The Firebird
 
Copycat presentation
Copycat presentationCopycat presentation
Copycat presentation
Mind The Firebird
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performance
Mind The Firebird
 
Overview of RedDatabase 2.5
Overview of RedDatabase 2.5Overview of RedDatabase 2.5
Overview of RedDatabase 2.5
Mind The Firebird
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in details
Mind The Firebird
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyond
Mind The Firebird
 
New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad Khorsun
Mind The Firebird
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and Logging
Mind The Firebird
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016
Mind The Firebird
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Mind The Firebird
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databases
Mind The Firebird
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in Firebird
Mind The Firebird
 
Firebird on Linux
Firebird on LinuxFirebird on Linux
Firebird on Linux
Mind The Firebird
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...
Mind The Firebird
 
Firebird meets NoSQL
Firebird meets NoSQLFirebird meets NoSQL
Firebird meets NoSQL
Mind The Firebird
 
Continuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace APIContinuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace API
Mind The Firebird
 
Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows Functions
Mind The Firebird
 

More from Mind The Firebird (20)

Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easily
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net provider
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions work
 
SuperServer in Firebird 3
SuperServer in Firebird 3SuperServer in Firebird 3
SuperServer in Firebird 3
 
Copycat presentation
Copycat presentationCopycat presentation
Copycat presentation
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performance
 
Overview of RedDatabase 2.5
Overview of RedDatabase 2.5Overview of RedDatabase 2.5
Overview of RedDatabase 2.5
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in details
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyond
 
New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad Khorsun
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and Logging
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databases
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in Firebird
 
Firebird on Linux
Firebird on LinuxFirebird on Linux
Firebird on Linux
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...
 
Firebird meets NoSQL
Firebird meets NoSQLFirebird meets NoSQL
Firebird meets NoSQL
 
Continuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace APIContinuous Database Monitoring with the Trace API
Continuous Database Monitoring with the Trace API
 
Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows Functions
 

Recently uploaded

Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 

Recently uploaded (20)

Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 

Understanding Numbers in Firebird SQL

  • 1. www.firebase.com.br 1 © 2014 – Carlos H. Cantu Understanding Numbers in Firebird Carlos H. Cantuwww.firebase.com.br www.firebirdnews.org
  • 2. www.firebase.com.br 2 © 2014 – Carlos H. Cantu Who am I? • Maintainer of www.firebase.com.br and www.firebirdnews.org • Author of 2 Firebird books published in Brazil • Software developer for about 30 years • Organizer of the Firebird Developers Day conference • Firebird consultant
  • 3. www.firebase.com.br 3 © 2014 – Carlos H. Cantu Do you wannago crazy?!
  • 4. www.firebase.com.br 4 © 2014 – Carlos H. Cantu Warnings! 1. Internal storage type depends on database dialect 2. The dialect has influence in the precision of some types and in the results of calculations 3. Depending on the datatypeused, the retrieved value can be different from the original value!! 4. Decimal separator is always the dot “.”
  • 5. www.firebase.com.br 5 © 2014 – Carlos H. Cantu INTEGER types • SMALLINT – 16 bits – between -32.768 and32.767 • INTEGER – 32 bits – between -2.147.483.648 and 2.147.483.647 • BIGINT – 64 bits – between -9.223.372.036.854.775.808 and 9.223.372.036.854.775.807 – Only available in dialect 3
  • 6. www.firebase.com.br 6 © 2014 – Carlos H. Cantu FLOATING POINT types • FLOAT – 32 bits: 1 for signal, 8 for exponent and 23 for the mantissa. – 7 digits of precision – Between -3.4 x 1038and 3.4 x 1038 • DOUBLE PRECISION – 64 bits: 1 for signal, 11 for exponent and 52 for the mantissa. – 15 digits of precision – Between -1.7 x 10308and 1.7 x 10308
  • 7. www.firebase.com.br 7 © 2014 – Carlos H. Cantu Pros and cons of floating types • Stored following the standard defined by the IEEE (Institute of Electrical and Electronics Engineers), with an approximated representation of the real number. • Calculations uses the math co-processor (faster). • Not recommended due to lack of precision.
  • 8. www.firebase.com.br 8 © 2014 – Carlos H. Cantu Accuracy in FLOAT/DOUBLE (1/2) SQL> select cast(1234567.1234 as float) from rdb$database; CAST ============== 1234567.1 Result displayed by IBExpert: 1234567.125
  • 9. www.firebase.com.br 9 © 2014 – Carlos H. Cantu Imprecision in FLOAT/DOUBLE (2/2) SQL> select cast(1234567.4321 as float) from rdb$database; CAST ============== 1234567.4 Result displayed by IBExpert: 1234567.375
  • 10. www.firebase.com.br 10 © 2014 – Carlos H. Cantu Fixed point • NUMERIC (p,s) / DECIMAL (p,s) • Is stored occupying either 16, 32 or 64 bits • p= precision (total digits) [1 <= p <= 18] s= scale (number of digits after the “comma”) • smust be always lower or equal to p • If pand sis not informed, the internal type will be INTEGER • In FB, palways determinates the minimum number of stored digits (not follow the standard) • The retrieved value is always exactly equal to the original value!
  • 11. www.firebase.com.br 11 © 2014 – Carlos H. Cantu Internal storage of NUMERIC and DECIMAL PRECISION INTERNAL TYPE DIALECT 3 DIALECT 1 1..4 NUMERIC SMALLINT (*) SMALLINT 1..4 DECIMAL INTEGER (*) INTEGER 5..9 NUMERIC e DECIMAL INTEGER INTEGER 10..18 NUMERIC e DECIMAL BIGINT DOUBLE PRECISION(!) In Firebird, DECIMAL andNUMERICsare thesamething, ifp < 10. (*) In this case, the range of supported values are different compared to NUMERIC and DECIMAL
  • 12. www.firebase.com.br 12 © 2014 – Carlos H. Cantu Determining the capacity of chosen numeric/decimals 1. Check the internal type used depending on the precision (p) of the field. 2. Check the range of values supported by the internal type. 3. Divide the min and max values by 10s to know the effective range of accepted values for the field.
  • 13. www.firebase.com.br 13 © 2014 – Carlos H. Cantu Determining the capacity of chosen numeric/decimals Example: 1. NUMERIC (9,2) or DECIMAL (9,2) 2. Internally stored as INTEGER 3. Integer = -2.147.483.648 to 2.147.483.647 4. As s = 2, divide by 102 5. Accepted range for a field declared as NUMERIC/DECIMAL (9,2) = -21.474.836,48 to 21.474.836,47
  • 14. www.firebase.com.br 14 © 2014 – Carlos H. Cantu Testing the limits of numeric/decimal SQL> select cast(-21474836.48 as numeric (9,2)), cast(-21474836.48 as decimal (9,2)) from rdb$database; CAST CAST ============ ============ -21474836.48 -21474836.48 SQL> select cast(-21474836.49as numeric (9,2)), cast(-21474836.49as decimal (9,2)) from rdb$database; CAST CAST ============ ============ Statement failed, SQLSTATE = 22003 arithmetic exception, numeric overflow, or string truncation -numeric value is out of range
  • 15. www.firebase.com.br 15 © 2014 – Carlos H. Cantu Testing the limits of numeric/decimal SQL> select cast(32768 as decimal(4,0)) from rdb$database; --integer CAST ============ 32768 SQL> select cast(32768 as numeric(4,0)) from rdb$database; --smallint CAST ======= Statement failed, SQLSTATE = 22003 arithmetic exception, numeric overflow, or string truncation -numeric value is out of range
  • 16. www.firebase.com.br 16 © 2014 – Carlos H. Cantu Moving from dialect 1 to 3 • Is there any field declared as NUMERIC or DECIMALwith p > 9? – No: there will be no problem at all – Yes: you may have problems! • NUMERIC and DECIMAL with p > 9 are stored as double precision in dialect 1 and the existing fields will stay like this if the DB is “migrated” to dialect 3 using gfix-sql_dialect3. • New fields declared as NUM/DEC with p > 9, created after the DB was converted to dialect 3 will use BIGINTinternally. • Recommended solution: create a new DB using a script and pump the data from old to new database.
  • 17. www.firebase.com.br 17 © 2014 – Carlos H. Cantu Integer divisions • Dialect 1, dividing intby intresults in double precisionI.e.: 1/3 = 0,3333333333333 • Dialect 3, divide intby intresults in integerI.e.: 1/3 = 0
  • 18. www.firebase.com.br 18 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics • In dialect 1, the division will always return a double precision. • In dialect 3, the result will be a type withp= 18 and s= sum of the scales of the involved types. SQL> select cast(0.33 as numeric (9,2))/ cast (1 as numeric(9,2)) from rdb$database; DIVIDE ===================== 0.3300
  • 19. www.firebase.com.br 19 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics SQL> select (3.00/1.00*3.5)*2.00as totalfrom rdb$database; TOTAL ===================== 21.0000000 SQL> select (3.00/1.00/3.5)/2.00as totalfrom rdb$database; TOTAL ===================== 0.4285700
  • 20. www.firebase.com.br 20 © 2014 – Carlos H. Cantu Division/Multiplication of fixed point numerics • There can be overflows, specially with calculations involving multiple arguments! select cast(1 as numeric(15,6))* cast(1 as numeric(9,8)) * cast(1 as numeric(15,5)) from rdb$database ~ 1.000000* 1.00000000* 1.00000 Integer overflow. The result of an integer operation caused the most significant bit of the result to carry.
  • 21. www.firebase.com.br 21 © 2014 – Carlos H. Cantu Addition/Subtraction of fixed point numbers • Result will have sequal the biggest scale of the bigger member of the operation. • In dialect 1, result will always have p= 9 • In dialect 3, result will always have p= 18 SQL> select cast(1 as numeric(9,2)) + cast(2 as integer) from rdb$database; ADD ===================== 3.00 SQL> select cast(0.5 as numeric(9,2)) – cast(1 as numeric(9,3)) from rdb$database; SUBTRACT ===================== -0.500
  • 22. www.firebase.com.br 22 © 2014 – Carlos H. Cantu Tips summary • Always create the database in dialect 3, and connect to it using the same dialect. • For “monetary” fields, choose numericor decimalto guarantee the accuracy. • When need to store numbers with variable scale (s), choose double precision. • To migrate a DB from dialect 1 to 3, prefers to PUMP the data instead of using gfix. • Take care with overflows in calculations involving numeric/decimal.
  • 23. www.firebase.com.br 23 © 2014 – Carlos H. Cantu Curiosities INDEXES • Numbers are stored in keys as double precision (exception to the rule is BIGINT) • Pros: – For numeric/decimal, allows changing p or s without needing to reindex – For smallint/integer, allows converting between the types or to a type having a scale (s) without need to reindex • Obs: Due to lack of precision of the double precision, the search if done in an interval between the bigger previous value and the lower next value related to the searched value. GENERATORS • Dialect 1 = integer • Dialect 3 = bigint
  • 24. www.firebase.com.br 24 © 2014 – Carlos H. Cantu Curiosities (do you wannago more crazy??) CHECK CONSTRAINTS and CLIENT DIALECTS The rules applied by a check constraint are based on the dialect used by the client connection in the time the constraint was created. Ex: check (int1 / int2) > 0.5 (rule created with dialect 1 connection) When connecting to the DB using dialect 3: Insert ... (int1, int2) values (2, 3); --Success! ~ 0.66666666 Ex: check (int1 / int2) > 0.5 (rule created with dialect 3 connection) Insert ... (int1, int2) values (2, 3); --FAILURE! ~ 0
  • 25. www.firebase.com.br 25 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields • Raising the scale means shortening the range of accepted values I.e.: numeric (9,2): range -21.474.836,48 to21.474.836,47 numeric (9,3): range -2.147.483,648 to2.147.483,647 This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2.
  • 26. www.firebase.com.br 26 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields • Changing the scale of (9,2) to 3. • Solutions: – Create new field declared as (9,3) – Copy the values to the new field – Drop the old field – Rename the new field as the old one • Changing to (10,3) – Problem if there are indexes defined for that field, since the internal type changes to bigint!
  • 27. www.firebase.com.br 27 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields create table test (afield numeric (9,2)); commit; insert into test values (10.12); commit; alter table test alter afield type numeric (9,3); This operation is not defined for system tables. Unsuccessful metadata update. New scale specified for column AFIELD must be at most 2. alter table test alter afield type numeric (10,3); commit; update test set afield = 10.123; commit; select afield from test; commit; Result:10.123
  • 28. www.firebase.com.br 28 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields alter table test alter afieldtype numeric (10,2); commit; select afield from test; commit; Result:10.12 alter table test alter afield type numeric (11,3); commit; select afield from test; commit; Result:10.123
  • 29. www.firebase.com.br 29 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields alter table test alter afield type numeric (10,2); commit; select afield from test; commit; Result:10.12 /* “Dumb” Update */ update test set afield = afield; commit; alter table test alter afield type numeric (11,3); commit; select afield from test; commit; Result:10.120
  • 30. www.firebase.com.br 30 © 2014 – Carlos H. Cantu Changing the scale of numeric/decimal fields “Hardcore” solution: update RDB$FIELDS setRDB$FIELD_SCALE = -3where RDB$FIELD_NAME = 'RDB$nnn'; Warning! • Be sure that the existing values “fits” in the new range, otherwise some records will be inaccessible(corruption). • Will not work in Firebird 3!
  • 31. www.firebase.com.br 31 © 2014 – Carlos H. Cantu Additional attention! • When changing the “size” of an existing field, it can be identified with a different type by the “language/access technology” used in the client application.
  • 32. www.firebase.com.br 32 © 2014 – Carlos H. Cantu Roudings • Firebird uses “standard rounding”: -Chose what digit will be the limit-Add 1 if the next digit is >= 5-Don’t change the digit if the next is < 5 I.e.: select cast(123.45 as numeric (9,1)) from rdb$database–result: 123.5 select cast(123.42 as numeric (9,1)) from rdb$database–result: 123.4
  • 33. www.firebase.com.br 33 © 2014 – Carlos H. Cantu FIM Questions? www.firebase.com.br www.firebirdnews.org ThankstoallConferencesponsors: