Skip to content

Commit

Permalink
feat(grammar): Improve support for CAST syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Sep 27, 2024
1 parent eb9b9ee commit e38d895
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@
"examples/call.sql" : [
1
],
"examples/cast.sql" : [
3
],
"examples/cluster.sql" : [
1
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1769,18 +1769,6 @@
"sqlrf/CAST-18.sql" : [
2
],
"sqlrf/CAST-2.sql" : [
4
],
"sqlrf/CAST-3.sql" : [
4
],
"sqlrf/CAST-4.sql" : [
4
],
"sqlrf/CAST-5.sql" : [
4
],
"sqlrf/CAST-6.sql" : [
5
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
DISALLOW("disallow"),
DISASSOCIATE("disassociate"),
DOCUMENT("document"),
DOMAIN("domain"),
DOUBLE("double"),
DURATION("duration"),
EACH("each"),
Expand Down Expand Up @@ -369,6 +370,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
NOORDER("noorder"),
NOSCHEMACHECK("noschemacheck"),
NOTFOUND("notfound"),
NOVALIDATE("novalidate"),
NOW("now"),
NULLS("nulls"),
NUMBER("number"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
XMLTABLE_EXPRESSION,
XMLTRANSFORM_EXPRESSION,

DEFAULT_ON_ERROR_CLAUSE,
TREAT_AS_EXPRESSION,
SET_EXPRESSION,
CAST_EXPRESSION,
Expand Down Expand Up @@ -194,6 +195,8 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
}

private fun createConversionFunctions(b: PlSqlGrammarBuilder) {
b.rule(DEFAULT_ON_ERROR_CLAUSE).define(DEFAULT, EXPRESSION, ON, CONVERSION, ERROR)

b.rule(TREAT_AS_EXPRESSION).define(
b.optional(TREAT),
LPARENTHESIS,
Expand All @@ -207,10 +210,13 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
b.rule(SET_EXPRESSION).define(SET, LPARENTHESIS, EXPRESSION, RPARENTHESIS)

b.rule(CAST_EXPRESSION).define(
CAST, LPARENTHESIS,
b.firstOf(b.sequence(MULTISET, EXPRESSION), EXPRESSION),
AS, DATATYPE,
RPARENTHESIS)
CAST, LPARENTHESIS,
b.firstOf(b.sequence(MULTISET, EXPRESSION), EXPRESSION),
AS, b.optional(DOMAIN), DATATYPE, b.optional(b.firstOf(VALIDATE, NOVALIDATE)),
b.optional(DEFAULT_ON_ERROR_CLAUSE),
b.optional(COMMA, EXPRESSION, b.optional(COMMA, EXPRESSION)),
RPARENTHESIS
)

b.rule(TABLE_EXPRESSION).define(
TABLE, LPARENTHESIS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ class CastExpressionTest : RuleTest() {
assertThat(p).matches("cast(foo as number)")
}

@Test
fun matchesCastWithDefaultClause() {
assertThat(p).matches("cast(foo as number default 0 on conversion error)")
}

@Test
fun matchesCastWithMultipleArguments() {
assertThat(p).matches("cast('01/01/2000' as date, 'dd/mm/yyyy', 'NLS_DATE_LANGUAGE = American')")
}

@Test
fun matchesCastMultiset() {
assertThat(p).matches("cast(multiset (select 1 from dual) as number)")
}

@Test
fun matchesComplexCast() {
assertThat(p).matches("cast((cast(localtimestamp as timestamp with time zone) at time zone 'gmt') at time zone custom_zone as timestamp)")
assertThat(p).matches("cast((cast(localtimestamp as domain timestamp with time zone validate) at time zone 'gmt') at time zone custom_zone as timestamp default 0 on conversion error)")
}

}

0 comments on commit e38d895

Please sign in to comment.