Skip to content

Commit

Permalink
Get tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-nambiar committed Sep 4, 2024
1 parent 19a52b9 commit f86e418
Show file tree
Hide file tree
Showing 25 changed files with 616 additions and 494 deletions.
31 changes: 31 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ ACS
AES
APIs
ARNs
aren
AST
AdministratorAccess
AllowKinesisAccess
AssumeRole
Auth
Avro
BigTable
Bitwise
Bytewax
Ceil
ceil
Concat
const
concat
CDF
CIDR
DDL
Expand All @@ -22,7 +29,12 @@ Deltalake
DescribeStream
DescribeStreamConsumer
DescribeStreamSummary
disambiguity
Dockerfile
expr
Eval
eval
endswith
FennelDataAccessRole
Flink
Flink's
Expand Down Expand Up @@ -54,10 +66,13 @@ Kinesis
Kubernetes
LHS
LastK
len
lits
ListShards
MSK
MockClient
Nones
nullness
OAuth
OOM
OWASP
Expand All @@ -69,10 +84,12 @@ OpenSSL's
PII
PLAINTEXT
POC
Polars
PagerDuty
Personalization
PoolableConnectionFactory
Preproc
predictate
PrivateLink
Pulumi
PyO
Expand Down Expand Up @@ -213,6 +230,7 @@ featureset
featuresets
fintech
firstName
fillnull
forgotten
frontend
func
Expand All @@ -236,7 +254,9 @@ indirections
init
initializer
interop
ints
ip
isnull
ish
ith
jdbc
Expand All @@ -262,6 +282,8 @@ mysql
namespace
nan
nat
NaT
num
natively
noop
noqa
Expand All @@ -280,6 +302,7 @@ personalization
pid
postgres
pre
precisions
precompute
precomputed
prepend
Expand Down Expand Up @@ -317,19 +340,27 @@ snowflakecomputing
src
sso
stateful
startswith
str
strftime
strptime
Strptime
struct
structs
sts
subnets
tiering
timeframes
Typeof
typeof
TODO
uDDsketch
ubuntu
uid
uids
uint
uints
unary
unbundled
uncomment
unhashable
Expand Down
28 changes: 24 additions & 4 deletions docs/examples/api-reference/expressions/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional
import pandas as pd


def test_unary_not():
# docsnip expr_unary_not
from fennel.expr import lit
Expand All @@ -15,6 +16,7 @@ def test_unary_not():
assert expr.eval(df, schema={"x": int}).tolist() == [False, False, False]
# /docsnip


def test_col():
# docsnip expr_col
from fennel.expr import col
Expand All @@ -38,10 +40,16 @@ def test_col():

# can be evaluated with a dataframe
import pandas as pd

df = pd.DataFrame({"x": [1, 2, 3], "y": [1.0, 2.0, 3.0]})
assert expr.eval(df, schema={"x": int, "y": float}).tolist() == [2.0, 4.0, 6.0]
assert expr.eval(df, schema={"x": int, "y": float}).tolist() == [
2.0,
4.0,
6.0,
]
# /docsnip


def test_when_then():
# docsnip expr_when_then
from fennel.expr import when, col, InvalidExprException
Expand All @@ -61,6 +69,7 @@ def test_when_then():

# can be evaluated with a dataframe
import pandas as pd

df = pd.DataFrame({"x": [True, False, True]})
assert expr.eval(df, schema={"x": bool}).tolist() == [1, 0, 1]

Expand All @@ -74,6 +83,7 @@ def test_when_then():
assert expr.typeof(schema={"x": bool}) == Optional[int]
# /docsnip


def test_isnull():
# docsnip expr_isnull
from fennel.expr import col
Expand All @@ -95,9 +105,14 @@ def test_isnull():
import pandas as pd

df = pd.DataFrame({"x": pd.Series([1, 2, None], dtype=pd.Int64Dtype())})
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [False, False, True]
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [
False,
False,
True,
]
# /docsnip


def test_fillnull():
# docsnip expr_fillnull
from fennel.expr import col, lit
Expand All @@ -118,9 +133,14 @@ def test_fillnull():

expr = col("x").fillnull(lit(10))
df = pd.DataFrame({"x": pd.Series([1, 2, None], dtype=pd.Int64Dtype())})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [1., 2., 10.]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
1.0,
2.0,
10.0,
]
# /docsnip


