Yicong-Huang commented on code in PR #3774: URL: https://github.com/apache/texera/pull/3774#discussion_r2384409644
########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpExec.scala: ########## @@ -0,0 +1,281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import edu.uci.ics.amber.core.executor.OperatorExecutor +import edu.uci.ics.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} +import edu.uci.ics.amber.operator.stablemergesort.StableMergeSortOpDesc.StableSortKey +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.sql.Timestamp +import java.util.Locale +import scala.collection.mutable.ArrayBuffer +import scala.jdk.CollectionConverters._ + +class StableMergeSortOpExec(descString: String) extends OperatorExecutor { + + private val desc: StableMergeSortOpDesc = + objectMapper.readValue(descString, classOf[StableMergeSortOpDesc]) + + private val configuredKeys: List[StableSortKey] = + Option(desc.keys).map(_.asScala.toList).getOrElse(Nil) + + if (configuredKeys.isEmpty) { + throw new IllegalArgumentException("StableMergeSort requires at least one sort key.") + } + + if (desc.offset < 0) { + throw new IllegalArgumentException("Offset must be non-negative.") + } + + private val limitOpt: Option[Int] = Option(desc.limit).map(_.intValue()) + + limitOpt.foreach { limitValue => + if (limitValue < 0) { + throw new IllegalArgumentException("Limit must be non-negative when specified.") + } + } Review Comment: ditto ########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpDesc.scala: ########## @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} +import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, JsonSchemaTitle} +import edu.uci.ics.amber.core.executor.OpExecWithClassName +import edu.uci.ics.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} +import edu.uci.ics.amber.core.workflow.{InputPort, OutputPort, PhysicalOp} +import edu.uci.ics.amber.operator.LogicalOp +import edu.uci.ics.amber.operator.metadata.annotations.AutofillAttributeName +import edu.uci.ics.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo} +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.util + +class StableMergeSortOpDesc extends LogicalOp { + + import StableMergeSortOpDesc._ + + @JsonProperty(value = "keys", required = true) + @JsonSchemaTitle("Sort Keys") + @JsonPropertyDescription("List of attributes to sort by with ordering preferences.") + var keys: util.List[StableSortKey] = new util.ArrayList[StableSortKey]() + + @JsonProperty("offset") + @JsonSchemaTitle("Offset") + @JsonPropertyDescription("Number of sorted tuples to skip before emitting results.") + var offset: Int = 0 + + @JsonProperty("limit") + @JsonSchemaTitle("Limit") + @JsonPropertyDescription("Maximum number of tuples to emit after applying the offset. Leave empty to emit all tuples.") + var limit: Integer = null + + override def getPhysicalOp( + workflowId: WorkflowIdentity, + executionId: ExecutionIdentity + ): PhysicalOp = + PhysicalOp + .oneToOnePhysicalOp( + workflowId, + executionId, + operatorIdentifier, + OpExecWithClassName( + "edu.uci.ics.amber.operator.stablemergesort.StableMergeSortOpExec", + objectMapper.writeValueAsString(this) + ) + ) + .withInputPorts(operatorInfo.inputPorts) + .withOutputPorts(operatorInfo.outputPorts) + + override def operatorInfo: OperatorInfo = + OperatorInfo( + "Stable Merge Sort", + "Stable per-partition sort with multi-key ordering (stable merge sort).", + OperatorGroupConstants.SORT_GROUP, + List(InputPort()), + List(OutputPort(blocking = true)) + ) +} + +object StableMergeSortOpDesc { + + class StableSortKey { + + @JsonProperty(value = "attribute", required = true) + @JsonSchemaTitle("Attribute") + @JsonPropertyDescription("Attribute to sort by.") + @AutofillAttributeName + var attribute: String = _ + + @JsonProperty("order") + @JsonSchemaTitle("Order") + @JsonPropertyDescription("Sort order: asc for ascending or desc for descending.") + @JsonSchemaInject(json = """{"enum": ["asc", "desc"]}""") + var order: String = "asc" + + @JsonProperty("nulls") + @JsonSchemaTitle("Nulls") + @JsonPropertyDescription("Placement of null values: first or last.") + @JsonSchemaInject(json = """{"enum": ["first", "last"]}""") + var nulls: String = "last" + + + @JsonProperty("caseInsensitive") + @JsonSchemaTitle("Case Insensitive") + @JsonPropertyDescription("Treat string comparisons as case-insensitive.") + var caseInsensitive: Boolean = false Review Comment: This field is specific to String type. Can we not put it in this general class? ########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpExec.scala: ########## @@ -0,0 +1,281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import edu.uci.ics.amber.core.executor.OperatorExecutor +import edu.uci.ics.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} +import edu.uci.ics.amber.operator.stablemergesort.StableMergeSortOpDesc.StableSortKey +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.sql.Timestamp +import java.util.Locale +import scala.collection.mutable.ArrayBuffer +import scala.jdk.CollectionConverters._ + +class StableMergeSortOpExec(descString: String) extends OperatorExecutor { + + private val desc: StableMergeSortOpDesc = + objectMapper.readValue(descString, classOf[StableMergeSortOpDesc]) + + private val configuredKeys: List[StableSortKey] = + Option(desc.keys).map(_.asScala.toList).getOrElse(Nil) + + if (configuredKeys.isEmpty) { + throw new IllegalArgumentException("StableMergeSort requires at least one sort key.") + } + + if (desc.offset < 0) { + throw new IllegalArgumentException("Offset must be non-negative.") + } + + private val limitOpt: Option[Int] = Option(desc.limit).map(_.intValue()) + + limitOpt.foreach { limitValue => + if (limitValue < 0) { + throw new IllegalArgumentException("Limit must be non-negative when specified.") + } + } + + private case class ResolvedKey( + attribute: String, + index: Int, + attributeType: AttributeType, + descending: Boolean, + nullsFirst: Boolean, + caseInsensitive: Boolean + ) + + private var bufferedTuples: ArrayBuffer[Tuple] = _ + private var inputSchema: Schema = _ + private var resolvedKeys: Array[ResolvedKey] = _ + + override def open(): Unit = { + bufferedTuples = ArrayBuffer.empty[Tuple] + } + + override def close(): Unit = { + if (bufferedTuples != null) { + bufferedTuples.clear() + } + } + + override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { + if (inputSchema == null) { + inputSchema = tuple.getSchema + resolvedKeys = resolveKeys(inputSchema) + } + bufferedTuples.append(tuple) + Iterator.empty + } + + override def onFinish(port: Int): Iterator[TupleLike] = { + if (bufferedTuples.isEmpty) { + Iterator.empty + } else { + if (resolvedKeys == null) { + resolvedKeys = resolveKeys(bufferedTuples.head.getSchema) + } + stableMergeSort(bufferedTuples) + val startIndex = math.min(desc.offset, bufferedTuples.size) + val endExclusive = limitOpt match { + case Some(limitValue) => math.min(startIndex + limitValue, bufferedTuples.size) + case None => bufferedTuples.size + } + if (startIndex >= endExclusive) { + Iterator.empty + } else { + bufferedTuples.slice(startIndex, endExclusive).iterator + } + } + } + + private def resolveKeys(schema: Schema): Array[ResolvedKey] = { + configuredKeys.map { key => + val attributeName = Option(key.attribute) + .getOrElse(throw new IllegalArgumentException("Sort key attribute must be provided.")) Review Comment: we don't have to be so defensive. schema check would guarantee this key being provided. ########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpDesc.scala: ########## @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} +import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, JsonSchemaTitle} +import edu.uci.ics.amber.core.executor.OpExecWithClassName +import edu.uci.ics.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} +import edu.uci.ics.amber.core.workflow.{InputPort, OutputPort, PhysicalOp} +import edu.uci.ics.amber.operator.LogicalOp +import edu.uci.ics.amber.operator.metadata.annotations.AutofillAttributeName +import edu.uci.ics.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo} +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.util + +class StableMergeSortOpDesc extends LogicalOp { + + import StableMergeSortOpDesc._ + + @JsonProperty(value = "keys", required = true) + @JsonSchemaTitle("Sort Keys") + @JsonPropertyDescription("List of attributes to sort by with ordering preferences.") + var keys: util.List[StableSortKey] = new util.ArrayList[StableSortKey]() + + @JsonProperty("offset") + @JsonSchemaTitle("Offset") + @JsonPropertyDescription("Number of sorted tuples to skip before emitting results.") + var offset: Int = 0 + + @JsonProperty("limit") + @JsonSchemaTitle("Limit") + @JsonPropertyDescription("Maximum number of tuples to emit after applying the offset. Leave empty to emit all tuples.") + var limit: Integer = null + + override def getPhysicalOp( + workflowId: WorkflowIdentity, + executionId: ExecutionIdentity + ): PhysicalOp = + PhysicalOp Review Comment: During compilation, we should check if parameters are valid, and if not, we can give users an early error to fix them. In the current framework, we tend to check those parameters during schema propagation. Schema propagation happens during compilation, which is before executor. ########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpExec.scala: ########## @@ -0,0 +1,281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import edu.uci.ics.amber.core.executor.OperatorExecutor +import edu.uci.ics.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} +import edu.uci.ics.amber.operator.stablemergesort.StableMergeSortOpDesc.StableSortKey +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.sql.Timestamp +import java.util.Locale +import scala.collection.mutable.ArrayBuffer +import scala.jdk.CollectionConverters._ + +class StableMergeSortOpExec(descString: String) extends OperatorExecutor { + + private val desc: StableMergeSortOpDesc = + objectMapper.readValue(descString, classOf[StableMergeSortOpDesc]) + + private val configuredKeys: List[StableSortKey] = + Option(desc.keys).map(_.asScala.toList).getOrElse(Nil) + + if (configuredKeys.isEmpty) { + throw new IllegalArgumentException("StableMergeSort requires at least one sort key.") + } + + if (desc.offset < 0) { + throw new IllegalArgumentException("Offset must be non-negative.") + } Review Comment: those checks are too late. should have done earlier during compilation, and become a compilation error, instead of a runtime error. ########## core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/stablemergesort/StableMergeSortOpExec.scala: ########## @@ -0,0 +1,281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package edu.uci.ics.amber.operator.stablemergesort + +import edu.uci.ics.amber.core.executor.OperatorExecutor +import edu.uci.ics.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} +import edu.uci.ics.amber.operator.stablemergesort.StableMergeSortOpDesc.StableSortKey +import edu.uci.ics.amber.util.JSONUtils.objectMapper + +import java.sql.Timestamp +import java.util.Locale +import scala.collection.mutable.ArrayBuffer +import scala.jdk.CollectionConverters._ + +class StableMergeSortOpExec(descString: String) extends OperatorExecutor { + + private val desc: StableMergeSortOpDesc = + objectMapper.readValue(descString, classOf[StableMergeSortOpDesc]) + + private val configuredKeys: List[StableSortKey] = + Option(desc.keys).map(_.asScala.toList).getOrElse(Nil) Review Comment: we should avoid empty case. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
