Skip to content

Commit 72dca57

Browse files
authored
feat(6107): runQuery with iox sql. (#6175)
* feat(6107): runQuery with iox sql. * add queryOptions for language, and have buildQueryRequest() and downstream consume this langauge option. * set download and submit button disablement and messages, based on presence of bucket selection when using SQL. * also remove the dateRangePicker when using sql. * Also make more explicit what the override mechanism type means (a.k.a. params versus variables). * chore: skip flaky test, and mark for later debugging
1 parent f3916e4 commit 72dca57

File tree

4 files changed

+89
-33
lines changed

4 files changed

+89
-33
lines changed

cypress/e2e/shared/fluxQueryBuilder.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ describe('Script Builder', () => {
165165
cy.wait('@query -15m')
166166
})
167167

168-
describe('data completeness', () => {
168+
// TODO(wiedld): this test is flaky. Need to debug later.
169+
describe.skip('data completeness', () => {
169170
const validateCsv = (csv: string, tableCnt: number) => {
170171
cy.wrap(csv)
171172
.then(doc => doc.trim().split('\n'))

src/dataExplorer/components/ResultsPane.tsx

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ import {
2828
// Contexts
2929
import {ResultsContext} from 'src/dataExplorer/components/ResultsContext'
3030
import {QueryContext} from 'src/shared/contexts/query'
31-
import {PersistanceContext} from 'src/dataExplorer/context/persistance'
31+
import {
32+
PersistanceContext,
33+
DEFAULT_FLUX_EDITOR_TEXT,
34+
DEFAULT_SQL_EDITOR_TEXT,
35+
} from 'src/dataExplorer/context/persistance'
3236

3337
// Components
3438
import TimeRangeDropdown from 'src/shared/components/TimeRangeDropdown'
3539
import Results from 'src/dataExplorer/components/Results'
3640
import {SubmitQueryButton} from 'src/timeMachine/components/SubmitQueryButton'
3741
import QueryTime from 'src/dataExplorer/components/QueryTime'
3842
import NewDatePicker from 'src/shared/components/dateRangePicker/NewDatePicker'
43+
import {SqlEditorMonaco} from 'src/shared/components/SqlMonacoEditor'
3944

4045
// Types
4146
import {TimeRange} from 'src/types'
@@ -52,7 +57,6 @@ import {getWindowPeriodVariableFromVariables} from 'src/variables/utils/getWindo
5257
// Constants
5358
import {TIME_RANGE_START, TIME_RANGE_STOP} from 'src/variables/constants'
5459
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
55-
import {SqlEditorMonaco} from 'src/shared/components/SqlMonacoEditor'
5660

5761
const FluxMonacoEditor = lazy(
5862
() => import('src/shared/components/FluxMonacoEditor')
@@ -100,6 +104,10 @@ const rangeToParam = (timeRange: TimeRange) => {
100104
}
101105
}
102106

107+
const isDefaultText = text => {
108+
return text == DEFAULT_FLUX_EDITOR_TEXT || text == DEFAULT_SQL_EDITOR_TEXT
109+
}
110+
103111
const ResultsPane: FC = () => {
104112
const {basic, query, cancel} = useContext(QueryContext)
105113
const {status, result, setStatus, setResult} = useContext(ResultsContext)
@@ -114,12 +122,25 @@ const ResultsPane: FC = () => {
114122
resource,
115123
} = useContext(PersistanceContext)
116124
const [csvDownloadCancelID, setCancelId] = useState(null)
125+
const language = resource?.language ?? LanguageType.FLUX
117126

118-
const submitButtonDisabled = !text && !selection.measurement
119-
120-
const disabledTitleText = submitButtonDisabled
121-
? 'Select measurement before running script'
122-
: ''
127+
let submitButtonDisabled = false
128+
let disabledTitleText = ''
129+
if (!text || isDefaultText(text)) {
130+
submitButtonDisabled = true
131+
disabledTitleText = 'Write a query before running script'
132+
} else if (language == LanguageType.SQL && !selection.bucket) {
133+
submitButtonDisabled = true
134+
disabledTitleText = 'Select a bucket before running script'
135+
} else if (
136+
language == LanguageType.FLUX &&
137+
selection.composition.synced && // using composition
138+
selection.bucket &&
139+
!selection.measurement
140+
) {
141+
submitButtonDisabled = true
142+
disabledTitleText = 'Select a measurement before running script'
143+
}
123144

124145
const download = async () => {
125146
event('CSV Download Initiated')
@@ -128,7 +149,11 @@ const ResultsPane: FC = () => {
128149
{
129150
vars: rangeToParam(range),
130151
},
131-
{rawBlob: true}
152+
{
153+
rawBlob: true,
154+
language,
155+
bucket: selection.bucket,
156+
}
132157
)
133158
setCancelId(promise.id)
134159

@@ -144,9 +169,16 @@ const ResultsPane: FC = () => {
144169

145170
const submit = useCallback(() => {
146171
setStatus(RemoteDataState.Loading)
147-
query(text, {
148-
vars: rangeToParam(range),
149-
})
172+
query(
173+
text,
174+
{
175+
vars: rangeToParam(range),
176+
},
177+
{
178+
language,
179+
bucket: selection.bucket,
180+
}
181+
)
150182
.then(r => {
151183
event('resultReceived', {
152184
status: r.parsed.table.length === 0 ? 'empty' : 'good',
@@ -165,7 +197,7 @@ const ResultsPane: FC = () => {
165197
event('resultReceived', {status: 'error'})
166198
setStatus(RemoteDataState.Error)
167199
})
168-
}, [text, range])
200+
}, [text, range, resource?.language, selection.bucket])
169201

170202
const timeVars = [
171203
getRangeVariable(TIME_RANGE_START, range),
@@ -176,6 +208,16 @@ const ResultsPane: FC = () => {
176208
getWindowPeriodVariableFromVariables(text, timeVars) || []
177209
)
178210

211+
const TimeRangePicker = () =>
212+
isFlagEnabled('newTimeRangeComponent') ? (
213+
<NewDatePicker />
214+
) : (
215+
<TimeRangeDropdown
216+
timeRange={range}
217+
onSetTimeRange={(range: TimeRange) => setRange(range)}
218+
/>
219+
)
220+
179221
return (
180222
<DraggableResizer
181223
handleOrientation={Orientation.Horizontal}
@@ -237,7 +279,9 @@ const ResultsPane: FC = () => {
237279
icon={IconFont.Download_New}
238280
onClick={download}
239281
status={
240-
text ? ComponentStatus.Default : ComponentStatus.Disabled
282+
!submitButtonDisabled
283+
? ComponentStatus.Default
284+
: ComponentStatus.Disabled
241285
}
242286
testID="data-explorer--csv-download"
243287
/>
@@ -251,13 +295,9 @@ const ResultsPane: FC = () => {
251295
color={ComponentColor.Danger}
252296
/>
253297
)}
254-
{isFlagEnabled('newTimeRangeComponent') ? (
255-
<NewDatePicker />
256-
) : (
257-
<TimeRangeDropdown
258-
timeRange={range}
259-
onSetTimeRange={(range: TimeRange) => setRange(range)}
260-
/>
298+
{isFlagEnabled('uiSqlSupport') &&
299+
resource?.language === LanguageType.SQL ? null : (
300+
<TimeRangePicker />
261301
)}
262302
<SubmitQueryButton
263303
className="submit-btn"

src/shared/contexts/query/context.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {FluxResult} from 'src/types/flows'
1111
import {
1212
simplify,
1313
updateWindowPeriod,
14+
sqlAsFlux,
1415
} from 'src/shared/contexts/query/preprocessing'
1516
import {trimPartialLines} from 'src/shared/contexts/query/postprocessing'
1617

@@ -27,23 +28,26 @@ import {notify} from 'src/shared/actions/notifications'
2728
import {resultTooLarge} from 'src/shared/copy/notifications'
2829

2930
// Types
30-
import {CancellationError} from 'src/types'
31+
import {CancellationError, OwnBucket} from 'src/types'
3132
import {RunQueryResult} from 'src/shared/apis/query'
3233
import {event} from 'src/cloud/utils/reporting'
3334
import {PROJECT_NAME} from 'src/flows'
35+
import {LanguageType} from 'src/dataExplorer/components/resources'
3436

3537
interface CancelMap {
3638
[key: string]: () => void
3739
}
3840

3941
export enum OverrideMechanism {
40-
Inline,
41-
AST,
42-
JSON,
42+
Inline = 'inject-variables-into-script',
43+
Extern = 'AST',
44+
Params = 'JSON', // TODO: code is incorrect & will need debugging
4345
}
4446

4547
export interface QueryOptions {
4648
overrideMechanism?: OverrideMechanism
49+
language?: LanguageType
50+
bucket?: OwnBucket
4751
rawBlob?: boolean
4852
}
4953

@@ -93,11 +97,15 @@ const buildQueryRequest = (
9397
override: QueryScope,
9498
options?: QueryOptions
9599
) => {
96-
const mechanism = options?.overrideMechanism ?? OverrideMechanism.AST
97-
const query =
98-
mechanism === OverrideMechanism.Inline
99-
? simplify(text, override?.vars ?? {}, override?.params ?? {})
100-
: text
100+
const mechanism = options?.overrideMechanism ?? OverrideMechanism.Extern
101+
const language = options?.language ?? LanguageType.FLUX
102+
103+
let query = text
104+
if (language == LanguageType.SQL) {
105+
query = sqlAsFlux(text, options?.bucket)
106+
} else if (mechanism === OverrideMechanism.Inline) {
107+
query = simplify(text, override?.vars ?? {}, override?.params ?? {})
108+
}
101109

102110
const orgID = override?.org || org.id
103111

@@ -119,13 +127,13 @@ const buildQueryRequest = (
119127
dialect: {annotations: ['group', 'datatype', 'default']},
120128
}
121129

122-
if (mechanism === OverrideMechanism.AST) {
130+
if (mechanism === OverrideMechanism.Extern) {
123131
const options = updateWindowPeriod(query, override, 'ast')
124132
if (options && Object.keys(options).length) {
125133
body.extern = options
126134
}
127135
}
128-
if (mechanism === OverrideMechanism.JSON) {
136+
if (mechanism === OverrideMechanism.Params) {
129137
const options = updateWindowPeriod(query, override, 'json')
130138
if (options && Object.keys(options).length) {
131139
body.options = options

src/shared/contexts/query/preprocessing.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {isFlagEnabled} from 'src/shared/utils/featureFlag'
55

66
// Types and Constants
77
import {SELECTABLE_TIME_RANGES} from 'src/shared/constants/timeRanges'
8-
import {File} from 'src/types'
8+
import {File, OwnBucket} from 'src/types'
99
import {QueryScope} from 'src/shared/contexts/query'
1010

1111
const DESIRED_POINTS_PER_GRAPH = 360
@@ -413,3 +413,10 @@ export const updateWindowPeriod = (
413413
return options
414414
}
415415
}
416+
417+
export const sqlAsFlux = (text: string, bucket: OwnBucket) => {
418+
return `import "experimental/iox"
419+
420+
iox.sql(bucket: "${bucket.name}", query: ${JSON.stringify(text)})
421+
`
422+
}

0 commit comments

Comments
 (0)