Forum Discussion

joakimE's avatar
joakimE
New Member
4 years ago

Add CAST to select in Odbc.Datasource?

Hi!

I have developed a custom connector for ODBC. It all works as intended but I need to make it add a CAST around the select of fields of a certain data type.

 

Today, the preview of Native Query looks like:

 

select "Id",
    "ModifiedBy",
    "Timestamp",
    "Comment"
from "dbo"."Table1"

 

 

What I want:

 

select "Id",
    "ModifiedBy",
    CAST("Timestamp" as datetime) as "TimestampDatetime",
    "Comment"
from "dbo"."Table1"

 

 

Using the below as the AstVisitor option in Odbc.Datasource, I get no actual change in the generated SQL query.

 

AstVisitor = [
            Constant =
                let
                    Cast = (value, typeName) => [
                        Text = Text.Format("CAST(#{0} as #{1})", { value, typeName })
                    ],
                    Visitor = [
                        12 = each Cast(_, "datetime")
                    ]
                in
                    (typeInfo, ast) => Record.FieldOrDefault(Visitor, typeInfo[DATA_TYPE], each null)(ast[Value])
        ],

 

 

I have not found much documentation around the AstVisitor, can anyone confirm it should be possible to modify the select statements as I need?

 

Adding a Change type step in Power query breaks the query folding, so that is no option.

Any help is very appriciated!

3 Replies