Skip to content

Commit

Permalink
test: add string function test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
richtia committed May 15, 2023
1 parent faa969f commit 3e03141
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 8 deletions.
5 changes: 4 additions & 1 deletion bft/dialects/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ def visit_scalar_function(self, func):
local_name = self._get_or_else(func, "local_name", name)
infix = self._get_or_else(func, "infix", False)
postfix = self._get_or_else(func, "postfix", False)
skip = self._get_or_else(func, "skip", False)
bad_kernels = self._visit_list(self.visit_kernel, func, "unsupported_kernels")
return DialectFunction(name, local_name, infix, postfix, required_opts, bad_kernels)
return DialectFunction(
name, local_name, infix, postfix, skip, required_opts, bad_kernels
)

def visit(self, dfile):
name = self._get_or_die(dfile, "name")
Expand Down
30 changes: 26 additions & 4 deletions bft/dialects/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Dict, List, NamedTuple

import pytest

from bft.cases.types import Case, CaseLiteral, Literal, case_to_kernel_str
from bft.core.function import Kernel

Expand All @@ -14,6 +16,7 @@ class DialectFunction(NamedTuple):
local_name: str
infix: bool
postfix: bool
skip: bool
required_options: Dict[str, str]
unsupported_kernels: List[DialectKernel]

Expand All @@ -28,6 +31,7 @@ class SqlMapping(NamedTuple):
local_name: str
infix: bool
postfix: bool
skip: bool
should_pass: bool
reason: str

Expand Down Expand Up @@ -100,18 +104,36 @@ def supports_kernel(self, function_name: str, kernel: Kernel) -> bool:

def mapping_for_case(self, case: Case) -> SqlMapping:
dfunc = self.__scalar_functions_by_name.get(case.function, None)
if dfunc.skip:
pytest.skip("Skipping unsupported function.")
if dfunc is None:
return None

kernel_failure = self.__supports_case_kernel(dfunc, case.args, case.result)
if kernel_failure is not None:
return SqlMapping(dfunc.local_name, dfunc.infix, dfunc.postfix, False, kernel_failure)
return SqlMapping(
dfunc.local_name,
dfunc.infix,
dfunc.postfix,
dfunc.skip,
False,
kernel_failure,
)

option_failure = self.__supports_options(dfunc, case)
if option_failure is not None:
return SqlMapping(dfunc.local_name, dfunc.infix, dfunc.postfix, False, option_failure)

return SqlMapping(dfunc.local_name, dfunc.infix, dfunc.postfix, True, None)
return SqlMapping(
dfunc.local_name,
dfunc.infix,
dfunc.postfix,
dfunc.skip,
False,
option_failure,
)

return SqlMapping(
dfunc.local_name, dfunc.infix, dfunc.postfix, dfunc.skip, True, None
)


class DialectsLibrary(object):
Expand Down
9 changes: 8 additions & 1 deletion bft/testers/postgres/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ def run_sql_case(self, case: Case, mapping: SqlMapping) -> SqlCaseResult:

arg_names = [f"arg{idx}" for idx in range(len(case.args))]
joined_arg_names = ",".join(arg_names)
arg_vals = ",".join([literal_to_str(arg) for arg in case.args])
arg_vals = ",".join(
[
"'" + literal_to_str(arg) + "'"
if arg.type == "string" and arg.value is not None
else literal_to_str(arg)
for arg in case.args
]
)
self.conn.execute(
f"INSERT INTO my_table ({joined_arg_names}) VALUES ({arg_vals});"
)
Expand Down
10 changes: 9 additions & 1 deletion bft/testers/sqlite/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"fp32": "REAL",
"fp64": "REAL",
"boolean": "BOOLEAN",
"string": "TEXT",
}


Expand Down Expand Up @@ -51,7 +52,14 @@ def run_sql_case(self, case: Case, mapping: SqlMapping) -> SqlCaseResult:

arg_names = [f"arg{idx}" for idx in range(len(case.args))]
joined_arg_names = ",".join(arg_names)
arg_vals = ",".join([literal_to_str(arg) for arg in case.args])
arg_vals = ",".join(
[
"'" + literal_to_str(arg) + "'"
if arg.type == "string" and arg.value is not None
else literal_to_str(arg)
for arg in case.args
]
)
self.conn.execute(
f"INSERT INTO my_table ({joined_arg_names}) VALUES ({arg_vals});"
)
Expand Down
2 changes: 1 addition & 1 deletion bft/testers/velox/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def is_type_supported(type):
return type in set({"i8", "i16", "i32", "i64", "fp32", "fp64", "boolean"})
return type in set({"i8", "i16", "i32", "i64", "fp32", "fp64", "boolean", "string"})


class VeloxRunner(SqlCaseRunner):
Expand Down
42 changes: 42 additions & 0 deletions cases/string/concat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function: concat
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcd'
type: string
- value: 'efg'
type: string
result:
value: 'abcdefg'
type: string
- group:
id: null_input
description: Examples with null as input
args:
- value: 'abcd'
type: string
- value: null
type: string
result:
value: null
type: string
- group: null_input
args:
- value: null
type: string
- value: 'abcd'
type: string
result:
value: null
type: string
- group: null_input
args:
- value: null
type: string
- value: null
type: string
result:
value: null
type: string
60 changes: 60 additions & 0 deletions cases/string/like.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function: like
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcdefg'
type: string
- value: 'abcdefg'
type: string
result:
value: true
type: boolean
- group: basic
args:
- value: 'abcdefg'
type: string
- value: 'abc'
type: string
result:
value: false
type: boolean
- group:
id: wildcard
description: Examples using wildcards
args:
- value: 'abcdefg'
type: string
- value: 'abc%'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: '%efg'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: '_bcdefg'
type: string
result:
value: true
type: boolean
- group: wildcard
args:
- value: 'abcdefg'
type: string
- value: 'abc_efg'
type: string
result:
value: true
type: boolean
54 changes: 54 additions & 0 deletions cases/string/substring.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function: substring
cases:
- group:
id: basic
description: Basic examples without any special cases
args:
- value: 'abcdefg'
type: string
- value: 1
type: i32
- value: 5
type: i32
result:
value: 'abcde'
type: string
- group:
id: zero_start
description: Example where start argument is zero
args:
- value: 'abcdefg'
type: string
- value: 0
type: i32
- value: 3
type: i32
result:
value: 'ab'
type: string
- group:
id: negative_start
description: Example where start argument is a negative integer
args:
- value: 'abcdefg'
type: string
- value: -3
type: i32
- value: 2
type: i32
result:
value: 'ef'
type: string
- group:
id: start_greater_than_length
description: Example where start argument greater than the length of the string
args:
- value: 'abcdefg'
type: string
- value: 10
type: i32
- value: 2
type: i32
result:
value: ''
type: string
7 changes: 7 additions & 0 deletions dialects/postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@ scalar_functions:
- i8
- i8
result: i8
- name: substring
skip: true
- name: concat
local_name: "||"
infix: True
- name: like
infix: True
6 changes: 6 additions & 0 deletions dialects/sqlite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ scalar_functions:
local_name: "NULLIF"
infix: False
- name: coalesce
- name: substring
- name: concat
local_name: "||"
infix: True
- name: like
infix: True
4 changes: 4 additions & 0 deletions dialects/velox_presto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ scalar_functions:
local_name: "!="
infix: True
- name: coalesce
- name: substring
local_name: substr
- name: concat
- name: like

0 comments on commit 3e03141

Please sign in to comment.