Skip to content

Deprecate exprnode #535

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

Merged
merged 8 commits into from
Feb 25, 2022
Merged
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: 2 additions & 2 deletions docs/dev/CODE-STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ fun calculateArea(shape: Shape): Double {
```Kotlin
val sources = ArrayList<CompiledLetSource>() // [sources] is mutable!
letSource.bindings.forEach {
sources.add(CompiledLetSource(name = it.name.name, thunk = compileExprNode(it.expr)))
sources.add(CompiledLetSource(name = it.name.name, thunk = compileAstExpr(it.expr)))
}
```

## Good

```Kotlin
val sources = letSource.bindings.map {
CompiledLetSource(name = it.name.name, thunk = compileExprNode(it.expr)))
CompiledLetSource(name = it.name.name, thunk = compileAstExpr(it.expr))
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.amazon.ion.system.IonSystemBuilder
import com.amazon.ionelement.api.toIonElement
import org.partiql.examples.util.Example
import org.partiql.lang.CompilerPipeline
import org.partiql.lang.ast.toAstStatement
import org.partiql.lang.ast.toExprNode
import org.partiql.lang.domains.PartiqlAst
import org.partiql.lang.eval.CompileOptions
import org.partiql.lang.eval.EvaluationSession
Expand Down Expand Up @@ -71,7 +69,7 @@ private class PartialEvaluationVisitorTransform(val ion: IonSystem, val compileO

return when {
transformedOps.all { it is PartiqlAst.Expr.Lit } -> {
val e = pipeline.compile(PartiqlAst.build { query(transformedNAry) }.toExprNode(ion) )
val e = pipeline.compile(PartiqlAst.build { query(transformedNAry) } )
val partiallyEvaluatedResult = e.eval(session)
PartiqlAst.build { lit(partiallyEvaluatedResult.ionValue.toIonElement(), metas) }
}
Expand All @@ -88,18 +86,17 @@ class PartialEvaluationVisitorTransformExample(out: PrintStream) : Example(out)

override fun run() {
val pipeline = CompilerPipeline.build(ion) {
addPreprocessingStep { node, stepContext ->
addPreprocessingStep { originalAst, stepContext ->
val visitorTransformer = PartialEvaluationVisitorTransform(ion, stepContext.compileOptions)

val originalAst = node.toAstStatement()
print("Original AST:", originalAst.toString())

val query = node.toAstStatement() as PartiqlAst.Statement.Query
val query = originalAst as PartiqlAst.Statement.Query

val transformedNode = PartiqlAst.build { query(visitorTransformer.transformExpr(query.expr)) }
print("Transformed AST:", transformedNode.toString())

transformedNode.toExprNode(ion)
transformedNode
}
}

Expand Down
13 changes: 5 additions & 8 deletions lang/jmh/org/partiql/jmh/PartiQLBenchmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ open class PartiQLBenchmark {
}
}
""".trimIndent()
// TODO: replace `parseExprNode` with `ParseStatement` once evaluator deprecates `ExprNode`
val bindings = pipeline.compile(parser.parseExprNode(data)).eval(EvaluationSession.standard()).bindings
val bindings = pipeline.compile(parser.parseAstStatement(data)).eval(EvaluationSession.standard()).bindings
val session = EvaluationSession.build { globals(bindings) }

val query = "SELECT * FROM hr.employeesNestScalars"
// TODO: replace `parseExprNode` with `ParseStatement` once evaluator deprecates `ExprNode`
val exprNode = parser.parseExprNode(query)
val expression = pipeline.compile(exprNode)
val astStatement = parser.parseAstStatement(query)
val expression = pipeline.compile(astStatement)
}

/**
Expand All @@ -70,8 +68,7 @@ open class PartiQLBenchmark {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
fun testPartiQLParser(state: MyState, blackhole: Blackhole) {
// TODO: replace `parseExprNode` with `ParseStatement` once evaluator deprecates `ExprNode`
val expr = state.parser.parseExprNode(state.query)
val expr = state.parser.parseAstStatement(state.query)
blackhole.consume(expr)
}

Expand All @@ -81,7 +78,7 @@ open class PartiQLBenchmark {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
fun testPartiQLCompiler(state: MyState, blackhole: Blackhole) {
val exprValue = state.pipeline.compile(state.exprNode)
val exprValue = state.pipeline.compile(state.astStatement)
blackhole.consume(exprValue)
}

Expand Down
36 changes: 19 additions & 17 deletions lang/src/org/partiql/lang/CompilerPipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package org.partiql.lang
import com.amazon.ion.IonSystem
import org.partiql.lang.ast.ExprNode
import org.partiql.lang.ast.toAstStatement
import org.partiql.lang.ast.toExprNode
import org.partiql.lang.domains.PartiqlAst
import org.partiql.lang.eval.Bindings
import org.partiql.lang.eval.CompileOptions
import org.partiql.lang.eval.EvaluatingCompiler
Expand Down Expand Up @@ -61,10 +61,10 @@ data class StepContext(
)

/**
* [ProcessingStep] functions accept an [ExprNode] and [StepContext] as an arguments and processes them in some
* way and then returns either the original [ExprNode] or a modified [ExprNode].
* [ProcessingStep] functions accept an [PartiqlAst.Statement] and [StepContext] as an arguments and processes them in some
* way and then returns either the original [PartiqlAst.Statement] or a modified [PartiqlAst.Statement].
*/
typealias ProcessingStep = (ExprNode, StepContext) -> ExprNode
typealias ProcessingStep = (PartiqlAst.Statement, StepContext) -> PartiqlAst.Statement

/**
* [CompilerPipeline] is the main interface for compiling PartiQL queries into instances of [Expression] which
Expand Down Expand Up @@ -103,9 +103,13 @@ interface CompilerPipeline {
/** Compiles the specified PartiQL query using the configured parser. */
fun compile(query: String): Expression

@Deprecated("ExprNode is deprecated. Please use PIG generated AST. ")
/** Compiles the specified [ExprNode] instance. */
fun compile(query: ExprNode): Expression

/** Compiles the specified [PartiqlAst.Statement] instance. */
fun compile(query: PartiqlAst.Statement): Expression

companion object {
/** Kotlin style builder for [CompilerPipeline]. If calling from Java instead use [builder]. */
fun build(ion: IonSystem, block: Builder.() -> Unit) = build(ExprValueFactory.standard(ion), block)
Expand All @@ -115,11 +119,11 @@ interface CompilerPipeline {

/** Fluent style builder. If calling from Kotlin instead use the [build] method. */
@JvmStatic
fun builder(ion: IonSystem): CompilerPipeline.Builder = builder(ExprValueFactory.standard(ion))
fun builder(ion: IonSystem): Builder = builder(ExprValueFactory.standard(ion))

/** Fluent style builder. If calling from Kotlin instead use the [build] method. */
@JvmStatic
fun builder(valueFactory: ExprValueFactory): CompilerPipeline.Builder = Builder(valueFactory)
fun builder(valueFactory: ExprValueFactory): Builder = Builder(valueFactory)

/** Returns an implementation of [CompilerPipeline] with all properties set to their defaults. */
@JvmStatic
Expand All @@ -145,7 +149,7 @@ interface CompilerPipeline {
private var globalTypeBindings: Bindings<StaticType>? = null

/**
* Specifies the [Parser] to be used to turn an PartiQL query into an instance of [ExprNode].
* Specifies the [Parser] to be used to turn an PartiQL query into an instance of [PartiqlAst].
* The default is [SqlParser].
*/
fun sqlParser(p: Parser): Builder = this.apply { parser = p }
Expand Down Expand Up @@ -252,12 +256,11 @@ internal class CompilerPipelineImpl(
procedures,
compileOptions)

override fun compile(query: String): Expression {
// TODO: replace `parseExprNode` with `ParseStatement` once evaluator deprecates `ExprNode`
return compile(parser.parseExprNode(query))
}
override fun compile(query: String): Expression = compile(parser.parseAstStatement(query))

override fun compile(query: ExprNode): Expression {
override fun compile(query: ExprNode): Expression = compile(query.toAstStatement())

override fun compile(query: PartiqlAst.Statement): Expression {
val context = StepContext(valueFactory, compileOptions, functions, procedures)

val preProcessedQuery = executePreProcessingSteps(query, context)
Expand All @@ -284,12 +287,11 @@ internal class CompilerPipelineImpl(
}
).flatten().toTypedArray())

val queryToCompile = transforms.transformStatement(preProcessedQuery.toAstStatement())
val queryToCompile = transforms.transformStatement(preProcessedQuery)

return compiler.compile(queryToCompile.toExprNode(valueFactory.ion))
return compiler.compile(queryToCompile)
}

internal fun executePreProcessingSteps(query: ExprNode, context: StepContext) = preProcessingSteps
.interruptibleFold(query) { currentExprNode, step -> step(currentExprNode, context)
}
internal fun executePreProcessingSteps(query: PartiqlAst.Statement, context: StepContext) = preProcessingSteps
.interruptibleFold(query) { currentAstStatement, step -> step(currentAstStatement, context) }
}
12 changes: 10 additions & 2 deletions lang/src/org/partiql/lang/ast/ExprNodeToStatement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,16 @@ fun ExprNode.toAstExpr(): PartiqlAst.Expr {
is NAry -> {
val args = node.args.map { it.toAstExpr() }
when(node.op) {
NAryOp.ADD -> plus(args, metas)
NAryOp.SUB -> minus(args, metas)
NAryOp.ADD -> when (args.size) {
0 -> throw IllegalArgumentException("Operator 'Add' must have at least one argument")
1 -> pos(args.first(), metas)
else -> plus(args, metas)
}
NAryOp.SUB -> when (args.size) {
0 -> throw IllegalArgumentException("Operator 'Sub' must have at least one argument")
1 -> neg(args.first(), metas)
else -> minus(args, metas)
}
NAryOp.MUL -> times(args, metas)
NAryOp.DIV -> divide(args, metas)
NAryOp.MOD -> modulo(args, metas)
Expand Down
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/ast/StatementToExprNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private class StatementTransformer(val ion: IonSystem) {
is PartiqlAst.Expr.Id -> VariableReference(name.text, case.toCaseSensitivity(), qualifier.toScopeQualifier(), metas)
is PartiqlAst.Expr.Parameter -> Parameter(index.value.toInt(), metas)
is PartiqlAst.Expr.Not -> NAry(NAryOp.NOT, listOf(expr.toExprNode()), metas)
is PartiqlAst.Expr.Pos -> expr.toExprNode()
is PartiqlAst.Expr.Pos -> NAry(NAryOp.ADD, listOf(expr.toExprNode()), metas)
is PartiqlAst.Expr.Neg -> NAry(NAryOp.SUB, listOf(expr.toExprNode()), metas)
is PartiqlAst.Expr.Plus -> NAry(NAryOp.ADD, operands.toExprNodeList(), metas)
is PartiqlAst.Expr.Minus -> NAry(NAryOp.SUB, operands.toExprNodeList(), metas)
Expand Down
1 change: 1 addition & 0 deletions lang/src/org/partiql/lang/ast/StaticTypeMeta.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ data class StaticTypeMeta(val type: StaticType) : Meta {
}
}

@Deprecated("Please use org.partiql.lang.domains.staticType")
val MetaContainer.staticType: StaticTypeMeta? get() = find(StaticTypeMeta.TAG) as StaticTypeMeta?
2 changes: 2 additions & 0 deletions lang/src/org/partiql/lang/ast/ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.partiql.lang.util.interruptibleMap
import org.partiql.lang.util.stringValue
import java.util.Arrays

@Deprecated("Please use PIG-generated AST instead.")
/**
* Base type for all AST nodes.
*/
Expand Down Expand Up @@ -62,6 +63,7 @@ sealed class AstNode : Iterable<AstNode> {
}
}

@Deprecated("Please use PIG-generated AST instead.")
/**
* The only nodes that inherit directly from ExprNode should just be generic expressions that can be used any
* place a value is allowed.
Expand Down
4 changes: 4 additions & 0 deletions lang/src/org/partiql/lang/ast/meta.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface HasMetas {
/**
* An immutable container for [Meta] instances.
*/
@Deprecated("Please use com.amazon.ionelement.api.MetaContainer")
interface MetaContainer : Iterable<Meta> {

/**
Expand Down Expand Up @@ -172,14 +173,17 @@ private data class MetaContainerImpl internal constructor(private val metas: Tre
}

/** Constructs a container with the specified metas. */
@Deprecated("Please use org.partiql.lang.domains.metaContainerOf")
fun metaContainerOf(vararg metas: Meta): MetaContainer = metaContainerOf(metas.asIterable())

/** Empty meta container. */
@Deprecated("Please use com.amazon.ionelement.api.emptyMetaContainer")
val emptyMetaContainer: MetaContainer = metaContainerOf()

/**
* Constructs a container with the elements found within [metas].
*/
@Deprecated("Please use com.amazon.ionelement.api.metaContainerOf")
fun metaContainerOf(metas: Iterable<Meta>): MetaContainer {
return MetaContainerImpl(
TreeMap<String, Meta>().apply {
Expand Down
9 changes: 8 additions & 1 deletion lang/src/org/partiql/lang/ast/passes/StatementRedactor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private class StatementRedactionVisitor(

private fun plusMinusRedaction(args: List<PartiqlAst.Expr>) {
when (args.size) {
1 -> redactExpr(args[0])
2 -> {
redactExpr(args[0])
redactExpr(args[1])
Expand All @@ -195,6 +194,10 @@ private class StatementRedactionVisitor(
}
}

private fun posNegRedaction(expr: PartiqlAst.Expr) {
redactExpr(expr)
}

private fun arithmeticOpRedaction(args: List<PartiqlAst.Expr>) {
if (args.size != 2) {
throw IllegalArgumentException(INVALID_NUM_ARGS)
Expand All @@ -220,6 +223,8 @@ private class StatementRedactionVisitor(
// Arithmetic Ops
is PartiqlAst.Expr.Plus -> plusMinusRedaction(node.operands)
is PartiqlAst.Expr.Minus -> plusMinusRedaction(node.operands)
is PartiqlAst.Expr.Pos -> posNegRedaction(node.expr)
is PartiqlAst.Expr.Neg -> posNegRedaction(node.expr)
is PartiqlAst.Expr.Times -> arithmeticOpRedaction(node.operands)
is PartiqlAst.Expr.Divide -> arithmeticOpRedaction(node.operands)
is PartiqlAst.Expr.Modulo -> arithmeticOpRedaction(node.operands)
Expand Down Expand Up @@ -305,6 +310,8 @@ private class StatementRedactionVisitor(
|| this is PartiqlAst.Expr.Lt
|| this is PartiqlAst.Expr.Lte
|| this is PartiqlAst.Expr.InCollection
|| this is PartiqlAst.Expr.Pos
|| this is PartiqlAst.Expr.Neg
|| this is PartiqlAst.Expr.Plus
|| this is PartiqlAst.Expr.Minus
|| this is PartiqlAst.Expr.Times
Expand Down
5 changes: 5 additions & 0 deletions lang/src/org/partiql/lang/domains/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.partiql.lang.domains
import com.amazon.ionelement.api.MetaContainer
import com.amazon.ionelement.api.emptyMetaContainer
import com.amazon.ionelement.api.metaContainerOf
import org.partiql.lang.ast.Meta
import org.partiql.lang.ast.SourceLocationMeta
import org.partiql.lang.ast.StaticTypeMeta
import org.partiql.lang.errors.Property
Expand All @@ -16,6 +17,10 @@ fun PartiqlAst.Builder.id(name: String) =

val MetaContainer.staticType: StaticTypeMeta? get() = this[StaticTypeMeta.TAG] as StaticTypeMeta?

/** Constructs a container with the specified metas. */
fun metaContainerOf(vararg metas: Meta): MetaContainer =
metaContainerOf(metas.map { Pair(it.tag, it) })

/**
* Returns a [MetaContainer] with *only* the source location of the receiver [MetaContainer], if present.
*
Expand Down
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/eval/AnyOfCastTable.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.partiql.lang.eval

import org.partiql.lang.ast.MetaContainer
import com.amazon.ionelement.api.MetaContainer
import org.partiql.lang.ast.sourceLocation
import org.partiql.lang.errors.ErrorCode
import org.partiql.lang.errors.Property
Expand Down
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/eval/ErrorSignaler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.partiql.lang.eval

import org.partiql.lang.ast.MetaContainer
import com.amazon.ionelement.api.MetaContainer
import org.partiql.lang.ast.SourceLocationMeta
import org.partiql.lang.errors.ErrorBehaviorInPermissiveMode
import org.partiql.lang.errors.ErrorCode
Expand Down
Loading