Skip to content

Commit b6605e7

Browse files
committed
updat sql types
1 parent 1dc428d commit b6605e7

File tree

11 files changed

+188
-89
lines changed

11 files changed

+188
-89
lines changed

es6/sql-bridge/src/main/scala/app/softnetwork/elastic/sql/bridge/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ package object bridge {
136136
)
137137
}
138138

139-
def applyNumericOp[A](n: SQLNumeric[_])(
139+
def applyNumericOp[A](n: SQLNumericValue[_])(
140140
longOp: Long => A,
141141
doubleOp: Double => A
142142
): A = n.toEither.fold(longOp, doubleOp)
@@ -149,7 +149,7 @@ package object bridge {
149149
return scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
150150
}
151151
value match {
152-
case n: SQLNumeric[_] =>
152+
case n: SQLNumericValue[_] =>
153153
operator match {
154154
case Ge =>
155155
maybeNot match {
@@ -231,7 +231,7 @@ package object bridge {
231231
}
232232
case _ => matchAllQuery()
233233
}
234-
case l: SQLLiteral =>
234+
case l: SQLStringValue =>
235235
operator match {
236236
case Like =>
237237
maybeNot match {

sql/bridge/src/main/scala/app/softnetwork/elastic/sql/bridge/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ package object bridge {
137137
)
138138
}
139139

140-
def applyNumericOp[A](n: SQLNumeric[_])(
140+
def applyNumericOp[A](n: SQLNumericValue[_])(
141141
longOp: Long => A,
142142
doubleOp: Double => A
143143
): A = n.toEither.fold(longOp, doubleOp)
@@ -150,7 +150,7 @@ package object bridge {
150150
return scriptQuery(Script(script = painless).lang("painless").scriptType("source"))
151151
}
152152
value match {
153-
case n: SQLNumeric[_] =>
153+
case n: SQLNumericValue[_] =>
154154
operator match {
155155
case Ge =>
156156
maybeNot match {
@@ -232,7 +232,7 @@ package object bridge {
232232
}
233233
case _ => matchAllQuery()
234234
}
235-
case l: SQLLiteral =>
235+
case l: SQLStringValue =>
236236
operator match {
237237
case Like =>
238238
maybeNot match {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,10 @@ case class DateTrunc(identifier: SQLIdentifier, unit: TimeUnit)
368368
case class Extract(unit: TimeUnit, override val sql: String = "extract")
369369
extends SQLExpr(sql)
370370
with DateTimeFunction
371-
with SQLTransformFunction[SQLTemporal, SQLNumber]
371+
with SQLTransformFunction[SQLTemporal, SQLNumeric]
372372
with ParametrizedFunction {
373373
override def inputType: SQLTemporal = SQLTypes.Temporal
374-
override def outputType: SQLNumber = SQLTypes.Number
374+
override def outputType: SQLNumeric = SQLTypes.Numeric
375375
override def params: Seq[String] = Seq(unit.sql)
376376
override def painless: String = s".get(${unit.painless})"
377377
}
@@ -403,9 +403,9 @@ object SECOND extends Extract(Second, Second.sql) {
403403
case class DateDiff(end: PainlessScript, start: PainlessScript, unit: TimeUnit)
404404
extends SQLExpr("date_diff")
405405
with DateTimeFunction
406-
with SQLBinaryFunction[SQLDateTime, SQLDateTime, SQLNumber]
406+
with SQLBinaryFunction[SQLDateTime, SQLDateTime, SQLNumeric]
407407
with PainlessScript {
408-
override def outputType: SQLNumber = SQLTypes.Number
408+
override def outputType: SQLNumeric = SQLTypes.Numeric
409409
override def left: PainlessScript = end
410410
override def right: PainlessScript = start
411411
override def toSQL(base: String): String = {
@@ -452,9 +452,9 @@ case class DateSub(identifier: SQLIdentifier, interval: TimeInterval)
452452
case class ParseDate(identifier: SQLIdentifier, format: String)
453453
extends SQLExpr("parse_date")
454454
with DateFunction
455-
with SQLTransformFunction[SQLString, SQLDate]
455+
with SQLTransformFunction[SQLVarchar, SQLDate]
456456
with SQLFunctionWithIdentifier {
457-
override def inputType: SQLString = SQLTypes.String
457+
override def inputType: SQLVarchar = SQLTypes.Varchar
458458
override def outputType: SQLDate = SQLTypes.Date
459459
override def toSQL(base: String): String = {
460460
s"$sql($base, '$format')"
@@ -470,10 +470,10 @@ case class ParseDate(identifier: SQLIdentifier, format: String)
470470
case class FormatDate(identifier: SQLIdentifier, format: String)
471471
extends SQLExpr("format_date")
472472
with DateFunction
473-
with SQLTransformFunction[SQLDate, SQLString]
473+
with SQLTransformFunction[SQLDate, SQLVarchar]
474474
with SQLFunctionWithIdentifier {
475475
override def inputType: SQLDate = SQLTypes.Date
476-
override def outputType: SQLString = SQLTypes.String
476+
override def outputType: SQLVarchar = SQLTypes.Varchar
477477
override def toSQL(base: String): String = {
478478
s"$sql($base, '$format')"
479479
}
@@ -514,9 +514,9 @@ case class DateTimeSub(identifier: SQLIdentifier, interval: TimeInterval)
514514
case class ParseDateTime(identifier: SQLIdentifier, format: String)
515515
extends SQLExpr("parse_datetime")
516516
with DateTimeFunction
517-
with SQLTransformFunction[SQLString, SQLDateTime]
517+
with SQLTransformFunction[SQLVarchar, SQLDateTime]
518518
with SQLFunctionWithIdentifier {
519-
override def inputType: SQLString = SQLTypes.String
519+
override def inputType: SQLVarchar = SQLTypes.Varchar
520520
override def outputType: SQLDateTime = SQLTypes.DateTime
521521
override def toSQL(base: String): String = {
522522
s"$sql($base, '$format')"
@@ -532,10 +532,10 @@ case class ParseDateTime(identifier: SQLIdentifier, format: String)
532532
case class FormatDateTime(identifier: SQLIdentifier, format: String)
533533
extends SQLExpr("format_datetime")
534534
with DateTimeFunction
535-
with SQLTransformFunction[SQLDateTime, SQLString]
535+
with SQLTransformFunction[SQLDateTime, SQLVarchar]
536536
with SQLFunctionWithIdentifier {
537537
override def inputType: SQLDateTime = SQLTypes.DateTime
538-
override def outputType: SQLString = SQLTypes.String
538+
override def outputType: SQLVarchar = SQLTypes.Varchar
539539
override def toSQL(base: String): String = {
540540
s"$sql($base, '$format')"
541541
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ case object NullIf extends SQLExpr("nullif") with SQLConditionalOperator
6464
case object Exists extends SQLExpr("exists") with SQLConditionalOperator
6565
case object Cast extends SQLExpr("cast") with SQLConditionalOperator
6666
case object Case extends SQLExpr("case") with SQLConditionalOperator
67-
case object When extends SQLExpr("when") with SQLConditionalOperator
68-
case object Then extends SQLExpr("then") with SQLConditionalOperator
69-
case object Else extends SQLExpr("else") with SQLConditionalOperator
70-
case object End extends SQLExpr("end") with SQLConditionalOperator
67+
case object When extends SQLExpr("when") with SQLRegex
68+
case object Then extends SQLExpr("then") with SQLRegex
69+
case object Else extends SQLExpr("else") with SQLRegex
70+
case object End extends SQLExpr("end") with SQLRegex
7171

7272
sealed trait SQLPredicateOperator extends SQLLogicalOperator
7373

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ case class SQLParserError(msg: String) extends SQLCompilationError
5959

6060
trait SQLParser extends RegexParsers with PackratParsers {
6161

62-
def literal: PackratParser[SQLLiteral] =
63-
""""[^"]*"|'[^']*'""".r ^^ (str => SQLLiteral(str.substring(1, str.length - 1)))
62+
def literal: PackratParser[SQLStringValue] =
63+
""""[^"]*"|'[^']*'""".r ^^ (str => SQLStringValue(str.substring(1, str.length - 1)))
6464

65-
def long: PackratParser[SQLLong] = """(-)?(0|[1-9]\d*)""".r ^^ (str => SQLLong(str.toLong))
65+
def long: PackratParser[SQLLongValue] =
66+
"""(-)?(0|[1-9]\d*)""".r ^^ (str => SQLLongValue(str.toLong))
6667

67-
def double: PackratParser[SQLDouble] = """(-)?(\d+\.\d+)""".r ^^ (str => SQLDouble(str.toDouble))
68+
def double: PackratParser[SQLDoubleValue] =
69+
"""(-)?(\d+\.\d+)""".r ^^ (str => SQLDoubleValue(str.toDouble))
6870

6971
def boolean: PackratParser[SQLBoolean] =
7072
"""(true|false)""".r ^^ (bool => SQLBoolean(bool.toBoolean))
@@ -171,29 +173,30 @@ trait SQLParser extends RegexParsers with PackratParsers {
171173
DateTrunc(i, u)
172174
}
173175

174-
def extract: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
176+
def extract: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
175177
"(?i)extract".r ~ start ~ time_unit ~ end ^^ { case _ ~ _ ~ u ~ _ =>
176178
Extract(u)
177179
}
178180

179-
def extract_year: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
181+
def extract_year: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
180182
Year.regex ^^ (_ => YEAR)
181183

182-
def extract_month: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
184+
def extract_month: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
183185
Month.regex ^^ (_ => MONTH)
184186

185-
def extract_day: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] = Day.regex ^^ (_ => DAY)
187+
def extract_day: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
188+
Day.regex ^^ (_ => DAY)
186189

187-
def extract_hour: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
190+
def extract_hour: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
188191
Hour.regex ^^ (_ => HOUR)
189192

190-
def extract_minute: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
193+
def extract_minute: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
191194
Minute.regex ^^ (_ => MINUTE)
192195

193-
def extract_second: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
196+
def extract_second: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
194197
Second.regex ^^ (_ => SECOND)
195198

196-
def extractors: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumber]] =
199+
def extractors: PackratParser[SQLUnaryFunction[SQLTemporal, SQLNumeric]] =
197200
extract | extract_year | extract_month | extract_day | extract_hour | extract_minute | extract_second
198201

199202
def date_add: PackratParser[DateFunction with SQLFunctionWithIdentifier] =
@@ -212,7 +215,7 @@ trait SQLParser extends RegexParsers with PackratParsers {
212215
"(?i)parse_date".r ~ start ~ (identifierWithTemporalFunction | identifierWithSystemFunction | identifierWithArithmeticFunction | literal | identifier) ~ separator ~ literal ~ end ^^ {
213216
case _ ~ _ ~ li ~ _ ~ f ~ _ =>
214217
li match {
215-
case l: SQLLiteral =>
218+
case l: SQLStringValue =>
216219
ParseDate(SQLIdentifier("", functions = l :: Nil), f.value)
217220
case i: SQLIdentifier =>
218221
ParseDate(i, f.value)
@@ -416,7 +419,7 @@ trait SQLParser extends RegexParsers with PackratParsers {
416419
)
417420
}
418421

419-
def string_type: PackratParser[SQLTypes.String.type] = "(?i)string".r ^^ (_ => SQLTypes.String)
422+
def string_type: PackratParser[SQLTypes.Varchar.type] = "(?i)string".r ^^ (_ => SQLTypes.Varchar)
420423

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

@@ -431,7 +434,7 @@ trait SQLParser extends RegexParsers with PackratParsers {
431434
def boolean_type: PackratParser[SQLTypes.Boolean.type] =
432435
"(?i)boolean".r ^^ (_ => SQLTypes.Boolean)
433436

434-
def long_type: PackratParser[SQLTypes.Long.type] = "(?i)long".r ^^ (_ => SQLTypes.Long)
437+
def long_type: PackratParser[SQLTypes.BigInt.type] = "(?i)long".r ^^ (_ => SQLTypes.BigInt)
435438

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

@@ -600,7 +603,7 @@ trait SQLWhereParser {
600603
case i ~ n ~ _ ~ _ ~ v ~ _ =>
601604
SQLIn(
602605
i,
603-
SQLLiteralValues(v),
606+
SQLStringValues(v),
604607
n
605608
)
606609
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@ package app.softnetwork.elastic.sql
33
sealed trait SQLType { def typeId: String }
44

55
trait SQLAny extends SQLType
6+
67
trait SQLTemporal extends SQLType
8+
79
trait SQLDate extends SQLTemporal
810
trait SQLTime extends SQLTemporal
911
trait SQLDateTime extends SQLTemporal
10-
trait SQLNumber extends SQLType
11-
trait SQLString extends SQLType
12+
trait SQLTimestamp extends SQLDateTime
13+
14+
trait SQLNumeric extends SQLType
15+
16+
trait SQLTinyInt extends SQLNumeric
17+
trait SQLSmallInt extends SQLNumeric
18+
trait SQLInt extends SQLNumeric
19+
trait SQLBigInt extends SQLNumeric
20+
trait SQLDouble extends SQLNumeric
21+
trait SQLReal extends SQLNumeric
22+
23+
trait SQLLiteral extends SQLType
24+
trait SQLVarchar extends SQLLiteral
25+
trait SQLChar extends SQLLiteral
26+
1227
trait SQLBool extends SQLType
28+
29+
trait SQLArray extends SQLType { def elementType: SQLType }
30+
31+
trait SQLStruct extends SQLType

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@ object SQLTypeUtils {
2121
).contains(
2222
out.typeId
2323
)) ||
24-
(out.typeId == Number.typeId && Set(Int.typeId, Long.typeId, Double.typeId, Float.typeId)
24+
(out.typeId == Numeric.typeId && Set(
25+
TinyInt.typeId,
26+
SmallInt.typeId,
27+
Int.typeId,
28+
BigInt.typeId,
29+
Double.typeId,
30+
Real.typeId
31+
)
2532
.contains(
2633
in.typeId
2734
)) ||
28-
(in.typeId == Number.typeId && Set(Int.typeId, Long.typeId, Double.typeId, Float.typeId)
35+
(in.typeId == Numeric.typeId && Set(
36+
TinyInt.typeId,
37+
SmallInt.typeId,
38+
Int.typeId,
39+
BigInt.typeId,
40+
Double.typeId,
41+
Real.typeId
42+
)
2943
.contains(
3044
out.typeId
3145
)) ||
32-
(out.typeId == String.typeId && in.typeId == String.typeId) ||
46+
(out.typeId == Varchar.typeId && in.typeId == Varchar.typeId) ||
3347
(out.typeId == Boolean.typeId && in.typeId == Boolean.typeId) ||
3448
out.typeId == Any.typeId || in.typeId == Any.typeId ||
3549
out.typeId == Null.typeId || in.typeId == Null.typeId
@@ -39,13 +53,16 @@ object SQLTypeUtils {
3953
if (distinct.size == 1) return distinct.head
4054

4155
// 1. String
42-
if (distinct.contains(SQLTypes.String)) return SQLTypes.String
56+
if (distinct.contains(SQLTypes.Varchar)) return SQLTypes.Varchar
4357

4458
// 2. Number
4559
if (distinct.contains(SQLTypes.Double)) return SQLTypes.Double
46-
if (distinct.contains(SQLTypes.Long)) return SQLTypes.Long
60+
if (distinct.contains(SQLTypes.Real)) return SQLTypes.Real
61+
if (distinct.contains(SQLTypes.BigInt)) return SQLTypes.BigInt
4762
if (distinct.contains(SQLTypes.Int)) return SQLTypes.Int
48-
if (distinct.contains(SQLTypes.Number)) return SQLTypes.Number
63+
if (distinct.contains(SQLTypes.SmallInt)) return SQLTypes.SmallInt
64+
if (distinct.contains(SQLTypes.TinyInt)) return SQLTypes.TinyInt
65+
if (distinct.contains(SQLTypes.Numeric)) return SQLTypes.Numeric
4966

5067
// 3. Temporal
5168
if (distinct.contains(SQLTypes.Timestamp)) return SQLTypes.Timestamp
@@ -86,21 +103,21 @@ object SQLTypeUtils {
86103
s"($expr).toLocalTime()"
87104

88105
// ---- NUMERIQUES ----
89-
case (SQLTypes.Int, SQLTypes.Long) =>
106+
case (SQLTypes.Int, SQLTypes.BigInt) =>
90107
s"((long) $expr)"
91108
case (SQLTypes.Int, SQLTypes.Double) =>
92109
s"((double) $expr)"
93-
case (SQLTypes.Long, SQLTypes.Double) =>
110+
case (SQLTypes.BigInt, SQLTypes.Double) =>
94111
s"((double) $expr)"
95112

96113
// ---- NUMERIC <-> TEMPORAL ----
97-
case (SQLTypes.Long, SQLTypes.Timestamp) =>
114+
case (SQLTypes.BigInt, SQLTypes.Timestamp) =>
98115
s"Instant.ofEpochMilli($expr).atZone(ZoneId.of('Z'))"
99-
case (SQLTypes.Timestamp, SQLTypes.Long) =>
116+
case (SQLTypes.Timestamp, SQLTypes.BigInt) =>
100117
s"$expr.toInstant().toEpochMilli()"
101118

102119
// ---- BOOLEEN -> NUMERIC ----
103-
case (SQLTypes.Boolean, SQLTypes.Number) =>
120+
case (SQLTypes.Boolean, SQLTypes.Numeric) =>
104121
s"($expr ? 1 : 0)"
105122

106123
// ---- IDENTITY ----

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@ package app.softnetwork.elastic.sql
22

33
object SQLTypes {
44
case object Any extends SQLAny { val typeId = "any" }
5+
56
case object Null extends SQLAny { val typeId = "null" }
7+
68
case object Temporal extends SQLTemporal { val typeId = "temporal" }
9+
710
case object Date extends SQLTemporal with SQLDate { val typeId = "date" }
811
case object Time extends SQLTemporal with SQLTime { val typeId = "time" }
912
case object DateTime extends SQLTemporal with SQLDateTime { val typeId = "datetime" }
10-
case object Timestamp extends SQLTemporal with SQLDateTime { val typeId = "timestamp" }
11-
case object Number extends SQLNumber { val typeId = "number" }
12-
case object Int extends SQLNumber { val typeId = "integer" }
13-
case object Long extends SQLNumber { val typeId = "long" }
14-
case object Double extends SQLNumber { val typeId = "double" }
15-
case object Float extends SQLNumber { val typeId = "float" }
16-
case object String extends SQLString { val typeId = "string" }
13+
case object Timestamp extends SQLTimestamp { val typeId = "timestamp" }
14+
15+
case object Numeric extends SQLNumeric { val typeId = "numeric" }
16+
17+
case object TinyInt extends SQLTinyInt { val typeId = "tinyint" }
18+
case object SmallInt extends SQLSmallInt { val typeId = "smallint" }
19+
case object Int extends SQLInt { val typeId = "int" }
20+
case object BigInt extends SQLBigInt { val typeId = "bigint" }
21+
case object Double extends SQLDouble { val typeId = "double" }
22+
case object Real extends SQLReal { val typeId = "float" }
23+
24+
case object Literal extends SQLLiteral { val typeId = "literal" }
25+
26+
case object Char extends SQLChar { val typeId = "char" }
27+
case object Varchar extends SQLVarchar { val typeId = "varchar" }
28+
1729
case object Boolean extends SQLBool { val typeId = "boolean" }
30+
31+
case class Array(elementType: SQLType) extends SQLArray {
32+
val typeId = s"array<${elementType.typeId}>"
33+
}
34+
35+
case object Struct extends SQLStruct { val typeId = "struct" }
1836
}

0 commit comments

Comments
 (0)