carloea2 commented on code in PR #8512:
URL: https://github.com/apache/texera/pull/8512#discussion_r4079828559


##########
common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/parquet/ParquetScanSourceOpDesc.scala:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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 org.apache.texera.amber.operator.source.scan.parquet
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.io.LocalInputFile
+import org.apache.texera.amber.core.executor.OpExecWithClassName
+import org.apache.texera.amber.core.storage.DocumentFactory
+import org.apache.texera.amber.core.tuple.Schema
+import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, 
WorkflowIdentity}
+import org.apache.texera.amber.core.workflow.{PhysicalOp, 
SchemaPropagationFunc}
+import org.apache.texera.amber.operator.StandaloneCodeGenerator
+import 
org.apache.texera.amber.operator.StandaloneCodeGenerator.SourceFilePlaceholder
+import org.apache.texera.amber.operator.source.scan.ScanSourceOpDesc
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+
+import java.io.IOException
+import java.net.URI
+import scala.util.Using
+
+/**
+  * Reads a Parquet file. The format states its own column types in a footer, 
so
+  * unlike the CSV and JSONL sources this one infers nothing: the INTEGER the
+  * file was written with is the INTEGER that arrives.
+  */
+@JsonIgnoreProperties(value = Array("fileEncoding"))
+class ParquetScanSourceOpDesc extends ScanSourceOpDesc with 
StandaloneCodeGenerator {
+
+  fileTypeName = Option("Parquet")
+
+  // A DECIMAL is the one column pandas does not land on the same type as the
+  // executor: it fills that column with decimal.Decimal objects, which a 
script
+  // cannot then multiply by a float. pyarrow is what pandas reads a Parquet 
file
+  // with; the footer is asked for directly here to read it the executor's way.
+  override def standaloneImports(): Seq[String] =
+    Seq("from decimal import Decimal", "import pyarrow.parquet as pq")
+
+  override def standaloneSourcePath(): Option[String] = fileName
+
+  override def generateStandaloneCode(): String = {
+    // No date columns to name, and no dtype map. pandas reads the types out of
+    // the same footer the executor does, which is the whole point of the 
format;
+    // the text formats have to be told because they carry nothing to read.
+    // Read into the nullable dtypes, as the Arrow source is. Parquet says of
+    // every value whether it is there, and a numpy column has nowhere to put
+    // that: pandas widens a holed integer column through a float, where every
+    // value past 2^53 is rounded and 9007199254740993 came back as ...992. The
+    // executor reads the exact long, and the declared column stays integral.
+    //
+    // The footer is handed back to the reader stripped of its metadata. A file
+    // pandas wrote records there which of its columns held the frame's index,
+    // and pandas restores those as an index rather than as columns: a file
+    // written from a frame keyed by `customer_id` reads back without that
+    // column, and a projection naming it raises. The executor reads the 
columns
+    // the footer states and knows nothing of an index, so it hands 
`customer_id`
+    // on like any other. Stripped, pandas reads every column the file holds,
+    // under the name the footer gives it and in the order it states them.
+    val read =
+      s"""|out1df = pd.read_parquet(
+          |    $SourceFilePlaceholder,
+          |    dtype_backend="numpy_nullable",
+          |    schema=pq.read_schema($SourceFilePlaceholder).remove_metadata(),
+          |)""".stripMargin
+    // The columns pandas does not land on the same value as the executor. A
+    // DECIMAL arrives as decimal.Decimal objects and a FLOAT keeps the single
+    // precision the executor widens; an unsigned column, a duration and a 
zoned
+    // timestamp come back as the Arrow types the writer left in the file's
+    // metadata, where the executor reads what the footer alone states.
+    //
+    // Named in the nullable spelling, `Float32` and not `float32`, and widened
+    // into the nullable dtype in turn, so a hole stays a hole rather than
+    // becoming the NaN a numpy column would have to write it as.
+    val columns =
+      """|for _column, _values in out1df.items():
+         |    if isinstance(next(iter(_values.dropna()), None), Decimal):
+         |        out1df[_column] = _values.astype("Float64")
+         |    elif isinstance(_values.dtype, pd.DatetimeTZDtype):
+         |        out1df[_column] = 
_values.dt.tz_convert("UTC").dt.tz_localize(None)
+         |    elif _values.dtype.kind in "um":
+         |        out1df[_column] = _values.astype("Int64")
+         |    elif _values.dtype == "Float32":
+         |        out1df[_column] = _values.astype("Float64")""".stripMargin
+    // The executor drops `offset` rows and then takes `limit` of them. Parquet
+    // can skip whole row groups but not an arbitrary row range, so the same
+    // window is taken once the frame is in memory, as the Arrow source does.
+    val window = (offset, limit) match {

Review Comment:
   Negative windows still use pandas indexing from the end. On [10,20,30], 
offset -1 returns [30] instead of all rows; limit -1 returns [10,20] instead of 
none. Scala drop and take confirm the native results. Imported or API-provided 
configurations can bypass the editor bounds. Please clamp both values before 
building the slice, as the other sources do.



##########
common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/parquet/ParquetScanSourceOpDesc.scala:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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 org.apache.texera.amber.operator.source.scan.parquet
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.io.LocalInputFile
+import org.apache.texera.amber.core.executor.OpExecWithClassName
+import org.apache.texera.amber.core.storage.DocumentFactory
+import org.apache.texera.amber.core.tuple.Schema
+import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, 
WorkflowIdentity}
+import org.apache.texera.amber.core.workflow.{PhysicalOp, 
SchemaPropagationFunc}
+import org.apache.texera.amber.operator.StandaloneCodeGenerator
+import 
org.apache.texera.amber.operator.StandaloneCodeGenerator.SourceFilePlaceholder
+import org.apache.texera.amber.operator.source.scan.ScanSourceOpDesc
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+
+import java.io.IOException
+import java.net.URI
+import scala.util.Using
+
+/**
+  * Reads a Parquet file. The format states its own column types in a footer, 
so
+  * unlike the CSV and JSONL sources this one infers nothing: the INTEGER the
+  * file was written with is the INTEGER that arrives.
+  */
+@JsonIgnoreProperties(value = Array("fileEncoding"))
+class ParquetScanSourceOpDesc extends ScanSourceOpDesc with 
StandaloneCodeGenerator {
+
+  fileTypeName = Option("Parquet")
+
+  // A DECIMAL is the one column pandas does not land on the same type as the
+  // executor: it fills that column with decimal.Decimal objects, which a 
script
+  // cannot then multiply by a float. pyarrow is what pandas reads a Parquet 
file
+  // with; the footer is asked for directly here to read it the executor's way.
+  override def standaloneImports(): Seq[String] =
+    Seq("from decimal import Decimal", "import pyarrow.parquet as pq")
+
+  override def standaloneSourcePath(): Option[String] = fileName
+
+  override def generateStandaloneCode(): String = {
+    // No date columns to name, and no dtype map. pandas reads the types out of
+    // the same footer the executor does, which is the whole point of the 
format;
+    // the text formats have to be told because they carry nothing to read.
+    // Read into the nullable dtypes, as the Arrow source is. Parquet says of
+    // every value whether it is there, and a numpy column has nowhere to put
+    // that: pandas widens a holed integer column through a float, where every
+    // value past 2^53 is rounded and 9007199254740993 came back as ...992. The
+    // executor reads the exact long, and the declared column stays integral.
+    //
+    // The footer is handed back to the reader stripped of its metadata. A file
+    // pandas wrote records there which of its columns held the frame's index,
+    // and pandas restores those as an index rather than as columns: a file
+    // written from a frame keyed by `customer_id` reads back without that
+    // column, and a projection naming it raises. The executor reads the 
columns
+    // the footer states and knows nothing of an index, so it hands 
`customer_id`
+    // on like any other. Stripped, pandas reads every column the file holds,
+    // under the name the footer gives it and in the order it states them.
+    val read =
+      s"""|out1df = pd.read_parquet(
+          |    $SourceFilePlaceholder,
+          |    dtype_backend="numpy_nullable",
+          |    schema=pq.read_schema($SourceFilePlaceholder).remove_metadata(),
+          |)""".stripMargin
+    // The columns pandas does not land on the same value as the executor. A
+    // DECIMAL arrives as decimal.Decimal objects and a FLOAT keeps the single
+    // precision the executor widens; an unsigned column, a duration and a 
zoned
+    // timestamp come back as the Arrow types the writer left in the file's
+    // metadata, where the executor reads what the footer alone states.
+    //
+    // Named in the nullable spelling, `Float32` and not `float32`, and widened
+    // into the nullable dtype in turn, so a hole stays a hole rather than
+    // becoming the NaN a numpy column would have to write it as.
+    val columns =
+      """|for _column, _values in out1df.items():
+         |    if isinstance(next(iter(_values.dropna()), None), Decimal):
+         |        out1df[_column] = _values.astype("Float64")
+         |    elif isinstance(_values.dtype, pd.DatetimeTZDtype):
+         |        out1df[_column] = 
_values.dt.tz_convert("UTC").dt.tz_localize(None)
+         |    elif _values.dtype.kind in "um":
+         |        out1df[_column] = _values.astype("Int64")
+         |    elif _values.dtype == "Float32":

Review Comment:
   Signed Int8 and Int16 columns are not widened to the INTEGER type declared 
by the native source. With Int8 start=120 and point=125, Interval Join adds 
width 10 and wraps the upper bound to -126, dropping a valid match. Int32 
passes. Please widen signed narrow integers before passing the frame downstream.



##########
common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/parquet/ParquetScanSourceOpDesc.scala:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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 org.apache.texera.amber.operator.source.scan.parquet
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.io.LocalInputFile
+import org.apache.texera.amber.core.executor.OpExecWithClassName
+import org.apache.texera.amber.core.storage.DocumentFactory
+import org.apache.texera.amber.core.tuple.Schema
+import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, 
WorkflowIdentity}
+import org.apache.texera.amber.core.workflow.{PhysicalOp, 
SchemaPropagationFunc}
+import org.apache.texera.amber.operator.StandaloneCodeGenerator
+import 
org.apache.texera.amber.operator.StandaloneCodeGenerator.SourceFilePlaceholder
+import org.apache.texera.amber.operator.source.scan.ScanSourceOpDesc
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+
+import java.io.IOException
+import java.net.URI
+import scala.util.Using
+
+/**
+  * Reads a Parquet file. The format states its own column types in a footer, 
so
+  * unlike the CSV and JSONL sources this one infers nothing: the INTEGER the
+  * file was written with is the INTEGER that arrives.
+  */
+@JsonIgnoreProperties(value = Array("fileEncoding"))
+class ParquetScanSourceOpDesc extends ScanSourceOpDesc with 
StandaloneCodeGenerator {
+
+  fileTypeName = Option("Parquet")
+
+  // A DECIMAL is the one column pandas does not land on the same type as the
+  // executor: it fills that column with decimal.Decimal objects, which a 
script
+  // cannot then multiply by a float. pyarrow is what pandas reads a Parquet 
file
+  // with; the footer is asked for directly here to read it the executor's way.
+  override def standaloneImports(): Seq[String] =
+    Seq("from decimal import Decimal", "import pyarrow.parquet as pq")
+
+  override def standaloneSourcePath(): Option[String] = fileName
+
+  override def generateStandaloneCode(): String = {
+    // No date columns to name, and no dtype map. pandas reads the types out of
+    // the same footer the executor does, which is the whole point of the 
format;
+    // the text formats have to be told because they carry nothing to read.
+    // Read into the nullable dtypes, as the Arrow source is. Parquet says of
+    // every value whether it is there, and a numpy column has nowhere to put
+    // that: pandas widens a holed integer column through a float, where every
+    // value past 2^53 is rounded and 9007199254740993 came back as ...992. The
+    // executor reads the exact long, and the declared column stays integral.
+    //
+    // The footer is handed back to the reader stripped of its metadata. A file
+    // pandas wrote records there which of its columns held the frame's index,
+    // and pandas restores those as an index rather than as columns: a file
+    // written from a frame keyed by `customer_id` reads back without that
+    // column, and a projection naming it raises. The executor reads the 
columns
+    // the footer states and knows nothing of an index, so it hands 
`customer_id`
+    // on like any other. Stripped, pandas reads every column the file holds,
+    // under the name the footer gives it and in the order it states them.
+    val read =
+      s"""|out1df = pd.read_parquet(
+          |    $SourceFilePlaceholder,
+          |    dtype_backend="numpy_nullable",
+          |    schema=pq.read_schema($SourceFilePlaceholder).remove_metadata(),
+          |)""".stripMargin
+    // The columns pandas does not land on the same value as the executor. A
+    // DECIMAL arrives as decimal.Decimal objects and a FLOAT keeps the single
+    // precision the executor widens; an unsigned column, a duration and a 
zoned
+    // timestamp come back as the Arrow types the writer left in the file's
+    // metadata, where the executor reads what the footer alone states.
+    //
+    // Named in the nullable spelling, `Float32` and not `float32`, and widened
+    // into the nullable dtype in turn, so a hole stays a hole rather than
+    // becoming the NaN a numpy column would have to write it as.
+    val columns =
+      """|for _column, _values in out1df.items():
+         |    if isinstance(next(iter(_values.dropna()), None), Decimal):
+         |        out1df[_column] = _values.astype("Float64")

Review Comment:
   Parquet DATE arrives as datetime.date, but the native reader declares 
TIMESTAMP and constructs midnight. I reproduced a downstream string cast 
returning `2024-01-01` instead of `2024-01-01 00:00:00.0`; a timestamp column 
passes. Please normalize DATE columns to the same timestamp representation.



-- 
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]

Reply via email to