Skip to content

Commit

Permalink
Add compileImpl macro for Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed Apr 9, 2024
1 parent 521bd7a commit fb90ac3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.mnedokushev.zio.apache.parquet.core.filter.internal

import scala.quoted._
import me.mnedokushev.zio.apache.parquet.core.filter.Predicate
import org.apache.parquet.filter2.predicate.FilterPredicate

object CompilePredicateMacro {

inline def compileImpl[A](predicate: Predicate[A]): Either[String, FilterPredicate] = ${ compileImplImpl[A]('predicate) }

def compileImplImpl[A: Type](predicate: Expr[Predicate[A]])(using Quotes): Expr[Either[String, FilterPredicate]] = {
import quotes.reflect._

val containsOptionalValue = predicate.asTerm match {
case Inlined(_, _, Block(stats, _)) =>
stats.exists {
case Apply(TypeApply(Select(Ident("scala"), "Some"), _), _) =>
true
case Select(Ident("scala"), "None") =>
true
case _ =>
false
}
case _ =>
false
}

if (containsOptionalValue)
report.errorAndAbort(
s"""
| The use of optional columns in filter predicate is prohibited. Please, use .nullable:
| column.nullable > 3
| Predicate: ${predicate.show}
""".stripMargin
)
else
'{ _root_.me.mnedokushev.zio.apache.parquet.core.filter.Filter.compile[A]($predicate) }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.mnedokushev.zio.apache.parquet.core

import me.mnedokushev.zio.apache.parquet.core.filter.internal.CompilePredicateMacro
import org.apache.parquet.filter2.predicate.FilterPredicate

package object filter {

extension[F, S, A](column: Lens[F, S, Option[A]]) {
def nullable(implicit typeTag: TypeTag[A]): Column.Named[A, column.Identity] =
Column.Named(column.path)
}

def compile[A](predicate: Predicate[A]): Either[String, FilterPredicate] =
CompilePredicateMacro.compileImpl[A](predicate)

}

0 comments on commit fb90ac3

Please sign in to comment.