Skip to content

Commit

Permalink
Improve Filter.not syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed Aug 30, 2024
1 parent 4ec2832 commit e206c00
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import me.mnedokushev.zio.apache.parquet.core.Lens
import me.mnedokushev.zio.apache.parquet.core.filter.CompiledPredicate
import me.mnedokushev.zio.apache.parquet.core.filter.internal.{ ColumnPathConcatMacro, SanitizeOptionalsMacro }

package object syntax {
package object syntax extends Predicate.Syntax {

implicit class NullableColumnSyntax[F, S, A](val column: Lens[F, S, Option[A]]) {
def nullable(implicit typeTag: TypeTag[A]): Column.Named[A, column.Identity] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.mnedokushev.zio.apache.parquet.core.filter
import me.mnedokushev.zio.apache.parquet.core.Lens
import me.mnedokushev.zio.apache.parquet.core.filter.internal.{ ColumnPathConcatMacro, SanitizeOptionalsMacro }

package object syntax {
package object syntax extends Predicate.Syntax {

extension [F, S, A](column: Lens[F, S, Option[A]]) {
def nullable(implicit typeTag: TypeTag[A]): Column.Named[A, column.Identity] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,4 @@ object Filter {
schema.makeAccessors(accessorBuilder)
}

// inline def compile[A](inline predicate: Predicate[A]): FilterPredicate =
// ${ CompilePredicateMacro.compileImpl[A]('predicate) }

def not[A](pred: Predicate[A]): Predicate[A] =
Predicate.Unary(pred, Operator.Unary.Not[A]())

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ sealed trait Predicate[A] { self =>

object Predicate {

private[filter] trait Syntax {
def not[A](pred: Predicate[A]) =
Predicate.Unary(pred, Operator.Unary.Not[A]())
}

final case class Binary[A](column: Column[A], value: A, op: Operator.Binary[A]) extends Predicate[A]

final case class BinarySet[A](column: Column[A], values: Set[A], op: Operator.Binary.Set[A]) extends Predicate[A]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.IOException

trait ParquetReader[+A <: Product] {

def readStream(path: Path): ZStream[Scope, Throwable, A]
def readStream(path: Path, filter: Option[CompiledPredicate] = None): ZStream[Scope, Throwable, A]

def readChunk[B](path: Path, filter: Option[CompiledPredicate] = None): Task[Chunk[A]]

Expand All @@ -29,9 +29,9 @@ final class ParquetReaderLive[A <: Product: Tag](
)(implicit decoder: ValueDecoder[A])
extends ParquetReader[A] {

override def readStream(path: Path): ZStream[Scope, Throwable, A] =
override def readStream(path: Path, filter: Option[CompiledPredicate] = None): ZStream[Scope, Throwable, A] =
for {
reader <- ZStream.fromZIO(build(path))
reader <- ZStream.fromZIO(build(path, filter))
value <- ZStream.repeatZIOOption(
ZIO
.attemptBlockingIO(reader.read())
Expand Down Expand Up @@ -67,7 +67,7 @@ final class ParquetReaderLive[A <: Product: Tag](

private def build[B](
path: Path,
filter: Option[CompiledPredicate] = None
filter: Option[CompiledPredicate]
): ZIO[Scope, IOException, HadoopParquetReader[RecordValue]] =
for {
inputFile <- path.toInputFileZIO(hadoopConf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import me.mnedokushev.zio.apache.parquet.core.filter.TypeTag._
import me.mnedokushev.zio.apache.parquet.core.filter.syntax._
import org.apache.parquet.filter2.predicate.FilterApi
import zio._
import zio.test.Assertion._
import zio.test.Assertion.{ equalTo, isRight }
import zio.test._

import java.time._
Expand All @@ -21,7 +21,7 @@ object ExprSpec extends ZIOSpecDefault {
val (a, b, _, _, _) = Filter[MyRecord].columns

val result = predicate(
Filter.not(
not(
(b >= 3 `or` b <= 100 `and` a.in(Set("foo", "bar"))) `or`
(a === "foo" `and` (b === 20 `or` b.notIn(Set(1, 2, 3)))) `or`
(a =!= "foo" `and` b > 2 `and` b < 10)
Expand Down

0 comments on commit e206c00

Please sign in to comment.