Skip to content

Commit 5c82e0c

Browse files
authored
Merge pull request #553 from julioyildo/bigquery-magic
Add BigQuery magic (%%bigquery)
2 parents 6dc8ab3 + 17d62f1 commit 5c82e0c

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## CHANGELOG
22

3-
### `@krassowski/jupyterlab-lsp 3.4.2` (unreleased)
3+
### `@krassowski/jupyterlab-lsp 3.5.0` (unreleased)
4+
5+
- features:
6+
7+
- adds `%%bigquery` IPython cell magic support for BigQuery ([#553], thanks @julioyildo)
48

59
- bug fixes:
610

@@ -9,6 +13,7 @@
913

1014
[#544]: https://github.com/krassowski/jupyterlab-lsp/pull/544
1115
[#547]: https://github.com/krassowski/jupyterlab-lsp/pull/547
16+
[#553]: https://github.com/krassowski/jupyterlab-lsp/pull/553
1217

1318
### `jupyter-lsp 1.1.4` (2020-02-21)
1419

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { JupyterFrontEndPlugin } from '@jupyterlab/application';
22
import { IPYTHON_RPY2_TRANSCLUSIONS } from './ipython-rpy2';
33
import { IPYTHON_SQL_TRANSCLUSIONS } from './ipython-sql';
4+
import { IPYTHON_BIGQUERY_TRANSCLUSIONS } from './ipython-bigquery';
45
import { IPYTHON_TRANSCLUSIONS } from './ipython';
56

67
export const DEFAULT_TRANSCLUSIONS: JupyterFrontEndPlugin<void>[] = [
78
IPYTHON_RPY2_TRANSCLUSIONS,
89
IPYTHON_SQL_TRANSCLUSIONS,
10+
IPYTHON_BIGQUERY_TRANSCLUSIONS,
911
IPYTHON_TRANSCLUSIONS
1012
];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { VirtualDocument } from '../../virtual/document';
2+
import { expect } from 'chai';
3+
import { foreign_code_extractors } from './extractors';
4+
import { extract_code, get_the_only_virtual } from '../../extractors/testutils';
5+
6+
describe('Bigquery SQL extractors', () => {
7+
let document: VirtualDocument;
8+
9+
function extract(code: string) {
10+
return extract_code(document, code);
11+
}
12+
13+
beforeEach(() => {
14+
document = new VirtualDocument({
15+
language: 'python',
16+
path: 'test.ipynb',
17+
overrides_registry: {},
18+
foreign_code_extractors: foreign_code_extractors,
19+
standalone: false,
20+
file_extension: 'py',
21+
has_lsp_supported_file: false
22+
});
23+
});
24+
25+
afterEach(() => {
26+
document.clear();
27+
});
28+
29+
describe('%%bigquery cell magic', () => {
30+
it('extracts simple commands', () => {
31+
let code = "%%bigquery\nselect * from character\nwhere abbrev = 'ALICE'";
32+
let { cell_code_kept, foreign_document_map } = extract(code);
33+
34+
expect(cell_code_kept).to.equal(code);
35+
let document = get_the_only_virtual(foreign_document_map);
36+
expect(document.language).to.equal('sql');
37+
expect(document.value).to.equal(
38+
"select * from character\nwhere abbrev = 'ALICE'\n"
39+
);
40+
});
41+
});
42+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IForeignCodeExtractorsRegistry } from '../../extractors/types';
2+
import { RegExpForeignCodeExtractor } from '../../extractors/regexp';
3+
4+
export const SQL_URL_PATTERN = '(?:(?:.*?)://(?:.*))';
5+
// note: -a/--connection_arguments and -f/--file are not supported yet
6+
const single_argument_options = [
7+
'--destination_table',
8+
'--project',
9+
'--use_bqstorage_api',
10+
'--use_rest_api',
11+
'--use_legacy_sql',
12+
'--verbose',
13+
'--params'
14+
];
15+
const zero_argument_options = ['-l', '--connections'];
16+
17+
const COMMAND_PATTERN =
18+
'(?:' +
19+
(zero_argument_options.join('|') +
20+
'|' +
21+
single_argument_options.map(command => command + ' \\w+').join('|')) +
22+
')';
23+
24+
export let foreign_code_extractors: IForeignCodeExtractorsRegistry = {
25+
// general note: to match new lines use [^] instead of dot, unless the target is ES2018, then use /s
26+
python: [
27+
new RegExpForeignCodeExtractor({
28+
language: 'sql',
29+
pattern: `^%%bigquery(?: (?:${SQL_URL_PATTERN}|${COMMAND_PATTERN}|(?:\\w+ << )|(?:\\w+@\\w+)))?\n?(.+\n)?([^]*)`,
30+
extract_to_foreign: '$1$2',
31+
is_standalone: true,
32+
file_extension: 'sql'
33+
})
34+
]
35+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { JupyterFrontEndPlugin } from '@jupyterlab/application';
2+
import { ILSPCodeExtractorsManager, PLUGIN_ID } from '../../tokens';
3+
import { foreign_code_extractors } from './extractors';
4+
5+
/**
6+
* Implements extraction of code for IPython Magics for BigQuery, see:
7+
* https://googleapis.dev/python/bigquery/latest/magics.html.
8+
*/
9+
export const IPYTHON_BIGQUERY_TRANSCLUSIONS: JupyterFrontEndPlugin<void> = {
10+
id: PLUGIN_ID + ':ipython-bigquery',
11+
requires: [ILSPCodeExtractorsManager],
12+
activate: (app, extractors_manager: ILSPCodeExtractorsManager) => {
13+
for (let language of Object.keys(foreign_code_extractors)) {
14+
for (let extractor of foreign_code_extractors[language]) {
15+
extractors_manager.register(extractor, language);
16+
}
17+
}
18+
},
19+
autoStart: true
20+
};

0 commit comments

Comments
 (0)