473,376 Members | 1,043 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

implicit casting problem

Hi,

We have an app which uses the latest version of perl DBI and DBD::Pg to
execute a query, using placeholders. It fails to cast the float I send
into a number and generates the following message

"DBD::Pg::st execute failed: ERROR: invalid input syntax for
integer: "2.63" at -e line 8."

Here is a little test case:

tom@tux6204:~ $ perl -MDBI -e '
my $dbh = DBI->connect( "DBI:Pg:dbname=prod;host=pgdb",
"compl", "devsql",
{RaiseError => 1,
AutoCommit => 0,
FetchHashKeyName => "NAME_lc"});
my $sth = $dbh->prepare("update tmp set a = a *?");
$sth->execute(2.63);
'
DBD::Pg::st execute failed: ERROR: invalid input syntax for
integer: "2.63" at -e line 8.

-------------------------- end test case

Here is me rooting around on the psql command line trying to work out what
is happening.

Welcome to psql 7.4.5, the PostgreSQL interactive terminal.
....
prod=> create table tmp (a int);
CREATE TABLE
prod=> insert into tmp values (2);
INSERT 2392267569 1
prod=> update tmp set a = a * 2.63;
UPDATE 1
prod=> update tmp set a = a * '2.63';
ERROR: invalid input syntax for integer: "2.63"
prod=> update tmp set a = (a::float * '2.63');
UPDATE 1
prod=> update tmp set a = a::float * '2.63';
UPDATE 1
prod=> update tmp set a = floort(a * '2.63');
ERROR: invalid input syntax for integer: "2.63"
prod=> update tmp set a = floort(a * 2.63);
ERROR: function floort(numeric) does not exist
HINT: No function matches the given name and argument types. You may need
to add explicit type casts.
prod=> update tmp set a = floor(a * 2.63);
UPDATE 1

I thought it was a problem with perl DBI, but then the fact "(a::float *
'2.63')" works suprises me.

Is this a problem with Postgres, or with perl's placeholder mechanism
quoting a number that doesn't need quoting.

Thanks,

Tom Larard
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
2 8039
Fixed, this was a problem with weakly typed languages such as perl,
sometime it doesn't know what SQL_TYPE to send, so it sends
SQL_VARCHAR.

The solution is to use
bind_param(<placeholder_number>,<value>,DBI::SQL_F LOAT);

Tom

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #2

Tom Larard <la****@cs.umb.edu> writes:

Just for reference these two cases are interpreted in ways that may not be
intuitive for programmers. Most languages treat constants that look like 'foo'
as string constants and then have rules for how string constants get cast.
Postgres has string datatypes but 'foo' isn't necessarily a string data type.
It's of unknown type. It default to being cast to a string in some cases but
if it's used in arithmetic or function calls it can be cast to whatever's
necessary.
prod=> update tmp set a = a * 2.63;
UPDATE 1
In this case 2.63 is recognized by the parser as a numeric constant which is
an arbitrary precision data type. So postgres sees <integer> * <numeric> and
picks the appropriate * operator. That operator returns a numeric result which
it then casts to integer to handle the assignment.

So it parses as "set a = (a::numeric * 2.63)::integer"
prod=> update tmp set a = a * '2.63';
ERROR: invalid input syntax for integer: "2.63"
In this case the parser sees '2.63' which is not a string, but rather a
constant of unknown type. In that case postgres sees <integer> * <unknown> and
picks the most convenient type for the unknown side. Usually (always? I'm not
sure) the choice is the same type as the other side of the operator. So it
then tries to cast the unknown to an integer and fails.
prod=> update tmp set a = (a::float * '2.63');
UPDATE 1
In this case postgers sees <float> * <unknown> and the same thing happens and
it works.
prod=> update tmp set a = a::float * '2.63';
UPDATE 1
<float> * <unknown>
prod=> update tmp set a = floort(a * '2.63');
ERROR: invalid input syntax for integer: "2.63"
<integer> * <unknown>
prod=> update tmp set a = floort(a * 2.63);
ERROR: function floort(numeric) does not exist
HINT: No function matches the given name and argument types. You may need
to add explicit type casts.
So in this case it sees <integer> * <numeric> which it can satisfy with the
numeric*numeric operator which returns a numeric. But there's no
floort(numeric) operator.
prod=> update tmp set a = floor(a * 2.63);
UPDATE 1


This is "set a = (floor(a::numeric * <numeric>))::integer"

--
greg
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
2
by: kalki70 | last post by:
Hello, If I have a function like : long long getNumber(); I can use implicit casting, so I can use this function with any compatible value, for instance: int a = getNumber();
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.