Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix how cast to boolean works to preserve null #301

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dialect/druidDialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class DruidDialect extends SQLDialect {
BOOLEAN: {
NUMBER: '($$ = 1)',
STRING: `($$ = 'true')`,
_: `(CAST($$ AS VARCHAR) IN ('1','true'))`,
_: `CASE CAST($$ AS VARCHAR) WHEN '1' THEN TRUE WHEN 'true' THEN TRUE WHEN '0' THEN FALSE WHEN 'false' THEN FALSE END`,
},
};

Expand Down
39 changes: 39 additions & 0 deletions test/simulate/simulateDruidSql.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const { expect } = require('chai');

const plywood = require('../plywood');
const { sane } = require('../utils');

const { Expression, External, Dataset, TimeRange, $, ply, r, s$ } = plywood;

Expand Down Expand Up @@ -508,6 +509,44 @@ describe('simulate DruidSql', () => {
]);
});

it('works with boolean cast', () => {
const ex = ply()
.apply('diamonds', $('diamonds').filter($('color').in(['red', 'green', 'blue'])))
.apply(
'Tags',
$('diamonds')
.split(s$('t.tags').cast('BOOLEAN'), 'B')
.apply('count', $('diamonds').count())
.sort('$count', 'descending')
.limit(10)
.select('B', 'count'),
);

const queryPlan = ex.simulateQueryPlan({
diamonds: External.fromJS({
engine: 'druidsql',
version: '0.20.0',
source: 'diamonds',
timeAttribute: 'time',
attributes,
allowSelectQueries: true,
mode: 'raw',
}),
});

expect(queryPlan.length).to.equal(1);
expect(queryPlan[0][0].query).to.deep.equal(sane`
SELECT
CASE CAST((t.tags) AS VARCHAR) WHEN '1' THEN TRUE WHEN 'true' THEN TRUE WHEN '0' THEN FALSE WHEN 'false' THEN FALSE END AS "B",
COUNT(*) AS "count"
FROM "diamonds" AS t
WHERE "color" IN ('red','green','blue')
GROUP BY 1
ORDER BY "count" DESC
LIMIT 10
`);
});

it('works with mvFilterOnly and mvOverlap', () => {
const ex = ply()
.apply('diamonds', $('diamonds'))
Expand Down