|
| 1 | +package org.partiql.examples |
| 2 | + |
| 3 | +import com.amazon.ion.IonDecimal |
| 4 | +import com.amazon.ion.IonStruct |
| 5 | +import com.amazon.ion.system.IonSystemBuilder |
| 6 | +import org.partiql.examples.util.Example |
| 7 | +import org.partiql.lang.CompilerPipeline |
| 8 | +import org.partiql.lang.errors.ErrorCode |
| 9 | +import org.partiql.lang.errors.Property |
| 10 | +import org.partiql.lang.errors.PropertyValueMap |
| 11 | +import org.partiql.lang.eval.BindingCase |
| 12 | +import org.partiql.lang.eval.BindingName |
| 13 | +import org.partiql.lang.eval.Bindings |
| 14 | +import org.partiql.lang.eval.EvaluationException |
| 15 | +import org.partiql.lang.eval.EvaluationSession |
| 16 | +import org.partiql.lang.eval.ExprValue |
| 17 | +import org.partiql.lang.eval.ExprValueFactory |
| 18 | +import org.partiql.lang.eval.ExprValueType |
| 19 | +import org.partiql.lang.eval.builtins.storedprocedure.StoredProcedure |
| 20 | +import org.partiql.lang.eval.builtins.storedprocedure.StoredProcedureSignature |
| 21 | +import org.partiql.lang.eval.stringValue |
| 22 | +import java.io.PrintStream |
| 23 | +import java.math.BigDecimal |
| 24 | +import java.math.RoundingMode |
| 25 | + |
| 26 | +private val ion = IonSystemBuilder.standard().build() |
| 27 | + |
| 28 | +/** |
| 29 | + * A simple custom stored procedure that calculates the moon weight for each crewmate of the given crew, storing the |
| 30 | + * moon weight in the [EvaluationSession] global bindings. This procedure also returns the number of crewmates we |
| 31 | + * calculated the moon weight for, returning -1 if no crew is found. |
| 32 | + * |
| 33 | + * This example demonstrates how to create a custom stored procedure, check argument types, and modify the |
| 34 | + * [EvaluationSession]. |
| 35 | + */ |
| 36 | +class CalculateCrewMoonWeight(private val valueFactory: ExprValueFactory): StoredProcedure { |
| 37 | + private val MOON_GRAVITATIONAL_CONSTANT = BigDecimal(1.622 / 9.81) |
| 38 | + |
| 39 | + // [StoredProcedureSignature] takes two arguments: |
| 40 | + // 1. the name of the stored procedure |
| 41 | + // 2. the arity of this stored procedure. Checks to arity are taken care of by the evaluator. However, we must |
| 42 | + // still check that the passed arguments are of the right type in our implementation of the procedure. |
| 43 | + override val signature = StoredProcedureSignature(name = "calculate_crew_moon_weight", arity = 1) |
| 44 | + |
| 45 | + // `call` is where you define the logic of the stored procedure given an [EvaluationSession] and a list of |
| 46 | + // arguments |
| 47 | + override fun call(session: EvaluationSession, args: List<ExprValue>): ExprValue { |
| 48 | + // We first check that the first argument is a string |
| 49 | + val crewName = args.first() |
| 50 | + // In the future the evaluator will also verify function argument types, but for now we must verify their type |
| 51 | + // manually |
| 52 | + if (crewName.type != ExprValueType.STRING) { |
| 53 | + val errorContext = PropertyValueMap().also { |
| 54 | + it[Property.EXPECTED_ARGUMENT_TYPES] = "STRING" |
| 55 | + it[Property.ACTUAL_ARGUMENT_TYPES] = crewName.type.name |
| 56 | + it[Property.FUNCTION_NAME] = signature.name |
| 57 | + } |
| 58 | + throw EvaluationException("First argument to ${signature.name} was not a string", |
| 59 | + ErrorCode.EVALUATOR_INCORRECT_TYPE_OF_ARGUMENTS_TO_PROCEDURE_CALL, |
| 60 | + errorContext, |
| 61 | + internal = false) |
| 62 | + } |
| 63 | + |
| 64 | + // Next we check if the given `crewName` is in the [EvaluationSession]'s global bindings. If not, we return 0. |
| 65 | + val sessionGlobals = session.globals |
| 66 | + val crewBindings = sessionGlobals[BindingName(crewName.stringValue(), BindingCase.INSENSITIVE)] |
| 67 | + ?: return valueFactory.newInt(-1) |
| 68 | + |
| 69 | + // Now that we've confirmed the given `crewName` is in the session's global bindings, we calculate and store |
| 70 | + // the moon weight for each crewmate in the crew. |
| 71 | + // In addition, we keep a running a tally of how many crewmates we do this for. |
| 72 | + var numCalculated = 0 |
| 73 | + for (crewmateBinding in crewBindings) { |
| 74 | + val crewmate = crewmateBinding.ionValue as IonStruct |
| 75 | + val mass = crewmate["mass"] as IonDecimal |
| 76 | + val moonWeight = (mass.decimalValue() * MOON_GRAVITATIONAL_CONSTANT).setScale(1, RoundingMode.HALF_UP) |
| 77 | + crewmate.add("moonWeight", ion.newDecimal(moonWeight)) |
| 78 | + |
| 79 | + numCalculated++ |
| 80 | + } |
| 81 | + return valueFactory.newInt(numCalculated) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Demonstrates the use of custom stored procedure [CalculateCrewMoonWeight] in PartiQL queries. |
| 87 | + */ |
| 88 | +class CustomProceduresExample(out: PrintStream) : Example(out) { |
| 89 | + override fun run() { |
| 90 | + /** |
| 91 | + * To make custom stored procedures available to the PartiQL query being executed, they must be passed to |
| 92 | + * [CompilerPipeline.Builder.addProcedure]. |
| 93 | + */ |
| 94 | + val pipeline = CompilerPipeline.build(ion) { |
| 95 | + addProcedure(CalculateCrewMoonWeight(valueFactory)) |
| 96 | + } |
| 97 | + |
| 98 | + // Here, we initialize the crews to be stored in our global session bindings |
| 99 | + val initialCrews = Bindings.ofMap( |
| 100 | + mapOf( |
| 101 | + "crew1" to pipeline.valueFactory.newFromIonValue( |
| 102 | + ion.singleValue("""[ { name: "Neil", mass: 80.5 }, |
| 103 | + { name: "Buzz", mass: 72.3 }, |
| 104 | + { name: "Michael", mass: 89.9 } ]""")), |
| 105 | + "crew2" to pipeline.valueFactory.newFromIonValue( |
| 106 | + ion.singleValue("""[ { name: "James", mass: 77.1 }, |
| 107 | + { name: "Spock", mass: 81.6 } ]""")) |
| 108 | + ) |
| 109 | + ) |
| 110 | + val session = EvaluationSession.build { globals(initialCrews) } |
| 111 | + |
| 112 | + val crew1BindingName = BindingName("crew1", BindingCase.INSENSITIVE) |
| 113 | + val crew2BindingName = BindingName("crew2", BindingCase.INSENSITIVE) |
| 114 | + |
| 115 | + out.println("Initial global session bindings:") |
| 116 | + print("Crew 1:", "${session.globals[crew1BindingName]}") |
| 117 | + print("Crew 2:", "${session.globals[crew2BindingName]}") |
| 118 | + |
| 119 | + // We call our custom stored procedure using PartiQL's `EXEC` clause. Here we call our stored procedure |
| 120 | + // 'calculate_crew_moon_weight' with the arg 'crew1', which outputs the number of crewmates we've calculated |
| 121 | + // the moon weight for |
| 122 | + val procedureCall = "EXEC calculate_crew_moon_weight 'crew1'" |
| 123 | + val procedureCallOutput = pipeline.compile(procedureCall).eval(session) |
| 124 | + print("Number of calculated moon weights:", "$procedureCallOutput") |
| 125 | + |
| 126 | + out.println("Updated global session bindings:") |
| 127 | + print("Crew 1:", "${session.globals[crew1BindingName]}") |
| 128 | + print("Crew 2:", "${session.globals[crew2BindingName]}") |
| 129 | + } |
| 130 | +} |
0 commit comments