h2database / h2database/h2database

'Unknown data type' error performing date arithmetic with parameter

Open
#1,383 11 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
4.6k
Forks
1.3k
Avg merge
1d 2h
Merged PRs (30d)
9

Description

(From https://stackoverflow.com/questions/51892300)

The following class

import java.sql.*;

public class H2Test {
    public static void main(String[] args) throws Exception {
        Connection conn = DriverManager.getConnection("jdbc:h2:./test.db;MODE=ORACLE");
        PreparedStatement ps = conn.prepareStatement("SELECT SYSDATE - ?");
    }
}

throws the following exception when run:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Unknown data type: "?"; SQL statement:
SELECT SYSDATE - ? [50004-197]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
        at org.h2.message.DbException.get(DbException.java:179)
        at org.h2.message.DbException.get(DbException.java:155)
        at org.h2.value.DataType.getDataType(DataType.java:827)
        at org.h2.expression.Operation.optimize(Operation.java:312)
        at org.h2.command.dml.Select.prepare(Select.java:858)
        at org.h2.command.Parser.prepareCommand(Parser.java:283)
        at org.h2.engine.Session.prepareLocal(Session.java:611)
        at org.h2.engine.Session.prepareCommand(Session.java:549)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247)
        at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
        at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:304)
        at H2Test.main(H2Test.java:6)

There is no exception if you replace the parameter ? with a hard-coded integer.

Incidentally (and this may be a separate issue already reported elsewhere), changing the compatibility mode in the connection string to an unrecognised value (e.g. UNKNOWN) changes the exception to the following:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: General error: "java.lang.NullPointerException" [50000-197]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
        at org.h2.message.DbException.get(DbException.java:168)
        at org.h2.message.DbException.convert(DbException.java:307)
        at org.h2.engine.Database.openDatabase(Database.java:319)
        at org.h2.engine.Database.<init>(Database.java:280)
        at org.h2.engine.Engine.openSession(Engine.java:66)
        at org.h2.engine.Engine.openSession(Engine.java:179)
        at org.h2.engine.Engine.createSessionAndValidate(Engine.java:157)
        at org.h2.engine.Engine.createSession(Engine.java:140)
        at org.h2.engine.Engine.createSession(Engine.java:28)
        at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:351)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:124)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:103)
        at org.h2.Driver.connect(Driver.java:69)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
        at H2Test.main(H2Test.java:5)
Caused by: java.lang.NullPointerException
        at org.h2.command.Parser.read(Parser.java:3731)
        at org.h2.command.Parser.readIf(Parser.java:3525)
        at org.h2.command.Parser.parseCreateUser(Parser.java:5077)
        at org.h2.command.Parser.parseCreate(Parser.java:4564)
        at org.h2.command.Parser.parsePrepared(Parser.java:380)
        at org.h2.command.Parser.parse(Parser.java:335)
        at org.h2.command.Parser.parse(Parser.java:307)
        at org.h2.command.Parser.prepare(Parser.java:262)
        at org.h2.engine.Session.prepare(Session.java:576)
        at org.h2.engine.Session.prepare(Session.java:560)
        at org.h2.engine.MetaRecord.execute(MetaRecord.java:56)
        at org.h2.engine.Database.open(Database.java:775)
        at org.h2.engine.Database.openDatabase(Database.java:286)
        ... 13 more

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the H2Test reproducer with MODE=ORACLE, then inspect the stack-trace entry points in src/main/org/h2/expression/Operation.java and src/main/org/h2/value/DataType.java. Check the related parser and compatibility-mode behavior shown in the trace. Done means the prepared date-arithmetic statement no longer fails with an unknown parameter type, with regression coverage for the reported query.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.