Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC][DON'T MERGE] Transform ANTLR4 to SqlNode #999

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lazy val jacksonVersion = "2.15.2"
lazy val opensearchVersion = "2.6.0"
lazy val opensearchMavenVersion = "2.6.0.0"
lazy val icebergVersion = "1.5.0"
lazy val calciteVersion = "1.37.0"

val scalaMinorVersion = scala212.split("\\.").take(2).mkString(".")
val sparkMinorVersion = sparkVersion.split("\\.").take(2).mkString(".")
Expand Down Expand Up @@ -120,6 +121,7 @@ lazy val flintCore = (project in file("flint-core"))
exclude ("com.fasterxml.jackson.core", "jackson-core")
exclude ("org.apache.httpcomponents.client5", "httpclient5"),
"org.opensearch" % "opensearch-job-scheduler-spi" % opensearchMavenVersion,
"org.apache.calcite" % "calcite-core" % calciteVersion,
"dev.failsafe" % "failsafe" % "3.3.2",
"com.amazonaws" % "aws-java-sdk" % "1.12.397" % "provided"
exclude ("com.fasterxml.jackson.core", "jackson-databind"),
Expand Down Expand Up @@ -193,6 +195,7 @@ lazy val pplSparkIntegration = (project in file("ppl-spark-integration"))
"com.stephenn" %% "scalatest-json-jsonassert" % "0.2.5" % "test",
"com.github.sbt" % "junit-interface" % "0.13.3" % "test",
"org.projectlombok" % "lombok" % "1.18.30",
"org.apache.calcite" % "calcite-core" % calciteVersion,
"com.github.seancfoley" % "ipaddress" % "5.5.1",
),
libraryDependencies ++= deps(sparkVersion),
Expand Down Expand Up @@ -228,6 +231,7 @@ lazy val flintSparkIntegration = (project in file("flint-spark-integration"))
libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk" % "1.12.397" % "provided"
exclude ("com.fasterxml.jackson.core", "jackson-databind"),
"org.apache.calcite" % "calcite-core" % calciteVersion,
"org.scalactic" %% "scalactic" % "3.2.15" % "test",
"org.scalatest" %% "scalatest" % "3.2.15" % "test",
"org.scalatest" %% "scalatest-flatspec" % "3.2.15" % "test",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.flint.spark.ppl

import java.util

import org.apache.calcite.sql.parser.SqlParserPos.ZERO
import org.apache.calcite.sql.{SqlCall, SqlLiteral, SqlNode, SqlOperator}

