Skip to content

Commit

Permalink
[fix](Nereids) fix toSql of date literal (apache#25243)
Browse files Browse the repository at this point in the history
toSql should return '2023-2-1 ' for DateLiteral 2023-2-1
  • Loading branch information
keanji-x authored Oct 11, 2023
1 parent 0d603dd commit 47578c0
Show file tree
Hide file tree
Showing 34 changed files with 85 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public String getStringValue() {

@Override
public String toSql() {
return toString();
return String.format("'%s'", toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public double getDouble() {

@Override
public String toSql() {
return toString();
return String.format("'%s'", toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ void testDateTypeDateTimeArithmeticFunctions() {
VarcharLiteral format = new VarcharLiteral("%Y-%m-%d");

String[] answer = {
"2000-01-30", "1999-12-01", "2029-12-31", "1969-12-31",
"2002-06-30", "1997-06-30", "2000-07-28", "1999-06-04",
"2000-01-30", "1999-12-01",
"'2000-01-30'", "'1999-12-01'", "'2029-12-31'", "'1969-12-31'",
"'2002-06-30'", "'1997-06-30'", "'2000-07-28'", "'1999-06-04'",
"'2000-01-30'", "'1999-12-01'",
"1999", "4", "12", "6", "31", "365", "31",
"'1999-12-31'", "1999-12-27", "1999-12-31"
"'1999-12-31'", "'1999-12-27'", "'1999-12-31'"
};
int answerIdx = 0;

Expand Down Expand Up @@ -348,12 +348,12 @@ void testDateTimeTypeDateTimeArithmeticFunctions() {
VarcharLiteral format = new VarcharLiteral("%Y-%m-%d");

String[] answer = {
"2000-01-30 23:59:59", "1999-12-01 23:59:59", "2029-12-31 23:59:59", "1969-12-31 23:59:59",
"2002-06-30 23:59:59", "1997-06-30 23:59:59", "2000-01-30 23:59:59", "1999-12-01 23:59:59",
"2000-01-02 05:59:59", "1999-12-30 17:59:59", "2000-01-01 00:29:59",
"1999-12-31 23:29:59", "2000-01-01 00:00:29", "1999-12-31 23:59:29",
"'2000-01-30 23:59:59'", "'1999-12-01 23:59:59'", "'2029-12-31 23:59:59'", "'1969-12-31 23:59:59'",
"'2002-06-30 23:59:59'", "'1997-06-30 23:59:59'", "'2000-01-30 23:59:59'", "'1999-12-01 23:59:59'",
"'2000-01-02 05:59:59'", "'1999-12-30 17:59:59'", "'2000-01-01 00:29:59'",
"'1999-12-31 23:29:59'", "'2000-01-01 00:00:29'", "'1999-12-31 23:59:29'",
"1999", "4", "12", "6", "31", "365", "31", "23", "59", "59",
"'1999-12-31'", "1999-12-27", "1999-12-31", "1999-12-31", "730484", "1999-12-31"
"'1999-12-31'", "'1999-12-27'", "'1999-12-31'", "'1999-12-31'", "730484", "'1999-12-31'"
};
int answerIdx = 0;

Expand Down Expand Up @@ -403,10 +403,10 @@ void testDateV2TypeDateTimeArithmeticFunctions() {
VarcharLiteral format = new VarcharLiteral("%Y-%m-%d");

String[] answer = {
"2000-01-30", "1999-12-01", "2029-12-31", "1969-12-31",
"2002-06-30", "1997-06-30", "2000-01-30", "1999-12-01",
"'2000-01-30'", "'1999-12-01'", "'2029-12-31'", "'1969-12-31'",
"'2002-06-30'", "'1997-06-30'", "'2000-01-30'", "'1999-12-01'",
"1999", "4", "12", "6", "31", "365", "31",
"'1999-12-31'", "1999-12-27", "1999-12-31"
"'1999-12-31'", "'1999-12-27'", "'1999-12-31'"
};
int answerIdx = 0;

Expand Down Expand Up @@ -440,12 +440,12 @@ void testDateTimeV2TypeDateTimeArithmeticFunctions() {
VarcharLiteral format = new VarcharLiteral("%Y-%m-%d");

String[] answer = {
"2000-01-30 23:59:59", "1999-12-01 23:59:59", "2029-12-31 23:59:59", "1969-12-31 23:59:59",
"2002-06-30 23:59:59", "1997-06-30 23:59:59", "2000-01-30 23:59:59", "1999-12-01 23:59:59",
"2000-01-02 05:59:59", "1999-12-30 17:59:59", "2000-01-01 00:29:59",
"1999-12-31 23:29:59", "2000-01-01 00:00:29", "1999-12-31 23:59:29", "1999-12-31 23:59:59",
"'2000-01-30 23:59:59'", "'1999-12-01 23:59:59'", "'2029-12-31 23:59:59'", "'1969-12-31 23:59:59'",
"'2002-06-30 23:59:59'", "'1997-06-30 23:59:59'", "'2000-01-30 23:59:59'", "'1999-12-01 23:59:59'",
"'2000-01-02 05:59:59'", "'1999-12-30 17:59:59'", "'2000-01-01 00:29:59'",
"'1999-12-31 23:29:59'", "'2000-01-01 00:00:29'", "'1999-12-31 23:59:29'", "'1999-12-31 23:59:59'",
"1999", "4", "12", "6", "31", "365", "31", "23", "59", "59",
"'1999-12-31'", "1999-12-27", "1999-12-31", "1999-12-31", "730484", "1999-12-31", "1999-12-31"
"'1999-12-31'", "'1999-12-27'", "'1999-12-31'", "'1999-12-31'", "730484", "'1999-12-31'", "'1999-12-31'"
};
int answerIdx = 0;

Expand Down Expand Up @@ -528,14 +528,14 @@ void testDateTrunc() {
String[] tags = {"year", "month", "day", "hour", "minute", "second"};

String[] answer = {
"2001-01-01 00:00:00", "2001-01-01 00:00:00", "2001-12-01 00:00:00", "2001-12-01 00:00:00",
"2001-12-31 00:00:00", "2001-12-31 00:00:00", "2001-12-31 01:00:00", "2001-12-31 01:00:00",
"2001-12-31 01:01:00", "2001-12-31 01:01:00", "2001-12-31 01:01:01", "2001-12-31 01:01:01",
"2001-01-01 00:00:00", "2001-01-01 00:00:00", "2001-01-01 00:00:00",
"2001-04-01 00:00:00", "2001-04-01 00:00:00", "2001-04-01 00:00:00",
"2001-07-01 00:00:00", "2001-07-01 00:00:00", "2001-07-01 00:00:00",
"2001-10-01 00:00:00", "2001-10-01 00:00:00", "2001-10-01 00:00:00",
"2001-01-15 00:00:00", "2001-02-12 00:00:00", "2001-03-12 00:00:00",
"'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'", "'2001-12-01 00:00:00'", "'2001-12-01 00:00:00'",
"'2001-12-31 00:00:00'", "'2001-12-31 00:00:00'", "'2001-12-31 01:00:00'", "'2001-12-31 01:00:00'",
"'2001-12-31 01:01:00'", "'2001-12-31 01:01:00'", "'2001-12-31 01:01:01'", "'2001-12-31 01:01:01'",
"'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'", "'2001-01-01 00:00:00'",
"'2001-04-01 00:00:00'", "'2001-04-01 00:00:00'", "'2001-04-01 00:00:00'",
"'2001-07-01 00:00:00'", "'2001-07-01 00:00:00'", "'2001-07-01 00:00:00'",
"'2001-10-01 00:00:00'", "'2001-10-01 00:00:00'", "'2001-10-01 00:00:00'",
"'2001-01-15 00:00:00'", "'2001-02-12 00:00:00'", "'2001-03-12 00:00:00'",
};
int answerIdx = 0;

Expand All @@ -561,7 +561,7 @@ void testDateTrunc() {
@Test
void testDateConstructFunction() {
String[] answer = {
"2001-07-19", "6411-08-17", "0000-01-01", "'1977-06-03 17:57:24'",
"'2001-07-19'", "'6411-08-17'", "'0000-01-01'", "'1977-06-03 17:57:24'",
"'1977-06-03'", "1008909293", "1008864000"
};
int answerIdx = 0;
Expand Down Expand Up @@ -594,7 +594,7 @@ void testDateConstructFunction() {

@Test
void testFoldNestedExpression() {
assertRewriteExpression("makedate(year('2010-04-10'), dayofyear('2010-04-11'))", "2010-04-11");
assertRewriteExpression("makedate(year('2010-04-10'), dayofyear('2010-04-11'))", "'2010-04-11'");
assertRewriteExpression("null in ('d', null)", "NULL");
assertRewriteExpression("null not in ('d', null)", "NULL");
assertRewriteExpression("'a' in ('d', null)", "NULL");
Expand All @@ -608,13 +608,13 @@ void testFoldNestedExpression() {

@Test
void testFoldCastStringToDate() {
assertRewriteExpression("cast('2021-01-01' as date)", "2021-01-01");
assertRewriteExpression("cast('20210101' as date)", "2021-01-01");
assertRewriteExpression("cast('2021-01-01T00:00:00' as date)", "2021-01-01");
assertRewriteExpression("cast('2021-01-01' as datetime)", "2021-01-01 00:00:00");
assertRewriteExpression("cast('20210101' as datetime)", "2021-01-01 00:00:00");
assertRewriteExpression("cast('2021-01-01T00:00:00' as datetime)", "2021-01-01 00:00:00");
assertRewriteExpression("cast ('2022-12-02 22:23:24.999999' as datetimev2(3))", "2022-12-02 22:23:25.000");
assertRewriteExpression("cast('2021-01-01' as date)", "'2021-01-01'");
assertRewriteExpression("cast('20210101' as date)", "'2021-01-01'");
assertRewriteExpression("cast('2021-01-01T00:00:00' as date)", "'2021-01-01'");
assertRewriteExpression("cast('2021-01-01' as datetime)", "'2021-01-01 00:00:00'");
assertRewriteExpression("cast('20210101' as datetime)", "'2021-01-01 00:00:00'");
assertRewriteExpression("cast('2021-01-01T00:00:00' as datetime)", "'2021-01-01 00:00:00'");
assertRewriteExpression("cast ('2022-12-02 22:23:24.999999' as datetimev2(3))", "'2022-12-02 22:23:25.000'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a < 2023-06-16 00:00:00)")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a < '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b < 111.12)")))
);

Expand All @@ -66,7 +66,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a <= 2023-06-16 00:00:00)")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a <= '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b <= 111.11)")))
);

Expand All @@ -82,7 +82,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a > 2023-06-16 00:00:00)")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a > '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b > 111.11)")))
);

Expand All @@ -91,7 +91,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a >= 2023-06-16 00:00:00)")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a >= '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b >= 111.12)")))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PhysicalResultSink
--------------------------------PhysicalOlapScan[web_sales]
------------------------------PhysicalDistribute
--------------------------------PhysicalProject
----------------------------------filter((date_dim.d_date <= 1998-05-06) and (date_dim.d_date >= 1998-04-06))
----------------------------------filter((date_dim.d_date <= '1998-05-06') and (date_dim.d_date >= '1998-04-06'))
------------------------------------PhysicalOlapScan[date_dim]
--------------------------PhysicalDistribute
----------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PhysicalResultSink
------------------------------------PhysicalOlapScan[customer_address]
----------------------------PhysicalDistribute
------------------------------PhysicalProject
--------------------------------filter((date_dim.d_date <= 2002-05-31) and (date_dim.d_date >= 2002-04-01))
--------------------------------filter((date_dim.d_date <= '2002-05-31') and (date_dim.d_date >= '2002-04-01'))
----------------------------------PhysicalOlapScan[date_dim]
----------------------PhysicalDistribute
------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PhysicalResultSink
--------------------------------PhysicalOlapScan[catalog_sales]
------------------------------PhysicalDistribute
--------------------------------PhysicalProject
----------------------------------filter((date_dim.d_date <= 2002-02-25) and (date_dim.d_date >= 2002-01-26))
----------------------------------filter((date_dim.d_date <= '2002-02-25') and (date_dim.d_date >= '2002-01-26'))
------------------------------------PhysicalOlapScan[date_dim]
--------------------------PhysicalDistribute
----------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PhysicalResultSink
------------------------------PhysicalOlapScan[item]
----------------------PhysicalDistribute
------------------------PhysicalProject
--------------------------filter((date_dim.d_date <= 2002-03-29) and (date_dim.d_date >= 2002-01-28))
--------------------------filter((date_dim.d_date <= '2002-03-29') and (date_dim.d_date >= '2002-01-28'))
----------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalDistribute
----------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ PhysicalResultSink
----------------------------------PhysicalOlapScan[item]
--------------------------PhysicalDistribute
----------------------------PhysicalProject
------------------------------filter((date_dim.d_date <= 1999-04-07) and (date_dim.d_date >= 1999-01-07))
------------------------------filter((date_dim.d_date <= '1999-04-07') and (date_dim.d_date >= '1999-01-07'))
--------------------------------PhysicalOlapScan[date_dim]

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ PhysicalResultSink
--------------------------------PhysicalOlapScan[item]
------------------------PhysicalDistribute
--------------------------PhysicalProject
----------------------------filter((date_dim.d_date <= 1999-04-22) and (date_dim.d_date >= 1999-02-21))
----------------------------filter((date_dim.d_date <= '1999-04-22') and (date_dim.d_date >= '1999-02-21'))
------------------------------PhysicalOlapScan[date_dim]

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PhysicalResultSink
--------------------------------PhysicalOlapScan[item]
------------------------PhysicalDistribute
--------------------------PhysicalProject
----------------------------filter((date_dim.d_date <= 2001-05-02) and (date_dim.d_date >= 2001-03-03))
----------------------------filter((date_dim.d_date <= '2001-05-02') and (date_dim.d_date >= '2001-03-03'))
------------------------------PhysicalOlapScan[date_dim]
------------------PhysicalDistribute
--------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PhysicalResultSink
--------------------------------------PhysicalProject
----------------------------------------PhysicalOlapScan[store_returns]
----------------------------------PhysicalDistribute
------------------------------------filter((date_dim.d_date <= 2000-09-02) and (date_dim.d_date >= 2000-08-19))
------------------------------------filter((date_dim.d_date <= '2000-09-02') and (date_dim.d_date >= '2000-08-19'))
--------------------------------------PhysicalOlapScan[date_dim]
--------------------------------PhysicalDistribute
----------------------------------PhysicalOlapScan[store]
Expand All @@ -44,7 +44,7 @@ PhysicalResultSink
--------------------------------------PhysicalProject
----------------------------------------PhysicalOlapScan[catalog_returns]
----------------------------------PhysicalDistribute
------------------------------------filter((date_dim.d_date <= 2000-09-02) and (date_dim.d_date >= 2000-08-19))
------------------------------------filter((date_dim.d_date <= '2000-09-02') and (date_dim.d_date >= '2000-08-19'))
--------------------------------------PhysicalOlapScan[date_dim]
--------------------------------PhysicalDistribute
----------------------------------PhysicalOlapScan[catalog_page]
Expand All @@ -65,7 +65,7 @@ PhysicalResultSink
------------------------------------------PhysicalOlapScan[web_sales]
------------------------------------------PhysicalOlapScan[web_returns]
----------------------------------PhysicalDistribute
------------------------------------filter((date_dim.d_date <= 2000-09-02) and (date_dim.d_date >= 2000-08-19))
------------------------------------filter((date_dim.d_date <= '2000-09-02') and (date_dim.d_date >= '2000-08-19'))
--------------------------------------PhysicalOlapScan[date_dim]
--------------------------------PhysicalDistribute
----------------------------------PhysicalOlapScan[web_site]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ PhysicalResultSink
------------------------------------------PhysicalAssertNumRows
--------------------------------------------PhysicalDistribute
----------------------------------------------PhysicalProject
------------------------------------------------filter((date_dim.d_date = 2001-03-24))
------------------------------------------------filter((date_dim.d_date = '2001-03-24'))
--------------------------------------------------PhysicalOlapScan[date_dim]
------------------------PhysicalDistribute
--------------------------PhysicalProject
Expand Down Expand Up @@ -60,7 +60,7 @@ PhysicalResultSink
--------------------------------------------PhysicalAssertNumRows
----------------------------------------------PhysicalDistribute
------------------------------------------------PhysicalProject
--------------------------------------------------filter((date_dim.d_date = 2001-03-24))
--------------------------------------------------filter((date_dim.d_date = '2001-03-24'))
----------------------------------------------------PhysicalOlapScan[date_dim]
--------------------------PhysicalDistribute
----------------------------PhysicalProject
Expand Down Expand Up @@ -89,7 +89,7 @@ PhysicalResultSink
--------------------------------------------PhysicalAssertNumRows
----------------------------------------------PhysicalDistribute
------------------------------------------------PhysicalProject
--------------------------------------------------filter((date_dim.d_date = 2001-03-24))
--------------------------------------------------filter((date_dim.d_date = '2001-03-24'))
----------------------------------------------------PhysicalOlapScan[date_dim]
--------------------------PhysicalDistribute
----------------------------PhysicalProject
Expand Down
Loading

0 comments on commit 47578c0

Please sign in to comment.