Skip to content

Commit

Permalink
Split program environment between Snapi and SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbranco80 committed Aug 6, 2024
1 parent 3c2ea83 commit 4aea6d2
Show file tree
Hide file tree
Showing 27 changed files with 326 additions and 347 deletions.
134 changes: 31 additions & 103 deletions client/src/main/scala/raw/client/api/ProgramEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,110 +12,38 @@

package raw.client.api

import com.fasterxml.jackson.annotation.JsonSubTypes.{Type => JsonType}
import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.scala.{ClassTagExtensions, DefaultScalaModule}
import raw.utils.RawUid

final case class ProgramEnvironment(
uid: RawUid,
maybeArguments: Option[Array[(String, RawValue)]],
scopes: Set[String],
secrets: Map[String, String],
jdbcServers: Map[String, JdbcLocation],
httpHeaders: Map[String, Map[String, String]],
s3Credentials: Map[String, S3Credential],
options: Map[String, String],
jdbcUrl: Option[String] = None,
maybeTraceId: Option[String] = None
)

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
Array(
new JsonType(value = classOf[MySqlJdbcLocation], name = "mysql"),
new JsonType(value = classOf[OracleJdbcLocation], name = "oracle"),
new JsonType(value = classOf[PostgresJdbcLocation], name = "postgres"),
new JsonType(value = classOf[SqlServerJdbcLocation], name = "sqlserver"),
new JsonType(value = classOf[SnowflakeJdbcLocation], name = "snowflake"),
new JsonType(value = classOf[SqliteJdbcLocation], name = "sqlite"),
new JsonType(value = classOf[TeradataJdbcLocation], name = "teradata")
)
)
trait JdbcLocation
final case class MySqlJdbcLocation(
host: String,
port: Int,
database: String,
username: String,
password: String
) extends JdbcLocation
final case class OracleJdbcLocation(
host: String,
port: Int,
database: String,
username: String,
password: String
) extends JdbcLocation
final case class PostgresJdbcLocation(
host: String,
port: Int,
database: String,
username: String,
password: String
) extends JdbcLocation
final case class SqlServerJdbcLocation(
host: String,
port: Int,
database: String,
username: String,
password: String
) extends JdbcLocation
final case class SnowflakeJdbcLocation(
database: String,
username: String,
password: String,
accountIdentifier: String,
parameters: Map[String, String]
) extends JdbcLocation
final case class SqliteJdbcLocation(
path: String
) extends JdbcLocation
final case class TeradataJdbcLocation(
host: String,
port: Int,
database: String,
username: String,
password: String,
parameters: Map[String, String]
) extends JdbcLocation

final case class S3Credential(
accessKey: Option[String],
secretKey: Option[String],
region: Option[String]
)

object ProgramEnvironment {

private val jsonMapper = new ObjectMapper with ClassTagExtensions {
registerModule(DefaultScalaModule)
registerModule(new JavaTimeModule())
registerModule(new Jdk8Module())
}

private val reader = jsonMapper.readerFor[ProgramEnvironment]
private val writer = jsonMapper.writerFor[ProgramEnvironment]

def serializeToString(env: ProgramEnvironment): String = {
writer.writeValueAsString(env)
}

def deserializeFromString(str: String): ProgramEnvironment = {
reader.readValue(str)
}
trait ProgramEnvironment {

/**
* The user id of the program.
* @return the user id
*/
def uid: RawUid

/**
* The arguments of the program.
* @return the arguments
*/
def maybeArguments: Option[Array[(String, RawValue)]]

/**
* The scopes of the program.
* @return the scopes
*/
def scopes: Set[String]

/**
* The options of the program.
* @return the options
*/
def options: Map[String, String]

/**
* The traceId to include in logging messages.
* @return the traceId
*/
def maybeTraceId: Option[String]

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package raw.client.python

import org.graalvm.polyglot.{Context, Engine, PolyglotAccess, PolyglotException, Source, Value}
import org.graalvm.polyglot.{Context, Engine, PolyglotException, Source, Value}
import raw.client.api._
import raw.client.writers.{PolyglotBinaryWriter, PolyglotCsvWriter, PolyglotJsonWriter, PolyglotTextWriter}
import raw.utils.{RawSettings, RawUtils}
Expand Down Expand Up @@ -266,16 +266,17 @@ class PythonCompilerService(engineDefinition: (Engine, Boolean))(implicit protec
environment: ProgramEnvironment,
maybeOutputStream: Option[OutputStream]
): Context = {
// Add environment settings as hardcoded environment variables.
val ctxBuilder = Context
.newBuilder("python")
.engine(engine)
.environment("RAW_PROGRAM_ENVIRONMENT", ProgramEnvironment.serializeToString(environment))
.allowExperimentalOptions(true)
.allowPolyglotAccess(PolyglotAccess.ALL)
maybeOutputStream.foreach(os => ctxBuilder.out(os))
val ctx = ctxBuilder.build()
ctx
???
// // Add environment settings as hardcoded environment variables.
// val ctxBuilder = Context
// .newBuilder("python")
// .engine(engine)
// .environment("RAW_PROGRAM_ENVIRONMENT", PythonProgramEnvironment.serializeToString(environment))
// .allowExperimentalOptions(true)
// .allowPolyglotAccess(PolyglotAccess.ALL)
// maybeOutputStream.foreach(os => ctxBuilder.out(os))
// val ctx = ctxBuilder.build()
// ctx
}

// private def withTruffleContext[T](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

package raw.client.rql2.api

import raw.client.api.{CompilerService, Message, ProgramEnvironment}
import raw.client.api.{CompilerService, Message}
import raw.compiler.base.source.{BaseNode, Type}
import raw.compiler.common.source.SourceProgram
import raw.compiler.rql2.api.Rql2ProgramEnvironment
import raw.utils.RawUid

trait Rql2CompilerService extends CompilerService {
Expand All @@ -23,11 +24,11 @@ trait Rql2CompilerService extends CompilerService {

def parseType(tipe: String, user: RawUid, internal: Boolean = false): ParseTypeResponse

def parse(source: String, environment: ProgramEnvironment): ParseResponse
def parse(source: String, environment: Rql2ProgramEnvironment): ParseResponse

def getType(
source: String,
environment: ProgramEnvironment
environment: Rql2ProgramEnvironment
): GetTypeResponse

}
Expand Down
Loading

0 comments on commit 4aea6d2

Please sign in to comment.