Skip to content

Commit

Permalink
sql (fix): Support POSITION (#3744)
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe authored Dec 5, 2024
1 parent 3d94d33 commit f6df0c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1336,4 +1336,10 @@ object Expression {
override def toString = s"Extract(interval:${interval}, ${expr})"
}

case class Position(substring: Expression, string: Expression, nodeLocation: Option[NodeLocation])
extends Expression {
override def children: Seq[Expression] = Seq(substring, string)
override def sqlExpr: String = s"POSITION(${substring.sqlExpr} IN ${string.sqlExpr})"
override def toString = sqlExpr
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -992,4 +992,8 @@ class SQLInterpreter(withNodeLocation: Boolean = true) extends SqlBaseBaseVisito
val ifExists = Option(ctx.EXISTS()).map(x => true).getOrElse(false)
DropView(viewName, ifExists, getLocation(ctx))
}

override def visitPosition(ctx: PositionContext): Expression = {
Position(expression(ctx.valueExpression(0)), expression(ctx.valueExpression(1)), getLocation(ctx))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,12 @@ class SQLGeneratorTest extends AirSpec {
val sql = SQLGenerator.print(resolvedPlan)
sql shouldBe "SELECT TIMESTAMP '1992-02-01 00:00 UTC' AT TIME ZONE 'Asia/Tokyo'"
}

test("print POSITION") {
val resolvedPlan =
SQLAnalyzer.analyze("SELECT xid FROM A WHERE POSITION('str' IN xid) > 0", "default", demoCatalog)

val sql = SQLGenerator.print(resolvedPlan)
sql shouldBe "SELECT xid FROM A WHERE POSITION('str' IN xid) > 0"
}
}

0 comments on commit f6df0c1

Please sign in to comment.