case class Function(functionName: String, sqlOperator: SqlOperator) {

def createCall(function: SqlNode, operands: util.List[SqlNode], qualifier: SqlLiteral): SqlCall =
sqlOperator.createCall(qualifier, ZERO, operands)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.flint.spark.ppl

import scala.collection.JavaConverters._

import org.apache.calcite.adapter.java.JavaTypeFactory
import org.apache.calcite.prepare.CalciteCatalogReader
import org.apache.calcite.rel.`type`.RelDataType
import org.apache.calcite.sql.parser.SqlParserPos.ZERO
import org.apache.calcite.sql.validate.SqlValidator.Config
import org.apache.calcite.sql.validate.SqlValidatorImpl
import org.apache.calcite.sql.{SqlNodeList, SqlOperatorTable, SqlSelect}

class MyValidator(opTab: SqlOperatorTable, catalogReader: CalciteCatalogReader, typeFactory: JavaTypeFactory, config: Config)
extends SqlValidatorImpl(opTab, catalogReader, typeFactory, config) {

override def expandStar(selectList: SqlNodeList, select: SqlSelect, includeSystemVars: Boolean): SqlNodeList = {
val (starExcepts, others) = selectList.asScala.partition(_.isInstanceOf[StarExcept])
val starExceptList = starExcepts.flatMap(starExcept => {
val originList = super.expandStar(SqlNodeList.of(starExcept), select, includeSystemVars)
val exceptList = super.expandStar(starExcept.asInstanceOf[StarExcept].exceptList, select, includeSystemVars)
val exceptListStr = exceptList.asScala.map(_.toString)
originList.asScala.filter(item => !exceptListStr.contains(item.toString))
})
val otherList = super.expandStar(SqlNodeList.of(ZERO, others.asJava), select, includeSystemVars)
val expandedList = SqlNodeList.of(ZERO, (otherList.asScala ++ starExceptList).asJava)
getRawSelectScope(select).setExpandedSelectList(expandedList)
expandedList
}

override def validateSelectList (selectItems: SqlNodeList, select: SqlSelect, targetRowType: RelDataType): RelDataType = {
val (starExcepts, others) = selectItems.asScala.partition(_.isInstanceOf[StarExcept])
val (starExceptList, starExceptExpandedList) = starExcepts.map(starExcept => {
val originList = super.validateSelectList(SqlNodeList.of(starExcept), select, targetRowType)
val originExpandedList = getRawSelectScope(select).getExpandedSelectList
val exceptList = super.validateSelectList(starExcept.asInstanceOf[StarExcept].exceptList, select, targetRowType)
val exceptListStr = exceptList.getFieldNames.asScala
val exceptExpandedList = getRawSelectScope(select).getExpandedSelectList
val exceptExpandedListStr = exceptExpandedList.asScala.map(_.toString)
(originList.getFieldList.asScala.filter(field => !exceptListStr.contains(field.getName)),
originExpandedList.asScala.filter(item => !exceptExpandedListStr.contains(item.toString)))
}).unzip
val otherList = super.validateSelectList(SqlNodeList.of(ZERO, others.asJava), select, targetRowType)
val newSelectItems = (getRawSelectScope(select).getExpandedSelectList.asScala ++ starExceptExpandedList.flatten).asJava
if (config.identifierExpansion) {
select.setSelectList(SqlNodeList.of(ZERO, newSelectItems))
}
getRawSelectScope(select).setExpandedSelectList(newSelectItems)
typeFactory.createStructType((otherList.getFieldList.asScala ++ starExceptList.flatten).asJava)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.flint.spark.ppl

import org.apache.calcite.sql.SqlOperator
import org.apache.calcite.sql.fun.SqlStdOperatorTable

case class PPLFunctionResolver() {
def resolve(name: String): SqlOperator = {
name match {
case "=" => SqlStdOperatorTable.EQUALS
case "+" => SqlStdOperatorTable.PLUS
case "-" => SqlStdOperatorTable.MINUS
case "avg" => SqlStdOperatorTable.AVG
case "min" => SqlStdOperatorTable.MIN
case "max" => SqlStdOperatorTable.MAX
case "count" => SqlStdOperatorTable.COUNT
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/
package org.opensearch.flint.spark.ppl

import org.antlr.v4.runtime.{CommonTokenStream, Lexer}
import org.antlr.v4.runtime.tree.ParseTree
import org.antlr.v4.runtime.{CommonTokenStream, Lexer}
import org.opensearch.sql.ast.statement.Statement
import org.opensearch.sql.common.antlr.{CaseInsensitiveCharStream, Parser, SyntaxAnalysisErrorListener}
import org.opensearch.sql.ppl.parser.{AstBuilder, AstExpressionBuilder, AstStatementBuilder}
import org.opensearch.sql.ppl.parser.{AstBuilder, AstStatementBuilder}

class PPLSyntaxParser extends Parser {
// Analyze the query syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.flint.spark.ppl


import com.google.common.collect.ImmutableList
import lombok.Getter
import scala.collection.JavaConverters._

import org.apache.calcite.sql.parser.SqlParserPos
import org.apache.calcite.sql.parser.SqlParserPos.ZERO
import org.apache.calcite.sql.{SqlIdentifier, SqlNodeList, SqlWriter}

@Getter
case class StarExcept(exceptList: SqlNodeList)(names: Seq[String] = Seq(""), pos: SqlParserPos = ZERO)
extends SqlIdentifier(names.asJava, pos) {

override def toString: String = {
super.toString + " EXCEPT " + exceptList.toString
}

override def unparse(writer: SqlWriter, leftPrec: Int, rightPrec: Int): Unit = {
super.unparse(writer, leftPrec, rightPrec)
writer.keyword("EXCEPT")
writer.list(SqlWriter.FrameTypeEnum.PARENTHESES, SqlWriter.COMMA, exceptList)
}
}
Loading
Loading