forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastExpressionTest.java
More file actions
143 lines (114 loc) · 5.54 KB
/
CastExpressionTest.java
File metadata and controls
143 lines (114 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
*
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
*/
public class CastExpressionTest {
@Test
public void testCastToRowConstructorIssue1267() throws JSQLParserException {
TestUtils.assertExpressionCanBeParsedAndDeparsed(
"CAST(ROW(dataid, value, calcMark) AS ROW(datapointid CHAR, value CHAR, calcMark CHAR))",
true);
TestUtils.assertExpressionCanBeParsedAndDeparsed(
"CAST(ROW(dataid, value, calcMark) AS testcol)", true);
}
@Test
void testDataKeywordIssue1969() throws Exception {
String sqlStr = "SELECT * FROM myschema.myfunction('test'::data.text_not_null)";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
@Test
void testImplicitCast() throws JSQLParserException {
String sqlStr = "SELECT UUID '4ac7a9e9-607c-4c8a-84f3-843f0191e3fd'";
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
sqlStr = "SELECT DECIMAL(5,3) '3.2'";
select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
}
@Test
void testImplicitCastTimestampIssue1364() throws JSQLParserException {
String sqlStr = "SELECT TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'";
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
}
@Test
void testImplicitCastDoublePrecisionIssue1344() throws JSQLParserException {
String sqlStr = "SELECT double precision '1'";
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertTrue(select.getSelectItem(0).getExpression() instanceof CastExpression);
}
@Test
public void testCastToSigned() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT CAST(contact_id AS SIGNED) A");
assertSqlCanBeParsedAndDeparsed(
"SELECT CAST(contact_id AS SIGNED INTEGER) A");
assertSqlCanBeParsedAndDeparsed(
"SELECT CAST(contact_id AS UNSIGNED) A");
assertSqlCanBeParsedAndDeparsed(
"SELECT CAST(contact_id AS UNSIGNED INTEGER) A");
assertSqlCanBeParsedAndDeparsed(
"SELECT CAST(contact_id AS TIME WITHOUT TIME ZONE) A");
}
@Test
void testDataTypeFrom() {
CastExpression.DataType float64 = CastExpression.DataType.from("FLOAT64");
Assertions.assertEquals(CastExpression.DataType.FLOAT64, float64);
CastExpression.DataType float128 = CastExpression.DataType.from("FLOAT128");
Assertions.assertEquals(CastExpression.DataType.UNKNOWN, float128);
}
@Test
void testParenthesisCastIssue1997() throws JSQLParserException {
String sqlStr = "SELECT ((foo)::text = ANY((ARRAY['bar'])::text[]))";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
sqlStr = "SELECT ((foo)::text = ANY((((ARRAY['bar'])))::text[]))";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
@Test
void testDateTimeCast() throws JSQLParserException {
String sqlStr = "SELECT\n"
+ " TIME(15, 30, 00) as time_hms,\n"
+ " TIME(DATETIME '2008-12-25 15:30:00') AS time_dt,\n"
+ " TIME(TIMESTAMP '2008-12-25 15:30:00+08', 'America/Los_Angeles')\n"
+ "as time_tstz;";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
@Test
void testNestedCompositeTypeCastIssue2341() throws JSQLParserException {
String sqlStr = "SELECT\n"
+ " (product_data::product_info_similarity).info.category AS category,\n"
+ " COUNT(*) AS num_products\n"
+ "FROM products\n"
+ "GROUP BY (product_data::product_info_similarity).info.category;";
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
RowGetExpression categoryAccess =
Assertions.assertInstanceOf(RowGetExpression.class,
select.getSelectItem(0).getExpression());
Assertions.assertEquals("category", categoryAccess.getColumnName());
RowGetExpression infoAccess = Assertions.assertInstanceOf(RowGetExpression.class,
categoryAccess.getExpression());
Assertions.assertEquals("info", infoAccess.getColumnName());
ParenthesedExpressionList<?> parenthesedCast =
Assertions.assertInstanceOf(ParenthesedExpressionList.class,
infoAccess.getExpression());
Assertions.assertEquals(1, parenthesedCast.size());
Assertions.assertInstanceOf(CastExpression.class, parenthesedCast.get(0));
}
}