← Booker Gong

Saspark: a SAS to Python/Scala Transpiler

Created a trans-compiler from SAS Data-steps transformation to Spark Dataframe API, generating well-organized Python or Scala code over Spark API with Scala & parser combinator, presented at Spark Summit East

Saspark is a Trans-Compiler from SAS Dataset Transformation Language to correpsonding structures in Spark Dataframes with Python, Scala. It has three main components:

Example

Variable

  1. Table Reference vs Variable
    • In SAS data step, all variables are inside SAS dataset, thus new variable and table column are not distinguished
    • In Spark transformation, however, we need to interpret if a variable is from source table before runtime.
    • In order to distinguish these, we keep track of all new varaible created, and regard all the rest referenece as table columns
  2. Table
    • In Python, table columns is called via row.xxx
    • In Scala, all column type need to be declared explicitly, like row.getAs[String]("xxx")
  3. Variable Scopes
    • In Python and SAS, variable declaration is not required, and global scope is always available
    • In Scala, situation become more complicated as declaration & type inference comes in
      • Top level varaibles can be directly created via var
      • Variables created in the inner scopes, corresponding to SAS's IF/DO block & Python's Indentation, are also required in outer scopes. Thus we have to create them and declare their types explicitly in Scala as well, like var someVar : Int
IF column=1 THEN b=1;
ELSE b=2;

y=0;
IF column=1 THEN DO; y="ONE"; END;
ELSE y="NOT ONE";
//[Any] must be replaced with actual type for scala to compile correctly
val column = row.getAs[Any]("column")
var b : Any

if (column == 1) b = 1
else b = 2
/*Local variables are promoted to global level in Scala transformation*/
var y = 0
if (column == 1) y = "ONE"
else y = "NOT ONE"

IF Statement

IF a=7 THEN r = "seven";
ELSE IF a=8 THEN r = "eight";
ELSE r = "other";
r = {
  7: "seven",
  8: "eight",
}.get(a) or "other"
r = a match {
  case 7 => "seven"
  case 8 => "eight"
  case _ => "other"
}

DO Statement

IF isNumber THEN DO; x=0; x=x+1; END;
ELSE x="number";
if row.isNumber:
    x = 0
    x = x + 1
else:
    x = "number"
if (isNumber) {
    x = 0
    x = x + 1
}
else x = "number"

Comparison Expression