Skip to content

Conversation

@wangyum
Copy link
Member

@wangyum wangyum commented Jul 18, 2019

What changes were proposed in this pull request?

PostgreSQL support implicit cast string type to interval type when adding timestamp type with string type:

postgres=# explain verbose select now() + '100 days', '100 days' + now();
                                QUERY PLAN
--------------------------------------------------------------------------
 Result  (cost=0.00..0.02 rows=1 width=16)
   Output: (now() + '100 days'::interval), (now() + '100 days'::interval)
(2 rows)

This pr add this feature.

Teradata, Oracle, DB2, Presto, MariaDB and MySQL does not support this syntax, PostgreSQL and Vertica support this syntax:

dbadmin=> select version(), current_timestamp + cast(interval '1' year as varchar(255)), cast(interval '2' year as varchar(255)) + current_timestamp, current_timestamp - cast(interval '3' year as varchar(255));
              version               |           ?column?            |           ?column?            |           ?column?
------------------------------------+-------------------------------+-------------------------------+-------------------------------
 Vertica Analytic Database v9.1.1-0 | 2019-07-24 13:14:03.003878+00 | 2019-07-25 13:14:03.003878+00 | 2019-07-20 13:14:03.003878+00
(1 row)

How was this patch tested?

unit tests

@wangyum wangyum changed the title [SPARK-28429][SQL] Support timestamp binaryArithmetic string [SPARK-28429][SQL] Support implicit cast string type to interval type in PromoteStrings Jul 18, 2019
@SparkQA
Copy link

SparkQA commented Jul 18, 2019

Test build #107836 has finished for PR 25190 at commit c5d751a.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jul 18, 2019

Test build #107844 has finished for PR 25190 at commit 073c113.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@maropu
Copy link
Member

maropu commented Jul 19, 2019

In postgresql, is this handled as implicit casts? I checked a cast list in postgresql though, I couldn't find an entry, text -> interval, for implicit casts;

postgres=# \dC text
                               List of casts
    Source type    |    Target type    |      Function      |   Implicit?   
-------------------+-------------------+--------------------+---------------
 "char"            | text              | text               | yes
 boolean           | text              | text               | in assignment
 character         | text              | text               | yes
 character varying | text              | (binary coercible) | yes
 cidr              | text              | text               | in assignment
 inet              | text              | text               | in assignment
 name              | text              | text               | yes
 pg_dependencies   | text              | (with inout)       | yes
 pg_ndistinct      | text              | (with inout)       | yes
 pg_node_tree      | text              | (binary coercible) | yes
 text              | "char"            | char               | in assignment
 text              | character         | (binary coercible) | yes
 text              | character varying | (binary coercible) | yes
 text              | name              | name               | yes
 text              | regclass          | regclass           | yes
 text              | xml               | xml                | no
 xml               | text              | (binary coercible) | in assignment
(17 rows)

In postgresql, it seems select now() + '100 days' is hanldes as select now() + interval '100 days' in the parser? (I'm not 100% sure though)

@dongjoon-hyun
Copy link
Member

dongjoon-hyun commented Jul 27, 2019

Hi, @wangyum .

@wangyum
Copy link
Member Author

wangyum commented Jul 29, 2019

Yes. We still need this fix. But I'm not sure how PostgreSQL support this feature.

@SparkQA
Copy link

SparkQA commented Jul 29, 2019

Test build #108328 has finished for PR 25190 at commit a227143.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@maropu
Copy link
Member

maropu commented Jul 30, 2019

PostgreSQL supports conversions from a string literal to any data type (e.g., text->interval), but IIUC it doesn't support implicit casts from text-typed data in a relation;

postgres=# create table t2 (t timestamp, i text);
postgres=# insert into t2 values(now(), '1 hour');
postgres=# select * from t2;
             t              |   i    
----------------------------+--------
 2019-07-30 09:24:07.728206 | 1 hour
(1 row)

postgres=# select t + i from t2;
ERROR:  operator does not exist: timestamp without time zone + text
LINE 1: select t + i from t2;
                 ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants