Skip to content

Commit a96e93b

Browse files
committed
add all casts
1 parent 2100d82 commit a96e93b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

sql/src/main/scala/app/softnetwork/elastic/sql/SQLParser.scala

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,18 @@ trait SQLParser extends RegexParsers with PackratParsers { _: SQLWhereParser =>
413413
"minute",
414414
"second",
415415
"quarter",
416+
"char",
416417
"string",
418+
"byte",
419+
"tinyint",
420+
"short",
421+
"smallint",
417422
"int",
418423
"integer",
419424
"long",
420425
"bigint",
426+
"real",
427+
"float",
421428
"double",
422429
"boolean",
423430
"time",
@@ -460,7 +467,11 @@ trait SQLParser extends RegexParsers with PackratParsers { _: SQLWhereParser =>
460467
)
461468
}
462469

463-
def string_type: PackratParser[SQLTypes.Varchar.type] = "(?i)string".r ^^ (_ => SQLTypes.Varchar)
470+
def char_type: PackratParser[SQLTypes.Char.type] =
471+
"(?i)char".r ^^ (_ => SQLTypes.Char)
472+
473+
def string_type: PackratParser[SQLTypes.Varchar.type] =
474+
"(?i)varchar|string".r ^^ (_ => SQLTypes.Varchar)
464475

465476
def date_type: PackratParser[SQLTypes.Date.type] = "(?i)date".r ^^ (_ => SQLTypes.Date)
466477

@@ -475,14 +486,22 @@ trait SQLParser extends RegexParsers with PackratParsers { _: SQLWhereParser =>
475486
def boolean_type: PackratParser[SQLTypes.Boolean.type] =
476487
"(?i)boolean".r ^^ (_ => SQLTypes.Boolean)
477488

489+
def byte_type: PackratParser[SQLTypes.TinyInt.type] =
490+
"(?i)(byte|tinyint)".r ^^ (_ => SQLTypes.TinyInt)
491+
492+
def short_type: PackratParser[SQLTypes.SmallInt.type] =
493+
"(?i)(short|smallint)".r ^^ (_ => SQLTypes.SmallInt)
494+
495+
def int_type: PackratParser[SQLTypes.Int.type] = "(?i)(int|integer)".r ^^ (_ => SQLTypes.Int)
496+
478497
def long_type: PackratParser[SQLTypes.BigInt.type] = "(?i)long|bigint".r ^^ (_ => SQLTypes.BigInt)
479498

480499
def double_type: PackratParser[SQLTypes.Double.type] = "(?i)double".r ^^ (_ => SQLTypes.Double)
481500

482-
def int_type: PackratParser[SQLTypes.Int.type] = "(?i)(int|integer)".r ^^ (_ => SQLTypes.Int)
501+
def float_type: PackratParser[SQLTypes.Real.type] = "(?i)float|real".r ^^ (_ => SQLTypes.Real)
483502

484503
def sql_type: PackratParser[SQLType] =
485-
string_type | datetime_type | timestamp_type | date_type | time_type | boolean_type | long_type | double_type | int_type
504+
char_type | string_type | datetime_type | timestamp_type | date_type | time_type | boolean_type | long_type | double_type | float_type | int_type | short_type | byte_type
486505

487506
private[this] def castFunctionWithIdentifier: PackratParser[SQLIdentifier] =
488507
"(?i)cast".r ~ start ~ (identifierWithTransformation | identifierWithSystemFunction | identifierWithArithmeticFunction | identifierWithFunction | date_diff_identifier | extract_identifier | identifier) ~ Alias.regex.? ~ sql_type ~ end ~ arithmeticFunction.? ^^ {

sql/src/main/scala/app/softnetwork/elastic/sql/SQLTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object SQLTypes {
1919
case object Int extends SQLInt { val typeId = "int" }
2020
case object BigInt extends SQLBigInt { val typeId = "bigint" }
2121
case object Double extends SQLDouble { val typeId = "double" }
22-
case object Real extends SQLReal { val typeId = "float" }
22+
case object Real extends SQLReal { val typeId = "real" }
2323

2424
case object Literal extends SQLLiteral { val typeId = "literal" }
2525

sql/src/test/scala/app/softnetwork/elastic/sql/SQLParserSpec.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ object Queries {
143143
"select coalesce(nullif(createdAt, parse_date('2025-09-11', 'yyyy-MM-dd') - interval 2 day), current_date) as c, identifier from Table"
144144
val cast: String =
145145
"select cast(coalesce(nullif(createdAt, parse_date('2025-09-11', 'yyyy-MM-dd')), current_date - interval 2 hour) bigint) as c, identifier from Table"
146+
val allCasts =
147+
"select cast(identifier as int) as c1, cast(identifier as bigint) as c2, cast(identifier as double) as c3, cast(identifier as real) as c4, cast(identifier as boolean) as c5, cast(identifier as char) as c6, cast(identifier as varchar) as c7, cast(createdAt as date) as c8, cast(createdAt as time) as c9, cast(createdAt as datetime) as c10, cast(createdAt as timestamp) as c11, cast(identifier as smallint) as c12, cast(identifier as tinyint) as c13 from Table"
146148
val caseWhen: String =
147149
"select case when lastUpdated > now - interval 7 day then lastUpdated when isnotnull(lastSeen) then lastSeen + interval 2 day else createdAt end as c, identifier from Table"
148150
val caseWhenExpr: String =
@@ -573,6 +575,13 @@ class SQLParserSpec extends AnyFlatSpec with Matchers {
573575
)
574576
}
575577

578+
it should "parse all casts function" in {
579+
val result = SQLParser(allCasts)
580+
result.toOption.flatMap(_.left.toOption.map(_.sql)).getOrElse("") should ===(
581+
allCasts
582+
)
583+
}
584+
576585
it should "parse case when expression" in {
577586
val result = SQLParser(caseWhen)
578587
result.toOption.flatMap(_.left.toOption.map(_.sql)).getOrElse("") should ===(

0 commit comments

Comments
 (0)