def test_lit():
# docsnip expr_lit
from fennel.expr import lit, col
Expand All @@ -135,4 +155,4 @@ def test_lit():
expr = col("x") + lit(1)
df = pd.DataFrame({"x": pd.Series([1, 2, None], dtype=pd.Int64Dtype())})
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [2, 3, pd.NA]
# /docsnip
# /docsnip
17 changes: 12 additions & 5 deletions docs/examples/api-reference/expressions/binary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional
import pytest


def test_typeof():
# docsnip expr_binary_arithmetic
import pandas as pd
Expand All @@ -10,18 +11,24 @@ def test_typeof():
assert expr.typeof(schema={"x": int, "y": int}) == int
assert expr.typeof(schema={"x": int, "y": float}) == float
assert expr.typeof(schema={"x": float, "y": float}) == float
assert expr.typeof(schema={"x": Optional[float], "y": int}) == Optional[float]
assert (
expr.typeof(schema={"x": Optional[float], "y": int}) == Optional[float]
)

df = pd.DataFrame({"x": [1, 2, None]})
expr = lit(1) + col("x")
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [2, 3, pd.NA]

expr = lit(1) - col("x")
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [0, -1, pd.NA]

expr = lit(1) * col("x")
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [1, 2, pd.NA]

expr = lit(1) / col("x")
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [1, 0.5, pd.NA]
assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [
1,
0.5,
pd.NA,
]
# /docsnip
6 changes: 3 additions & 3 deletions docs/examples/api-reference/expressions/eval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional
import pytest


def test_typeof():
# docsnip expr_typeof
from fennel.expr import lit, col
Expand All @@ -12,7 +13,6 @@ def test_typeof():
assert expr.typeof(schema={"amount": Optional[int]}) == Optional[int]
assert expr.typeof(schema={"amount": Optional[float]}) == Optional[float]


# typeof raises an error if type of 'amount' isn't provided
with pytest.raises(ValueError):
expr.typeof()
Expand Down Expand Up @@ -48,5 +48,5 @@ def test_eval():
df = pd.DataFrame({"other": [1, 2, 3]})
with pytest.raises(Exception):
expr.eval(df, schema={"amount": int})
# /docsnip

# /docsnip
41 changes: 31 additions & 10 deletions docs/examples/api-reference/expressions/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional
import pandas as pd


def test_abs():
# docsnip abs
from fennel.expr import col
Expand All @@ -28,15 +29,19 @@ def test_floor():
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").floor() # equivalent to col("x").num.floor()
expr = col("x").floor() # equivalent to col("x").num.floor()
assert expr.typeof(schema={"x": int}) == int
assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
assert expr.typeof(schema={"x": float}) == int
assert expr.typeof(schema={"x": Optional[float]}) == Optional[int]

# can be evaluated with a dataframe
df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [1, -3, pd.NA]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
1,
-3,
pd.NA,
]

with pytest.raises(ValueError):
expr.typeof(schema={"x": str})
Expand All @@ -48,15 +53,19 @@ def test_ceil():
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").ceil() # equivalent to col("x").num.ceil()
expr = col("x").ceil() # equivalent to col("x").num.ceil()
assert expr.typeof(schema={"x": int}) == int
assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
assert expr.typeof(schema={"x": float}) == int
assert expr.typeof(schema={"x": Optional[float]}) == Optional[int]

# can be evaluated with a dataframe
df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [2, -2, pd.NA]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
2,
-2,
pd.NA,
]

with pytest.raises(ValueError):
expr.typeof(schema={"x": str})
Expand All @@ -68,7 +77,7 @@ def test_round():
from fennel.expr import col

# docsnip-highlight next-line
expr = col("x").round() # equivalent to col("x").num.round()
expr = col("x").round() # equivalent to col("x").num.round()

assert expr.typeof(schema={"x": int}) == int
assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
Expand All @@ -77,7 +86,11 @@ def test_round():

# can be evaluated with a dataframe
df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [1, -2, pd.NA]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
1,
-2,
pd.NA,
]

# can also explicit specify the number of decimals
# docsnip-highlight next-line
Expand All @@ -89,16 +102,24 @@ def test_round():
assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]

df = pd.DataFrame({"x": pd.Series([1.12, -2.37, None])})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [1.1, -2.4, pd.NA]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
1.1,
-2.4,
pd.NA,
]

df = pd.DataFrame({"x": pd.Series([1, -2, None])})
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [1.0, -2.0, pd.NA]
assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
1.0,
-2.0,
pd.NA,
]

# /docsnip

# invalid number of decimals
with pytest.raises(Exception):
expr = col("x").round(-1)

with pytest.raises(Exception):
expr = col("x").round(1.1)
expr = col("x").round(1.1)
Loading

0 comments on commit f86e418

Please sign in to comment.