<commit_before># This file is part of MLDB. Copyright 2015 Datacratic. All rights reserved.

import json
from urllib import urlopen


dataset = mldb.create_dataset({  
    "id": "iris",
    "type": "text.csv.tabular",
    "params": {
        "dataFileUrl": "file://mldb/testing/dataset/iris.data",
        "headers": [ "a", "b", "c", "d", "class" ]
    }
})

result = mldb.perform("PUT", "/v1/functions/feats", [], {
    'type' : 'sql.expression',
    'params' : {
        "expression": "{a,b,c,d} as row"
    }
})
assert result["statusCode"] < 400, result["response"]

result = mldb.perform("PUT", "/v1/procedures/create_train_set", [], {
    'type' : 'transform',
    'params' : {
        "inputData": "select feats({*}) as *, class='Iris-setosa' as label from iris",
        "outputDataset": "train_set",
        "runOnCreation": True
    }
})
assert result["statusCode"] < 400, result["response"]

result = mldb.perform("PUT", "/v1/procedures/train", [], {
    'type' : 'classifier.train',
    'params' : {
        'trainingData' : """
            select 
                {* EXCLUDING(label)} as features,
                label
            from train_set
        """,
        "modelFileUrl": "file://tmp/MLDB-1273.cls",
        "configuration": {
            "dt": {
                "type": "decision_tree",
                "max_depth": 8,
                "verbosity": 3,
                "update_alg": "prob"
            },
        },
        "algorithm": "dt",        
        "functionName": "cls",
        "runOnCreation": True
    }
})
assert result["statusCode"] < 400, result["response"]

def query(sql):
    result = mldb.perform("GET", "/v1/query", [["q", sql], ["format", "table"]], {})
    assert result["statusCode"] < 400, result["response"]
    return result["response"]

with_flattening =  query("""
                select cls({features: {
                    a as "row.a", b as "row.b", c as "row.c", d as "row.d"
                    }}) as *
                from iris 
                limit 10
                """)

without_flattening =  query("""
                select cls({features: {feats({*}) as *}}) as *
                from iris 
                limit 10
                """)

assert with_flattening == without_flattening, "results do not match"

mldb.script.set_return("success")
<commit_msg>MLDB-1314 test case<commit_after># This file is part of MLDB. Copyright 2015 Datacratic. All rights reserved.

import json
from urllib import urlopen


dataset = mldb.create_dataset({  
    "id": "iris",
    "type": "text.csv.tabular",
    "params": {
        "dataFileUrl": "file://mldb/testing/dataset/iris.data",
        "headers": [ "a", "b", "c", "d", "class" ]
    }
})

result = mldb.perform("PUT", "/v1/functions/feats", [], {
    'type' : 'sql.expression',
    'params' : {
        "expression": "{a,b,c,d} as row"
    }
})
assert result["statusCode"] < 400, result["response"]

result = mldb.perform("PUT", "/v1/procedures/create_train_set", [], {
    'type' : 'transform',
    'params' : {
        "inputData": "select feats({*}) as *, class='Iris-setosa' as label from iris",
        "outputDataset": "train_set",
        "runOnCreation": True
    }
})
assert result["statusCode"] < 400, result["response"]

result = mldb.perform("PUT", "/v1/procedures/train", [], {
    'type' : 'classifier.train',
    'params' : {
        'trainingData' : """
            select 
                {* EXCLUDING(label)} as features,
                label
            from train_set
        """,
        "modelFileUrl": "file://tmp/MLDB-1273.cls",
        "configuration": {
            "dt": {
                "type": "decision_tree",
                "max_depth": 8,
                "verbosity": 3,
                "update_alg": "prob"
            },
        },
        "algorithm": "dt",        
        "functionName": "cls",
        "runOnCreation": True
    }
})
assert result["statusCode"] < 400, result["response"]

def query(sql):
    result = mldb.perform("GET", "/v1/query", [["q", sql], ["format", "table"]], {})
    assert result["statusCode"] < 400, result["response"]
    return result["response"]

with_flattening =  query("""
                select cls({features: {
                    a as "row.a", b as "row.b", c as "row.c", d as "row.d"
                    }}) as *
                from iris 
                limit 10
                """)

without_flattening =  query("""
                select cls({features: {feats({*}) as *}}) as *
                from iris 
                limit 10
                """)

assert with_flattening == without_flattening, "results do not match"

with_aliasing =  query("""
                select cls({features: {{a,b,c,d} as row}}) as *
                from iris 
                limit 10
                """)

assert with_flattening == with_aliasing, "results do not match"

with_aliasing =  query("""
                select cls({features: {* as "row."*}}) as *
                from iris 
                limit 10
                """)

assert with_flattening == with_aliasing, "results do not match"

without_aliasing =  query("""
                select cls({features: feats({*}) }) as *
                from iris 
                limit 10
                """)

assert with_flattening == without_aliasing, "results do not match"

result = mldb.perform("PUT", "/v1/functions/feats2", [], {
    'type' : 'sql.expression',
    'params' : {
        "expression": "feats({*}) as features"
    }
})
assert result["statusCode"] < 400, result["response"]

without_name =  query("""
                select cls( feats2({*}) ) as *
                from iris 
                limit 10
                """)

# uncomment for MLDB-1314 test case
# assert with_flattening == without_name, "results do not match"

mldb.script.set_return("success")

{'fraction_non_alphanumeric': 0.1667241379310345, 'fraction_numerical': 0.014137931034482758, 'mean_word_length': 1.9476626016260163, 'pattern_counts': {'":': 39, '<': 12, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3388288', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1799, 'n_words': 416}